Rspack hot reloading for Rails

For new Rails apps: every save lands in the open page.

$ rails new myapp -m https://raw.githubusercontent.com/tungnt1203/rspack_rails/main/template.rb
Read the guide

Two servers, one origin

Rails proxies /rspack/* to rspack serve. Only the hot update socket goes to it directly.

  1. Browserthe page you are editing
  2. page and /rspack/*
  3. Railslocalhost:3000
  4. /rspack/* proxied
  5. rspack servelocalhost:3036

Hot update socket, from rspack serve straight back to the browser

The helpers point at /rspack/…, so bundles, chunks, workers and hot updates are same-origin with the page.

  1. Browsera plain page load
  2. page and assets
  3. Railslocalhost:3000
  4. last build
  5. app/assets/buildswritten by rspack build

Stop bin/dev and the helpers fall back to the last build. Nothing to configure.

  1. rspack:buildruns before assets:precompile
  2. bundles
  3. app/assets/buildschunks already hashed
  4. fingerprinted
  5. Propshaft or Sprocketsserves public/assets

Ship with Kamal: Node.js joins the build stage, and the final image ships without it. Dev containers get Node.js and port 3036 too.

Dockerfile, build stage
# Install Node.js: assets:precompile runs rspack (rspack_rails)
ARG NODE_VERSION=22.12.0
RUN curl -sL …/node-build/archive/master.tar.gz | tar xz -C /tmp/ && …

# Install node modules
COPY package.json package-lock.json ./
RUN npm ci

RUN SECRET_KEY_BASE_DUMMY=1 ./bin/rails assets:precompile
RUN rm -rf node_modules
counter_controller.ts in the editor, the label changed from click to tap
The demo page showing 0 taps, with the amber card still in place
home.html.erb in the editor, bg-amber-100 added to the first card
The demo page reloaded with the first card in amber

Save, and the page follows

  1. Under bin/dev

    Scripts and styles swap in place. Recorded in the demo app: a view, then a controller.

  2. Edit a controller

    counter_controller.ts: “click” becomes “tap”.

  3. The count stays

    The controller is swapped; the page never reloads.

  4. Edit a view

    home.html.erb gains bg-amber-100.

  5. The page reloads

    The class is already built when it lands.

Create the app

The template adds the gem, asks five questions and builds once.

A new appterminal
$ rails new myapp -m …/rspack_rails/main/template.rb
  Add Stimulus with controller autoloading? (y/N) y
  Add TypeScript type checking? (y/N) y
  Add React with Fast Refresh? (y/N) n
  Add Tailwind CSS v4? (y/N) y
  Add Sass? (y/N) n
rspack_rails is set up with stimulus, typescript, tailwind.
Run bin/dev to start.
An existing appthe same steps, one by one
$ bundle add rspack_rails
$ bin/rails rspack:install
$ bin/rails rspack:install:stimulus
$ bin/rails rspack:install:typescript
$ bin/rails rspack:install:react
$ bin/rails rspack:install:tailwind
$ bin/rails rspack:install:sass

Ask the doctor

bin/rails rspack:doctor checks the setup and says what to fix. An error exits with status 1, so CI can run it.

Output of rspack:doctor in a new app
CheckResultDetail
Node.jsPassv26.5.1 (rspack needs ^20.19.0 || >=22.12.0)
@rspack/corePass2.3.2
LayoutPassapplication.html.erb uses rspack_javascript_tag
DockerfilePassinstalls Node.js and node modules
Dev serverWarningnot running at http://localhost:3036

The demo app, running

React, CSS modules, a Web Worker and Turbo Streams, deployed with Kamal. Open it or read its source.

The demo app's home page: its heading beside the manifest the helpers read, with the hashed script and stylesheet
A histogram of thirty million samples built in a Web Worker
Thirty million samples in a Web Worker, 574 ms, and the page keeps moving.
The admin page: application.js at 439 KB beside admin.js at 1.28 KB
/admin links its own entry: 1.28 KB of JavaScript.

Tested on every pull request

Rails 7.2 Rails 8.1

A new app on each, built and precompiled

Ruby
3.2 or newer
Node.js
20.19 or 22.12 and newer
Package manager
bun, yarn, pnpm or npm, read from the lockfile
Asset pipeline
Propshaft or Sprockets

Start a new app

Already have an app? bundle add rspack_rails, then bin/rails rspack:install.

$ rails new myapp -m https://raw.githubusercontent.com/tungnt1203/rspack_rails/main/template.rb
See the demo app