Node server rendering
Node server rendering gives Rails a way to produce the first HTML for selected React components before the browser takes over. Rails chooses the component and page data, the Node renderer evaluates the server bundle, and React on Rails returns the rendered result to the Rails view. This path improves the first page response while keeping later interaction in the browser.
Rendering boundary
The Rails-to-Node boundary separates page composition from React execution. Rails owns the request, route, view, and initial data. Node owns the server evaluation of the registered React component and sends the result back to Rails.
Rails request path
The page request starts in Rails and reaches a view helper such as react_component. The helper uses the configured server bundle when prerendering is enabled. Rails then places the returned HTML inside the component mount point and emits the data needed for browser hydration.
The root route uses this path for RouterApp. The /simple page opts out of prerendering, so it leaves an empty mount point for browser-only rendering instead. See React rendering modes for the wider comparison of these choices.
References: config/routes.rb• L11-19 app/…/pages_controller.rb• L28-40 app/…/index.html.erb• L200-205 app/…/simple.html.erb• L13-16 config/…/react_on_rails.rb• L19-23 config/…/react_on_rails.rb• L46-48
Node renderer service
The Node renderer is a separate local or deployed service started by renderer/node-renderer.js. React on Rails is configured to use NodeRenderer and to call the service at RENDERER_URL. The shared password protects this boundary. In production the Node process requires an explicit RENDERER_PASSWORD, and Rails does not fall back to another JavaScript renderer when the Node service is unavailable.
The service keeps a bundle cache and can use several workers. It also provides the runtime values needed by the server-rendered examples, such as URL and AbortController. These values let code that expects a Node-like runtime work during server rendering.
References: renderer/node-renderer.js• L1-10 renderer/node-renderer.js• L31-56 config/…/react_on_rails_pro.rb• L7-14 config/…/react_on_rails_pro.rb• L21-24
Server bundle entry points
The server bundle is the private JavaScript artifact that the Node renderer evaluates. It is separate from browser assets and uses a stable name so Rails and the renderer can refer to the same artifact across environments.
Source entry point
The fully qualified source entry point is client/…/server-bundle.js. It is a small assembly point that loads store registration and the generated server registration module. The generated module is the link between the React on Rails pack task and the components that the Node renderer can resolve by name.
The server registration module registers the React components and Redux stores used by the examples. This registration is the contract used by the Rails view helper: a view names a component such as RouterApp, and the server bundle provides that name for server evaluation.
References: client/…/server-bundle.js• L1-5 client/…/serverRegistration.jsx• L1-23
Build entry point
The server build selects the server-bundle entry from the Shakapacker-generated entries. It writes a Node-compatible CommonJS bundle to the private ssr-generated output path. The client build removes this entry, which prevents the browser asset build from trying to load server-only code.
The build can produce the client, server, and React Server Components bundles together. Development can watch only the server bundle with SERVER_BUNDLE_ONLY=yes. The RSC bundle is a separate artifact named rsc-bundle.js; it supports the React Server Components flow rather than the classic HTML prerendering path described here. See React Server Components for that flow.
References: config/…/serverWebpackConfig.js• L19-29 config/…/serverWebpackConfig.js• L32-49 config/…/serverWebpackConfig.js• L72-80 config/…/clientWebpackConfig.js• L22-26 config/…/webpackConfig.js• L16-34 config/…/rscWebpackConfig.js• L3-9 config/…/rscWebpackConfig.js• L49-50 config/shakapacker.yml• L3-17
Registered React entry points
The registered React entry points define what a server-rendered page means. Each entry receives the props and Rails context for the request, selects any shared state, and returns the component tree that Node renders.
Routed application entry point
RouterApp is the main server-rendered example. Its server entry reads the current request location from railsContext and uses StaticRouter around the shared route tree. This lets a direct request to /react-router/... render the same route that the browser later manages with BrowserRouter.
The server entry also uses the router-aware Redux store. The store and route tree therefore cross the boundary together: Rails supplies the request context, the server selects the initial route, and the browser continues from the HTML with the matching client entry.
References: client/…/RouterApp.server.jsx• L1-41 client/…/RouterApp.client.jsx• L1-21 client/…/routes.jsx• L1-16
Shared shell entry points
The application layout mounts shared React components such as NavigationBarApp and Footer around page content. These components use the same registration boundary as page components, so the shell can be server-rendered when the page uses the normal prerender setting.
NavigationBarApp chooses whether to use a store from the Rails pathname. This keeps the navigation connected to comment state on the router and no-router pages while allowing it to render without that state on other tutorial pages.
References: app/…/application.html.erb• L1-12 client/…/NavigationBarApp.jsx• L19-44 client/…/serverRegistration.jsx• L12-23
Server-rendered page flow
The classic server-rendering flow combines a Rails route, a React on Rails mount, a registered server entry, and a browser hydration step. The following diagram shows how a direct request becomes HTML and then an interactive React page.
sequenceDiagram
participant Browser
participant Rails
participant Node as Node renderer
participant Bundle as Server bundle
Browser->>Rails: Request page
Rails->>Rails: Select route, view, props, and Rails context
Rails->>Node: Request component render
Node->>Bundle: Evaluate registered component
Bundle-->>Node: Rendered React output
Node-->>Rails: Render result
Rails-->>Browser: HTML with mount and hydration data
Browser->>Rails: Load browser assets as needed
Browser->>Browser: Hydrate the matching React tree
Initial data flow
The controller prepares data before the view calls React on Rails. On the root and no-router pages, the controller also seeds a named Redux store. The shared application layout emits the store hydration data so the browser can rebuild the same initial state instead of starting from a different comment list.
This contract matters for server-rendered output because the server and browser must see equivalent props, store state, component names, and route context. A mismatch can produce different HTML or a hydration warning even when the Node service itself is healthy.
References: app/…/pages_controller.rb• L6-35 app/…/pages_controller.rb• L50-61 app/…/application.html.erb• L1-12 client/…/serverRegistration.jsx• L12-23
Route context flow
Rails remains the first request handler for React Router paths. The wildcard route sends /react-router/*all to the same page action, and the server entry reads the resulting location from the Rails context. The shared route tree then chooses the page content for that location.
After the first response, the client entry replaces StaticRouter with BrowserRouter. This preserves the same route model while moving later transitions fully into the browser.
References: config/routes.rb• L11-19 client/…/RouterApp.server.jsx• L10-37 client/…/RouterApp.client.jsx• L10-18 client/…/routes.jsx• L8-16
Rendering guarantees
The server-rendering checks confirm the boundary at the HTTP response rather than only checking that the Node process starts. The root response contains rendered content inside the RouterApp mount, and the rendered route link has the expected path. The simple page has no server-rendered children because its view explicitly disables prerendering.
These checks define the useful product behavior: selected pages arrive with meaningful React HTML, while pages marked browser-only remain empty until client JavaScript runs.
Service readiness
The development and test process starts the Node renderer before requests that need server rendering. CI waits for the service to listen on port 3800 before it runs the Rails request suite. This catches a missing or unhealthy renderer before the response assertions run.
References: spec/…/server_render_check_spec.rb• L5-25 .github/…/rspec_test.yml• L85-110 Procfile.dev• L10-17
Related rendering systems
Classic server rendering returns HTML for a normal Rails response. React Server Components use a different bundle and a streamed payload route, even though both systems share the Node renderer service and the React on Rails integration.
The server-components page opts into stream_react_component and uses rsc-bundle.js. Treat changes to that path as a related but separate contract. The React on Rails integration page covers the broader Rails and React registration model.
React Server Components boundary
The RSC configuration uses the same server build foundation but changes the entry name and React resolution mode for server-component payloads. The Rails initializer enables RSC support and declares the payload path. This boundary does not replace the server-bundle.js path used for ordinary react_component prerendering.
References: app/…/pages_controller.rb• L42-46 app/…/server_components.html.erb• L1-5 config/…/react_on_rails_pro.rb• L26-29 config/…/rscWebpackConfig.js• L3-9 config/…/rscWebpackConfig.js• L38-50 config/routes.rb• L3-4
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the React component names in the Rails views aligned with the names registered by the server bundle.
- Keep server-bundle.js, the private server output path, and the React on Rails bundle setting aligned across build and Rails configuration.
- Keep the Node renderer URL and password aligned with the Rails Pro renderer settings in every environment.
- Keep server and client router entries aligned so the HTML route matches the route selected after hydration.
- Keep controller store setup, layout hydration data, and client store registration aligned when initial props change.
- Keep the classic SSR bundle separate from the rsc-bundle.js contract when changing React Server Components support.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The Node renderer is not running or its URL or password does not match Rails | A prerendered request cannot cross the Rails-to-Node boundary | Start the renderer and keep RENDERER_URL and RENDERER_PASSWORD synchronized. |
| server-bundle.js is missing from the expected pack entry or private output | Rails cannot resolve the registered component for server rendering | Keep the pack entry, server build entry, and server_bundle_js_file setting aligned. |
| A component name is changed in a view but not in server registration | The request asks the bundle for an unknown component | Change the view mount and registration as one contract. |
| Server and client router entries use different location or route behavior | The browser can disagree with the HTML after hydration | Keep the shared route tree and the server and client router boundaries aligned. |
| Initial Redux state differs between Rails props and browser hydration data | The browser can replace or warn about server-rendered content | Keep controller store setup, layout hydration output, and client store registration consistent. |
| The server bundle is included in the client build | Browser loading can fail on server-only assumptions such as window or bundle chunks | Build the server entry only through the server configuration. |
| A server-rendered component depends on a runtime global that the Node sandbox lacks | Rendering fails before Rails can return the page | Add required runtime context through the Node renderer configuration and verify the affected page response. |
Key files
| File | Why to read it |
|---|---|
| renderer/node-renderer.js | Defines the Node service boundary, runtime context, worker settings, and production password rule. |
| config/…/react_on_rails_pro.rb | Connects Rails to NodeRenderer, its URL, its password, and the separate RSC settings. |
| config/…/react_on_rails.rb | Defines the classic server bundle name, private output path, prerender default, and server error behavior. |
| client/…/server-bundle.js | Provides the stable source entry for the private server bundle. |
| client/…/serverRegistration.jsx | Registers the components and stores available to server-rendered examples. |
| config/…/serverWebpackConfig.js | Shapes the Node-compatible server artifact and its private output. |
| config/…/webpackConfig.js | Selects client, classic server, or RSC builds for each build mode. |
| client/…/RouterApp.server.jsx | Shows how Rails location and the shared route tree become server-rendered React output. |
| client/…/RouterApp.client.jsx | Shows the browser-side continuation of the server-rendered router tree. |
| app/…/pages_controller.rb | Shows where Rails prepares initial data and selects the page rendering path. |
app/views/pages/index.html.erb | Shows the main server-rendered React mount used by the tutorial. |
app/views/layouts/application.html.erb | Shows the shared React shell and Redux hydration boundary. |
| spec/…/server_render_check_spec.rb | States the response-level guarantees for prerendered and browser-only pages. |