Cache Busting Explained: Hashed Filenames and Long Caching
Cache busting puts a content hash in asset filenames so browsers can cache files for a year yet still fetch new versions the moment you deploy.
Cache busting is the practice of changing an asset’s URL whenever its contents change, so browsers and CDNs can cache it aggressively without ever serving a stale version. The standard technique is to put a hash of the file’s contents into its filename, such as app.3f9a2c1b.js. Because the URL is tied to the bytes, the file can be cached for a year with immutable, and a deploy that changes the code simply references a new URL.
The problem cache busting solves
Caching is one of the cheapest performance wins on the web. If a returning visitor already has your JavaScript and CSS, the page can render without downloading them again. The catch is invalidation: once a browser has cached app.js with a long lifetime, it will keep using that copy even after you deploy a new one.
That leaves two bad options without cache busting:
- Short cache lifetimes, so updates propagate quickly but returning visitors re-download or revalidate assets constantly.
- Long cache lifetimes, so the site is fast but users run old code for days, sometimes mixed with new HTML that expects a different version.
Cache busting removes the trade-off. Assets get long lifetimes, and the HTML, which is cached briefly or not at all, points to whichever versions are current.
How content hashing works
At build time, the bundler computes a hash of each output file’s contents and embeds a short slice of it in the filename:
dist/assets/index.a1b2c3d4.js
dist/assets/index.9e8f7a6b.css
dist/assets/logo.5c4d3e2f.svg
It then rewrites every reference, in the HTML, in CSS url() calls, and in JavaScript imports, to use the hashed names. Change one line in a module and its hash changes, so its filename changes, so every file that imports it also gets new contents and a new hash. Files you did not touch keep their old names, which means users keep their cached copies.
Modern JavaScript bundlers do this by default for production builds. With code splitting, the benefit compounds: a change to one route’s chunk only invalidates that chunk, not the whole application.
The headers that go with it
Hashed filenames only pay off when paired with the right caching headers. A typical setup looks like this:
| Resource | Example URL | Cache-Control |
|---|---|---|
| Hashed assets | /assets/app.a1b2c3d4.js | public, max-age=31536000, immutable |
| HTML documents | /, /blog/post/ | no-cache or a short max-age |
| Unhashed public files | /robots.txt, /favicon.ico | Short max-age |
max-age=31536000 is one year in seconds. The immutable directive tells the browser the file will never change at this URL, so it should not even revalidate on a reload. no-cache on HTML does not mean “do not store”; it means “check with the server before reusing,” which is what you want for the document that decides which asset versions to load. Our guide to HTTP caching headers explains the directives in more depth.
Query strings vs filename hashes
An older approach is appending a version to the URL: app.js?v=42. It works in browsers, but it has drawbacks:
- Some proxies and CDNs have historically been configured to ignore or strip query strings when caching, which can defeat the scheme.
- A manually bumped version number changes for every file at once, so a one-line fix invalidates everything.
- Forgetting to bump the version reintroduces the stale-cache problem.
Content hashes in the filename avoid all three. The version is derived automatically from the bytes, and only changed files get new URLs.
Why hash the content instead of using a timestamp
A build timestamp or commit SHA in the filename changes on every deploy, even for files whose contents are identical. That throws away users’ cached copies for no reason. Content hashes are deterministic: the same input produces the same name across builds and machines. That also makes builds reproducible and lets a CDN keep serving files it already has at the edge.
Common pitfalls
Caching the HTML too long. If the document itself is cached for hours, users will not see the new asset references until it expires. Keep HTML short-lived or revalidated. Patterns like stale-while-revalidate can help where a slightly stale document is acceptable.
Deleting old assets on deploy. A user who loaded the old HTML a minute before your deploy may still request app.a1b2c3d4.js when they navigate or lazily load a chunk. If that file is gone, they get a 404 and a broken page. Keep previous builds’ assets available for a while after each release.
Unstable hashes. If a bundler embeds absolute paths, timestamps, or nondeterministic module IDs into output, hashes change even when the source did not. That silently erodes caching across deploys. Most modern tools handle this well, but it is worth checking when cache hit rates look worse than expected.
Forgetting non-bundled assets. Images and fonts referenced from places the bundler does not process, such as content files or a CMS, will not be hashed automatically. Either route them through the build or give them shorter lifetimes.
Service worker caches. A service worker has its own cache that sits in front of the HTTP cache. If it serves an old HTML shell, hashed filenames cannot help. Make sure your service worker updates its precache list on each deploy.
A note on import maps
Hashing creates a cascade: changing a deeply imported module changes its importers’ contents too, and so on up the tree. Import maps can break that chain by letting modules import a stable bare name that the map resolves to a hashed URL. Only the map changes when one module updates, which keeps more of the cache intact. It is an optimization most sites do not need, but it is useful for large applications that deploy often.
The takeaway
Cache busting ties each asset’s URL to its contents with a hash in the filename. That lets you cache JavaScript, CSS, and images for a year with immutable while keeping HTML short-lived, so every deploy reaches users immediately and unchanged files stay cached. Keep old assets around after a release, make sure hashes are deterministic, and let your bundler do the rewriting.
Keep reading
Takina · · 5 min read Largest Contentful Paint (LCP) Explained and How to Fix It
Largest Contentful Paint measures when the biggest visible element renders. What counts, the four phases of LCP, and practical ways to make it faster.
Takina · · 4 min read What Is Minification? Shrinking JavaScript, CSS, and HTML
Minification strips whitespace, comments, and long names from JavaScript, CSS, and HTML without changing behavior. How it works and pairs with compression.
Takina · · 5 min read Cumulative Layout Shift (CLS) Explained and How to Fix It
Cumulative Layout Shift measures how much visible content unexpectedly moves while a page loads. How CLS is scored, what causes it, and how to fix it.