EasyCaching Strategies📖 Theory Question

What is content-based cache busting and how does it work?

💡

Hint

Hash file contents and embed the hash in the filename — hash changes when content changes, so old URLs stay cached forever

Full Answer

Content-based cache busting includes a fingerprint of the file's content in its URL (filename or query param). The URL changes only when the file changes, allowing infinite cache lifetimes for unchanged files.

// Webpack/Vite output
main.a1b2c3d4.js  // hash changes when source changes
vendor.e5f6a7b8.js

// Cache-Control for hashed files
Cache-Control: public, max-age=31536000, immutable

How the browser update flow works:

  1. User's browser has main.a1b2c3d4.js cached for 1 year.
  2. You deploy a code change — the new file is main.x9y0z1w2.js.
  3. The updated HTML references the new filename — browser fetches it fresh.
  4. Old main.a1b2c3d4.js is simply never requested again (it expires naturally).

Key insight: The HTML file itself should have a short/no cache time (or use no-cache) because it's the entry point that references all hashed assets. If the HTML is stale, users get the old filenames.

More Caching Strategies Questions

EasyWhat is the Cache-Control header and what are its key directives?EasyWhat is stale-while-revalidate and why is it a good default for HTML pages?EasyWhat caching strategies can a service worker implement?EasyWhat is ETags and conditional requests, and when does the browser use them?

Practice this in a timed sprint →

5 free questions, no signup required

⚡ Start Sprint