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

Builds fail for components requiring window #16

Closed
jmandel1027 opened this issue Oct 9, 2017 · 4 comments
Closed

Builds fail for components requiring window #16

jmandel1027 opened this issue Oct 9, 2017 · 4 comments

Comments

@jmandel1027
Copy link

Hi there!

Really great work on this, very nice alternative to Next + Gatsby and very slick. I've been getting errors on my build, I have 2 components that need the browsers window object and they crash the build process, I've tried a few different thinks like ErrorBoundary etc. My project runs just fine in the dev environment (minus my font but ill get to that later).

Here is the error: ReferenceError: window is not defined

Here is the Repo

Anyway, thanks for this killer project, deff gonna use this for some more things in the future.

@tannerlinsley
Copy link
Contributor

Since react-static builds static versions of your site, most times you'll need to make sure the window object is available (which is essentially making sure you are in a browser environment). For most cases, simply wrapping your browser-only code in something like this is sufficient:

if (typeof window !== 'undefined') {
  // browser-only code
}

@ineghi
Copy link

ineghi commented Dec 24, 2017

Hi guys !

Is React static can be used in other context than browser ?
If no, could we assume document and window are globally available and get rid on conditionnal imports and stuff like that everywhere ? Or better, an option to make some variables globally accessible in the react static config, to let the project versatile.

let SmoothScroll
if (typeof window !== 'undefined') {
    SmoothScroll = require('smooth-scroll')
}

this exemple works, but when you are using multiple front libraries it quickly become a mess ^^

What you think ?

@tconn89
Copy link

tconn89 commented May 1, 2018

Hello, I'm also having troubles with window variable.

When I run react-static start the bundle compiles successfully, even though I have things like

componentDidMount(){
  window.addEventListener('resize', this.myFlexStyle)
}

But when I run react-static build it fails. Why does the first command above succeed?

@tannerlinsley
Copy link
Contributor

You must write node safe code. Using window without a document check will error in the build stage because there is no DOM. https://react-static.js.org/docs/concepts#writing-universal-node-safe-code

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

No branches or pull requests

4 participants