Introducing a new way to review your PRs.

Read the announcement

shakacode / react_on_rails

Integration of React + Webpack + Rails including server-side rendering of React, enabling a better developer experience and faster client performance.

View on GitHub

19 documents · Updated Nov 18, 2026

docs / rsc-request-and-rendering-flow

RSC request and rendering flow

React Server Components (RSC) requests turn a component name and its props into a streamed RSC payload. Rails Pro accepts the request, asks the Node renderer to execute the server component tree, transforms the returned stream into the Rails response format, and sends the records to the browser for decoding and hydration. For the wider product model, see React Server Components; for the browser-side part of this flow, see RSC client hydration and routing. For Node renderer process behavior, see Node renderer processes; for delayed props and related streaming, see Async props and streaming SSR.

Request boundary

The request boundary connects a browser or a Rails view to the RSC rendering system. The same rendering path supports a direct payload route and an embedded payload inside a streamed Rails view.

RSC endpoint

The route helper in react_on_rails_pro/…/routes.rb maps /:component_name to the RSC payload controller. The controller concern in react_on_rails_pro/…/rsc_payload_renderer.rb checks access, parses optional JSON props, and renders a text response with content type application/x-ndjson. This endpoint is a public boundary when mounted, so component access must be controlled by the configured authorizer or by an app-owned controller.

The view helper rsc_payload_react_component is also used inside the endpoint's text template. It marks the render as an RSC payload stream rather than an HTML stream. This keeps the route and embedded-payload cases on the same renderer path.

References: react_on_rails_pro/…/routes.rb• L16-24 react_on_rails_pro/…/rsc_payload_renderer.rb• L16-80 react_on_rails_pro/…/rsc_payload.text.erb• L1-15 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L225-290

Render modes

ReactOnRails::ReactComponent::RenderOptions distinguishes synchronous rendering, streamed HTML, and streamed RSC payloads. For an RSC request, react_on_rails_pro/…/react_on_rails_pro_helper.rb creates render options with render_mode: :rsc_payload_streaming, then asks the shared server-rendering path for a stream.

The generated JavaScript selects serverRenderRSCReactComponent when it runs in the RSC bundle. The same generated request can select the normal streamed HTML function when it runs in the server bundle. This lets Rails use one request description while the selected bundle defines the output type.

References: react_on_rails/…/render_options.rb• L172-193 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1428-1444 react_on_rails_pro/…/server_rendering_js_code.rb• L106-166

Rails request orchestration

Rails Pro separates component rendering from HTTP response writing. The rendering pool chooses the RSC bundle and renderer endpoint, while the stream request coordinates records from the Node renderer with the Rails response.

Renderer request

The rendering pool in react_on_rails_pro/…/node_rendering_pool.rb marks the request as an RSC payload when its render mode is RSC. It selects the RSC bundle identity and calls either the normal render endpoint or the incremental-render endpoint. ReactOnRailsPro::Request sends the generated JavaScript and request metadata to the Node renderer. If the renderer does not have the selected bundle, Rails can upload the matching bundle and retry before it exposes the stream to the browser.

The request stays tied to the bundle identity used to build the render. This keeps the JavaScript request, the RSC runtime, and the client-reference metadata on one compatible build.

References: react_on_rails_pro/…/node_rendering_pool.rb• L68-105 react_on_rails_pro/…/node_rendering_pool.rb• L154-195 react_on_rails_pro/…/request.rb• L101-117 react_on_rails_pro/…/request.rb• L519-545

Stream coordination

ReactOnRailsPro::StreamRequest consumes the Node response without waiting for the full payload. It parses each length-prefixed record, sends normal content records to the Rails stream, and handles protocol control records separately. StreamDecorator then applies actions such as prepend, transform, append, and error recovery at the stream boundary.

For a Rails view, stream_view_containing_react_components renders the shell first. Each component helper returns its first record so Rails can write an initial response, then later records enter a bounded queue. One consumer writes that queue to response.stream, while producer tasks continue to read component streams. The first write commits the response headers, so the response content type and timing headers must be set before it.

References: react_on_rails_pro/…/stream_request.rb• L20-109 react_on_rails_pro/…/stream_request.rb• L138-225 react_on_rails_pro/…/stream.rb• L220-301 react_on_rails_pro/…/stream.rb• L463-515 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1341-1415

Node component execution

The Node renderer is the execution service for the RSC bundle. It validates the render request, prepares an execution context for the requested bundle, runs the generated JavaScript, and returns either a complete value or a readable stream.

Peer compatibility

The Node renderer checks the RSC package, React, and React DOM versions before it serves RSC work. The check runs once per process and compares the app's resolved package versions with the supported RSC peer window. It fails startup when the versions do not match, unless REACT_ON_RAILS_PRO_DISABLE_VERSION_CHECK=1 changes the failure to a warning. This prevents a renderer from producing a payload with an incompatible Flight runtime.

The support window follows the Flight version bundled by react-on-rails-rsc, not only the package minor version. It accepts stable releases from the supported RSC line and requires React and React DOM to use the matching stable range:

react-on-rails-rsc lineMatching React and React DOM floor
19.2.1 and later on 19.2.x19.2.7
19.3.019.2.8
19.3.119.3.0
19.3.2 and later on 19.3.x19.3.1

The last two floors reflect patched Flight releases. Prerelease RSC versions are rejected. For known prerelease lines, the diagnostic names the stable successor and the React floor that matches it. If the app does not use react-on-rails-rsc, the check does not block the non-RSC path.

References: packages/…/rscPeerSupport.ts• L16-62 packages/…/runRscPeerCompatibilityCheck.ts• L22-30 packages/…/runRscPeerCompatibilityCheck.ts• L96-130 packages/…/checkRscPeerCompatibility.ts• L160-233

RSC rendering

The Node render handler in packages/…/handleRenderRequest.ts resolves the bundle and runs the rendering request in the bundle's execution context. The RSC capability in packages/…/proRSC.ts uses React's server renderer to serialize the server component tree. It also loads the client and server client-reference manifests so client components can remain references in the RSC payload instead of running as server components.

React emits records as the tree becomes ready. The RSC capability passes those records to the shared stream layer and tracks request-scoped RSC payload work and post-render cleanup. A client component reference therefore travels through the payload as data for the browser rather than as client code executed in Node.

References: packages/…/handleRenderRequest.ts• L87-148 packages/…/handleRenderRequest.ts• L302-422 packages/…/proRSC.ts• L89-123 packages/…/proRSC.ts• L149-175 packages/…/proRSC.ts• L182-212

Execution context

The Node renderer uses isolated virtual-machine contexts for bundle execution and reuses them through a bounded pool. The current bundle generation remains available during a rolling update while older generations drain. This keeps requests on a compatible build without allowing old contexts to grow without limit. (packages/…/vm.ts• L95-105, packages/…/vm.ts• L271-380, packages/…/vm.ts• L516-573, packages/…/vm.ts• L1105-1155)

Each HTTP request gets one shared execution map for its initial render and any later update records. The renderer exposes that map only while code runs, then removes the request reference from the reusable VM. This lets incremental rendering share state within one request without sharing it with another request. (packages/…/vm.ts• L897-910, packages/…/vm.ts• L963-1019, packages/…/vm.ts• L1090-1102)

References: packages/…/vm.ts• L95-105 packages/…/vm.ts• L271-380 packages/…/vm.ts• L897-910 packages/…/vm.ts• L963-1019 packages/…/vm.ts• L1105-1155

Payload framing

The Node-to-Rails stream uses a length-prefixed record format. Each record has metadata, a tab, an eight-character hexadecimal byte length, a newline, and raw content bytes. The metadata describes the record while the raw content carries the RSC payload text without JSON escaping.

The shared transform also adds console replay and render state metadata. Rails reconstructs each record with ReactOnRails::LengthPrefixedParser; the RSC helper then preserves the payload content and emits a text stream for the browser. This two-step framing lets the internal renderer stream raw bytes while the public endpoint remains a newline-delimited response.

References: packages/…/streamingUtils.ts• L119-161 react_on_rails/…/length_prefixed_parser.rb• L6-23 react_on_rails/…/length_prefixed_parser.rb• L56-87 react_on_rails/…/length_prefixed_parser.rb• L133-171 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1428-1444

Incremental rendering

Incremental rendering adds a request channel for props that become available after the initial render starts. It is used when a component helper supplies an async props block and is available only with RSC support.

Async prop exchange

Rails opens a bidirectional NDJSON request to the Node incremental-render endpoint. It sends the initial rendering request, then runs the async props block in a separate task while it reads the response stream. Each emitted prop becomes an update record on the open request.

Node runs the initial request and all update records in the same execution context. The component can suspend while it waits for a prop, resume when Rails sends the value, and add more records to the response. In pull mode, Node can send a propRequest control record; Rails validates its name and the emitter supplies the requested value. A renderComplete record closes the pull-side response after the source stream ends.

References: react_on_rails_pro/…/request.rb• L120-203 packages/…/handleIncrementalRenderRequest.ts• L180-203 packages/…/handleIncrementalRenderRequest.ts• L235-310 packages/…/handleIncrementalRenderRequest.ts• L312-358 packages/…/streamingUtils.ts• L16-37 react_on_rails_pro/…/stream_request.rb• L246-283

The incremental path has two end conditions. Rails closes the request body when the async props block finishes, which lets Node reject unresolved props through its close hook. The response stream ends when the RSC render completes. These ends are separate because the request may finish sending props before the last resumed component record reaches the browser.

References: react_on_rails_pro/…/request.rb• L188-201 packages/…/handleIncrementalRenderRequest.ts• L335-356 packages/…/worker.ts• L876-1003

Response delivery

Response delivery carries the stream from Node to Rails and then to the browser without converting it into one large body. A direct RSC endpoint exposes newline-delimited records, while a streamed Rails view embeds the same payload in HTML for the browser-side RSC provider to consume before it needs an HTTP fetch.

Embedded Payload Registry

The Embedded Payload Registry is the page-global browser registry that stores RSC payload text while Rails streams the HTML shell. injectRSCPayload initializes the registry before the related component markup, then appends payload scripts as Node records arrive. The browser can therefore start decoding an embedded RSC payload as soon as its scripts reach the page.

The injector keeps initialization scripts, stylesheet links, HTML, and payload scripts in an order that preserves both execution and progressive display. It keeps incomplete HTML and raw-text boundaries intact so an injected script does not split markup or application script content. A configured CSP nonce is applied to injected scripts. (packages/…/injectRSCPayload.ts• L1620-1711, packages/…/injectRSCPayload.ts• L1730-1894, packages/…/injectRSCPayload.ts• L2152-2210)

For client components that arrive in a later streamed boundary, the injector can place stylesheet links before React's reveal script. It uses manifest CSS when available and falls back to streamed preload tags when the optional loadable-stats.json file is absent or not yet ready. This protects the order needed for styled progressive reveal without changing the RSC payload contract. (packages/…/injectRSCPayload.ts• L126-138, packages/…/injectRSCPayload.ts• L297-358, packages/…/injectRSCPayload.ts• L1557-1617, packages/…/injectRSCPayload.ts• L1763-1770, packages/…/injectRSCPayload.ts• L1841-1847)

If a render reports an error, the injector sends a client error signal with the embedded payload. Development and test responses can include the supplied diagnostic details. Other environments receive only a generic error signal while server-side reporting retains the full details. (packages/…/injectRSCPayload.ts• L88-124, packages/…/injectRSCPayload.ts• L2037-2049)

References: packages/…/injectRSCPayload.ts• L88-124 packages/…/injectRSCPayload.ts• L126-138 packages/…/injectRSCPayload.ts• L1557-1617 packages/…/injectRSCPayload.ts• L1620-1711 packages/…/injectRSCPayload.ts• L1730-1894 packages/…/injectRSCPayload.ts• L2037-2087

