Full answer
A long cache time is only safe if the URL changes when the content changes. Hashed file names make that happen.
- The build hashes the content of each output file and puts the hash into the name:
app.3f9a1c2b.js. - The HTML references the hashed name.
- Every new release produces new names only for files that changed. Unchanged files keep the same URL and stay in the browser cache, so a small change costs a small download.
The headers follow from this.
- Hashed files:
Cache-Control: public, max-age=31536000, immutable. Theimmutablehint tells the browser not to revalidate even on reload. - The HTML document:
Cache-Control: no-cachewith an ETag. It must reach the user fresh, because it contains the names. If the HTML were cached for a day, users would keep requesting old file names, and a deleted old file would break the page. - Files without a hash in the name, such as
robots.txtor a stable image URL: use a short time or revalidation.
Things that go wrong:
- Keeping only the newest files on the server after a deploy. A user who has the old HTML open will request an old script and get a 404. Keep the previous release files for a while.
- A hash that changes without a content change, for example because of a build timestamp. Every deploy then invalidates all caches.
- Hashing the file name but also forgetting to hash files loaded by code, such as lazy chunks. Bundlers do this for chunks, but custom loaders need care.
The script below computes short hashes for two files, rewrites the references in the HTML, then changes one file and shows that only its name changes.
javascript
import { createHash } from 'node:crypto';
function build(files) {
let html = '<link rel="stylesheet" href="style.css"><script src="app.js"></script>';
for (const [name, content] of Object.entries(files)) {
const hash = createHash('sha256').update(content).digest('hex').slice(0, 8);
const hashed = name.replace(/(\.\w+)$/, `.${hash}$1`);
html = html.replaceAll(name, hashed);
}
return html;
}
const v1 = build({ 'app.js': 'console.log("v1")', 'style.css': 'body{margin:0}' });
const v2 = build({ 'app.js': 'console.log("v2")', 'style.css': 'body{margin:0}' });
console.log(v1);
console.log(v2);Run with Node 24.22.1. The output matched the text in the answer in two runs. Run with Node and the output was identical in two runs. Only the naming step was run. No bundler, server or CDN was used.
Key points
- The hash of the content is part of the URL, so the URL changes with the content.
- Hashed files get max-age of one year plus immutable.
- The HTML gets no-cache so it always names the current files.
- Unchanged files keep their URL and stay cached.
- Keep old files for a while after a deploy.
Common mistakes
- Giving the HTML page a long max-age.
- Using a version query such as ?v=2 that is forgotten on release.
- Deleting old files at once, which breaks users with an open page.
- Hashes that change on every build because of embedded dates.
Follow-ups
- How does a long-lived cache interact with a CDN purge?
- What does immutable change on a reload?
- How do dynamic import chunks get hashed names?
Tips
- Describe the header for each file type, not only the idea of hashing.
- Mention the old-release overlap problem to show deployment experience.