shakacode/react_on_rails

Setting webpack target to node

ArthurWD opened this issue · 1 comments

I want to use react-markdown during prerender. In the default setup, it will load the web bundle for module decode-named-character-reference, which is calling document on load. I tried setting the webpack target to node in serverWebpackConfig.js, so it would pick the node bundle for decode-named-character-reference which doesn't call document.

Environment

  1. Ruby version: 2.7.5
  2. Rails version: 6
  3. Webpacker version: 5.73
  4. React on Rails version: 13

Expected behavior

Correctly prerender with target = node.

Actual behavior

It raises during prerender:

ReferenceError: require is not defined
Object.stream ((execjs):428:1)
__webpack_require__ ((execjs):452:42)
eval (webpack:///./node_modules/react-dom/cjs/react-dom-server.node.development.js?:18:14)
eval (webpack:///./node_modules/react-dom/cjs/react-dom-server.node.development.js?:4382:5)
Object../node_modules/react-dom/cjs/react-dom-server.node.development.js ((execjs):118:1)

It seems to me that require should be defined in node?
Note, it is using react-dom-server.node.development.js instead of react-dom-server.browser.development.js, which is caused by changing the target to node.

Small, reproducible repo

Clone https://github.com/shakacode/react_on_rails_demo_ssr_hmr
Uncomment // serverWebpackConfig.target = 'node' in serverWebpackConfig.js.

@ArthurWD this requires using https://www.shakacode.com/react-on-rails-pro/ so that you use a Node renderer.

It looks like you're using execjs to do the rendering.