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.

Rendering a component on the server requires DOM (document)

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
wereHamstercommented, Apr 24, 2017

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.

const React = require('preact-compat');
const {renderToString} = require('preact-compat/server');

const Bar = () => React.createElement('div');

class Foo extends React.Component {
  componentWillMount() {
    this.forceUpdate();
  }
  render() {
    return React.createElement(Bar);
  }
}

renderToString(React.createElement(Foo))
{
  "dependencies": {
    "preact": "^8.1.0",
    "preact-compat": "^3.15.0"
  }
}
1reaction
sirgallifreycommented, Feb 24, 2017

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

  • preact-render-to-string 3.6.0
  • preact 7.2.0

it would be helpful to know what versions are you using, @wereHamster including preact-compat and react-redux if you are using those.

Read more comments on GitHub >

github_iconTop 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 >

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