question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"window is not defined" - SSR support

See original GitHub issue

I received this error when attempting to retrieve the value from a selector. This occurs in a Next.js 9.4 project, which is inherently server side rendered.

Looking through the source code it looks like there are a few references to the window object that aren’t available in SSR. Are there any plans to support SSR? I tried to work around it by checking for the presence of the window object but because I’m trying to conditionally use state it breaks the rules of hooks 😦

ReferenceError: window is not defined
    at r (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:712)
    at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:1276
    at e (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:1290)
    at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:19654
    at i (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:19929)
    at Object.c [as get] (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:19966)
    at x (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:4554)
    at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:6719
    at Object.replaceState (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:10416)
    at C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:6684
    at Object.trace (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:4312)
    at getRecoilValueAsLoadable (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:6652)
    at n (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:13390)
    at r (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:13682)
    at Object.getRecoilState (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:13775)
    at useRecoilState (C:\git\nextjs-workflows\node_modules\recoil\dist\recoil.js:1:14988)

Example consumer code:


export const projectNameState = selector({
  key: "projectName",
  get: ({ get }) => ...
});

export const ProjectNameProvider = () => {
  const [projectName, setProjectName] = useRecoilState(projectNameState);
  return (
    <ProjectNameComponent
      projectName={projectName}
      setProjectName={setProjectName}
    />
  );
};

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:9
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
davidmccabecommented, May 15, 2020

We plan to support SSR but haven’t really needed to try it yet. If you want to fix these window references that PR would be greatly appreciated.

3reactions
hampuskraftcommented, May 31, 2020

For now, I’ll downgrade to 0.0.7.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React JS Server side issue - window not found - Stack Overflow
BROWSER to my advantage because it will be defined as undefined if it is server side, and it will be true if the...
Read more >
window is not defined, when code render on server SSR ...
I'm using Next.js and there is no window when doing server side rendering.
Read more >
SSR issue: window not defined - Sitecore Stack Exchange
The window object is not available during server side rendering because Node.js does not have a window object - it is a browser...
Read more >
How to solve Next.js window is not defined
Solve a the common ReferenceError: next.js window is not defined error that you may see when doing SSR & pre-rendering.
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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found