HTTP response

Fastify sends a readable Node stream as the renderer response. Rails reads that stream, writes the first response chunk to ActionController::Live, and drains later chunks through its single response writer. A client disconnect destroys the upstream stream and aborts the active React render so server work does not continue for a closed connection.

The response can fail before or after its first chunk. Before the first write, Rails can still return an HTTP error or redirect. After headers are committed, the stream must carry the render error or terminate; Rails cannot replace the already-started HTTP response with a new status.

References: packages/…/worker.ts• L155-180 packages/…/worker.ts• L183-279 packages/…/streamingUtils.ts• L239-302 packages/…/streamServerRenderedReactComponent.ts• L194-245 react_on_rails_pro/…/stream.rb• L279-300

End-to-end flow

The diagram shows how a component request becomes an RSC payload and reaches the browser.

flowchart LR
  A[Browser or Rails view] --> B[RSC route or payload helper]
  B --> C[Rails render options]
  C --> D[Rails renderer request]
  D --> E[Node RSC execution context]
  E --> F[React server component render]
  F --> G[Length-prefixed stream records]
  G --> H[Rails parser and stream queue]
  H --> I[HTML injection or live HTTP response]
  I --> J[Embedded Payload Registry or NDJSON client]
  J --> K[Browser RSC decode and hydration]

The important boundary is between Node execution and Rails delivery. Node produces a stream of render records, while Rails owns the public HTTP response and its lifecycle. In the embedded path, Rails also preserves the order between registry initialization, HTML, styles, and payload scripts. The browser sees the RSC payload only after both sides preserve record order and stream completion.

References: react_on_rails_pro/…/rsc_payload_renderer.rb• L25-48 react_on_rails_pro/…/node_rendering_pool.rb• L68-105 packages/…/proRSC.ts• L149-175 packages/…/injectRSCPayload.ts• L1830-1875 react_on_rails/…/length_prefixed_parser.rb• L56-87 react_on_rails_pro/…/stream.rb• L474-515

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the length-prefixed format aligned across LengthPrefixedParser, StreamRequest, and the Node streaming utilities. A change to metadata names or control records can silently break both normal and incremental streams.
  • Keep the configured RSC payload route path aligned with rsc_payload_generation_url_path. A custom public route with the old generation path sends requests to the wrong endpoint.
  • Keep the RSC bundle, client-reference manifests, and bundle identity from the same build. Mismatched artifacts can produce payloads that refer to browser modules the client cannot load.
  • If a control message changes, update the Ruby and Node control-message definitions and the incremental close behavior together.
  • Preserve the pre-first-write order for content type, server timing, and the initial stream chunk. ActionController::Live commits headers on that first write.
  • For incremental rendering, preserve the distinction between request-body close and response-stream completion. Closing either side too early can reject pending props or release the execution context before the final payload records are sent.
  • Keep the Embedded Payload Registry initialization ahead of component HTML and keep injected scripts safe for CSP and HTML raw-text boundaries. A change to stream ordering can make a payload unavailable or stop later page markup from executing.
  • Keep the stylesheet source and the reveal ordering aligned. Changes to manifest CSS, loadable-stats.json, preload promotion, or reveal deferral can reintroduce unstyled streamed content without changing the payload itself.
  • Keep the per-request shared execution map isolated from the reusable VM context. Incremental update records must reuse the same request map, but a later request must not see it.
  • Keep VM pool limits and declared current bundle generations compatible. A current generation that cannot fit in the pool cannot remain ready for all requests.
  • Keep the RSC peer support window aligned with the Flight line bundled by react-on-rails-rsc. Update the Node-side check and its Ruby mirror together so startup does not accept a renderer and app pair that cannot exchange a valid payload. Include patched Flight floors and stable-only prerelease handling when those ranges change.

Important failure modes

