Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: handle “window is not defined” via JSDom context #6669

Closed
Jarred-Sumner opened this issue Mar 15, 2019 · 2 comments
Closed

Idea: handle “window is not defined” via JSDom context #6669

Jarred-Sumner opened this issue Mar 15, 2019 · 2 comments

Comments

@Jarred-Sumner
Copy link

A common source of frustration for SSR in React is “window is not defined”.

The current solution to this problem is requiring the developer to remember to check for “typeof window !== ‘undefined’” in many places and a number of conditional requires.

Instead of asking developers to fix this themselves, what if Next.js just fixed it by default?

While this loader wouldn’t specifically work for Next.js, a technique like it probably would: https://github.com/GoogleChromeLabs/prerender-loader. Basically, if we do the SSR in the context of the window object provided by JSDOM, then most browser APIs will “just work” and developers will rarely have to worry about making their code support SSR. I have this working in a project not using Next.js and so far it works well.

There’s a few things to consider here:

  • What is the performance impact?
  • Does this break any libraries?
  • If this is added, should it be enabled by default? (Yes, IMO)

30506749-85C1-4A28-921B-5596B79C6653

@timneutkens
Copy link
Member

I strongly believe that we shouldn't. If you import a module that breaks because of window usage it means that the module itself is not working correctly and should be using the correct React lifecycle methods, being either componentDidMount or useEffect (hook).

@Jarred-Sumner
Copy link
Author

👍

@lock lock bot locked as resolved and limited conversation to collaborators Mar 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants