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 / async-props-and-streaming-ssr

Async props and streaming SSR

React on Rails Pro supports progressive server rendering for React components and React Server Components. It can start work in parallel, send a page shell before all data is ready, and deliver later values through the same request. This page explains how asynchronous props move between Rails, the Node renderer, React SSR, and the browser. See the parent page, Pro rendering platform, for the wider rendering model. Related context is in Node renderer processes, renderer cache and rolling deploys, and Pro licensing and observability.

Asynchronous rendering values

Asynchronous rendering values let a Rails view start several component renders without waiting for each one in order. The view later reads each value when it needs the HTML, so independent renderer requests can overlap.

Concurrent component rendering

The async_react_component helper returns an AsyncValue instead of rendered HTML. Its .value call waits for the result and re-raises a render error. A controller must enable the async rendering context before it uses this helper. The context also checks for tasks that were started but never consumed.

This model improves page work when components have separate data or render costs. It does not make the final view complete until the view reads the values that it places in the response.

References: react_on_rails_pro/…/react_on_rails_pro_helper.rb• L376-408 react_on_rails_pro/…/async_value.rb• L16-46 react_on_rails_pro/…/async_rendering.rb• L18-84

Cached asynchronous rendering

cached_async_react_component checks the Rails cache before it starts renderer work. A hit returns an ImmediateAsyncValue, while a miss returns an AsyncValue and writes the completed HTML to the cache. The props block runs only on a miss, which keeps cache hits from repeating the data work used to build props.

Cache conditions, cache keys, expiry, and tags follow the normal Pro component-cache rules. A cache entry is written only after the render finishes successfully.

References: react_on_rails_pro/…/react_on_rails_pro_helper.rb• L410-435 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1254-1338 react_on_rails_pro/…/immediate_async_value.rb• L16-38

Asynchronous prop delivery

Asynchronous props let a server-rendered component suspend on values that Rails sends after the initial render starts. The feature uses React Server Components support and gives the component a getReactOnRailsAsyncProp capability instead of placing every value in the first request.

Push and pull prop delivery

The system supports two delivery choices in one stream:

  • Push props are sent by Rails as soon as the server has them. They suit values that the page is expected to use.
  • Pull props are requested by React when a component asks for them. They suit expensive or conditional values that may not be needed for the first view.

The push_props list marks which names are sent eagerly when pull mode is active. A pull request contains only a prop name. Rails receives that request through its pull_requests queue and can resolve it with emit.call.

The render-side manager keeps one promise per prop name. Repeated reads return the same promise, which lets React suspend and resume without changing the promise during a render. A value may arrive before or after the component asks for it. If the request bridge is not ready, the manager buffers pull requests for catch-up. It bounds that buffer and keeps requests that exceed the cap eligible for a later catch-up rather than silently marking them complete.

References: react_on_rails_pro/…/react_on_rails_pro_helper.rb• L201-211 packages/…/AsyncPropsManager.ts• L73-119 packages/…/AsyncPropsManager.ts• L162-241 react_on_rails_pro/…/MixedPropsComponent.tsx• L22-91

Prop update protocol

The async-props request is a bidirectional NDJSON stream. Rails sends the initial render request and later update records to the Node renderer. The Node renderer keeps the same request-scoped execution context for the initial render and all updates, so each update reaches the manager used by the suspended React render.

The protocol has two directions:

DirectionRecordPurpose
Rails to NodeUpdate chunkResolve or reject one async prop in the active render.
Node to RailspropRequest control messageAsk Rails for a pull-mode prop.
Node to RailsrenderComplete control messageTell Rails that the renderer has finished its response stream.

When the Rails request body ends, the Node renderer closes the async-props stream. Any prop that is still pending then rejects instead of leaving React suspended forever. Rails treats a closed request as a normal end for the producer block and stops later writes.

References: react_on_rails_pro/…/request.rb• L120-203 react_on_rails_pro/…/stream_request.rb• L113-151 react_on_rails_pro/…/stream_request.rb• L246-283 packages/…/handleIncrementalRenderRequest.ts• L117-203 packages/…/streamingUtils.ts• L16-36

Prop rejection and stream closure

Rails can reject a named prop so the component's React error boundary can handle the failure. The render-side manager exposes a general rejection reason rather than server details. This keeps database errors and other internal data out of the browser while leaving the detail available to server-side logs.

An unresolved prop also rejects when the stream closes. A prop that was already resolved remains usable, but a new prop cannot be set after closure. These rules make disconnects and incomplete producer work visible to React instead of turning them into indefinite waits.

References: react_on_rails_pro/…/async_props_emitter.rb• L91-118 react_on_rails_pro/…/async_props_emitter.rb• L197-223 packages/…/AsyncPropsManager.ts• L121-159

Streaming server-side rendering

Streaming SSR sends an initial HTML shell and then sends completed React boundaries as they become ready. Rails owns the browser response while the Node renderer owns the React render. The two streams are coordinated so Rails can begin the response without waiting for every component or async prop.

Shell and progressive reveal

The stream path renders the template shell first. This first chunk commits the response and gives the browser usable markup while later component output continues to arrive. React's server stream adds later content as Suspense work resolves.

For a React Server Component tree, the server first turns the RSC payload stream into React elements. React then renders those elements with its streaming SSR runtime. The resulting HTML and RSC-related bootstrap data travel through the Rails response so the browser can hydrate the same tree.

The shell boundary is important for errors. A failure before the first chunk can still produce a normal HTTP-level error response or redirect. A failure after the shell has been sent must be handled within the already-started stream. Render errors keep the React owner stack and matching RSC diagnostics when those details are available. A consumer disconnect is treated as teardown rather than as an application render error.

References: react_on_rails_pro/…/stream.rb• L193-301 packages/…/streamServerRenderedReactComponent.ts• L169-332 packages/…/getReactServerComponent.server.ts• L77-122

RSC payload and stylesheet coordination

The RSC payload injector connects the server stream to client hydration. It initializes a page-scoped payload registry before related HTML arrives, then places payload data and stylesheet links into later output in an order that lets the browser reveal styled content.

The injector can infer stylesheets for client references from the build asset map. If loadable-stats.json is missing, it keeps the streamed-preload fallback and retries with backoff. The Node renderer reports a missing file once per renderer process through a host diagnostic path. This diagnostic stays out of VM console history, so the server file path is not replayed to the browser.

For development RSC streams, delivery also stays outside React Flight's console-capture context. This prevents consumer logs from becoming RSC payload records while preserving the render's request context for related work.

The stream also protects HTML and reveal boundaries from unsafe split points. It uses React's flush signal when available and a short event-loop fallback so buffered payload data does not wait for stream close.

References: packages/…/injectRSCPayload.ts• L297-357 packages/…/injectRSCPayload.ts• L1557-1617 packages/…/injectRSCPayload.ts• L1653-1705 packages/…/injectRSCPayload.ts• L1742-1875 packages/…/streamingUtils.ts• L61-103 packages/…/streamingUtils.ts• L120-169 packages/…/vm.ts• L63-80 packages/…/vm.ts• L683-694

Concurrent stream coordination

Several component streams can produce data at the same time. Rails uses producer tasks for those streams and one consumer task to write their chunks to the browser response. A bounded queue creates backpressure: producers slow down when the response writer cannot keep up.

The Node stream utility treats a consumer close before readable end as an abort. It stops downstream writes and aborts or destroys the upstream React source, including a source that is created after the disconnect. Expected abort errors are ignored during this teardown. Failures in cleanup are logged without masking the disconnect.

The stream lifecycle follows this sequence:

sequenceDiagram
    participant B as Browser
    participant R as Rails
    participant N as Node renderer
    participant X as React SSR

    R->>N: Initial render request
    N->>X: Start React render
    X-->>N: Shell and ready boundaries
    N-->>R: Length-prefixed render chunks
    R-->>B: Shell and progressive reveals
    R->>N: Async prop update chunks
    N->>X: Resolve or reject prop promise
    X-->>N: Newly completed boundary
    N-->>R: More render chunks
    R-->>B: More progressive reveals
    R->>N: Close request body
    N->>X: End async-props stream
    N-->>R: Render complete

If the browser disconnects, Rails stops the writer and cancels producer work. The Node renderer also aborts the active React render and releases its request-scoped resources. A transport retry is safe only before the first content chunk, because retrying after the shell would duplicate page content.

References: react_on_rails_pro/…/stream.rb• L255-301 react_on_rails_pro/…/stream.rb• L463-515 packages/…/streamingUtils.ts• L196-383 react_on_rails_pro/…/stream_request.rb• L155-199

Browser-side component management

The async-props manager is part of the render-side JavaScript rather than long-lived browser state. It stores each prop's promise and resolve or reject state in the shared execution context used by the Node VM. It does not use process-wide state, so concurrent requests cannot share prop values.

Pull mode asks for a prop only once. Requests that occur before the Node renderer installs its request bridge are buffered and sent during catch-up. The manager limits buffered requests to protect memory, while requests that arrive over the limit remain eligible for catch-up. Prop names have a fixed size limit, and an oversized pull request rejects instead of entering the stream.

The client wrapper places the server component inside an RSCProvider and a Suspense boundary. On the server this provider reads RSC payloads from the request stream. On the browser it hydrates the server HTML when present or creates a client root when no server HTML exists. The browser therefore receives progressive HTML and hydrates the resulting tree, while the render-side manager handles the async prop promises that produce that HTML.

References: packages/…/AsyncPropsManager.ts• L61-70 packages/…/AsyncPropsManager.ts• L162-192 packages/…/AsyncPropsManager.ts• L221-297 packages/…/client.tsx• L120-190 packages/…/server.tsx• L92-103

Stream cache behavior

Pro can cache the complete sequence of streamed chunks, not only the first HTML fragment. Each entry also keeps the mount-point ID used by the producing render, so a cache hit can rebind embedded IDs to the current render before replay.

Stream cache hits and misses

The cache key includes the component and the render options that define the output. On a hit, the props block is not evaluated. Rails sends the first cached chunk to the view and schedules the remaining chunks on the normal output queue, so a hit keeps the same progressive shape as a live stream. Before replay, cached markup and console replay data are adjusted from the producing mount-point ID to the current DOM node ID.

On a miss, Rails evaluates props, streams the live result, and collects the chunks. It writes the entry only after the response stream is fully consumed. A client disconnect does not turn a partial response into a cache entry. Older entries that contain only a bare chunk array are treated as misses because they do not have enough information for safe mount-point rebinding.

References: react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1140-1164 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1166-1233 react_on_rails_pro/…/stream_cache.rb• L33-69 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1388-1415

Stream cache safety

The cached value is an entry with an ordered array of chunks and the producing mount-point ID. On replay, the cache layer rewrites the embedded DOM ID in markup and console replay data. It preserves chunk boundaries when the old and new IDs have the same byte length; otherwise it delivers the rewritten markup in the first markup-bearing chunk while keeping the full output unchanged when joined.

The cache layer keeps a shallow copy before it gives a chunk to downstream framing code, so a consumer cannot remove payload data from the stored value. It also refuses to cache a stream when any chunk reports an error, including an error in a later Suspense boundary after a successful shell.

Cache writes are delayed until the response drain is complete when the stream runs inside the page-level coordinator. This avoids making cache storage part of the critical path for sending the final browser bytes. Cache expiry and tag revalidation still bound how long a successful stream remains available.

References: react_on_rails_pro/…/stream_cache.rb• L72-149 react_on_rails_pro/…/stream_cache.rb• L163-209 react_on_rails_pro/…/stream.rb• L21-63 react_on_rails_pro/…/react_on_rails_pro_helper.rb• L1192-1224

Change checklist

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

  • Keep the shared async-props context keys and the maximum pull-prop name length aligned across AsyncPropsManager, the Node incremental request handler, and StreamRequest.
  • Keep pull-request catch-up idempotent. Requests made before the emitter exists must be delivered once after installation, while requests over the buffer cap must remain eligible for that catch-up.
  • Keep propRequest and renderComplete control records aligned with the length-prefixed parser on both the Rails and Node sides.
  • Preserve the first-chunk boundary. Changes that move a failure or retry across that boundary change whether Rails can still return an HTTP redirect.
  • Keep stream cache writes dependent on complete, error-free consumption. A later boundary error must not make a broken stream reusable.
  • Keep cached stream entries tied to the producing DOM node ID. Replay must rewrite embedded mount IDs before a stream is served under another node ID.
  • Keep producer cancellation tied to response closure. Otherwise a disconnected browser can leave React work and async prop fetches running.
  • Keep the loadable-stats diagnostic callback key aligned between the payload injector and every Node VM context. Missing asset-map files must still use the streamed-preload fallback and must not enter replayable VM console history.
  • Keep consumer-abort handling upstream of every stream source, including sources created after an awaited render result. Abort teardown must not turn expected React abort errors into render failures.
  • Keep RSC stream delivery outside development Flight console capture when consumer output is replayed. Otherwise ordinary consumer logs can enter the payload.

Important failure modes

TriggerConsequenceGuard
An async component value is created but the view never reads .value.The task stays pending until the async context stops it, and the response may omit the intended HTML.The async rendering context reports unresolved tasks; consume every value used by the view.
Rails closes the incremental request before a prop is sent.React receives a rejected promise instead of waiting forever.The close update calls endStream; component error boundaries handle the rejection.
A pull prop name exceeds the protocol limit.No pull request is sent, and the prop promise rejects.The Rails and Node sides enforce the same maximum name length.
Pull requests fill the pre-bridge buffer before the emitter is installed.Some requests wait for catch-up instead of being emitted at once.The manager bounds memory and leaves over-cap requests eligible for catch-up.
loadable-stats.json is missing when RSC stylesheet inference starts.The injector uses streamed preload tags and retries later; the Node renderer emits one actionable INFO diagnostic per process.Keep the asset map in the server bundle directory when inference is required; do not treat the diagnostic as a browser console event.
Consumer logs run inside a development React Flight request context.React can encode those logs as RSC payload records, which can corrupt or change the payload seen by the client.Deliver RSC stream events in a context with Flight console capture disabled.
A cached stream is replayed under a different mount-point ID without rebinding.The browser can miss the payload for its own node or replay console data against the wrong node.Store the producing DOM node ID and rewrite embedded IDs to the current node before replay.
A stream emits a later error chunk after a valid shell.Caching that sequence would serve broken content to later requests.Stream cache code tracks chunk error flags and skips the cache write.
The browser disconnects during streaming.The response writer cannot continue and further output is no longer useful.Rails cancels producer tasks, and the Node renderer aborts React work.
A transport fails after the first content chunk.Retrying would duplicate content in the browser response.Stream request retry is limited to failures before the first content chunk.

Key files

FileWhy to read it
react_on_rails_pro/…/react_on_rails_pro_helper.rbMain Rails-facing helpers for async values, async props, streamed components, and stream-cache decisions.
react_on_rails_pro/…/request.rbRails-to-Node request setup for one-way and bidirectional streaming.
react_on_rails_pro/…/stream_request.rbResponse parsing, control-message routing, retries, and pull-request delivery.
react_on_rails_pro/…/async_props_emitter.rbServer-side prop send, reject, pull queue, and request-close behavior.
react_on_rails_pro/…/stream.rbPage-level SSR stream coordination, backpressure, disconnect handling, and delayed cache writes.
react_on_rails_pro/…/stream_cache.rbOrdered stream replay, mount-point ID rebinding, and safe persistence of complete streams.
packages/…/AsyncPropsManager.tsRequest-scoped promise management for push and pull props, including bridge catch-up and buffer limits.
packages/…/streamingUtils.tsShared stream delivery, console-capture isolation, consumer-abort propagation, and request-scoped RSC stream support.
packages/…/injectRSCPayload.tsRSC payload embedding, stylesheet coordination, stream flush ordering, and missing asset-map fallback.
packages/…/handleIncrementalRenderRequest.tsNode-side setup of the incremental render sink and the request-scoped async-props bridge.
packages/…/handleIncrementalRenderStream.tsNDJSON request reading, update dispatch, size limits, and stream timing.
packages/…/vm.tsRequest-scoped execution sharing and the renderer-owned diagnostic path for missing stylesheet asset metadata.
packages/…/streamServerRenderedReactComponent.tsReact SSR shell handling, progressive output, render diagnostics, and consumer-abort coordination.
packages/…/server.tsxServer-side provider and Suspense boundary used for server component rendering.
packages/…/client.tsxBrowser hydration or client-root setup for the server component tree.