fix `window is not defined` for server-side render react-rails with webpacker split chunks in Rails

--

TL:DR you need to remove server_side_rendering.js from your splitChunks. We did this in config/webpack/environment.js

credit to Riccardo Margiotta who supplied this solution here https://github.com/reactjs/react-rails/issues/970#issuecomment-500349150

code snippet available at the end of the blog

This was an issue we struggled with for quite a while so I’m writing this up so others may find the answer a bit faster then trawling through Github issues.

Code snippet of above image

const { environment } = require('@rails/webpacker')const notServerRendering = name => name !== 'server_rendering'const splitChunksConfig = {
optimization: {
splitChunks: {
chunks(chunk) {
return notServerRendering(chunk.name);
}
}
}
};
environment.config.merge(splitChunksConfig)
environment.config.set('output.filename', 'js/[name].js')
environment.config.set('output.chunkFilename', 'js/[name].js')
module.exports = environment

--

--

No responses yet