Moving Rails CSS From Sprockets to Propshaft
At FastRuby.io, in some Ruby on Rails applications we have worked on, we found that the JavaScript had moved to a bundler but Sprockets still compiled the CSS through sass-rails. In a previous article , I covered the JavaScript half of that move, and this one covers the CSS half.
Recently we replaced Sprockets with Propshaft and cssbundling-rails in a customer’s application. Propshaft mostly fingerprints and serves files, so the Sass CLI has to compile the CSS before Rails ever sees it. The application used Bootstrap 4, vendored locally along with a few plugins, so that is the stylesheet we had to move.
In this article, you will learn how to replace Sprockets with Propshaft, convert a Sprockets CSS manifest into a Sass entry point built by cssbundling-rails, and fix what breaks once Sprockets is no longer compiling your assets for you.
Starting Point
The application was on Rails 7.2 and had already moved its JavaScript to esbuild with jsbundling-rails, which writes app/assets/builds/application.js. CSS was a different story. These were the relevant lines in the Gemfile:
# Sass integration
gem "sass-rails"
# Modern JavaScript bundling with esbuild
gem "jsbundling-rails"
Sprockets decided what was publicly available through app/assets/config/manifest.js. Ours linked the images, the main stylesheet, every plain CSS file in the stylesheets directory, and the build output:
//= link_tree ../images
//= link application-bootstrap4.css
//= link_directory ../stylesheets .css
//= link_tree ../builds
The main stylesheet, which pulled in Bootstrap 4, was a CSS file with Sprockets directives in a comment block at the top, followed by a few plain CSS rules. The file names below are generalized, but the shape is the same as the real one:
/*
*= require ./theme/contents
*= require bootstrap-select.min.css
*= require feature_styles
*= require_self
*/
/* ...a few plain CSS rules... */
The application’s layouts loaded it with stylesheet_link_tag "application-bootstrap4", media: "all". There was also an application.css file left over from a much older version of the application, full of *= require lines, which no layout used anymore. We deleted it during the migration, but it is a good reminder to look for dead code before you start, since a *= require line in a file nobody loads is easy to mistake for something that matters.
What Propshaft Does
Sprockets does several jobs at once. It resolves *= require directives, compiles Sass and ERB, concatenates files, and fingerprints the result. Propshaft mostly just fingerprints. It looks through the asset directories of your application and its gems, adds a digest to each file name, and serves the files. In production, assets:precompile copies them with their digests into public/assets. It copies every file in the asset directories, not only the ones a manifest lists, which is a real difference from Sprockets. If you want to know how Rails ended up with so many asset tools, our article about the history of the asset pipeline covers Sprockets, Webpacker and the rest. And if you’re not sure you need to move off Sprockets at all, Navigating Asset Management Madness in Rails walks through the options for each Rails version.
Propshaft does rewrite url() references inside CSS files so they point to the fingerprinted images and fonts (it updates source map comments in the same way), but it doesn’t run Sass, ERB or Sprockets directives on the file. A *= require line is just a comment, a .scss file is just text, and ERB inside a .css.erb file is never evaluated. Anything Sprockets used to generate now has to exist before Rails serves it, which is where cssbundling-rails comes in. Rails 8 made Propshaft the default for new applications, but it also works on Rails 7.0 and 7.1, and we ran it on Rails 7.2.
Adding cssbundling-rails and Sass
The first step is to give the CSS its own build. We added gem "cssbundling-rails" to the Gemfile next to jsbundling-rails, and then we added sass as a development dependency in package.json, along with a build:css script. The script is what cssbundling-rails runs, so the name matters:
"devDependencies": {
"sass": "^1.77.0"
},
"scripts": {
"build:css": "sass ./app/assets/stylesheets/application-bootstrap4.scss:./app/assets/builds/application-bootstrap4.css --no-source-map --load-path=node_modules --load-path=app/assets/stylesheets --load-path=vendor/assets/stylesheets"
}
The command takes an input:output pair. The input is the entry point we create in the next section, and the output goes to app/assets/builds, the same directory jsbundling-rails already writes to. Propshaft finds the compiled file there, and because the output keeps the name application-bootstrap4.css, the stylesheet_link_tag calls in the layouts didn’t need to change.
The three --load-path flags tell Sass where to look when it sees an @import. node_modules is for packages installed with npm, app/assets/stylesheets is for our own files, and vendor/assets/stylesheets is where the Bootstrap plugin’s stylesheet lived. We also added a build:css:watch script, which is the same command with --watch at the end.
We wrote all of this by hand, but cssbundling-rails also ships an installer for it: bin/rails css:install:sass, with variants for bootstrap, bulma, postcss and tailwind. On a fresh Rails 8 app, the Sass installer creates app/assets/stylesheets/application.sass.scss, adds sass and a build:css script to package.json, and adds a CSS watcher to Procfile.dev. It also deletes app/assets/stylesheets/application.css, and if there is a Sprockets manifest.js, it removes the link_directory ../stylesheets .css line from it and adds //= link_tree ../builds. The new entry point is empty, so it doesn’t convert an existing manifest for you.
For a new app, or one with a simple stylesheet, the installer is the faster and less error-prone route. We skipped it for four reasons: we wanted to keep the existing application-bootstrap4 file name so the layouts wouldn’t change, Bootstrap 4 was vendored and not installed from npm, we needed an extra load path for vendor/assets/stylesheets, and we didn’t want a tool deleting application.css before we had read it. If you are in the same position, hand-edit. Otherwise, run the installer on a branch and review the diff before you commit it.
cssbundling-rails also hooks into assets:precompile. In version 1.4.3, which is what we had installed, the task runs css:build first. The gem picks the package manager from the lockfile it finds, so it ran npm run build:css for us because we have a package-lock.json (a project with a yarn.lock would get yarn build:css instead). It runs the matching install command first, npm install in our case, unless you set SKIP_YARN_INSTALL. Despite the name, that variable skips the install step for npm and pnpm too. If your deploy runs assets:precompile, it now builds the CSS inside that task, so our article about speeding up assets precompilation is worth a look if the step feels slow.
Converting the Manifest to a Sass Entry Point
Sass doesn’t know what a *= require directive is, so the manifest from the first section needs to become a real Sass file. We renamed application-bootstrap4.css to application-bootstrap4.scss and replaced each directive with an @import:
// Entry point compiled by cssbundling-rails (Sass CLI)
// Output: app/assets/builds/application-bootstrap4.css
// Bootstrap 4 core (vendored locally) + custom styles
@import 'theme/contents';
// Bootstrap-select plugin (from vendor/assets/stylesheets via --load-path)
@import 'bootstrap-select.min';
// Feature-specific styles
@import 'feature_styles';
/* ...a few plain CSS rules... */
There are two small differences from the original. The .css extension is gone from bootstrap-select.min, because Sass finds the file through the load paths we configured in the previous section. And there is no equivalent of require_self. In Sprockets it decided where the file’s own rules landed relative to the required files, and here the plain CSS rules simply come after the imports, which is where require_self was in our manifest anyway. If yours sits in the middle of the list, move the rules to match.
The build works after this change, but it’s noisy. With Sass 1.98.0, which is what our lockfile resolved to, npm run build:css exits successfully and prints about 420 deprecation warnings. They come in five types (import, slash-div, if-function, global-builtin and color-functions), and most come from Bootstrap 4’s own SCSS, which was written long before Sass deprecated those features. The import warnings also point at our own entry point, since @import itself is one of the deprecated features. Sass prints the first few of each type and then says “398 repetitive deprecation warnings omitted”. We didn’t touch the vendored Bootstrap files as part of this migration, so the warnings are still there.
The import warnings are the ones to keep an eye on, because Sass says @import will be removed in Dart Sass 3.0.0. The vendored Bootstrap 4.5 SCSS uses @import throughout and we found no @use in it, so moving to @use would probably mean changing Bootstrap’s own files, and we left that out of this migration. If you want to move your own stylesheets to @use, our article about migrating from sass-rails to Dart Sass covers the @use change and the Sass Migrator. Our ^1.77.0 range for sass in package.json doesn’t pull in a 3.x release by itself, so nothing breaks until someone chooses to upgrade.
Swapping Gems and Deleting Sprockets Config
With the CSS building on its own, the Sprockets-specific parts could go. In the Gemfile, we removed sass-rails and added propshaft (cssbundling-rails was already there from the previous step):
-gem "sass-rails"
+gem "propshaft"
Then we deleted the files that only Sprockets read. The first is app/assets/config/manifest.js, the one shown at the beginning. The second is config/initializers/assets.rb, which held three things: an asset version, a line adding node_modules to the asset load path, and a list of image extensions to precompile. We didn’t need any of it, but whether yours is safe to delete depends on which of the three you have:
- The image extensions are never needed. Propshaft has no precompile list and copies every image in the asset directories.
- The
node_modulesline is only needed if you serve files fromnode_modulesthrough asset helpers, and it has a cost, because Propshaft copies everything under its asset paths. Sass finds npm packages through the--load-pathflags instead. - The asset version still works. Propshaft mixes
config.assets.versioninto every digest, so if you bump it on purpose to expire cached assets, keep that line. Ours was Rails’ default"1.0", and deleting the file dropped it to Propshaft’s default of"1", so every digest changed once on the first deploy.
You might wonder if leaving assets.rb in place would break anything. When we reproduced it in development, the application booted without errors. A line like config.assets.precompile += [...] would normally fail if the setting didn’t exist, but Propshaft defines placeholders for precompile, compile, debug and the two compressors (an empty array for precompile, nil for the rest), so Sprockets-era code still loads. Propshaft ignores those settings, so our precompile list did nothing, and we didn’t need the other two lines either, so we deleted the file.
The last change was in config/environments/production.rb. It had a commented-out css_compressor line and config.assets.compile = true, which told Sprockets to compile any asset missing from public/assets at request time. Propshaft doesn’t compile anything, so that setting should have nothing to do anymore. We replaced both with a comment describing who does what now: esbuild builds the JavaScript, Sass builds the CSS, and Propshaft serves the results.
What Broke
Most of the migration was uneventful, but a few things didn’t survive the switch. The most important thing to know is when they fail. Propshaft raises an error when a view asks for an asset that isn’t in the load path, so these problems show up when the page renders and not when the application boots or when you precompile. These are the two errors we saw when we reproduced them, with the file names generalized:
Propshaft::MissingAssetError: The asset 'pdf_scripts.js' was not found in the load path.
Propshaft::MissingAssetError: The asset 'pdf-fonts.css' was not found in the load path.
The first one came from two PDF views that included a script which did not exist in the application. We removed the helper call from both views. If you generate PDFs with wicked_pdf, be aware that its own helper reports a missing asset differently. When we reproduced it by calling wicked_pdf_javascript_include_tag with a script that was not there, we got undefined local variable or method 'content' for an instance of WickedPdf::WickedPdfHelper::Assets::PropshaftAsset instead of MissingAssetError. The message is misleading, and the cause is the same missing file.
The second error came from a stylesheet named pdf-fonts.css.erb. It contained nothing but an ERB comment. Propshaft doesn’t process ERB, and it looks for the file by its full name, so the pdf-fonts.css that the view asked for didn’t exist. Renaming the file to pdf-fonts.css was enough, since there were no ERB directives to evaluate. If you do have real ERB in your CSS, you will need to generate that file before Propshaft sees it.
Two other things are not errors, but they can confuse you. The first is that assets:precompile in development leaves a public/assets/.manifest.json behind, and Propshaft serves from it. Propshaft warns about it (“Rails will not serve any changed assets until you delete public/assets/.manifest.json”), but it is easy to miss. When we reproduced the .erb case after a precompile, the page still found a fingerprinted pdf-fonts file, and the error only appeared after we deleted public/assets. If you have precompiled locally, delete that directory before you decide that a fix worked or that a problem is gone. This isn’t specific to Propshaft. Sprockets and Webpacker also write manifest files when you precompile (.sprockets-manifest-*.json in public/assets, manifest.json in public/packs), and leftover precompiled assets can hide changes in development with them too.
The second is that Propshaft also fingerprinted and copied application-bootstrap4.scss into public/assets, next to the compiled CSS. This is the copy-everything behavior from earlier, since the Sass source lives in app/assets/stylesheets. As far as we could tell it’s harmless, but it surprised us.
Running the CSS Build
Propshaft only serves files that already exist in its asset directories, and app/assets/builds is gitignored in our application, so the compiled CSS only exists where something builds it. In practice that means every place that used to depend on Sprockets compiling on demand now needs a build step. We touched three of them:
# Procfile.dev
css: npm run build:css -- --watch
# Dockerfile
RUN npm run build
RUN npm run build:css
# GitHub Actions setup action
- name: Build CSS assets
run: npm run build:css
In development, the Procfile.dev entry runs Sass in watch mode next to the Rails server and the esbuild watcher, so a change in a .scss file is rebuilt, and Propshaft checks for updated assets before each request. Without the watcher, you would be looking at whatever CSS was built last. In the Dockerfile, the new line sits right after the existing JavaScript build. In CI, we added the CSS build to the shared setup action, and we added the JavaScript build there too, so the test environment has both bundles before the tests start. We needed this step because our CI never runs assets:precompile, so the css:build hook from earlier never fires there. cssbundling-rails also hooks css:build into test:prepare, spec:prepare and db:test:prepare, so if your CI runs one of those, your CSS may already be built. If you want to see how the Docker and Procfile changes fit into a full development workflow, we described them in more detail in our article about migrating from Sprockets to esbuild .
Conclusion
In this article, we talked about replacing Sprockets with Propshaft and cssbundling-rails. We added the gem and the sass package, converted a *= require manifest into a Sass entry point, removed sass-rails and the Sprockets configuration files, and made sure the CSS gets built in development, Docker and CI. We also looked at what stopped working along the way: a missing script and an ERB stylesheet. A stale public/assets directory hid the ERB stylesheet error until we deleted it.
Propshaft does a lot less than Sprockets, so what breaks in your application depends on what Sprockets was doing for you. Before you start, search for *= require directives, stylesheets that end in .erb, and views that ask for assets that may not exist anymore. It’s also worth noting that everything we reproduced for this article ran in development mode, so check your production settings, such as a leftover config.assets.compile, before you deploy.
Has your team been struggling to move off Sprockets as part of an upgrade? We can help.