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

add logic to solve for window not defined error; fix line indents #1

Merged
merged 1 commit into from
Jul 6, 2022

Conversation

derrak
Copy link
Collaborator

@derrak derrak commented Jul 6, 2022

Some clues and explanations of the error:
Just one of the gotchas of server-side rendering with React.
When Node tries to build, it instantiates the class, and in the constructor finds window, which is a browser global (Node doesn’t have window). gatsbyjs/gatsby#5835

Some of your code references “browser globals” like window or document that aren’t available in Node.js. If this is your problem you should see an error above like “window is not defined”. To fix this, find the offending code and either a) check before calling the code if window is defined so the code doesn’t run while Gatsby is building (see code sample below) or b) if the code is in the render function of a React.js component, move that code into...
https://www.gatsbyjs.com/docs/debugging-html-builds/#how-to-check-if-code-classlanguage-textwindowcode-is-defined

Solution was to wrap our code in an if/else block that checks is window is defined, (which it should be when the code is interpreted in the browser.)
https://bobbyhadz.com/blog/javascript-referenceerror-window-is-not-defined#:~:text=the%20following%20way%3A-,index.js,are%20on%20the%20server%27)%0A%20%20//%20%E2%9B%94%EF%B8%8F%20Don%27t%20use%20window%20here%0A%7D,-We%20check%20if

@AsheUrban AsheUrban merged commit 6296c24 into main Jul 6, 2022
@AsheUrban AsheUrban deleted the dr branch July 6, 2022 15:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants