JavaScript runtime and component registration
React on Rails uses shared JavaScript entry points to expose named components and stores to Rails, the browser, and server rendering. The browser reads Rails page data and mounts registered components. The server runtime uses the same names to produce render results. This page is a child of Rails integration; see Rails engine and configuration for Ruby setup, component rendering and hydration for the mount path, server-rendering protocol for the Ruby bridge, and development and diagnostics for pack and integration checks.
Runtime entry points
The JavaScript package provides separate entry points for browser use, server rendering, and React Server Components. Each entry point builds a ReactOnRails API with only the capabilities that its runtime can support.
Browser entry points
The browser entry point is react-on-rails/client. It provides component and store registration plus client lifecycle methods, but it does not include the server-rendering API. The full entry point can also run in a browser, but it adds server-rendering code and warns about the larger browser payload (packages/…/ReactOnRails.client.ts• L1-21, packages/…/ReactOnRails.full.ts• L1-22, packages/…/ssr.ts• L8-20).
Pro uses the react-on-rails-pro package for its runtime. Its default entry point is the full Pro runtime. The react-on-rails-pro/client entry point is focused on browser work and adds async registration access and incremental hydration. Pro and RSC applications use the matching Pro package instead of adding a direct react-on-rails dependency (packages/…/package.json• L36-65, packages/…/ReactOnRails.client.ts• L16-22, packages/…/install-and-upgrade.md• L93-112).
References: packages/…/ReactOnRails.client.ts• L1-21 packages/…/ReactOnRails.full.ts• L1-22 packages/…/ReactOnRails.client.ts• L16-22 packages/…/package.json• L36-65 packages/…/install-and-upgrade.md• L93-112
Server entry points
The standard package's full entry point supplies normal server-side rendering. Pro also exposes a Node entry point for server rendering and streaming, plus an RSC entry point for React Server Components. A Pro application can use the standalone NodeRenderer package when its server setup requires it. RSC applications use a separately pinned react-on-rails-rsc package (packages/…/ReactOnRails.full.ts• L16-23, packages/…/ReactOnRails.node.ts• L16-27, packages/…/ReactOnRailsRSC.ts• L16-26, packages/…/install-and-upgrade.md• L100-112).
Rails selects the server operation through the runtime API. Normal server rendering uses serverRenderReactComponent. Pro streaming uses streamServerRenderedReactComponent. RSC rendering uses serverRenderRSCReactComponent (packages/…/index.ts• L664-695).
References: packages/…/ReactOnRails.full.ts• L16-23 packages/…/ReactOnRails.node.ts• L16-27 packages/…/ReactOnRailsRSC.ts• L16-26 packages/…/index.ts• L664-695 packages/…/install-and-upgrade.md• L100-112
ReactOnRails API construction
ReactOnRails is an API object assembled from capabilities. A capability groups related methods. The entry point combines core, lifecycle, server-rendering, streaming, or RSC capabilities that fit its runtime. This keeps one public name while allowing browser and server bundles to expose different behavior.
The construction model is shown below.
flowchart LR
Core[Core capability] --> Factory[ReactOnRails API factory]
Lifecycle[Lifecycle capability] --> Factory
SSR[Server-rendering capability] --> Factory
ProMethods[Pro method capability] --> Factory
ProLifecycle[Pro lifecycle capability] --> Factory
Streaming[Pro streaming capability] --> Factory
RSC[Pro RSC capability] --> Factory
Factory --> Global[globalThis.ReactOnRails]
Core capability
The core capability owns the common API contract. It manages options, registration, store access, direct rendering, authenticity headers, and base error and console helpers. It also reports when a method needs a server or Pro runtime (packages/…/core.ts• L50-60, packages/…/core.ts• L65-141, packages/…/core.ts• L147-247, packages/…/core.ts• L249-323).
The public API type shows the boundary. Application code registers components and store generators. Rails-facing and diagnostic methods extend the internal API used by the rendering bridge (packages/…/index.ts• L493-557, packages/…/index.ts• L581-729).
References: packages/…/core.ts• L50-60 packages/…/core.ts• L65-141 packages/…/core.ts• L147-247 packages/…/index.ts• L493-557 packages/…/index.ts• L581-729
Capability variants
The factory merges capabilities in order and keeps one API object for a runtime. On first construction it assigns the object to globalThis.ReactOnRails. Later construction attempts reuse that object and add only later capabilities. This prevents a second bundle from replacing stronger server or streaming methods with base stubs (packages/…/createReactOnRails.ts• L11-19, packages/…/createReactOnRails.ts• L95-127).
The factory checks for multiple runtimes, mixed core and Pro packages, and different registry objects. These checks protect the shared global state that registration and rendering require (packages/…/createReactOnRails.ts• L31-93).
References: packages/…/createReactOnRails.ts• L11-19 packages/…/createReactOnRails.ts• L31-127 packages/…/ReactOnRails.full.ts• L9-18 packages/…/createReactOnRailsPro.ts• L25-52
Console replay scripts
Server rendering can pass captured console messages back to the browser as a replay script. The runtime escapes the completed script before Rails or Pro streaming places it in HTML. This protects script-end and HTML-comment markers while the browser still runs the original messages. The Rails helper can then add the request's CSP nonce when it wraps the script (packages/…/buildConsoleReplay.ts• L13-18, packages/…/buildConsoleReplay.ts• L54-60, react_on_rails/…/helper.rb• L739-752).
The same escaping contract applies to Pro RSC payload injection. This protects server-generated inline scripts in both the normal Ruby response path and the streaming path (packages/…/escapeScript.ts• L10-20, packages/…/injectRSCPayload.ts• L23-25, packages/…/injectRSCPayload.ts• L59-75).
References: packages/…/buildConsoleReplay.ts• L13-18 packages/…/buildConsoleReplay.ts• L54-60 packages/…/escapeScript.ts• L10-20 react_on_rails/…/helper.rb• L739-752 packages/…/injectRSCPayload.ts• L23-25 packages/…/injectRSCPayload.ts• L59-75
Component and store registries
Registration turns application names into runtime lookup keys. Rails emits a component or store name into page data. The matching JavaScript bundle must register that name before the client or server uses it.
Component registry
The component registry maps a name to a component entry. The entry records the value and whether it is a two-argument render function or a three-argument renderer that owns its browser mount. A missing name is an immediate error that includes the names known to the runtime (packages/…/ComponentRegistry.ts• L6-63, packages/…/index.ts• L356-370).
ReactOnRails.register({ Name: Value }) is the normal application operation. Re-registering the same object supports hot reload. Reusing a name for a different object logs a warning because the name-to-component contract is no longer stable (packages/…/core.ts• L147-177).
References: packages/…/ComponentRegistry.ts• L6-63 packages/…/core.ts• L147-177 packages/…/index.ts• L356-370
Store registry
The store registry has two related roles. A store generator is registered code that creates a store from props and Rails context. A hydrated store is the page-specific store created from Rails data. This separation lets one bundle remain loaded across navigations while page state is replaced (packages/…/StoreRegistry.ts• L3-21, packages/…/StoreRegistry.ts• L23-49, packages/…/StoreRegistry.ts• L104-141).
The registry keeps store generators as shared runtime registrations. It keeps hydrated stores in the active store scope when one is supplied by the host runtime. Without such a scope it uses the default page store map. This allows separate request or render scopes to keep page state apart without changing the registration contract (packages/…/StoreRegistry.ts• L3-21).
registerStoreGenerators registers the generator. Client startup later creates and stores the hydrated instance. getStore reads that instance for components, while getStoreGenerator is used during initialization. The older registerStore name remains as an alias for compatibility (packages/…/core.ts• L179-227, packages/…/index.ts• L499-517).
Pro adds wait-for-registration access for components or stores that arrive after the first page work. Its registry rejects pending waits on page unload so a new page does not receive stale page data (packages/…/StoreRegistry.ts• L88-125, packages/…/StoreRegistry.ts• L151-165, packages/…/CallbackRegistry.ts• L64-103).
References: packages/…/StoreRegistry.ts• L3-21 packages/…/StoreRegistry.ts• L23-49 packages/…/StoreRegistry.ts• L59-123 packages/…/StoreRegistry.ts• L127-141 packages/…/core.ts• L179-227 packages/…/StoreRegistry.ts• L88-165 packages/…/CallbackRegistry.ts• L64-103
Rails context and page declarations
Rails provides the browser runtime with a JSON Rails context and one declaration for each component or store. These declarations connect server output to the names held in the JavaScript registries.
Rails context
The Rails context is the shared page record for runtime state. It includes the environment, locale, URL, Pro availability, registration timeout, and a serverSide flag. Pro server-component pages can add payload and manifest data to the same record (react_on_rails/…/helper.rb• L344-413, packages/…/index.ts• L20-62).
The browser reads the element with id js-react-on-rails-context. It caches the parsed value only while the source element and its text remain unchanged. A page replacement or DOM morph can therefore provide a new context (packages/…/context.ts• L10-41).
References: react_on_rails/…/helper.rb• L344-413 react_on_rails/…/helper.rb• L764-777 packages/…/context.ts• L10-41 packages/…/index.ts• L20-62
Component and store declarations
Rails renders component data as a script element with the registered component name, DOM id, props, and optional store dependencies. Store data uses a matching store name and JSON props. In Pro, each declaration can also invoke the matching load method as streamed markup arrives (react_on_rails/…/pro_helper.rb• L5-41, react_on_rails/…/pro_helper.rb• L60-87).
Generated packs follow the same contract. A client component pack imports the value and calls ReactOnRails.register. A store pack imports the generator and calls registerStore. RSC-aware generated packs use the separate server-component registration API instead of adding that component to the normal registry (react_on_rails/…/packs_generator.rb• L265-313).
References: react_on_rails/…/pro_helper.rb• L5-41 react_on_rails/…/pro_helper.rb• L60-87 react_on_rails/…/packs_generator.rb• L265-313
Client startup lifecycle
Client startup waits until the runtime, registration bundles, and page declarations can be considered together. Core performs a page-wide render sweep. Pro can act earlier for streamed or asynchronously registered content and then runs the same page-load recovery sweep.
The main lifecycle is shown below.
sequenceDiagram
participant Bundle as Browser bundle
participant API as ReactOnRails API
participant DOM as Rails page DOM
participant Life as Page lifecycle
participant Renderer as Client renderer
Bundle->>API: Construct global API
Bundle->>API: Set options and register components/stores
API->>Life: Register page-load callback
Life->>DOM: Wait for DOM ready or navigation event
Life->>Renderer: Run page-load sweep
Renderer->>API: Look up names and hydrated stores
Renderer->>DOM: Render or hydrate component roots
Core startup
The core entry point defers startup to the next event-loop turn. This gives synchronous scripts time to register components before the DOM scan begins. The startup handler runs once in a browser, waits for the page lifecycle, and then renders the page's components (packages/…/ReactOnRails.client.ts• L8-18, packages/…/clientStartup.ts• L13-30).
Page lifecycle support covers a normal page load plus Turbo or Turbolinks soft navigation. It also avoids starting the initial sweep during the interactive state when deferred scripts may still be registering components (packages/…/pageLifecycle.ts• L31-58, packages/…/pageLifecycle.ts• L61-114).
References: packages/…/ReactOnRails.client.ts• L8-18 packages/…/clientStartup.ts• L13-30 packages/…/pageLifecycle.ts• L31-58 packages/…/pageLifecycle.ts• L61-114
Pro startup
Pro starts its client lifecycle synchronously because streamed markup may need hydration before the first paint. If the Rails context is not in the DOM yet, Pro skips early work and relies on the later page-load sweep. When the context marks the page as Pro-enabled, it can hydrate complete components and stores as their markup becomes available (packages/…/proClientStartup.ts• L33-63).
On page load, Pro hydrates stores and renders or hydrates all components together. On page unload, it unmounts roots and clears page-specific hydrated stores, while registered code remains available for the next page (packages/…/proLifecycle.ts• L23-41, packages/…/proClientStartup.ts• L28-64).
References: packages/…/proClientStartup.ts• L28-64 packages/…/proLifecycle.ts• L23-41 packages/…/proMethods.ts• L28-49
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the package export conditions aligned with the runtime behavior. A browser, Node, and RSC import must not receive a bundle with the wrong capability set.
- Keep the component and store names aligned across generated packs, Rails data attributes, server registration, and client lookup. A name mismatch fails only when a page tries to render or hydrate it.
- Keep the selected package set aligned with the app stack. Pro and RSC apps must use the matching Pro package and must not add a direct base package. Standalone NodeRenderer and RSC packages have separate conditions or version pins.
- Keep
RailsContextaligned with the Rubyrails_contextoutput. Missing Pro or RSC fields can move a page from early hydration to fallback behavior or fail later during rendering. - Keep the one-global and one-registry rule intact when changing bundle entry points or Webpack runtime settings. Multiple runtime chunks can split registration state.
- Keep page unload behavior aligned for all registries. Hydrated stores are page data and must not survive a Turbo or Turbolinks page swap. Registered generators are bundle code and should survive it.
- Keep scoped hydrated-store behavior aligned with the host runtime. A changed scope contract can make stores leak between requests or disappear from the active render.
- Keep the shared script-escaping contract aligned across console replay, Ruby HTML wrapping, and Pro RSC streaming. Escaping must happen after the replay or payload code is formed so browser-visible message and payload values remain unchanged.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A component name in Rails data has no matching registration | The client or server cannot create the requested component | Register the exact name in the bundle used by that runtime; lookup errors include known names |
| A store generator is missing when a store declaration is processed | Store hydration fails before a component can read its shared state | Register the generator early, or use Pro's wait-for-registration path for late bundles |
| A store scope is created or cleared at the wrong lifecycle boundary | A request can read another request's hydrated store, or a component can lose its page store | Keep the host-provided scope request-local and clear page stores on navigation while retaining generators |
| A Pro or RSC app adds a direct base package or uses an unrelated package version | The app can load the wrong runtime or combine incompatible runtime capabilities | Follow the stack-specific package matrix and keep matching releases together |
| A second bundle creates a separate ReactOnRails global or registry set | Components and stores can be split across incompatible runtime state | Use one runtime chunk and do not mix core and Pro packages on one page |
| Startup scans during deferred script execution | A registered component may appear missing even though its script loads later | Let the page lifecycle wait for the load boundary, or use Pro incremental registration for streamed content |
| Hydrated stores are retained across soft navigation | The next page can read state and props from the previous page | Clear hydrated stores on page unload while retaining store generators |
turbo is enabled while the page uses Turbolinks | Load and unload callbacks listen for events that never occur | Set the option for the navigation library that actually runs on the page |
| Generated inline script content reaches HTML without the shared escaping step | A message or payload can close the script element or alter how the browser parses the page | Use escapeScript on the completed script content before Ruby or Pro wraps it |
Key files
| File | Why to read it |
|---|---|
| packages/…/ReactOnRails.client.ts | Browser entry point for the core runtime |
| packages/…/ReactOnRails.full.ts | Full runtime composition with server-rendering capability |
| packages/…/createReactOnRailsPro.ts | Pro API composition and registry selection |
| packages/…/createReactOnRails.ts | Global API construction, reuse, and runtime safety checks |
| packages/…/core.ts | Common registration, options, store, and rendering contract |
| packages/…/ComponentRegistry.ts | Named component lookup and render-role metadata |
| packages/…/StoreRegistry.ts | Store generator registration and scoped page-hydrated store boundary |
| packages/…/buildConsoleReplay.ts | Server console capture converted into browser replay code |
| packages/…/escapeScript.ts | Shared boundary that makes generated inline scripts safe for HTML |
| packages/…/clientStartup.ts | Core browser startup handoff to page lifecycle |
| packages/…/pageLifecycle.ts | DOM-ready and Turbo/Turbolinks load and unload events |
| packages/…/proClientStartup.ts | Pro early hydration and soft-navigation startup |
| react_on_rails/…/pro_helper.rb | Rails declarations consumed by the browser |
| react_on_rails/…/packs_generator.rb | Generated registration entry points for components and stores |
| react_on_rails/…/helper.rb | Rails wrapping of server-generated console replay scripts |
| packages/…/index.ts | Shared API, registry, and Rails context contracts |
| packages/…/install-and-upgrade.md | Stack-specific package selection and version alignment during install or upgrade |