With Propshaft in Rails 8.1, how is a digested name like application-1a2b3c4d.css computed, and how do asset helpers find it through .manifest.json?
answer
- SHA1 of content, first 8 hex
- referenced assets feed the digest
- config.assets.version changes every digest
- manifest present means static resolver
- Propshaft::MissingAssetError on a miss
basics
~20 sPropshaft hashes the file's content, the content of assets it references and config.assets.version with SHA1, keeps 8 hex characters and inserts them before the extension. Helpers translate logical paths through public/assets/.manifest.json when it exists, or compute digests live from the load path otherwise.
solid answer
~40 sThe digest is SHA1 over the file's bytes plus the bytes of every asset its CSS `url()` references point at (recursively), plus `config.assets.version`; the first 8 hex characters go before the extension: `application-1a2b3c4d.css`. Files already named `-<digest>.digested.js` are left alone. `bin/rails assets:precompile` writes `public/assets/.manifest.json`, keyed by logical path, each entry holding `digested_path` and an optional `integrity`. At runtime Propshaft picks a **static resolver** that reads that file if it exists, and a **dynamic resolver** that computes digests from the load path if not. `asset_path`, `stylesheet_link_tag` and friends go through `compute_asset_path`, which raises `Propshaft::MissingAssetError` when the logical path is unknown. Bumping `config.assets.version` (the generated initializer sets `"1.0"`) changes every digest at once.
code
json · 10 lines{
"application.css": {
"digested_path": "application-1a2b3c4d.css",
"integrity": null
},
"bakery/rye.jpg": {
"digested_path": "bakery/rye-5e0c9a12.jpg",
"integrity": null
}
}go deeper
Recall that the name carries a content hash and that public/assets/.manifest.json maps logical names to digested ones.
Explain the digest inputs, including referenced assets and config.assets.version, and the static versus dynamic resolver switch on the manifest.
Diagnose MissingAssetError, stale manifests and leading-slash paths that skip digesting, and know how SRI hashes ride along in the manifest.
Weigh content digests against a global version bump: per-file invalidation keeps caches warm across deploys, a version bump is a blunt reset.
## Why a digest at all A **digest** (or fingerprint) is a short hash embedded in an asset's filename. Because the name changes whenever the content changes, a browser or CDN can cache the file for as long as it likes and still never serve a stale copy after a deploy. The caching theory belongs elsewhere; the Rails question is how **Propshaft** computes the name and how a view finds it. ## How Propshaft computes the digest `Propshaft::Asset#digest` builds a string and hashes it: 1. Take the file's raw bytes. 2. Append the raw bytes of every asset this file **references** through a compiler — for CSS, each `url()` that resolves to a file on the load path, followed recursively. 3. Append `config.assets.version`. 4. SHA1 the result and keep the **first 8 hex characters**. The digest is inserted before the extension, so `bakery/rye.jpg` becomes `bakery/rye-5e0c9a12.jpg` and a source map `app.js.map` becomes `app-<digest>.js.map`. Two consequences interviewers like: - **Changing a referenced image changes the stylesheet's name too.** If `menu.css` points at `rye.jpg`, replacing the photo changes `menu.css`'s digest, so a cached stylesheet never points at an old image path. - **`config.assets.version` is a global cache-buster.** Propshaft's default is `"1"`; the `config/initializers/assets.rb` a new app gets sets `"1.0"`. Changing it renames every asset on the next precompile. Files whose names already end in `-<digest>.digested.<ext>` (for example a bundler's chunk and its source map that refer to each other) are treated as pre-digested and keep their names. ## The manifest file `bin/rails assets:precompile` first writes `public/assets/.manifest.json` (the path is `config.assets.manifest_path`, defaulting to `.manifest.json` inside `config.assets.output_path`), then copies every asset whose digested file is not already there. In Propshaft 1.3 each entry is an object: - the key is the **logical path** (`application.css`) - `digested_path` is the stamped name (`application-1a2b3c4d.css`) - `integrity` is a Subresource Integrity hash, or `null` unless `config.assets.integrity_hash_algorithm` is set Propshaft still reads the older shape where each value was a bare string. The Rails asset pipeline guide prints that older shape and a longer digest in its example; the pinned Propshaft source writes the object form and 8-character digests. ## How helpers resolve a logical path `Propshaft::Assembly#resolver` chooses once per process: | Condition | Resolver | Behaviour | |---|---|---| | `.manifest.json` exists | `Propshaft::Resolver::Static` | looks the logical path up in the manifest; no disk scan | | no manifest | `Propshaft::Resolver::Dynamic` | finds the file on the load path and computes its digest | Propshaft's helper overrides `compute_asset_path`, so `asset_path("bakery/rye.jpg")` returns `/assets/bakery/rye-5e0c9a12.jpg` (the prefix is `config.assets.prefix`, `/assets` by default). When neither resolver knows the path it raises **`Propshaft::MissingAssetError`** — "The asset 'x' was not found in the load path." — instead of emitting a bare URL that 404s later. One escape hatch: Action View's `asset_path` returns a source that starts with `/` untouched, so `asset_path("/rye.jpg")` is never digested. ## Development versus production - **Development:** no manifest, so the dynamic resolver hashes files on demand, and the dev server (`Propshaft::Server`) answers `/assets/...` requests. It returns 404 if the digest in the URL no longer matches the file's current digest. - **Production:** the manifest exists after precompile, so lookups are a hash read and the files themselves are served from `public/assets`. ## Integrity hashes Setting `config.assets.integrity_hash_algorithm` to `"sha256"`, `"sha384"` or `"sha512"` makes precompile store an SRI hash per entry, and `stylesheet_link_tag "application", integrity: true` then adds the `integrity` attribute. Propshaft omits it unless the request is SSL or local. Any other algorithm name raises when the hash is computed. ## Diagnosing a broken reference When an interviewer hands you "the stylesheet 404s after deploy" or "the page raises while rendering", walk the chain from logical path to file: 1. **Is the logical path right?** `bin/rails assets:reveal` lists every logical path; the folder `app/assets/images` is a root, so its files have no `images/` prefix. 2. **Is it in the manifest?** Open `public/assets/.manifest.json` in the built image and look for the key. If the build ran before a generated file existed — a CSS bundle in `app/assets/builds`, for example — the key is missing and the helper raises `Propshaft::MissingAssetError`. 3. **Does the digested file exist?** The manifest can list a name whose file was never copied if `public/assets` came from a different build than the manifest. 4. **Did the helper digest at all?** A source passed with a leading slash, such as `asset_path("/logo.svg")`, bypasses the resolver and points at an undigested path that only exists if the file sits in `public/`. Each step is a single lookup, which is why Propshaft's static resolver keeps production asset helpers cheap: no file system scan and no hashing at request time.
- How do you get Subresource Integrity attributes on Propshaft-served stylesheets?Set `config.assets.integrity_hash_algorithm` to `"sha256"`, `"sha384"` or `"sha512"`, then pass `integrity: true` to `stylesheet_link_tag` or `javascript_include_tag`. Precompile stores the hash in `.manifest.json`; the helper adds the attribute only when the request is SSL or local, and drops the option otherwise.
- Why does replacing only a background image change the digest of the stylesheet that uses it?`Propshaft::Asset#digest` hashes the file plus the content of every asset its compilers say it references — for CSS, each resolvable `url()`, recursively. A new image changes that input, so the stylesheet gets a new name and no cached copy keeps pointing at the old image.
A library catalogue card: you ask for a book by title (the logical path) and the card gives today's shelf code (the digested path). Re-shelve a revised edition and the code changes while the title you ask for stays the same; ask for a title the catalogue lacks and the librarian says so instead of sending you to an empty shelf.
- The digest is computed from the deploy timestamp or the git commit, not the content
- Propshaft appends the digest as a ?v= query string instead of renaming the file
- A missing asset in asset_path quietly returns an undigested /assets URL under Propshaft
- config.assets.version only labels the build and never changes digested filenames
- Helpers rescan the load path on every request in production