-
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
HMR and Server-Side rendering results in window is not defined
#925
Comments
Reproduction script: #!/bin/bash
gem install rails --version=4.2.0 --no-ri --no-rdoc
rails _4.2.0_ new foo
cd foo
bundle install
bin/rails g scaffold bar
echo "gem 'webpacker'" >> Gemfile
echo "gem 'react-rails'" >> Gemfile
bundle install
bin/rake webpacker:install
bin/rake webpacker:install:react
bin/rake db:migrate RAILS_ENV=development
bin/rails g react:install
bin/rails g react:component HelloWorld greeting:string
echo "<%= react_component('HelloWorld', { greeting: 'hello' }, prerender: true) %>" >> app/views/bars/index.html.erb
# adding "<%= javascript_pack_tag 'application' %>" to `body` in app/views/layouts/application.html.erb
# change 'hmr: false' to 'hmr: true' in 'config/webpacker.yml'
# start 'bin/webpack-dev-server'
# start 'bundle exec rails server -e development'
# request 'http://localhost:3000/bars' The last five steps needs to be done manually, the lines before can be executed as a shell script and results in the described |
window is not defined
window is not defined
Thank you a bunch for the reproduction, that clears it up. I'll make a guess that HMR is causing SSR to fail, which would make sense as there is no reloading in a serverside world. We don't currently have the ability to support HMR so I'm unable to help you at this time. It is a desirable feature though. |
Thank you for the confirmation ( and for your work on My team is just starting to migrate our project to use As you pointed out it does not make sense to have |
For now I recommend not using HMR when using SSR in this gem. It's part of the longer-term plan that I would like it to work successfully but I haven't had the time to be able to enable it yet. It's possible to get them to work together as I'm keeping my eye on the renchap/webpacker-react project as they are working at merging the HMR with SSR in this PR. It's definitely our project to sort as the upstream webpacker gem does let you use HMR, we just need to be clever about not running the |
Interesting, thanks for the explanation! I'm looking forward to it 😊 |
Is there any way for me to configure react-rails such that it would ignore prerender: true when hmr: true in webpacker.yml? Thus I could turn SSR on/off via the single hmr flag? |
Interestingly, I have |
Well, It is because somewhere in the config i setup jQuery. This was the reason for |
I'm facing the same issue with the new version of https://github.com/shakacode/react_on_rails. With the simplest hello_world example that uses the same bundle for both client and server rendering from the running of bin/webpack-dev-server with the default rails/webpacker setup, 5.1.1, server rendering errors out due to references to One cause of this reference to window is that the default globalObject value is "window." Setting that value to "this" on the server bundle solves that problem. There might be others like this. I suspect that it makes much more sense to just skip the idea of having the webpack-dev-server produce bundles for SSR. My solution for React on Rails is to have a separate bundle for SSR and to use Also, for hot reloading, react-hot-loader is deprecated. I'm having good luck in using https://github.com/pmmmwh/react-refresh-webpack-plugin. The setup is dead simple. The only tricky part is to make sure this is not included in the server bundle, and if you're using loadable-components, then you want to have a setup that allows running HMR/hot-reloading without loadable-components. Just to give full disclosure, I'm the creator of the https://github.com/shakacode/react_on_rails. I'm actively working in this area as my company https://www.shakacode.com/react-on-rails-pro is providing support for the integration of React with Ruby on Rails. That includes support for vanilla rails/webpacker and this gem. For those of you digging into the rails/webpacker part of the webpack config, you might find my RailsConf 2020 talk helpful: Webpacker, It-Just-Works, But How?. |
I've made some more progress on this issue: If you are using server-side rendering, the
|
Wanted to follow up and confirm that it's still not recommended to allow for HMR when using SSR in this gem? I know @BookOfGreg mentioned it was part of a longer term plan to allow, so just wanted to see if it's been revisited. Thanks! |
@mariaxtina we've got HMR working nicely with SSR, Webpack 5, and pretty much everything with https://github.com/shakacode/react_on_rails. There should be no reason this won't work for react-rails. If anybody here is interested support for react-rails, please see https://www.shakacode.com/react-on-rails-pro/. I'd be happy to provide support. |
see #985 (comment) for a |
Closing this issue. Please refer to 985 comment. |
Steps to reproduce
<%= react_component('HelloWorld', { greeting: 'hello' }, prerender: true) %>
webpacker.yml ( diff to default, docker environment )
Expected behavior
Rendering of page
Actual behavior
ExecJS::ProgramError ReferenceError: window is not defined
System configuration
Sprockets or Webpacker version: 3.12.0
React-Rails version: 2.4.7
Rect_UJS version: 2.4.4
Rails version: 4.2
Ruby version: 2.3
When I request the page without server rendering (e.g.
<%= react_component('HelloWorld', { greeting: 'ho' }) %>
) or when I disableHMR
(e.g.hmr: false
) it works fine. But leaving it results in described error.Since there is a fixed issue with the same error in #615 I wonder if I'm missing something obvious? Something like
prerender_polyfill.js
doesn't seem to work in my case.Stack trace beginning from a view file:
In case you wonder
react_component
inapplication_helper
does nothing exciting, just sets instance variable and callssuper
.The text was updated successfully, but these errors were encountered: