Updating cached assets with WhiteNoise

Static asset caching looks straightforward until you ship a CSS fix and visitors still see old styles from browser cache.

Appending manual query strings like style.css?v=2 breaks down because some proxies and CDNs skip caching URLs with query parameters.

Content-Hash Manifests

I use WhiteNoise's CompressedManifestStaticFilesStorage in Django to fingerprint static files at build time:

STORAGES = {
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}
WHITENOISE_MAX_AGE = 31_536_000  # 1 year in seconds

During collectstatic, WhiteNoise runs four steps:

  1. Computes an MD5 hash of each static file's contents.
  2. Writes a fingerprinted filename such as style.0bc7e7298d0c.css.
  3. Pre-compresses Gzip and Brotli copies alongside the file.
  4. Maps {% static %} tags to the fingerprinted URL.

One-Year Immutable Caching

Because a file edit produces a new filename, I send a one-year immutable cache header:

Cache-Control: public, max-age=31536000, immutable

Repeat visits load assets from disk cache, I need no external CDN rules, and a new deploy busts stale styles on the first request.

← Journal