Closed rubenochiavone closed 1 year ago
Seem related to either a configuration issue or maybe using the dynamic bundle loading and the bundles are loaded twice. Check out the source of the pages.
I had this issue once and the solution was to change the defer strategy to false
<%= javascript_pack_tag 'app', defer: false%>
With the defer equals to false the script is executed as soon as it's encountered in the document, which might block the HTML parsing until the script is fully loaded and executed
Thanks for the comments @justin808 and @devjoaov.
After digging into this issue with the help of @justin808 and @Judahmeek, we found out that the root cause was a tweak in the webpack's optimization configuration. Setting optimization.runtimeChunk
to true
or multiple
causes webpack to load ReactOnRails one time for each runtime chunk which leads to ReactOnRails global variable being overridden and some registered components going missing - see https://webpack.js.org/configuration/optimization/#optimizationruntimechunk.
So, to overcome this issue, we could use shakapacker's default optimization configuration (pseudo-code):
const { webpackConfig: baseClientWebpackConfig } = require('shakapacker');
// ...
config.optimization = baseClientWebpackConfig.optimization;
As it set the optimization.runtimeChunk
to single
. See its source:
package/environments/base.js:115
optimization: {
splitChunks: { chunks: 'all' },
runtimeChunk: 'single'
},
Or set optimization.runtimeChunk
to single
directly.
Configuring webpack to embed the runtime in each chunk and calling
react_component
multiple times in a rails view/partial causes the client render to crash with the following error:Webpack configuration
Rails view
Environment
Expected behavior
Should render both components.
Actual behavior
Crashes on the client with the aforementioned error/stacktrace.
Small, reproducible repo
Checkout the branch rubenochiavone/repro-issue-1558 and open the dummy/app in the following URL: http://localhost:3000/render_with_layouts_wrapper. You should see the following errors in the dev-tools console: