React rendering modes
This application is a Rails tutorial that compares three ways to deliver React UI: client rendering, React on Rails server rendering, and React Server Components. The modes share the same Rails page and data model, but place the first render and later work in different runtimes so an engineer can compare browser interactivity, fast server HTML, and selective server execution.
Rendering mode model
The application uses Rails as the page and data boundary while React supplies selected interactive views. A route chooses whether the browser receives an empty mount point, HTML that React can hydrate, or a streamed React Server Components result.
| Mode | First result | Browser responsibility | Example |
|---|---|---|---|
| Client rendering | An empty React mount point | Build the UI and add all interaction | /simple via SimpleCommentScreen |
| React on Rails server rendering | HTML from a server-rendered React tree | Hydrate the existing tree and handle later events | / and /no-router |
| React Server Components | A streamed server component payload and HTML | Hydrate only client components and request later server output | /server-components |
These are complementary delivery choices rather than separate applications. The simple page shows the smallest browser-first path. The main page and the no-router page show the same Rails-to-React integration with an initial server render. The server-components page adds a server and client boundary within one React tree.
Client rendering
Client rendering leaves the first React render to the browser. Rails places the component mount point in the page and the client bundle later fills it, which suits a small view that does not need server HTML or preloaded props. The simple.html.erb page sets prerender: false for this comparison.
The mode still uses the Rails application as its backend. It does not make the initial UI available in the response body, so the browser must load the JavaScript before the component appears.
References: app/…/simple.html.erb• L1-16 config/routes.rb• L11-16 spec/…/server_render_check_spec.rb• L21-25
React on Rails server rendering
React on Rails server rendering creates the first React HTML while Rails serves the page. The browser then hydrates that HTML with the matching client component, so users can see the page before the client code takes over. The root route renders RouterApp, while the no-router route renders App; both use React on Rails registration and shared Redux state.
The server-rendered tree must match the browser tree closely enough for hydration. Routing is also split by runtime: the server uses a static location and the browser uses its live URL, which lets the same route tree support both the first response and later navigation.
The high-level request path is:
flowchart LR
Request[Browser request] --> Rails[Rails route and view]
Rails --> Props[Props and shared state]
Props --> Node[Node renderer]
Node --> HTML[React HTML in response]
HTML --> Hydrate[Browser hydration]
Hydrate --> UI[Interactive React UI]
See React on Rails integration for the Rails entry points and component registration. See Node server rendering for the renderer boundary and server bundle.
References: app/…/index.html.erb• L180-205 app/…/no_router.html.erb• L1-6 app/…/pages_controller.rb• L8-35 client/…/RouterApp.server.jsx• L10-38 client/…/RouterApp.client.jsx• L10-18 spec/…/server_render_check_spec.rb• L5-19
React Server Components
React Server Components keep selected React components on the server and send their rendered result to the browser. A server component can use server-only data or libraries without adding them to the client bundle, while a 'use client' component marks the smaller part that needs browser JavaScript for events or state.
The /server-components page demonstrates this boundary in one view. Server-only panels show Node and formatted comment data. The client toggle supplies interaction around server-rendered children. The comments feed can wait behind a Suspense boundary, so the page shell can arrive before slower content.
The page also supports client-driven server work. A refresh requests a new server component payload for live activity. An error boundary turns a failed request into a retry action, so a server failure does not remove the rest of the page.
flowchart TD
RailsPage[Rails server-components page] --> RSC[Server Components tree]
RSC --> ServerOnly[Server-only panels and data]
RSC --> ClientPart[Client component boundary]
RSC --> Suspense[Suspense-delayed content]
ServerOnly --> Stream[Streamed RSC result]
Suspense --> Stream
Stream --> Browser[Browser renders server result]
ClientPart --> Browser
Browser --> Refetch[Refresh requests a new RSC payload]
Refetch --> RSC
See React Server Components demo for the component boundaries, live activity behavior, and dedicated RSC path.
References: config/routes.rb• L3-4 config/routes.rb• L11-14 app/…/pages_controller.rb• L42-46 app/…/server_components.html.erb• L1-5 client/…/ServerComponentsPage.jsx• L11-105 client/…/LiveActivityRefresher.jsx• L56-159 client/…/CommentsFeed.jsx• L10-18
Mode selection
The choice of mode follows the needs of the first response and the work that must remain in the browser. The application keeps the choices visible through separate routes so an engineer can compare them without changing the Rails backend.
- Use client rendering when the view is small and the initial response does not need React HTML.
- Use React on Rails server rendering when the full React tree should arrive as HTML and then become interactive.
- Use React Server Components when most of the view can stay on the server and only selected controls need browser code.
All three modes depend on built assets and the Rails request lifecycle. Their child pages explain those boundaries in more detail: React on Rails integration, Node server rendering, and React Server Components demo.
References: config/…/react_on_rails.rb• L19-28 config/…/react_on_rails.rb• L42-72 config/…/react_on_rails_pro.rb• L7-29 package.json• L81-93
Change checklist
When these files or sections are changed, remember to consider these:
- A change to a page's
prerenderchoice must stay aligned with the request contract that checks for HTML or an empty mount point. - A change to the server and client versions of
RouterAppmust preserve the same route tree and state shape so hydration remains safe. - A change to the RSC component boundary must update the RSC route, the client bundle path, and the browser behavior that refetches a named server component.
- A change to server-rendering defaults or the renderer URL must keep the Rails process and Node renderer on the same bundle and protocol contract.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Client and server router trees use different locations or markup | Hydration can fail or route links can differ on the first page | Keep the static server router and browser router aligned; request coverage checks server HTML and links. |
| A page expects server HTML but disables prerendering | The response contains only the mount point until JavaScript loads | Treat each page's prerender setting as part of its visible delivery contract. |
| The Node renderer is unavailable | React on Rails server rendering cannot produce the expected first HTML | The Pro configuration disables silent fallback and points Rails to the renderer service. |
| A client component boundary moves server-only work into the browser | Browser code grows and may try to use server-only APIs | Keep Node-only data and heavy formatting work in server components; hydrate only controls that need events. |
| An RSC refetch fails | The live section shows an error instead of fresh server output | The client boundary catches ServerComponentFetchError and offers a retry. |
Key files
| File | Why to read it |
|---|---|
| config/routes.rb | Shows the user-facing routes for the main, simple, and Server Components comparisons. |
| app/…/pages_controller.rb | Shows how Rails supplies comment data and selects normal or streamed React delivery. |
app/views/pages/index.html.erb | Shows the main interactive page and its React mount point. |
app/views/pages/simple.html.erb | Shows the explicit client-rendered comparison. |
app/views/pages/server_components.html.erb | Shows the Rails entry point for the streamed Server Components page. |
| client/…/RouterApp.server.jsx | Shows the server-side route tree used by React on Rails. |
| client/…/RouterApp.client.jsx | Shows the browser-side counterpart that hydrates and navigates. |
| client/…/ServerComponentsPage.jsx | Shows the server and client component mix, Suspense content, and the page-level RSC story. |
| client/…/LiveActivityRefresher.jsx | Shows client-driven RSC refresh and error recovery. |
| config/…/react_on_rails.rb | Defines the normal React on Rails rendering defaults. |
| config/…/react_on_rails_pro.rb | Connects Rails to Node SSR and enables the RSC delivery path. |