Introducing a new way to review your PRs.

Read the announcement

shakacode / react-webpack-rails-tutorial

Legacy full-app React on Rails tutorial repo and live demo. See reactonrails.com/examples for current maintained demos.

View on GitHub

16 documents · Updated Nov 4, 2026

docs / react-rendering-modes

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.

ModeFirst resultBrowser responsibilityExample
Client renderingAn empty React mount pointBuild the UI and add all interaction/simple via SimpleCommentScreen
React on Rails server renderingHTML from a server-rendered React treeHydrate the existing tree and handle later events/ and /no-router
React Server ComponentsA streamed server component payload and HTMLHydrate 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 prerender choice 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 RouterApp must 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

TriggerConsequenceGuard
Client and server router trees use different locations or markupHydration can fail or route links can differ on the first pageKeep the static server router and browser router aligned; request coverage checks server HTML and links.
A page expects server HTML but disables prerenderingThe response contains only the mount point until JavaScript loadsTreat each page's prerender setting as part of its visible delivery contract.
The Node renderer is unavailableReact on Rails server rendering cannot produce the expected first HTMLThe Pro configuration disables silent fallback and points Rails to the renderer service.
A client component boundary moves server-only work into the browserBrowser code grows and may try to use server-only APIsKeep Node-only data and heavy formatting work in server components; hydrate only controls that need events.
An RSC refetch failsThe live section shows an error instead of fresh server outputThe client boundary catches ServerComponentFetchError and offers a retry.

Key files

FileWhy to read it
config/routes.rbShows the user-facing routes for the main, simple, and Server Components comparisons.
app/…/pages_controller.rbShows how Rails supplies comment data and selects normal or streamed React delivery.
app/views/pages/index.html.erbShows the main interactive page and its React mount point.
app/views/pages/simple.html.erbShows the explicit client-rendered comparison.
app/views/pages/server_components.html.erbShows the Rails entry point for the streamed Server Components page.
client/…/RouterApp.server.jsxShows the server-side route tree used by React on Rails.
client/…/RouterApp.client.jsxShows the browser-side counterpart that hydrates and navigates.
client/…/ServerComponentsPage.jsxShows the server and client component mix, Suspense content, and the page-level RSC story.
client/…/LiveActivityRefresher.jsxShows client-driven RSC refresh and error recovery.
config/…/react_on_rails.rbDefines the normal React on Rails rendering defaults.
config/…/react_on_rails_pro.rbConnects Rails to Node SSR and enables the RSC delivery path.