React Server Components integration
React Server Components (RSC) integration connects a Rails application, a Node renderer, and the browser so that server components render on the server while client components remain interactive in the browser. React on Rails Pro sends the rendered tree as a stream, uses that stream for the first HTML response, and reuses the embedded result for hydration or later route updates.
System roles
The integration divides work by runtime. Rails owns the web request and application data, the Node renderer runs React in a server runtime, and the browser displays HTML and activates client components.
Bundle roles
The bundle set gives each runtime the view of the component tree that it needs. The RSC bundle renders server components and records client component references. The server bundle turns that result into HTML. The client bundle supplies the browser code for those client components.
| Runtime | System role | Result |
|---|---|---|
| Rails | Starts page or payload requests and supplies request data | HTTP response or RSC payload stream |
| Node renderer | Runs the RSC and server render work | Server component output and HTML stream |
| Browser | Reads the embedded or fetched payload | Hydrated React tree with interactive client components |
References: packages/…/server.rsc.ts• L20-39 packages/…/server.tsx• L30-41 packages/…/client.tsx• L49-60
Request ownership
Rails remains the application boundary. Controllers and views choose the component and props. The Node renderer receives a render request and returns React output for Rails to stream. It does not replace Rails data access or authorization.
References: react_on_rails_pro/…/rsc_payload_renderer.rb• L25-47 react_on_rails_pro/…/routes.rb• L18-24 packages/…/handleRenderRequest.ts• L286-302
Hydrated store coordination
The browser also coordinates page data needed by components that use hydrated stores. Store generators are application code that persists while bundles stay loaded. Hydrated stores are page data created from the current page props, so the registry clears those values during soft navigation while keeping pending requests able to wait for the next page's hydration data.
This separation prevents a store from an old page from satisfying a dependency on a new page. The same wait-and-timeout model also lets component rendering wait for registrations that arrive later in the page lifecycle.
References: packages/…/StoreRegistry.ts• L19-37 packages/…/StoreRegistry.ts• L126-143 packages/…/CallbackRegistry.ts• L89-107 packages/…/ClientSideRenderer.ts• L648
Rendering pipeline
An initial RSC page uses one server-side pipeline to produce visible HTML and the data needed for browser hydration. The browser can show the shell while slower component content continues to arrive.
Node RSC capability
The Node RSC capability turns the server component result into a stream that the HTML renderer and browser can consume. It keeps the build's client manifests and identity aligned with the render, adds async prop access to the component props, and reports client-only hook errors with guidance about the "use client" boundary. If the browser disconnects, it stops the related RSC work and releases request-scoped resources.
This capability is the Node-side link between Rails render requests and the transport stages below. It preserves the stream's error state so Rails can avoid treating a failed render as reusable output, while normal errors remain visible to the matching server or browser error path.
References: packages/…/proRSC.ts• L62-87 packages/…/proRSC.ts• L89-176 packages/…/proRSC.ts• L183-212
Initial page render
The initial page render starts in a Rails view and ends as one streamed response. The Node renderer creates the RSC payload first. The server render turns that result into HTML while React on Rails Pro keeps the payload available for the browser.
The main sequence is:
- Rails starts a streamed component render with a component name and props.
- The Node renderer runs the server component tree.
- React produces an RSC payload with server output and client component references.
- The server render produces HTML from that result.
- Rails streams HTML and embedded payload data to the browser.
- The browser uses the embedded data to hydrate the same tree.
This diagram shows how the server result supplies both the HTML response and the browser hydration data.
flowchart LR
A[Browser page request] --> B[Rails controller and view]
B --> C[Node renderer]
C --> D[RSC bundle]
D --> E[RSC payload]
E --> F[Server render]
F --> G[HTML shell and streamed content]
E --> H[Embedded payload registry]
G --> I[Browser hydration]
H --> I
For the detailed request stages and stream behavior, see RSC request and rendering flow. The wider Rails-to-Node rendering platform provides the request boundary and renderer lifecycle around this pipeline. See Pro rendering platform for that shared platform view.
References: react_on_rails_pro/…/stream.rb• L220-245 react_on_rails_pro/…/request.rb• L101-117 packages/…/getReactServerComponent.server.ts• L77-122 packages/…/streamServerRenderedReactComponent.ts• L194-245
On-demand component render
A component that is not part of the initial page can use the RSC payload route. Rails receives the component name and JSON props, checks whether the request is allowed, and asks the Node renderer for a payload. The browser decodes the response into React content.
The same path supports client navigation and refresh. It avoids a full page response when a client route needs new server content.
References: react_on_rails_pro/…/routes.rb• L18-24 react_on_rails_pro/…/rsc_payload_renderer.rb• L25-47 packages/…/getReactServerComponent.client.ts• L201-289
Deferred route render
An RSC route can defer its server content when its root is marked ssr=false. The page still renders the surrounding shell on the server. After the browser starts, the route fetches its payload and replaces its loading state with server component content.
This allows one page to keep server-rendered sibling routes while a selected route loads through the payload API. A normal client-rendered page does not load the RSC client fetch runtime or make an RSC payload request.
References: react_on_rails_pro/…/rsc_route_ssr_false.spec.ts• L43-86 react_on_rails_pro/…/rsc_route_ssr_false.spec.ts• L88-136 react_on_rails_pro/…/rsc_route_ssr_false.spec.ts• L185-203
Payload transport
The RSC payload is the transport form of a rendered server component tree. It carries tree data, references to client modules, and stream status so React can rebuild the tree in another runtime.
Payload forms
The payload has two main transport paths. The initial page path embeds payload pieces in script tags as Rails streams the HTML. The on-demand path sends the pieces through an HTTP response. In both cases the browser feeds the decoded stream to React rather than treating it as ordinary page JSON.
| Path | Producer | Consumer | Main purpose |
|---|---|---|---|
| Initial page | Rails and the Node renderer | Browser hydration | Avoid a second payload request after SSR |
| Payload route | Rails endpoint and the Node renderer | Browser route or component loader | Supply content after the page is loaded |
| Server-side bridge | Node renderer RSC output | Node renderer HTML render | Turn server component output into streamed HTML |
References: react_on_rails_pro/…/rsc_payload.text.erb• L15 packages/…/injectRSCPayload.ts• L48-85 packages/…/getReactServerComponent.client.ts• L329-364 packages/…/getReactServerComponent.server.ts• L30-74
Client references
Server components can include client components without sending their full implementation in the RSC bundle. The payload names the client module and its browser assets. A client manifest maps that name to the code that the browser must load.
This boundary keeps server-only code on the server. It also makes the manifest part of the render contract: the server payload and client bundle must describe the same client references.
References: docs/…/how-react-server-components-work.md• L9-17 docs/…/how-react-server-components-work.md• L79-85 packages/…/getReactServerComponent.server.ts• L37-51
Stream continuity
Streaming is part of the browser contract. The browser must receive the live response so each payload piece can unblock its matching React boundary. A proxy or cache that waits for the full response can leave HTML visible while hydration waits for data that has not arrived.
The Rails-to-renderer request supports streamed RSC output and can send the selected RSC bundle before the render request when the renderer does not have it. Rails keeps the renderer connection and response stream open for the render lifetime.
References: react_on_rails_pro/…/request.rb• L101-117 react_on_rails_pro/…/request.rb• L519-527 react_on_rails_pro/…/stream.rb• L463-515 packages/…/handleIncrementalRenderStream.ts• L167-199
Stylesheet readiness
Stylesheet readiness keeps streamed client-component content from appearing before its CSS. FOUC (Flash of Unstyled Content) is the visible interval in which content appears without its required styles.
The stream uses client asset information to place stylesheet links before HTML that a React reveal can make visible. Manifest CSS is the main source of client-reference style data. The stream can also infer CSS from a loadable-stats.json chunk map and use preload hints when direct mapping is not ready. The Node renderer reports a missing map once per process so deployment problems remain visible without stopping the render.
The stylesheet link and the streamed reveal must stay in the same order. This protects later boundaries during progressive rendering. It does not replace the shell stylesheets that control the first page paint.
References: packages/…/injectRSCPayload.ts• L126-139 packages/…/injectRSCPayload.ts• L297-357 packages/…/injectRSCPayload.ts• L1751-1770 packages/…/injectRSCPayload.ts• L1834-1847 packages/…/vm.ts• L63-80
Render diagnostics
An RSC stream can carry an error signal beside its payload. The browser uses this signal to let the matching error boundary handle a failed render. Server-side reporting remains the source of full diagnostics.
Development and test responses can include full render error metadata. Other environments receive only a generic error signal. When an error is present, other metadata is removed before the payload crosses into the browser.
References: packages/…/injectRSCPayload.ts• L92-123 packages/…/injectRSCPayload.ts• L2033-2048 packages/…/vm.ts• L1022-1059 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1446-1484
Cache layers
RSC uses separate caches for separate lifetimes. Server caches can reuse rendered bytes across requests. Page-scoped browser caches avoid duplicate work during one page lifetime. The embedded payload has priority because it already belongs to the current HTML response.
Server render cache
Server-side caching can store rendered RSC chunks and replay them to later requests. A cache key includes the build identity and function inputs so a new bundle does not silently reuse output from an old bundle. Concurrent requests for the same key share one render while the first result is being produced.
Rails view helpers provide a related cache for streamed HTML fragments. They do not write a stream that contains an error because a failed render must not persist for later visitors.
References: packages/…/unstable_cache.ts• L52-70 packages/…/unstable_cache.ts• L75-103 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L119-125 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1192-1224
Embedded payload registry
The Embedded Payload Registry is a page-scoped browser record populated while Rails streams the initial response. A matching component name, props value, and root can read from this record without making an HTTP request.
The registry is the first lookup for initial hydration. It is removed with the RSC bootstrap markers as the root is prepared. The decoded React result remains available to the Provider Cache.
References: packages/…/rscPayloadGlobals.ts• L16-30 packages/…/getReactServerComponent.client.ts• L367-423 packages/…/rscHydrationDom.ts• L108-135
Provider Cache
Each RSC provider owns a bounded cache of decoded payload promises. The key is the component name plus its props. A cache hit gives matching routes in that provider the same React result.
The Prefetch Store is a page-scoped store for decoded payload promises created before a route renders. On the first cache miss, a provider can adopt a matching entry. A failed prefetch removes its own entry, so the next render can fetch normally. An explicit refetch bypasses both prefetch adoption and the normal cache entry to ask Rails for fresh content.
Mounted routes retain their current cache entry while it is in use. When a refetch fails, the provider can keep the last successful payload available for route recovery instead of treating the new promise as usable content. A replacement payload is only marked successful after its stream completes without an error.
References: packages/…/RSCProvider.tsx• L217-275 packages/…/RSCProvider.tsx• L314-331 packages/…/RSCProvider.tsx• L333-503 packages/…/RSCProvider.tsx• L505-665 packages/…/RSCProviderCache.ts• L16-44 packages/…/RSCPrefetchStore.ts• L48-82
Browser lifecycle
The browser has two related jobs: hydrate HTML that already exists and fetch new payloads when the current tree changes. The client wrapper sets up the provider and selects the correct job from the state of the root.
Hydration root
The client renderer finds the Rails root, prepares the embedded RSC markers, and chooses hydration when the root contains server HTML. If the root is empty, it creates a new React root instead. The RSC provider surrounds both paths so later route renders use the same cache and fetch rules.
Hydration can proceed by boundary. A visible client component can become interactive while slower server content is still arriving. Stylesheet and script resources that arrive with the stream are moved into the document head before hydration uses them.
For the browser-side sequence and resource behavior, see RSC client hydration and routing.
References: packages/…/client.tsx• L103-145 packages/…/client.tsx• L162-191 packages/…/rscHydrationDom.ts• L23-47 packages/…/rscHydrationDom.ts• L108-135
Route updates
RSCRoute is the bridge for server content inside a client component. On the first render it reads embedded data when available. On client navigation it fetches the payload route. Its error boundary adds component context for synchronous reads, transport failures, and streamed descendant errors. Its handle can request fresh content without replacing the whole page.
Route loaders can call prefetch before navigation. The next provider cache miss can adopt that result. A direct refetch still means fresh data and does not reuse the prefetch entry.
In production, a failed refetch can keep the last successful route content visible and expose a ServerComponentFetchError through the route handle. A retry uses the route's current component name and props. A decoded Flight root can resolve before all descendants arrive, so recovery treats stream completion as the point at which the replacement payload is known to be successful.
References: packages/…/RSCRoute.tsx• L41-81 packages/…/RSCRoute.tsx• L155-291 packages/…/RSCRouteErrorBoundary.tsx• L25-95 packages/…/RSCStreamCompletion.ts• L16-20 packages/…/getReactServerComponent.client.ts• L399-426 packages/…/RSCPrefetchStore.ts• L57-69
Page teardown
RSC state is page-scoped rather than global application state. Soft navigation tears down mounted roots and clears prefetched entries so a later page cannot adopt data from the old page. The wrapper also unmounts the React root during navigation. Hydrated stores follow the same page boundary: old values are dropped without defeating the wait for new page data.
References: packages/…/RSCPrefetchStore.ts• L33-46 packages/…/client.tsx• L183-191 packages/…/rscPayloadGlobals.ts• L16-25 packages/…/StoreRegistry.ts• L126-143 packages/…/ClientSideRenderer.ts• L638-652
Security boundary
The RSC payload route is a server API, not a private browser detail. The component name and props come from the request. Rails must decide which caller may render which component before the Node renderer receives the work.
Payload authorization
The default route supports an authorizer hook or an app-owned controller with normal Rails filters. The authorization decision must use the Rails request and session. Props are input data and must not decide whether the request is allowed.
The route treats malformed props as a client error rather than passing invalid JSON into the render path. This keeps request validation at the Rails boundary.
References: react_on_rails_pro/…/rsc_payload_renderer.rb• L25-37 react_on_rails_pro/…/rsc_payload_renderer.rb• L61-76 docs/…/how-react-server-components-work.md• L223-239
Change checklist
When these files or sections are changed, remember to consider these:
- When a client boundary or registered component changes, keep the RSC bundle, server bundle, client bundle, and both manifests from the same build.
- When the payload route path changes, update both the Rails route and the Rails context value used by browser fetches.
- When payload framing or stream metadata changes, update the Rails request, the Node renderer bridge, and the browser decoder as one contract.
- When embedded payload markers or global names change, update injection, browser lookup, root preparation, and page teardown together.
- When a cache key changes, check the build identity, component props, Rails fragment cache, Provider Cache, and Prefetch Store for matching invalidation rules.
- When client stylesheet assets or chunk metadata change, keep manifest CSS, loadable-stats.json, stylesheet ordering, and streamed reveal behavior aligned.
- When route access changes, review the payload authorizer. Do not move access control into component props or client code.
- When RSC error metadata changes, keep the inline and fetched payload paths on the same fail-closed browser-facing allowlist while preserving full details for server-side errors.
- When refetch recovery changes, keep the route error boundary, Provider Cache's last-successful payload, and stream-completion state aligned so an early root resolution cannot hide a late descendant failure.
- When renderer request dependencies or React versions change, keep the Rails gem, Node package, RSC package, and dummy app lockfiles on compatible versions.
- When page lifecycle cleanup changes, clear page data without rejecting waiters that the next page must resolve, while still rejecting waiters that belong to an unloaded page.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Server and client manifests come from different builds | The browser cannot resolve a client reference during hydration | Publish the RSC bundle, server bundle, client bundle, and manifests as one build generation |
A server component uses a client-only hook without a "use client" boundary | The RSC runtime fails before it can produce the expected payload | Mark the component as a client component or move the hook and event logic into a client boundary |
| The payload route has no authorization rule | A caller can choose a component and props that Rails did not intend to expose | Configure rsc_payload_authorizer or use an app controller with request filters |
| A proxy buffers the streamed payload | HTML can appear while hydration remains blocked on unseen payload pieces | Keep the RSC response live from Rails or the Node renderer to the browser |
| A stream with an error is written to the render cache | Later requests receive the broken result until cache expiry | Keep error-containing stream chunks out of cache writes |
| A route changes props on every render | Each new key can cause another payload request and reduce cache reuse | Keep RSCRoute props stable and use explicit refetch for fresh data |
| The RSC payload path is missing from Rails context | Client navigation cannot fetch server content | Set the payload route and the matching render context path |
| loadable-stats.json is absent or unreadable | The stream cannot infer some client chunk stylesheets and uses fallback hints instead | Emit the file beside the server bundle when stylesheet inference is required and watch the renderer diagnostic |
An RSC route with ssr=false is treated as a normal SSR route | The route may request payload data during server rendering or log an SSR bailout | Keep the deferred route boundary and client root behavior aligned |
| A payload reports a rendering error with full metadata in a non-development environment | Server error details can reach the browser | Use the fail-closed hasErrors signal for browser payloads and keep full diagnostics in server-side reporting |
| A refetch treats root promise resolution as full stream success | A late streamed descendant error can be missed or the old route content can be restored at the wrong time | Track stream completion separately and let the route error boundary handle the completed result |
| Rails and the Node renderer use incompatible request protocol or versions | Render requests fail before React output reaches the page | Keep the Pro gem, renderer package, React RSC package, and lockfiles on compatible release lines |
| Hydrated stores survive a soft navigation | Components can read stores from the previous page instead of waiting for new page data | Clear hydrated values on unload while preserving the new page's wait path |
Key files
| File | Why to read it |
|---|---|
| react_on_rails_pro/…/request.rb | Shows how Rails selects the RSC bundle role and sends normal or streamed requests to the Node renderer |
| react_on_rails_pro/…/stream.rb | Shows how Rails starts and drains the streamed response |
| react_on_rails_pro/…/rsc_payload_renderer.rb | Shows the Rails boundary for payload requests, props, and authorization |
| react_on_rails_pro/…/routes.rb | Shows how the payload API becomes a Rails route |
| react_on_rails_pro/…/react_on_rails_pro_helper.rb | Shows Rails-side streaming, cache protection, and browser-facing RSC error metadata handling |
| packages/…/handleRenderRequest.ts | Shows how the Node renderer receives a render request and prepares the execution context |
| packages/…/vm.ts | Shows the isolated Node execution context and its deployment diagnostics for RSC asset metadata |
| packages/…/getReactServerComponent.server.ts | Shows how server-side RSC output becomes input to the HTML render |
| packages/…/proRSC.ts | Shows the Node RSC capability that aligns render metadata, streams the payload, and handles render cleanup and errors |
| packages/…/streamServerRenderedReactComponent.ts | Shows how HTML and embedded RSC payload data leave the renderer |
| packages/…/injectRSCPayload.ts | Shows how streamed payload pieces, stylesheet hints, and render diagnostics enter the page |
| packages/…/RSCProvider.tsx | Shows the bounded Provider Cache, prefetch adoption, refetch recovery, and shared route access model |
| packages/…/RSCRoute.tsx | Shows the client bridge for initial content, navigation, refetch, and route error state |
| packages/…/RSCRouteErrorBoundary.tsx | Shows how route read failures and late streamed errors gain component context and recovery behavior |
| packages/…/RSCStreamCompletion.ts | Defines the stream-completion state used to distinguish a resolved root from a successful full stream |
| packages/…/client.tsx | Shows the browser root, hydration choice, and teardown boundary |
| packages/…/StoreRegistry.ts | Shows the boundary between persistent store generators and page-scoped hydrated stores |
| packages/…/CallbackRegistry.ts | Shows registration waits, lifecycle timeout handling, and unload cancellation |
| packages/…/unstable_cache.ts | Shows the server-side RSC result cache and single-flight behavior |
| react_on_rails_pro/…/rsc_route_ssr_false.spec.ts | Shows the contract for deferred RSC routes and the boundary between RSC and normal client rendering |
| packages/…/package.json | Shows the package entry points and compatible React and RSC package range |
| docs/…/rsc-request-and-rendering-flow.md | Detailed request and render stages |
| docs/…/rsc-client-hydration-and-routing.md | Detailed browser hydration and route update stages |