skip to content

With Propshaft, why does url("croissant.jpg") in app/assets/stylesheets/menu/specials.css 404 when the image lives in app/assets/images, and how do you fix it?

level: middleimportance: should knowfreq 40%

answer

  1. relative to the CSS file's folder
  2. leading slash means load-path root
  3. no images/ prefix in logical paths
  4. unresolved url() left as written
  5. no image_url() in plain CSS

basics

~20 s

Propshaft resolves a url() without a leading slash relative to the stylesheet's own logical folder, so it looks for menu/croissant.jpg, fails, logs a warning and leaves the URL undigested. Write url("/croissant.jpg"): a leading slash means the load-path root.

solid answer

~40 s

Propshaft's CSS compiler rewrites every `url()` it can resolve to a digested path. A bare name is resolved **relative to the stylesheet's logical directory**, here `menu/`, so it looks for `menu/croissant.jpg`. Nothing has that logical path, so it logs `Unable to resolve 'croissant.jpg' for missing asset` and leaves the `url()` as written, and the browser asks for `/assets/menu/croissant.jpg`, which does not exist. A **leading `/`** means "from a load-path root", and because every `app/assets` subfolder is its own root, the image's logical path is `croissant.jpg`, so `url("/croissant.jpg")` works; `url("../croissant.jpg")` does too. `url("../images/croissant.jpg")` does not, because `images/` is not part of the logical path. Sprockets-era `image_url('croissant.jpg')` is not CSS and Propshaft never expands it.

code

javascript · 9 lines
javascript
// app/javascript/controllers/specials_controller.js
import { Controller } from "@hotwired/stimulus"

export default class extends Controller {
  connect() {
    this.element.style.backgroundImage =
      `url(${RAILS_ASSET_URL("/croissant.jpg")})`
  }
}

go deeper

for a junior

Remember that a leading slash in a Propshaft url() means the asset root, and a bare name means next to the stylesheet.

for a middle

Explain logical paths per load-path root, the four resolution rules, and why an unresolved url() is left as written with only a log warning.

for a senior

Show how you would catch these before production: watch for Unable to resolve warnings, use assets:reveal, and replace Sprockets helpers during a migration.

for a principal

Explain the trade-off Propshaft made: relative resolution lets vendored CSS work unmodified at the price of a migration step for Sprockets-era stylesheets.

## The scenario A bakery's site keeps its styles in `app/assets/stylesheets/menu/specials.css`, its photos in `app/assets/images/` and its web fonts in `app/assets/fonts/`. The stylesheet sets a background with `url("croissant.jpg")` and declares a font with `@font-face`. In production the background 404s, and the browser's network tab shows a request for `/assets/menu/croissant.jpg`. ## How Propshaft rewrites CSS url() **Propshaft** runs a small compiler, `Propshaft::Compiler::CssAssetUrls`, over every `text/css` asset, both in the development server and during `bin/rails assets:precompile`. For each `url(...)` it: 1. **Skips** values that are not local files: `data:` URIs, `http:` and `https:` URLs, protocol-relative `//host/...` URLs and fragment-only references starting with `#`. 2. **Resolves** the path to a logical path: - starting with `/` → strip the slash and look it up from the load-path roots; - starting with `../` → join it to the stylesheet's logical directory and normalise; - anything else (including `./`) → treat it as relative to the stylesheet's logical directory. 3. **Rewrites** a found asset to `url("/assets/<digested path>")`, keeping any `?query` or `#fragment` suffix, so a font's `#iefix` survives. 4. **Leaves** an unresolved reference exactly as written and logs `Unable to resolve '<url>' for missing asset '<resolved>' in <stylesheet>`. Step 4 is why the failure is quiet: the page renders, the stylesheet loads, and only the image request fails. ## Why the image's logical path has no images/ in it Rails declares `app/assets`, `lib/assets` and `vendor/assets` with `glob: "*"`, and Propshaft adds **each subdirectory** as a separate load-path root. So: | File on disk | Logical path | |---|---| | `app/assets/stylesheets/menu/specials.css` | `menu/specials.css` | | `app/assets/images/croissant.jpg` | `croissant.jpg` | | `app/assets/fonts/brioche/brioche.woff2` | `brioche/brioche.woff2` | From `menu/specials.css`, the candidates resolve like this: - `url("croissant.jpg")` → `menu/croissant.jpg` → not found, left alone, 404. - `url("/croissant.jpg")` → `croissant.jpg` → found, rewritten to `/assets/croissant-<digest>.jpg`. - `url("../croissant.jpg")` → `croissant.jpg` → found. - `url("../images/croissant.jpg")` (thinking in folders on disk) → `images/croissant.jpg` → not found. The relative rule exists so third-party CSS such as an icon font's stylesheet, which refers to its fonts next to itself, works when dropped into `vendor/assets` unchanged. ## The fix for the bakery ```css /* app/assets/stylesheets/menu/specials.css */ @font-face { font-family: "Brioche"; src: url("/brioche/brioche.woff2") format("woff2"); } .specials { background-image: url("/croissant.jpg"); } ``` Prefer the leading-slash form for app assets: it does not change when the stylesheet moves to another folder. ## What does not work any more - **Sprockets-era asset helpers in CSS.** `image_url('croissant.jpg')` and `font_url(...)` were Sass helpers supplied through the Sprockets stack. Propshaft does not process Sass and has no such functions; in a plain `.css` file the browser sees an unknown function. Action View's `image_url` still exists, but only as a Ruby helper in views. - **ERB in stylesheets.** A `.css.erb` file is not evaluated, so `<%= asset_path(...) %>` cannot be used to build the URL. - **JavaScript strings.** The CSS compiler ignores `.js` files. In JavaScript you opt in with the `RAILS_ASSET_URL("/croissant.jpg")` pseudo-call, which Propshaft replaces with the quoted digested path. ## Checking it - Watch the log for `Unable to resolve` lines while loading the page in development; each one is a `url()` that will 404 in production. - Run `bin/rails assets:reveal` to see the exact logical paths you can reference. - Remember the digest of `specials.css` includes the bytes of `croissant.jpg` and the font, so replacing either renames the stylesheet as well. ## Why the rule is relative at all Sprockets resolved helper calls such as `image_url('croissant.jpg')` from the asset roots, so a stylesheet could live anywhere and still name an image by its bare filename. Propshaft chose the browser's own rule instead: a relative URL in a stylesheet is relative to the stylesheet. That is what packaged CSS assumes. An icon-font kit that ships `css/icons.css` beside `fonts/icons.woff2` and writes `url("../fonts/icons.woff2")` works the moment its folder is placed under `vendor/assets`, with no edits, because the `../` is resolved against the stylesheet's own logical directory. The cost falls on stylesheets written for Sprockets, which assumed root-relative names. A migration therefore means one pass over every `url()`: 1. Bare names in stylesheets at the top of `app/assets/stylesheets` keep working, because their logical directory is the root. 2. Bare names in stylesheets inside subfolders such as `menu/` break and need a leading `/`. 3. Paths that include a disk folder like `images/` or `fonts/` break everywhere, because those folders are roots, not path segments.

  • Which url() values does Propshaft's CSS compiler leave alone on purpose?
    Anything that is not a local asset: `data:` URIs, `http:` and `https:` URLs, protocol-relative `//host/...` references and fragment-only `#...` references. Everything else is resolved; a `?query` or `#fragment` suffix on a resolved asset is kept after the digested path.
  • How do you reference a digested image from a JavaScript file served by Propshaft?
    Wrap the path in the `RAILS_ASSET_URL("/croissant.jpg")` pseudo-call. Propshaft's JS compiler replaces it with a quoted string such as `"/assets/croissant-5e0c9a12.jpg"`. Plain string literals are never rewritten, because the compiler only looks for that call.

  • url() paths in Propshaft CSS resolve from the app/assets folder on disk
  • You must write url("/images/croissant.jpg") because the file sits in app/assets/images
  • Use image_url('croissant.jpg') in the .css file and Propshaft expands it
  • An unresolvable url() makes bin/rails assets:precompile fail
  • Propshaft rewrites any "/croissant.jpg" string literal inside JavaScript files