Rendering a component on the server requires DOM (document)
See original GitHub issueI have the following stack trace when I try to render one of my pages on the server:
ERROR in ReferenceError: document is not defined
at createNode (static:3586:163)
at idiff (static:3627:26)
at diff (static:3602:20)
at renderComponent (static:3771:33)
at renderComponent (static:3762:26)
at cl.extend.forceUpdate (static:3917:14)
at static:55402:16
at Object.sig.subscribe (static:55288:10)
at cl.React.createClass.componentWillMount (static:55401:21)
at Object.renderToString (static:4546:34)
Somehow a code path is taken which causes a crash due to non-existing document
in the global scope. I tried to follow the code in renderComponent
but didn’t get far.
Issue Analytics
- State:
- Created 7 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
ReactDOM – React
Render a React element into the DOM in the supplied container and return a reference to the component (or returns null for stateless...
Read more >Preparing React Components for Server–Side Rendering
In this post we'll explore a few tips to prepare your React components for server–side usage. One of the great things about React...
Read more >rendering react on server - node.js - Stack Overflow
For an isomorphic React application, you need to have a client.js file that calls the render function for the same component you are...
Read more >React Server Components. - It's not server-side rendering.
The Webpack build for the client-side React bundle, using the scripts/build.js script. Looking at the server script, you can see the app.server.
Read more >Managing DOM components with ReactDOM - LogRocket Blog
js server using hydrate , we need to remove all references to the window and document , because we will render our markup...
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
The error can be reproduced by this code. You need a double-nested component for the bug to be triggered. This is why you couldn’t reproduce it, because you always returned a DOM element in your render functions.
I wasn’t able to reproduce the error 😦 I tried the promise example, I tried setTimeout, I tried setting a ref to setState and calling it after the render, I tried calling forceUpdate on promise, on setTimeout and none of those are throwing errors, at least not on my setup. I could just apply the correction @developit suggested but as I’m in love with TDD I can’t fix what I can’t reproduce.
It’s worth mentioning that I made these tests on
it would be helpful to know what versions are you using, @wereHamster including
preact-compat
andreact-redux
if you are using those.