Rails engine, configuration, and setup
React on Rails establishes a Rails integration through one Ruby load boundary, one Rails engine, and a shared configuration model. The setup path connects the Ruby gem to the JavaScript package, the asset bundler, generated entry points, and the checks that must pass before the application can render React. See the parent page Rails integration and application lifecycle for the wider request and rendering flow.
Integration boundary
The integration boundary joins a Rails application with the React on Rails Ruby gem and its matching JavaScript package. It provides the Rails-side services that the JavaScript runtime and component registration, component rendering and hydration, and server-rendering protocol pages describe in more detail.
Ruby entry point
The public Ruby entry point is react_on_rails. Loading it makes the Rails helpers, controllers, configuration, engine, render support, and operational tools available to the host application. The gem requires Rails and declares Rails, Shakapacker, ExecJS, and connection-pool support as runtime dependencies. react_on_rails.gemspec sets Ruby 3.3 as the minimum supported Ruby version and Rails 5.2 as the minimum Rails version.
References: react_on_rails/…/react_on_rails.rb• L3-32 react_on_rails/react_on_rails.gemspec• L27-45
Rails engine lifecycle
The ReactOnRails::Engine is the Rails-owned lifecycle boundary. During boot it coordinates three concerns: it avoids applying Shakapacker's package-manager guard when Shakapacker is only a transitive dependency, validates Ruby and JavaScript package compatibility after initialization, and resets the server-rendering pool before application preparation. This lets a client-only app avoid an unrelated Shakapacker boot failure while still preserving the guard when Shakapacker is the app's configured bundler.
References: react_on_rails/…/engine.rb• L5-37 react_on_rails/…/engine.rb• L105-160 react_on_rails/…/engine.rb• L210-232
Configuration model
React on Rails uses a single ReactOnRails.configuration object. The initializer changes this object through ReactOnRails.configure, then the configuration derives defaults and checks cross-system rules so Rails, the asset bundler, generated packs, and server rendering use the same view of the application.
Application configuration
The configuration model groups settings by the system behavior they control rather than by individual method. The main groups are:
| Group | Purpose |
|---|---|
| JavaScript location | Finds package.json, lockfiles, and installed packages. |
| Client registration | Controls automatic bundle loading, component and store source locations, and the wait time for client registration. |
| Server rendering | Selects the server bundle, pool size, timeout, error behavior, and whether the server bundle must stay outside public/. |
| Build and test flow | Connects production and test build commands to Rails asset tasks. |
| Generated assets | Describes manifest files, output names, loading strategy, and optional client/server bundle sharing. |
| Optional features | Covers translations, custom rendering extensions, generated server-bundle entry points, and development database checks. |
Development defaults favor tracing, development mode, and raising on prerender errors. Production can use a larger renderer pool, while development forces the pool to one renderer. Configuration setup also fills required generated-file names, checks loading strategies, and keeps the server bundle output aligned with Shakapacker's private output path when that feature exists.
References: react_on_rails/…/configuration.rb• L23-71 react_on_rails/…/configuration.rb• L74-160 react_on_rails/…/configuration.rb• L164-271
Bundler configuration
Shakapacker remains the asset-bundler contract for the open-source Rails integration. Its configuration defines the JavaScript source root, entry-point root, nested-entry behavior, public output, test output, development server, transpiler, and active assets bundler. Fresh setup prefers Rspack when the installed Shakapacker version supports it, but an existing explicit assets_bundler choice remains authoritative. The installer passes the same resolved choice to Shakapacker so dependency setup and generated configuration stay aligned. When no valid choice is available, shared diagnostics treat Webpack as the fallback.
References: react_on_rails/…/shakapacker.yml.tt• L4-27 react_on_rails/…/shakapacker.yml.tt• L41-77 react_on_rails/…/shakapacker_config_helpers.rb• L20-23 react_on_rails/…/shakapacker_config_helpers.rb• L61-92 react_on_rails/…/generator_helper.rb• L373-447
Server bundle security
Server-rendered JavaScript is a private application resource. The generated configuration uses Shakapacker's private_output_path when available and can enforce that server bundles never fall back to a public asset directory. If private-bundle enforcement is enabled, the output path must exist outside public/; otherwise configuration fails early instead of allowing server code to be exposed.
References: react_on_rails/…/react_on_rails.rb.tt• L9-32 react_on_rails/…/configuration.rb• L196-220 react_on_rails/…/utils.rb• L88-110
Configuration path resolution
Path resolution keeps Rails boot, diagnostics, and generators pointed at the same files. SHAKAPACKER_CONFIG overrides the default config/shakapacker.yml; relative override paths resolve from Rails.root during Rails boot and from the current directory for tools that run without Rails. The JavaScript package root follows node_modules_location: a relative value is under Rails.root, an absolute value is used as given, and . means the Rails root. Missing package roots or package.json files produce one warning per path and cause checks that depend on them to stop rather than inspect unrelated files.
References: react_on_rails/…/engine.rb• L105-121 react_on_rails/…/shakapacker_config_helpers.rb• L27-59 react_on_rails/…/config_path_resolver.rb• L30-75
The path rules form one resolution flow from process input to package and bundler files.
flowchart TD
A[Process and Rails root] --> B{SHAKAPACKER_CONFIG set?}
B -- yes --> C[Resolve configured Shakapacker file]
B -- no --> D[Use config/shakapacker.yml]
C --> E[Read active bundler and paths]
D --> E
A --> F[node_modules_location]
F --> G[Resolve package root]
G --> H[Read package.json and lockfiles]
E --> I[Find Webpack or Rspack config]
H --> J[Run package and version checks]
I --> K[Run bundler checks]
Setup generation
Generators turn an existing Rails app into a working integration. They select a product and bundler path, install or verify prerequisites, write the Rails and Shakapacker configuration, create the initial client and server entry points, and add the commands that build and run them.
Installation generator
rails generate react_on_rails:install is the main setup coordinator. It checks Node.js, a JavaScript package manager, the app state, and any requested Pro or Tailwind requirements before it changes the app. Product selection can come from explicit flags or an interactive prompt, while incompatible standard-only and Pro choices are rejected. The generator then ensures Shakapacker is available, forwards the selected options to the base and feature generators, installs JavaScript dependencies, adds build scripts, creates development and CI launch files, and prints the next steps. Dependency install problems produce warnings and manual commands so the generator can leave a reviewable setup instead of failing on a network or package-manager error. Its post-install message reports the detected process and package managers, gives the available start modes, shows the generated visit URL and render example, and adds testing guidance when the app has no RSpec setup. Generated CI setup follows the package manager used by the app and supplies a pnpm version only when the project does not declare one. The generator writes consumer-scoped agent guidance by default when AGENTS.md is absent, but it does not overwrite existing guidance. It preserves customized development launchers and existing guidance files instead of replacing them. During setup it temporarily skips boot-time version validation because the matching JavaScript package may not exist until the generator finishes.
References: react_on_rails/…/install_generator.rb• L187-234 react_on_rails/…/install_generator.rb• L246-271 react_on_rails/…/install_generator.rb• L282-337 react_on_rails/…/install_generator.rb• L340-388 react_on_rails/…/install_generator.rb• L401-423 react_on_rails/…/install_generator.rb• L536-563 react_on_rails/…/install_generator.rb• L709-740 react_on_rails/…/generator_messages.rb• L21-115 react_on_rails/…/generator_messages.rb• L119-190
Generated Rails wiring
The base generator creates the Rails initializer, Shakapacker configuration, bundler configuration, server-bundle entry point, layouts, example route, and development support files. It reads the app's Shakapacker source and entry paths so generated files follow an existing app layout instead of assuming app/javascript/packs. Fresh Rspack setup can remove only recognized stale Webpack files; unknown files, directories, and custom content remain in place. The generator also preserves customized bundler files, adds Tailwind pack tags only when the layout has a known safe shape, and keeps existing bin/dev logic when replacement would be unsafe.
References: react_on_rails/…/base_generator.rb• L208-241 react_on_rails/…/base_generator.rb• L250-321 react_on_rails/…/base_generator.rb• L394-454 react_on_rails/…/base_generator.rb• L989-1027 react_on_rails/…/base_generator.rb• L1120-1199 react_on_rails/…/generator_helper.rb• L104-145 react_on_rails/…/generator_helper.rb• L223-265
JavaScript dependency setup
The JavaScript dependency setup turns the selected integration features into a matching package set. The base path installs the JavaScript package that matches the Ruby gem, while Pro replaces that package and RSC adds its own tested package and React version line. Pro package versions follow the gem version, including the conversion from RubyGems prerelease notation to npm notation. RSC setup pins react-on-rails-rsc to the tested stable 19.3.1 package and keeps react and react-dom on the ~19.3.1 line. This keeps the RSC runtime, React Flight packages, and bundler plugins on one supported line.
The setup always runs a package install after it updates dependency declarations. It uses the app's package manager when its package helper is available and falls back to direct package-manager commands when it is not. A failed install does not hide the required package versions: the generator records versioned entries in package.json when possible and prints a command for a manual install. For RSC, a failed pin install keeps the tested package pin instead of silently selecting an unverified latest version.
References: react_on_rails/…/js_dependency_manager.rb• L140-171 react_on_rails/…/js_dependency_manager.rb• L175-199 react_on_rails/…/js_dependency_manager.rb• L261-271 react_on_rails/…/js_dependency_manager.rb• L398-447 react_on_rails/…/js_dependency_manager.rb• L573-646
Feature generators
The installation flow composes focused generators for the initial React example, legacy Redux scaffolding, Pro support, and React Server Components. The default flow creates a simple React example; Redux remains a hidden legacy path for existing apps. Pro adds its gem and JavaScript package, creates the Node Renderer process, and upgrades the active generated Webpack or Rspack configuration only when its complete pair matches a supported generated form. Customized, incomplete, ambiguous, or older unsupported pairs remain unchanged and require manual migration. A standalone Pro upgrade replaces the base gem in the Gemfile, updates JavaScript imports, and can restore the earlier Gemfile when bundle installation fails. The standalone Pro and RSC generators expect a base React on Rails setup first, which keeps upgrades distinct from a new installation.
The RSC setup also checks the installed React version before it changes the app. It requires the supported React 19.3 line and warns when the version is below the patched 19.3.1 release. The warning names the React, React DOM, and React Flight packages that must move together, then points to the doctor command and package-manager overrides for a retained nested Flight copy. The setup adds the RSC initializer settings, bundle watcher, example server component flow, and routes. It also keeps the client-reference contract current in existing Webpack or Rspack configuration. It can create or migrate the shared client-reference resolver, use the generated manifest when available, include client components supplied by the Pro package, and add that resolver to the matching server or client plugin. If the configuration is custom, stale, ambiguous, or outside the generator's safe rewrite shape, it leaves the file unchanged or rolls back a partial change and gives a manual action instead of risking a broken bundler file.
References: react_on_rails/…/install_generator.rb• L298-336 react_on_rails/…/react_with_redux_generator.rb• L16-44 react_on_rails/…/pro_generator.rb• L33-57 react_on_rails/…/pro_generator.rb• L101-183 react_on_rails/…/pro_generator.rb• L203-307 react_on_rails/…/rsc_generator.rb• L51-68 react_on_rails/…/rsc_setup.rb• L47-127 react_on_rails/…/rsc_setup.rb• L138-379 react_on_rails/…/rsc_setup.rb• L437-521 react_on_rails/…/rsc_setup.rb• L612-746 react_on_rails/…/client_references.rb• L31-240 react_on_rails/…/client_references.rb• L347-371 react_on_rails/…/client_references.rb• L1444-1475 react_on_rails/…/pro_setup.rb• L59-94 react_on_rails/…/pro_setup.rb• L433-489 react_on_rails/…/generated_webpack_config_pair.rb• L5-32 react_on_rails/…/USAGE• L1-32
The installation flow moves from prerequisite checks to shared Rails wiring and then to the selected feature set.
flowchart LR
A[Install command] --> B[Check Node and package manager]
B --> C[Ensure Shakapacker]
C --> D[Generate Rails and bundler configuration]
D --> E[Generate React example]
E --> F{Selected feature}
F -- base --> G[Client and server setup]
F -- Pro --> H[Pro renderer setup]
F -- RSC --> I[Pro and RSC setup]
G --> J[Build and run guidance]
H --> J
I --> J
Compatibility and diagnostics
Setup validation protects the contract between the Ruby gem, the JavaScript package, the Rails app, and the asset toolchain. Validation runs both during installation and at application boot, while the doctor command gives a broader report for an app that already exists. See development and diagnostics for the wider development and support workflow.
Version alignment
The Ruby gem and its JavaScript package must use matching exact versions for normal installed packages. The checker reads the package root selected by node_modules_location, prefers the Pro package when present, and also reads lockfiles when they provide the installed version. It rejects missing packages, both base and Pro packages together, a Pro gem paired with the base package, a Pro package without the Pro gem, non-exact ranges, and mismatched versions. RubyGems prerelease notation and npm prerelease notation are converted before comparison.
RSC adds a separate runtime pairing rule. Its pinned 19.3.1 package is coordinated with React and React DOM ~19.3.1, with React Flight at the patched 19.3.1 level. The RSC generator warns about an existing incompatible React version or a lower patch level and gives one upgrade command for the related packages. The Pro node renderer can refuse to start when those React lines do not match.
References: react_on_rails/…/version_checker.rb• L27-41 react_on_rails/…/version_checker.rb• L45-189 react_on_rails/…/version_checker.rb• L191-247 react_on_rails/…/version_checker.rb• L447-505 react_on_rails/…/version_syntax_converter.rb• L5-20 react_on_rails/…/js_dependency_manager.rb• L147-171 react_on_rails/…/rsc_setup.rb• L84-127
The react_on_rails:sync_versions task can update supported package entries in package.json. It is a dry run by default and writes only when REACT_ON_RAILS_WRITE=true or WRITE=true; lockfiles still need a package-manager install after the file changes.
References: react_on_rails/…/sync_versions.rake• L6-22 react_on_rails/…/version_synchronizer.rb• L24-45 react_on_rails/…/version_synchronizer.rb• L120-145
System checks
The react_on_rails:doctor task gives a health view of the integration. It checks environment prerequisites, package and version state, key Rails and Shakapacker files, bundler choice, generated output, development launchers, test setup, and optional Pro or RSC requirements. Text output is for engineers; JSON output follows a stable schema with check IDs, statuses, details, and remediation data for automation.
References: react_on_rails/…/doctor.rake• L39-65 react_on_rails/…/doctor.rb• L225-255 react_on_rails/…/doctor_schema.rb• L3-17 react_on_rails/…/doctor_schema.rb• L19-78
The system checker treats missing setup pieces as actionable results rather than assuming a standard app shape. It detects npm, pnpm, Yarn, or Bun from the lockfile beside the configured package root, accepts Webpack or Rspack config paths, checks the Rails initializer, and reports version and dependency mismatches. If the configured package root or its package.json cannot be read, it blocks lockfile detection so a lockfile elsewhere does not produce a false manager result. When both bundler config families exist, an explicit Shakapacker choice wins; without one, the checker reports the ambiguity and uses Webpack as the fallback. Node must be available; Node 18 or newer is the supported target for the setup checks.
References: react_on_rails/…/system_checker.rb• L50-135 react_on_rails/…/system_checker.rb• L138-184 react_on_rails/…/system_checker.rb• L310-358 react_on_rails/…/system_checker.rb• L456-482
Change checklist
When these files or sections are changed, remember to consider these:
- Keep
node_modules_locationpath rules aligned across boot-time version checks, doctor checks, and bundler diagnostics. - Keep
SHAKAPACKER_CONFIGresolution aligned between the Rails engine and command-line helpers. - When changing a configuration default, review generated initializer text and the Shakapacker template together.
- When changing the active bundler choice, update the Shakapacker install handoff, generator destination paths, stale-config cleanup, and doctor detection so Webpack and Rspack do not enter a mixed state.
- When changing package-manager detection, keep lockfile scope, generated CI cache settings, and the pnpm fallback rule aligned.
- When changing version rules, preserve the gem-to-npm prerelease conversion and the exact-version contract used by both boot validation and sync tools.
- When changing the RSC package pin or supported React range, update dependency installation, the RSC compatibility warning, the React Flight package guidance, and the Pro runtime pairing guidance together.
- When adding a doctor check, keep its check ID, schema metadata, status, and remediation shape in sync.
- When changing Pro setup migration rules, keep full generated-pair recognition, active Webpack or Rspack paths, and the manual migration warning aligned.
- When changing RSC client-reference setup, keep manifest discovery, Pro client-component registration, active bundler plugin wiring, structural verification, and safe rollback or manual-migration warnings aligned.
- When changing Node Renderer setup, keep the initializer password behavior, legacy renderer detection, and process-manager commands aligned.
- When changing dependency fallback behavior, keep the package pins in package.json consistent with the manual install command shown to the user.
- When changing generated agent guidance, preserve the rule that an existing AGENTS.md prevents pointer files from being written.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| The Ruby gem and JavaScript package use different exact versions. | Rails boot fails before the integration can serve requests. | Boot validation and react_on_rails:sync_versions expose the mismatch early. |
Both react-on-rails and react-on-rails-pro are installed. | The package boundary is ambiguous and Pro setup can conflict with the base package. | The version checker rejects the duplicate package state. |
node_modules_location points to the wrong root. | Version, dependency, and package-manager checks cannot find the app's package.json. | Shared path resolution warns about the missing root or file. |
| The configured package root has no readable package.json. | The checker cannot identify the app's package manager and may skip related dependency checks. | The package-root check blocks lockfile scanning instead of using an unrelated lockfile. |
SHAKAPACKER_CONFIG points to a missing file. | Bundler detection can select the wrong fallback or fail to find the active config. | Engine boot logs the resolved missing path and advises fixing or removing the override. |
| Webpack and Rspack config files both exist without a clear active bundler. | Diagnostics can inspect a different config family from the one used by the app. | An explicit Shakapacker bundler choice selects the family; otherwise diagnostics warn and default to Webpack. |
Private server-bundle enforcement uses a path under public/. | Server-side code can be exposed as a public asset. | Configuration raises an error and requires an output path outside public/. |
| Auto-bundling is enabled without a component source subdirectory. | Generated component packs cannot be mapped to source files. | Configuration rejects the setup before pack generation. |
--standard-only is combined with --pro or --rsc. | The install request has conflicting product requirements. | The generator rejects the choice before it invokes child generators. |
| An installation runs while the package is not yet installed. | Boot validation would report a false missing-package failure during generation. | The installer sets a temporary validation-skip flag and removes it when the run ends. |
| A package install fails during generation. | package.json can lack the packages needed by the generated app. | Dependency setup warns, preserves versioned package entries when possible, and prints a manual command. |
| An RSC install cannot install its tested package pin. | The app can be left with an unverified RSC package choice. | The generator keeps the tested pin in package.json and gives a manual install command. |
| The RSC package, React, and React Flight use different supported lines. | The Pro node renderer can refuse to start or the RSC build can use an unsafe Flight copy. | RSC dependency setup pins the package and React line, warns below the patched React level, and points to lockfile overrides and doctor. |
| The Pro generator finds customized, incomplete, ambiguous, or unsupported Webpack or Rspack config files. | Pro settings are not added automatically, so server rendering can remain on the old setup. | The generator leaves both files unchanged and gives the manual migration guide. |
| An RSC client-reference manifest is missing or older than the server-component registration entry. | The bundle can use incomplete client-reference data or stop before it can build the RSC config. | The setup checks manifest freshness, warns when it is stale, and requires the precompile hook when discovery is active. |
| An existing RSC config uses a custom or unparseable plugin shape. | Automatic migration can leave client references unscoped or corrupt the bundler config. | The generator skips the rewrite or rolls back a partial rewrite and asks for manual clientReferences setup. |
| A generated RSC resolver predates Pro client-component registration. | Pro-owned client components can be absent from the client manifest and fail during render. | The generator warns and requires the resolver to be regenerated or updated with the Pro component references. |
| A legacy client/node-renderer.js remains in use after Pro setup. | The process manager can launch the old renderer or use a password that differs from the new initializer. | The generator keeps the legacy file, warns about stale process entries, and requires the same RENDERER_PASSWORD during migration. |
| Pro creates an initializer beside an existing renderer without a shared literal. | Rails and the Node renderer can reject each other or fail to authenticate. | The generated initializer uses ENV["RENDERER_PASSWORD"] when it cannot create both files together. |
Key files
| File | Why to read it |
|---|---|
| react_on_rails/…/react_on_rails.rb | Ruby load boundary for the Rails integration. |
| react_on_rails/…/engine.rb | Rails boot hooks, package validation, Shakapacker guard handling, and pool preparation. |
| react_on_rails/…/configuration.rb | Shared application settings and cross-setting validation. |
| react_on_rails/…/shakapacker_config_helpers.rb | Shared interpretation of Shakapacker paths, bundler choice, and development server settings. |
| react_on_rails/…/config_path_resolver.rb | Package-root and bundler-config path discovery used by diagnostics. |
| react_on_rails/…/install_generator.rb | Main installation lifecycle, product selection, package-manager setup, and feature selection. |
| react_on_rails/…/base_generator.rb | Rails initializer, Shakapacker, bundler, layout, development-file, and safe cleanup generation. |
| react_on_rails/…/js_dependency_manager.rb | Feature-aware JavaScript package setup, exact version pins, RSC React pairing, and fallback behavior. |
| react_on_rails/…/generator_messages.rb | Post-install guidance, manager detection, generated render examples, and setup next steps. |
| react_on_rails/…/pro_generator.rb | Standalone Pro upgrade and safe Gemfile or import migration. |
| react_on_rails/…/pro_setup.rb | Pro renderer setup, password coordination, process-manager wiring, and safe bundler migration. |
| react_on_rails/…/rsc_setup.rb | RSC files, React and Flight compatibility warnings, and safe configuration migration. |
| react_on_rails/…/client_references.rb | RSC client-reference discovery, Pro component registration, bundler wiring, and safe config migration. |
| react_on_rails/…/generated_webpack_config_pair.rb | Recognition of supported generated config pairs before Pro rewrites them. |
react_on_rails/lib/generators/react_on_rails/pro/USAGE | User-facing Pro prerequisites and the rules for automatic config migration. |
| react_on_rails/…/version_checker.rb | Ruby gem and JavaScript package compatibility rules. |
| react_on_rails/…/system_checker.rb | Setup checks for existing applications and package-manager or bundler detection. |
| react_on_rails/…/doctor.rb | User-facing diagnostic orchestration and text or JSON reporting. |
react_on_rails/lib/generators/react_on_rails/templates/base/base/config/initializers/react_on_rails.rb.tt | Recommended generated configuration shape. |
react_on_rails/lib/generators/react_on_rails/templates/base/base/config/shakapacker.yml.tt | Generated asset-bundler defaults and environment model. |
react_on_rails/lib/generators/react_on_rails/templates/pro/base/config/initializers/react_on_rails_pro.rb.tt | Generated Pro renderer endpoint, timeout, cache, tracing, and password configuration. |