If you've built a Rails app, you have utilized an asset pipeline, even if you didn't realize it. Anytime you run
rails new <appname>, the
sprockets-rails gem is implemented by default. The asset pipeline has three main benefits:
- Concatenation of assets: This was more important before HTTP2 because you no longer pay a large penalty for sending many small files vs. one big file -- a single connection can multiplex all of the responses you need. Now that HTTP2 is the norm, this feature is outdated (more details on how this fits in to Propshaft are provided later.)
- Minification/compression: This process makes your asset files smaller. For CSS, whitespace and comments are removed.
- Ability to code assets via a higher level language: This allows you to use languages like Sass for CSS, CoffeeScript for JS, and ERB for both by default.
- Fingerprinting: This technique makes cache busting possible. Essentially, the name of the file is dependent on the contents, so when the file contents change, so does the filename. Thus, when content is updated, the fingerprint will change, and remote clients will need to request a new copy (i.e., cache busting).
From Sprockets to Propshaft
Why is a new solution needed? Well, I'm excited to say that the impetus for this change is the need for less complexity, not more. Propshaft offers benefits over Sprockets while also being much more straightforward and simpler. How is this possible? There are three reasons.
- HTTP2 is the norm: We discussed this earlier in the article, but to reiterate, with HTTP2, we no longer need to send one large file. In fact, bundling all of your JS into a single file loses many of its performance benefits.
- ES6 is standard on all browsers: Yes, even Internet Explorer 11! The importance of this is that we don't need a transpiling step to turn ES6 into something that'll run in the browser. It will just work. Yippee!
- Import maps in Rails 7: Import maps allow for logical references for modules in ES6 vs. explicit file references because explicit file references are a pain when it comes to long-life caches with digest stamped file names. For example, import maps allow bare important specifiers such as
import moment from "moment"to work. For additional details, I recommend reading the proposal or looking at the importmap-rails gem.
The main features of Propshaft are as follows:
- Load Paths: Propshaft will keep track of asset load paths from both the app and Rails gem plugins. Thus, there is no more guesswork when it comes to finding assets.
- Digest stamping: Assets defined in the load paths will be digested and copied into the
./publicdirectory. This will enable longer cache expiries.
- Dev server: Propshaft will run a development server inside of Rails, allowing developers to run
bin/rails serverwithout a Procfile.
- Compilers: Propshaft will allow for basic compilation steps, such as replacing
url(asset)strings in CSS files with
DHH released a blog post introducing Propshaft and stated: "At its core, Propshaft seeks to provide just the configurable load path where assets can live in app/assets, lib/assets, vendor/assets, and inside of gems, alongside the digest stamping and URL rewriting needed for far-future cache expiration. All this accessible through either static precompilation in production or a thin dynamic server in development". I recommend reading the full post if you're interested in learning more!
Migrating from Sprockets to Propshaft
Eventually, the goal is for Propshaft to replace Sprockets. However, if you want to check it out now, you can add the
-a propshaft flag when creating a new app in Rails. If you already have a Rails app that is using Sprockets, here are the steps to migrate to Propshaft.
Deprecate Sprockets: You will need to remove several gems. You can use the
bundle remove [gem]command:
bundle remove sprockets bundle remove sprockets-rails bundle remove sass-rails
assets/config/manifest.jsfiles from your project either manually via your code editor of choice or via the command line using
Install the Propshaft Gem:
bundle add propshaft
config.assets.paths << Rails.root.join('app', 'assets')line from your
Migrate asset helpers: Replace all instances of asset helpers (e.g.,
image_url) with standard URLs because Propshaft utilizes relative paths. You'll need to prepend the asset path with
/to link to the asset. For example,
If you run into any issues, you can refer to this file regarding upgrading in the Propshaft repo.
It's an exciting time for the evolution of asset pipelines, as it's always nice to lower the complexity will seeing increased performance. It may be awhile, though, before Propshaft is widespread, mostly due to many gems having been written to depend on Sprockets (DHH touches on this at the end of his post linked above). However, if you're creating a brand new Rails app, consider giving Propshaft a try.