react-rails: Server rendering with CommonsChunkPlugin raises webpackJsonp is not defined
I started a clean project using:
$ bundle install
$ rails webpacker:install
$ rails webpacker:install:react
$ rails generate react:install
$ rails g react:component HelloWorld greeting:string
Then I added a server render tag in my view:
<%= react_component('HelloWorld', {name: 'John'}, {prerender: true}) %>
And it worked as expected.
Then I tried to setup a commons chunk plugin in the webpack/environment.js file:
const { environment } = require('@rails/webpacker')
const webpack = require('webpack');
const commonLibraries = [
'/querobolsa/node_modules/jquery/dist/jquery.js',
];
environment.plugins.insert('CommonsChunkPlugin',
new webpack.optimize.CommonsChunkPlugin({
name: 'common',
minChunks: function (module, count) {
if (commonLibraries.indexOf(module.resource) !== -1) return true;
}
})
);
module.exports = environment
It makes the server rendering stop working with the error:
ReferenceError: webpackJsonp is not defined.
It looks like the compiled file server_rendering.js requires that the common.js chunk to be loaded first.
So I tried to add the following code in config/application.rb:
# Settings for the pool of renderers:
config.react.server_renderer_pool_size ||= 1 # ExecJS doesn't allow more than one on MRI
config.react.server_renderer_timeout ||= 20 # seconds
config.react.server_renderer = React::ServerRendering::BundleRenderer
config.react.server_renderer_options = {
files: ["common.js", "server_rendering.js"], # files to load for prerendering
replay_console: true, # if true, console.* will be replayed client-side
}
# Changing files matching these dirs/exts will cause the server renderer to reload:
config.react.server_renderer_extensions = ["jsx", "js"]
config.react.server_renderer_directories = ["/app/assets/javascripts", "/app/javascript/"]
And it raises the following error:
/tmp/execjs20180122-30273-6tq9dmjs:27938
/* 51 */,
^
SyntaxError: Unexpected token ,
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:588:28)
at Object.Module._extensions..js (module.js:635:10)
at Module.load (module.js:545:32)
at tryModuleLoad (module.js:508:12)
at Function.Module._load (module.js:500:3)
at Function.Module.runMain (module.js:665:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
Please, can someone give me a solution to this problem? I know if I skip the server_rendering in commons chunk process it would work, but I don’t know how to do this.
System configuration
Webpacker version: 3.2.1 React-Rails version: 2.4.3 Rect_UJS version: 2.4.3 Rails version: 5.1.4 Ruby version: 2.4.1
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (8 by maintainers)
I managed to get this working on our main project by stopping the
server_rendering.js
file from being chunked.We’re using fairly standard setups for
webpacker
andreact-rails
, withapplication.js
andserver_rendering.js
entry points. I began by using the Webpacker example for setting up the CommonChunksPlugin (https://github.com/rails/webpacker/blob/master/docs/webpack.md#add-common-chunks):I set the first chunk to only run on the
application
entry point, thereby ignoringserver_rendering
.Then I set the second chunk (which extracts the Webpack runtime) to only run on the chunk called
vendor
we just created - effectively again ignoringserver_rendering
, because it didn’t get chunked out to vendor.So I wound up with:
In
application.html.erb
, we now load three JS files on the client side:Hope that helps! For reference, I ran
bin/webpack --display-entrypoints
for more detailed output:So you see
application
gets split into three, whileserver_rendering
stays whole.The simple solution is make sure your
server_rendering.js
isn’t chunked. There is no need for doing it as chunking is only for the client’s benefit, and it makes the code more complex.The first possible solution is that there is some sort of name conflict with the chunk and file name, and the other one was look at other react with rails solutions, the next best one being https://github.com/renchap/webpacker-react incase that doesn’t have the chunking issues.
I took @RiccardoMargiotta’s solution and made it a bit more dynamic. This is what I ended up with, in case it helps anyone:
@jDeppen Ah, we’re not actually using sourcemaps, we’ve updated our webpack
development.js
andproduction.js
configs to remove them. I’m not sure if you’d be able to get around that while still inlining the file.@raapperez You might find you solution in rails/webpacker#119 or webpack/webpack#368