-
Notifications
You must be signed in to change notification settings - Fork 759
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
SplitChunks and SSR: Cannot read property 'serverRender' of undefined #970
Comments
I've created a repo using a fresh Rails 5.1.6.2 install that reproduces the issue using SplitChunks defaults: https://github.com/RyanMcDonald/react-rails-ssr-webpacker-splitchunks/blob/master/config/webpack/environment.js |
I found the issue and updated my sample repo with the solution that I found. It looks like react-rails's default
|
A few people have ran into issues with CommonsChunkPlugin or SplitChunksPlugin, I'll likely have to do something to some detection for both depending on which module is loaded but this is a really helpful tip. 👍 Thanks @RyanMcDonald this is the tip-off I needed. Edit: |
Thank you for reporting this, it helped me debug my application. |
I configured SplitChunks to ignore // We have two entry points, application and server_rendering.
// We always want to keep server_rendering intact, and must
// exclude it from being chunked.
const notServerRendering = name => name !== 'server_rendering';
module.exports = {
optimization: {
splitChunks: {
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks(chunk) {
return notServerRendering(chunk.name);
}
}
}
}
}
}; |
When importing the chunks in your rails layout, do you still use the javascript_packs_with_chunks_tag? |
We don't have that many chunks yet, mainly just an app and vendor chunk, along with a few used only in specific views. So we're just using standard pack tags for each one. |
@ericraio I checked and yes you can still use |
I tried the solutions mentioned here with a fresh Rails 6 app and nothing helped. Is there any movement on this? |
Same here. Rails 6. Neither monkey-patch nor chunk exclusion helped... |
This worked for me, too! But why does it work? UPDATE: |
My understanding was that I mentioned on the other post, I have an updated version of that snippet to also create a separate We still just have one large application bundle, though. In future, I'd like to at least route-split the app JS with additional entry points, but haven't gotten that working with the // We have two entry points, application and server_rendering.
// We always want to keep server_rendering intact, and must
// exclude it from being chunked.
const notServerRendering = name => name !== 'server_rendering';
module.exports = {
optimization: {
splitChunks: {
chunks(chunk) {
return notServerRendering(chunk.name);
},
cacheGroups: {
vendor: {
name: 'vendor',
priority: -10,
test: /[\\/]node_modules[\\/]/
},
vendor_react: {
name: 'vendor_react',
test: /[\\/]node_modules[\\/](react|react-dom)[\\/]/
}
}
}
}
}; <%= javascript_pack_tag "vendor_react.js", defer: true %>
<%= javascript_pack_tag "vendor.js", defer: true %>
<%= javascript_pack_tag "application.js", defer: true %> |
Hey @RyanMcDonald, thanks so much for your example and the repro! Between the two, I got this working as well. For anyone else who attempts this next, one other piece that I needed to do was add this: // environment.js
environment.config.set(
'output.globalObject',
"(typeof self !== 'undefined' ? self : this)"
) Otherwise you'll get a |
I was not able to get the |
As per the above conversations, we have a resolution provided by @RyanMcDonald. Closing the issue. |
Steps to reproduce
Expected behavior
The component should be rendered on the page.
Actual behavior
ReferenceError: window is not defined
globalObject
:#<ExecJS::ProgramError: TypeError: Cannot read property 'serverRender' of undefined>
Stack trace
System configuration
Sprockets or Webpacker version:
Webpacker 4.0.2
React-Rails version:
2.4.7
Rect_UJS version:
2.4.4
Rails version:
5.1
Ruby version:
2.5.1
It works perfectly fine when SplitChunks is not enabled. There is something happening where the
ReactRailsUJS
object is being lost on the server, so this line fails.This is the SplitChunks config that I'm using:
The text was updated successfully, but these errors were encountered: