React Server Components demo
The React Server Components demo shows how Rails can deliver a page whose main tree runs on the server while selected client components add browser interaction. It also shows progressive output, server-only data work, and client-triggered server re-renders through the React on Rails Pro Node Renderer.
Page purpose
The /server-components page is a teaching page for the boundary between server and client work. Rails supplies the first comment data set, the server component tree renders the page and server facts, and the browser hydrates only the parts that need events or local state. See the parent overview in react-rendering-modes, with related delivery patterns in react-on-rails-integration and node-server-rendering.
Page composition
Server-rendered sections
The page is entered through the Rails route /server-components. The controller selects the latest comments and asks React on Rails Pro to stream the view that contains ServerComponentsPage. The page then combines server-only sections with client boundaries instead of treating the whole page as browser code.
The visible sections have distinct roles:
| Section | System behavior |
|---|---|
| Server Environment | Reads runtime facts from the Node server. |
| Interactive Client Component | Uses browser state while receiving server-rendered children. |
| Live Server Activity | Re-renders one server component after a browser action. |
| Streamed Comments | Formats and sanitizes comment text on the server behind a Suspense boundary. |
ServerInfo proves that server components can use Node-only data. CommentsFeed keeps its markdown and HTML processing on the server, so those large helper libraries do not become browser work. ServerComponentsPage places both components in the server tree and supplies the comments received from Rails. References: app/…/pages_controller.rb• L42-46, app/views/pages/server_components.html.erb#L1-L5, client/…/ServerComponentsPage.jsx• L11-20, client/…/ServerInfo.jsx• L8-21, client/…/CommentsFeed.jsx• L12-17
Server and client boundaries
The 'use client' directive marks a component as a client boundary. TogglePanel owns the button state and event handler, but its children can remain server-rendered. This is the donut pattern: a small interactive client shell surrounds content that does not need browser JavaScript.
The boundary controls both behavior and delivery. Server components can use server resources and do not need hydration. Client components receive the JavaScript needed for interaction, while the RSC system represents them as client references when the server tree crosses into the browser.
The demo therefore keeps the page shell and data display on the server without removing interaction. The toggle opens and closes in the browser, while the explanatory content passed into it comes from the server component tree. References: client/…/TogglePanel.jsx• L1-5, client/…/TogglePanel.jsx• L9-25, client/…/ServerComponentsPage.jsx• L35-58, config/…/clientWebpackConfig.js• L28-35, config/…/serverWebpackConfig.js• L56-69
Progressive rendering
The page uses Suspense to keep the outer page usable while an async server section is still resolving. CommentsFeed may pause briefly in the demo, so the page can show its fallback first and then stream the comments section when the server work completes.
This separates page readiness from section readiness. A delay flag exists only to make the fallback visible during review; the normal path does not add the artificial delay. References: client/…/ServerComponentsPage.jsx• L79-105, client/…/CommentsFeed.jsx• L10-17
Live activity flow
Activity refresh
LiveActivity is an async server component that produces current server time, free memory, and process uptime. LiveActivityRefresher is its client-side control surface. A Refresh click asks the RSC provider to render LiveActivity again, then gives the returned result to RSCRoute for display without a full page reload.
The flow keeps the changing values authoritative on the server. The browser owns only the refresh count, request state, and the controls that start the request. A local Suspense fallback preserves the shape of the activity card while the new server result is in flight.
The following flow shows how a browser action becomes a new server component result.
flowchart LR
A[Refresh click] --> B[Client refresher]
B --> C[RSC refetch request]
C --> D[Node Renderer]
D --> E[LiveActivity server render]
E --> F[RSC payload]
F --> G[RSCRoute updates card]
References: client/…/LiveActivityRefresher.jsx• L56-82 client/…/LiveActivityRefresher.jsx• L121-158 client/…/LiveActivity.jsx• L4-21 config/…/react_on_rails_pro.rb• L7-14
Error recovery
The demo has a deliberate server failure path. Simulate Error sends props that make LiveActivity throw, and the client converts the failed refetch into a server-component fetch error for the error boundary. Retry sends corrected props and restores the route result when the request succeeds.
Request identifiers prevent an older response from replacing state created by a newer action. The error boundary shows a focused fallback for the activity card, so a failed refresh does not remove the rest of the page. References: client/…/LiveActivityRefresher.jsx• L27-54, client/…/LiveActivityRefresher.jsx• L84-119, client/…/LiveActivity.jsx• L4-7
RSC delivery path
Page and payload endpoints
The normal page request and an RSC payload request serve different stages of the same experience. Rails handles /server-components to provide the initial HTML stream. The registered RSC payload route handles component-level renders such as ServerComponentsPage and LiveActivity after the page is loaded.
The RSC payload is a streamed response. React on Rails Pro uses the configured Node Renderer to evaluate the RSC bundle and return the component result to the client. This lets a client action request a server component without asking the browser to duplicate the server render logic.
The route relationship is:
| Request | Role |
|---|---|
/server-components | Builds the initial page with Rails props. |
/rsc_payload/ServerComponentsPage | Produces an RSC result for the page component. |
/rsc_payload/LiveActivity | Produces a fresh activity card result. |
References: config/routes.rb• L3-12 app/…/pages_controller.rb• L42-46 config/…/react_on_rails_pro.rb• L26-29 spec/…/server_components_spec.rb• L79-97
Dedicated RSC bundle
The RSC bundle is the server-side build used to create React Server Components payloads. It starts from the server entry but emits rsc-bundle.js as a separate artifact. Its build resolves packages for the react-server condition and marks 'use client' modules as client references, which preserves the server/client boundary during payload generation.
The regular client and SSR builds remain separate. A full build creates all three targets, while the RSC-only build is useful when only payload generation needs to be rebuilt. The Pro configuration points the renderer to the same RSC artifact name and to the payload route prefix.
References: config/…/webpackConfig.js• L8-34 config/…/rscWebpackConfig.js• L3-10 config/…/rscWebpackConfig.js• L38-50 config/…/react_on_rails_pro.rb• L26-29 client/…/server-bundle.js• L1-5
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the page component name aligned across the Rails view, the RSC payload route, and client calls to
RSCRouteorrefetchComponent. - Keep the RSC artifact name aligned between the RSC webpack output and
ReactOnRailsProconfiguration. - Preserve the client-reference scan when moving or adding files with
'use client'; otherwise the browser boundary can be lost during RSC builds. - Keep the initial comment prop shape compatible with
CommentsFeedand with direct RSC payload requests. - Preserve a local Suspense boundary around the live route if its loading state must not collapse the full page.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The RSC bundle name differs from the Pro configuration. | The renderer cannot load the bundle needed for RSC payloads. | Keep rsc-bundle.js in both the RSC output and rsc_bundle_js_file setting. |
| A client component is not classified as a client reference. | Browser interaction can fail because the server build does not describe the client boundary. | Keep the RSC loader and client-reference plugin active for source files. |
| The Node Renderer is unavailable or uses the wrong endpoint. | Initial RSC rendering and component refetches fail. | Keep the renderer URL and shared renderer setup aligned between Rails and Node. |
LiveActivity throws during a refetch. | The activity card shows an error instead of fresh server data. | Use the error fallback and Retry path with corrected props. |
| An old refetch completes after a newer action. | Stale activity or error state can replace newer state. | Keep request-id checks around asynchronous result handling. |
Key files
| File | Why to read it |
|---|---|
| client/…/ServerComponentsPage.jsx | Shows the page-level composition of server sections, client boundaries, and Suspense. |
| client/…/LiveActivityRefresher.jsx | Shows client-driven RSC refresh, loading state, and error recovery. |
| client/…/LiveActivity.jsx | Shows the server-generated live activity result and deliberate failure path. |
| app/…/pages_controller.rb | Shows how Rails prepares initial props and starts the streamed page response. |
app/views/pages/server_components.html.erb | Shows the React on Rails entry for the page. |
| config/routes.rb | Shows the page route and the RSC payload route registration. |
| config/…/rscWebpackConfig.js | Shows the dedicated RSC build target and output artifact. |
| config/…/react_on_rails_pro.rb | Shows how Rails names the RSC artifact and payload path. |