Cache Busting Techniques
You'll learn how to force browsers and CDNs to fetch fresh versions of your updated files instead of serving stale cached copies.
The Problem with Caching
When you set long Cache-Control headers (like max-age=31536000) for immutable assets, browsers happily cache them for a year. That's great for performance—until you update your styles.css file. The browser doesn't know you changed it and keeps serving the old version from cache.
Cache busting solves this: you change the filename or URL whenever the content changes, forcing the browser to treat it as a completely new resource.
Three Core Techniques
1. Query Strings (Versioning)
Append a version number or timestamp to the URL:
<link rel="stylesheet" href="styles.css?v=2.1.4">
<script src="app.js?v=20240115"></script>
When you update the file, change the query string. Simple but has reliability issues—some proxies ignore query strings when caching.
2. Content Hashes (Fingerprinting)
Generate a hash based on the file's actual content and embed it in the filename:
<link rel="stylesheet" href="styles.a3f5c8b2.css">
<script src="app.9d4e1f7a.js"></script>
Build tools (Webpack, Vite, Parcel) generate these automatically. Content changes = different hash = new filename = cache miss. Most reliable method.
3. Version-Based Filenames
Use semantic versioning in the filename itself:
<script src="app-v2.1.4.js"></script>
Simpler than hashing but requires manual updates with each release.
How It Works with Immutable Assets
Combine cache busting with the immutable directive:
Cache-Control: public, max-age=31536000, immutable
Your HTML references app.abc123.js. When you deploy updates, the HTML changes to app.xyz789.js. The browser sees a new URL and fetches the new file—the old one stays cached but is never requested again.
Key Takeaway: Cache busting changes the resource URL whenever content changes, ensuring browsers always fetch the latest version while maintaining aggressive caching for unchanged files.