"window is undefined" in nextjs
See original GitHub issue- FYI: going forward this will likely continue to be an issue due to the popularity of nextjs.
- The root cause is that pixi does not work in node.js so it blows up when run in SSR.
- This is related to an old bug with pixi.js v4: https://github.com/pixijs/pixi.js/issues/3224
- The workaround is to use dynamic imports to remove it from the SSR phase. See https://medium.com/frontend-digest/why-is-window-not-defined-in-nextjs-44daf7b4604e
- Unfortunately, this is not an ideal solution.
- The use case is a React component that uses pixi.js under the hood. https://gitlab.com/robotandkid/awesome-react-cutscenes
- Because of the dynamic import, this component is unable to manage it’s SSR lifecycle unless it includes nextjs-specific code.
- But really all that needs to happen is for pixi to abort loading if the window is not present. (Something like wrapping everything in a giant
if (typeof window === 'undefined')
😃 )
Expected Behavior
- pixi errors gracefully when the
window
object is not present
Current Behavior
- pixi cannot run in SSR
Possible Solution
- Something like wrapping everything in a giant
if (typeof window === 'undefined')
😃
Steps to Reproduce
Environment
-
pixi.js - v5.3.3
-
pixi.js
version: e.g. 4.7.1 -
Browser & Version: e.g. Chrome 67
-
OS & Version: e.g. Ubuntu 18.04
-
Running Example: url
Issue Analytics
- State:
- Created 3 years ago
- Reactions:5
- Comments:16 (2 by maintainers)
Top Results From Across the Web
How to solve Next.js window is not defined
ReferenceError: window is not defined is a pretty common error you may run into when using Next.js for the first time but don't...
Read more >Window is not defined in Next.js React app - Stack Overflow
Next.js is universal, which means it executes code first server-side, then client-side. The window object is only present client-side, so ...
Read more >How to Fix "window is not defined" in Next.js - Upmostly
This issue has to do with Server-Side Rendering in Next.js. Next.js will by default try to use SSR for your site. This means...
Read more >How to fix window is not defined error in Next.js
As you can see the error occurs in the terminal itself. That means it is a compilation error. Next.js is compiled/built on Node.js...
Read more >Fix for Next.js ReferenceError window is not defined
The window object is the reference to an open window in a browser. Therefore, the window object is only available on the client-side....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Released v6.3.1, wait a few minutes for it to publish
wrap pixi.js in a component
and them dynamic import that with no ssr