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 / asset-build-pipeline

JavaScript and asset build pipeline

The JavaScript and asset build pipeline turns client source into browser assets, a server-rendering bundle, and a React Server Components (RSC) bundle. Shakapacker connects these outputs to Rails. Shared rules keep the targets aligned while keeping server-only code out of the browser.

Build system

Shakapacker is the Rails-facing build layer. It finds pack entry points under client/app/packs, writes public browser assets under public/packs, and supplies the asset names that Rails uses in its layouts. The project supports both Webpack and Rspack, while assets_bundler selects the active tool.

Source and pack boundary

The source boundary starts at client/app, with packs as the entry-point directory. Each named pack is a separate product that Rails can request. This makes pack names part of the Rails-to-JavaScript contract.

References: config/shakapacker.yml• L3-17 client/…/stimulus-bundle.js• L1-21 client/…/stores-registration.js• L1-13 app/…/application.html.erb• L21-25

Bundler selection

The shared configuration asks Shakapacker which bundler is active. The same choice controls the plugins and build operations used by the pipeline. The package manifest keeps both Webpack and Rspack packages available, but a process name does not change the active bundler.

References: config/shakapacker.yml• L12-17 config/…/bundlerUtils.js• L1-31 config/…/clientWebpackConfig.js• L10-19 package.json• L50-52 package.json• L103-114

Build targets

The pipeline creates three related targets from the server-bundle entry. Each target serves a different runtime and has its own output contract.

This diagram shows how one source entry becomes browser code, traditional server-rendered React code, and RSC payload code.

flowchart LR
  S[client/app/packs/server-bundle.js]
  C[Client target]
  SSR[SSR target]
  RSC[RSC target]
  P[public/packs]
  N[ssr-generated/server-bundle.js]
  Q[ssr-generated/rsc-bundle.js]

  S --> C --> P
  S --> SSR --> N
  S --> RSC --> Q

Client target

The client target produces code for the browser and supports hot module replacement in development. It includes browser-facing packs and the client side of RSC references. It excludes the server-bundle entry because that entry belongs to the server targets.

References: config/…/clientWebpackConfig.js• L10-38 config/shakapacker.yml• L34-63 app/…/application.html.erb• L21-25

Server-rendering target

The server-rendering target produces one Node-compatible file for React on Rails Pro. Rails does not serve this file as a public asset. The Node renderer loads it from the private ssr-generated path when a page uses server rendering.

References: config/…/serverWebpackConfig.js• L19-29 config/…/serverWebpackConfig.js• L49-80 config/…/react_on_rails.rb• L19-28 config/…/react_on_rails_pro.rb• L7-14

React Server Components target

The RSC target produces the RSC payload bundle. It uses the react-server resolution condition and marks modules at the 'use client' boundary so the client can load interactive parts separately. Its output is rsc-bundle.js, which React on Rails Pro uses for RSC payload generation.

References: config/…/rscWebpackConfig.js• L1-50 config/…/react_on_rails_pro.rb• L26-29 client/…/stores-registration.js• L1-4

Target selection

The build runner chooses one target for a focused watcher or all three for a normal build. Development uses separate client, SSR, and RSC watchers so a change in one runtime does not rebuild every target.

Build settingTarget
WEBPACK_SERVE or CLIENT_BUNDLE_ONLYClient only
SERVER_BUNDLE_ONLYServer-rendering only
RSC_BUNDLE_ONLYRSC only
No target-only settingClient, server-rendering, and RSC

References: config/…/webpackConfig.js• L8-34 Procfile.dev• L11-17

Shared configuration

The target configurations start from one common Shakapacker configuration. This keeps module lookup, style handling, and compatibility rules consistent while each target changes only what its runtime needs.

Module resolution

The shared rules search the application source and node_modules for imports. They also recognize the .res.js output used by ReScript. The exact react-on-rails import maps to react-on-rails-pro so third-party code does not load the incompatible core package beside the Pro package.

References: config/…/commonWebpackConfig.js• L8-23 config/…/commonWebpackConfig.js• L39-55

Asset aliases

The active shared alias is a package compatibility alias rather than a business-domain path. A separate Assets path alias exists for the client asset directory, but the custom configuration assembly does not wire it into the active aliases. Alias changes are build contracts, so imports that depend on an alias must be checked against the configuration that assembles the targets.

References: config/…/commonWebpackConfig.js• L13-23 config/…/alias.js• L1-9 config/…/webpack.config.js• L5-13

Runtime-specific rules

The server target removes browser style extraction and file emission because Node needs the module result rather than public files. The RSC target also blocks react-dom/server under the react-server condition. These rules keep browser-only and server-only dependencies in their proper runtimes.

References: config/…/serverWebpackConfig.js• L82-127 config/…/rscWebpackConfig.js• L38-50

Environment settings

Shakapacker settings control when Rails compiles assets and how the development server exposes them. JavaScript environment files then select the matching target set for development, test, or production.

The toolchain requires Node 22 or newer and Yarn 1.22. The package manifest pins the package manager to Yarn 1.22.22. These constraints apply to source preparation and pack build commands.

References: package.json• L5-7 package.json• L181

Development settings

Development disables request-time compilation because development processes run a watcher or a dev server. The client dev server uses HMR on port 3035, while separate watchers build the private SSR and RSC outputs.

References: config/shakapacker.yml• L34-63 config/…/development.js• L4-10 Procfile.dev• L7-17

Test settings

Test enables compilation so the test build can create a complete asset set before the test suite runs. The test command first builds generated ReScript and locale inputs. It then creates generated React on Rails pack imports before it runs Shakapacker.

References: config/shakapacker.yml• L64-66 package.json• L29-33 config/…/test.js• L1-7

Production settings

Production expects packs to exist before Rails starts. It caches the manifest for faster lookup and enables early hints so Rails can advertise asset files while it prepares a response. The production build command performs source generation before the final Shakapacker build.

References: config/shakapacker.yml• L68-80 config/…/production.js• L4-12 config/…/react_on_rails.rb• L14-17

Bundler environment

The assets_bundler setting is the source of truth for Webpack versus Rspack. Some process comments call a watcher “Rspack,” but the runner still follows Shakapacker’s configured bundler. A process name alone does not switch the project to Rspack.

References: config/shakapacker.yml• L12-17 config/…/bundlerUtils.js• L9-20 Procfile.dev-static-assets• L5-10

Generated pack workflow

Generated inputs sit between application source and the final bundles. ReScript creates JavaScript modules, React on Rails creates pack registration imports, and Shakapacker resolves those inputs into public and private build products.

Source preparation

The build starts by compiling ReScript and generating locale output. The React on Rails generation task then updates the generated registration input used by the server-bundle entry. These files are build products, so changes belong in the source components and registrations rather than in generated output.

References: package.json• L21-25 package.json• L31-34 config/…/react_on_rails.rb• L9-17 client/…/server-bundle.js• L1-5

Pack compilation

Shakapacker reads the prepared entries and writes browser assets to public/packs. It writes the non-public SSR and RSC bundles to ssr-generated, while the Rails asset manifest records the names needed by pack helpers. In development, Rails reloads the manifest so it can see watcher output. In production, it caches the manifest because precompiled assets are stable.

References: config/shakapacker.yml• L5-10 config/shakapacker.yml• L23-27 config/shakapacker.yml• L68-75 config/…/serverWebpackConfig.js• L72-80 config/…/rscWebpackConfig.js• L49-50

Rails and renderer handoff

Rails layouts request browser packs through Shakapacker helpers. React on Rails Pro uses the private server bundle for traditional rendering and the private RSC bundle for RSC payload generation. This split lets the browser receive only public client assets while the Node renderer keeps server code off the asset path.

References: app/…/application.html.erb• L21-25 config/…/react_on_rails.rb• L19-28 config/…/react_on_rails_pro.rb• L7-29

The build pipeline owns how source inputs become packs. The build configuration page explains the configuration layers behind that process. The asset styling layer page explains the images, fonts, Sass, PostCSS, and Tailwind inputs that those packs consume.

The development runtime page explains how watchers and the Rails runtime use these build targets. The automated validation page describes checks that depend on generated packs.

Change checklist

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

  • A new or renamed pack needs matching Rails pack-helper usage and any target-specific entry handling.
  • A change to server-bundle.js affects both the SSR and RSC targets because they derive their entries from it.
  • A change to assets_bundler must be checked against plugin selection and CSS extraction behavior in every target.
  • A change to source_path or source_entry_path must preserve the server entry lookup and the RSC client-reference scope.
  • A change to generated ReScript or React on Rails inputs must remain before the Shakapacker command in test, development, and production builds.
  • A change to compile or manifest caching must match the process that prebuilds assets and the Rails environment that reads them.

Important failure modes

TriggerConsequenceGuard
The generated pack task does not run before Shakapackerserver-bundle.js imports a missing generated module or misses registrationsRun the environment-specific build command that performs ReScript, locale, and pack generation first
The server entry is absent or the source paths no longer matchThe server target cannot find server-bundle.jsKeep source_path, source_entry_path, and the shared server entry aligned
Server code is allowed into the client entry setThe browser build can try to load Node-only code or server chunksKeep the server entry restricted to the server and RSC targets
Production starts without precompiled packsRails cannot resolve the cached asset manifest or pack filesRun the production build before boot and keep production compilation disabled
An RSC module imports react-dom/serverThe react-server runtime can fail while creating the RSC payloadKeep server-rendering-only imports out of the RSC graph
The watcher is renamed to Rspack without changing assets_bundlerThe process still uses Webpack and may mislead build diagnosisTreat config/shakapacker.yml as the bundler source of truth

Key files

FileWhy to read it
config/shakapacker.ymlDefines source paths, output paths, compile behavior, dev-server settings, and the active bundler.
config/…/webpack.config.jsSelects the environment-specific configuration entry.
config/…/webpackConfig.jsCoordinates client, SSR, and RSC target selection.
config/…/commonWebpackConfig.jsDefines shared module resolution and style compatibility rules.
config/…/clientWebpackConfig.jsShows the browser target and its RSC client-reference role.
config/…/serverWebpackConfig.jsShows the private Node bundle contract and server-only adjustments.
config/…/rscWebpackConfig.jsShows the RSC target and react-server resolution boundary.
config/…/bundlerUtils.jsDefines how Webpack or Rspack is selected and accessed.
client/…/server-bundle.jsShows the shared server-side entry and its generated input.
package.jsonShows the required JavaScript runtime, source-preparation commands, environment-specific build commands, and the Webpack/Rspack tool dependencies.
config/…/react_on_rails.rbConnects build outputs and build commands to Rails rendering.
config/…/react_on_rails_pro.rbConnects the private RSC and Node renderer outputs to React on Rails Pro.
app/views/layouts/application.html.erbShows how Rails requests the browser packs.