TriggerConsequenceGuard
The RSC endpoint has no authorizer for an app that exposes it publiclyA caller can choose the component name and props sent to the rendererConfigure rsc_payload_authorizer or use an app-owned authenticated controller.
Props contain invalid JSONRails returns a bad-request response and does not start renderingParse props at the controller boundary and keep the endpoint's JSON contract clear.
A renderer record has a bad header or a truncated bodyRails cannot reconstruct the payload and the stream reports a protocol or incomplete-stream failureKeep both ends on the same length-prefixed format and do not buffer or rewrite records in a proxy.
A shell error occurs before the first response writeRails can return an error or redirect with a valid HTTP statusKeep shell errors visible before ActionController::Live commits headers.
A render fails after the first response writeThe HTTP status cannot be changed; the browser receives an error record or an aborted streamTreat post-first-chunk errors as stream errors and keep the client path able to stop decoding.
A browser or proxy closes or buffers the responseUpstream work may be aborted, or the browser may not observe records that unblock hydrationDeliver the live stream end to end and propagate disconnects to the active renderer.
Payload scripts arrive after related component HTML or split an HTML raw-text boundaryThe browser may not find the embedded payload or may execute page content incorrectlyPreserve injector ordering and retain incomplete tags and raw-text tails between flushes.
loadable-stats.json is missing or malformed during a streamed client-boundary renderCSS inference is delayed or falls back to preload tags; a later reveal can lack the intended stylesheet gateEmit the stats file when the build supports it and keep the streamed fallback path intact.
A declared current bundle generation exceeds maxVMPoolSizeThe renderer cannot keep the full current generation readySize the VM pool for the declared bundle set and fail readiness rather than serving a partial generation.
Incremental update code receives a shared map from another requestRequest data can leak across users or updates can use the wrong async prop stateCreate the map per ExecutionContext and remove its VM-global reference after each execution.
react-on-rails-rsc, React, or React DOM falls outside the supported peer window, or uses an unsupported prereleaseThe renderer can load mismatched or unpatched Flight code and produce an invalid or unsafe RSC payloadKeep the resolved app versions on the mapped stable ranges and patched Flight floors. Treat the startup check as an error unless the operator explicitly sets REACT_ON_RAILS_PRO_DISABLE_VERSION_CHECK=1.

Key files

FileWhy to read it
react_on_rails_pro/…/rsc_payload_renderer.rbDefines the public RSC request boundary, access check, prop parsing, and response setup.
react_on_rails_pro/…/react_on_rails_pro_helper.rbShows how an RSC payload helper selects the render mode and converts renderer records into the public stream.
react_on_rails_pro/…/node_rendering_pool.rbConnects render options to the RSC bundle and the normal or incremental renderer endpoint.
react_on_rails_pro/…/request.rbDefines Rails-to-Node requests, bundle retry behavior, and incremental prop exchange.
react_on_rails_pro/…/stream_request.rbParses renderer records and coordinates content with control messages and retries.
react_on_rails_pro/…/stream.rbOwns the Rails live response, first-chunk handoff, queue drain, and disconnect cleanup.
packages/…/proRSC.tsDefines Node-side RSC rendering and the server-component runtime boundary.
packages/…/injectRSCPayload.tsShows how Rails SSR output embeds the RSC payload, protects stream ordering, and gates streamed styles.
packages/…/streamingUtils.tsDefines shared stream framing, error delivery, and upstream abort behavior.
packages/…/handleRenderRequest.tsShows how the Node service prepares an execution context and runs a render request.
packages/…/rscPeerSupport.tsDefines the supported RSC and React peer-version mappings, patched Flight floors, and stable-release guidance used before Node serves RSC work.
packages/…/runRscPeerCompatibilityCheck.tsShows how the renderer resolves app dependencies and applies the once-per-process startup check.
packages/…/handleIncrementalRenderRequest.tsShows the shared execution context and control flow for async prop updates.
packages/…/vm.tsDefines isolated bundle execution contexts, request-scoped state, bundle-generation retention, and VM pool limits.
react_on_rails/…/length_prefixed_parser.rbDefines the record format that Rails uses to rebuild streamed render results.