reactjs/react-rails

Server side rendering broken in 2.7

tf opened this issue · 2 comments

tf commented

Steps to reproduce

Render any component with prerender: true.

I've created this sample Rails app to reproduce the issue:
https://github.com/tf/react-rails-2-7-ssr-bug

Expected behavior

HTML is generated.

Actual behavior

Server side executed JS causes:

TypeError: Cannot read properties of undefined (reading 'version')

System configuration

  • Sprockets version: 4.2
  • React-Rails version: 2.7
  • Rect_UJS version: ?
  • Rails version: 7
  • Ruby version: 2.7

Probably caused by this line: https://github.com/reactjs/react-rails/pull/1269/files#diff-46071864dd030eabbafccec157424caade6e2c3b0f30ebaf9f4fd3f224377216R3

Maybe ReactDOM is not present in server context?

tf commented

Can confirm that changing the line to const reactMajorVersion = ReactDOM?.version?.split('.')[0] || 16 (inserting ? after ReactDOM) fixes the issue. Would still like to understand what ReactDOM is expected to be in the server context. Also is optional chaining safe to use here (https://caniuse.com/?search=%3F.)?

@tf
React-rails 2.7.1 just released. Please check and let us know if your issue is resolved.