react-rails: Server-side rendering is not working with sprockets-rails 3.0
In sprockets-rails 3.0 Rails.application.assets
is disabled when config.assets.compile=false
(https://github.com/rails/sprockets-rails/pull/220).
And React::ServerRendering::SprocketsRenderer
is failing on production environment at this line, with undefined method '[]' for nil:NilClass
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 53 (19 by maintainers)
Commits related to this issue
- try fix from https://github.com/reactjs/react-rails/issues/443#issuecomment-180544359 — committed to andrewpatterson3001/action-cable-chat-second-attempt by andrewpatterson3001 8 years ago
- workaround for react server render error https://github.com/reactjs/react-rails/issues/443#issuecomment-180544359 — committed to koffeinfrei/unnote by koffeinfrei 8 years ago
- quick fix — committed to C-Higgins/Go by C-Higgins 7 years ago
- Server side react assets should also be precompiled Fixes run time bug where react components won't load with 500 error Solution taken from: https://github.com/reactjs/react-rails/issues/443#issueco... — committed to abhchand/reely by abhchand 6 years ago
I can confirm that I only had to add
Rails.application.config.assets.precompile += %w( react-server.js components.js )
inconfig/initalizers/assets.rb
to get server side rendering work on Heroku.Ohh strange. When it creates the manifest, the manifest has
production/react-server
instead ofreact-server
. Then, SprocketsRenderer fails to findreact-server
since it has a different logical path.I found a workaround:
Add a JS file,
server_rendering.js
Make sure
server_rendering.js
is compiled (Rails.application.config.assets.precompile += %w( server_rendering.js )
)Add to
server_rendering.js
:Specify only
server_rendering.js
for server rendering:This way, it bundles react-server into server_rendering.js, it doesn’t look it up when you try to render a page!
I’ll look for a proper fix tonight or tomorrow!