Caching Static Assets With Cache-Control immutable
See how Cache-Control immutable prevents revalidation for content-hashed assets. Set safe, path-scoped cache headers on Express, nginx, Netlify, and Vercel.
Cache-Control: immutable tells the browser that the response at a URL will never change, so it should not revalidate the cached copy while that copy is fresh, even when the user reloads the page.
You set a one-year max-age on your bundles, Lighthouse stopped complaining, and the Network panel still shows a column of 304s for every JS and CSS file each time someone hits reload. Each of those 304s means the browser asked the server whether a file had changed when the file could not have changed.
This article covers what immutable adds to max-age, why it is only safe on content-hashed URLs, how to scope it by path on Express, nginx, Netlify and Vercel, and which browsers honor it. For max-age, ETags and cache busting in general, start with caching basics for web developers.
Key Takeaways
- The standard header for content-hashed static assets is
Cache-Control: public, max-age=31536000, immutable, where 31536000 seconds is exactly 365 days. immutablestops revalidation only while the response is fresh. It does not extend caching pastmax-age.- Apply
immutableto the directory that holds your hashed build output, such as/assets/, not by file extension. Never apply it toindex.htmlor fixed-name files. - Firefox (since version 49) and Safari honor
immutable. Chrome does not implement it, but since 2017 its normal reload revalidates only the main document, which has a similar effect.
What Does Cache-Control immutable Do?
The immutable directive is a promise from the origin that the representation at this URL will not change during its freshness lifetime. RFC 8246 defines it, and a client that honors it skips the conditional request it would normally send on reload.
There are three things the directive does not do:
- It does not lengthen caching. RFC 8246 §2 limits the directive to fresh responses. Once
max-ageruns out, the browser revalidates the file exactly as it would withoutimmutable. - It does not block a forced reload. RFC 8246 §3 expects clients to ignore
immutablewhen the user explicitly forces a reload and to refetch the file. MDN’s HTTP caching guide describes how normal and forced reloads differ. - It does not make unhashed URLs safe. It makes them dangerous, as later sections show.
The same section of the RFC also says clients should ignore immutable on responses that are not part of an authenticated context such as HTTPS.
The Standard Header
The standard Cache-Control header for content-hashed static assets is:
Cache-Control: public, max-age=31536000, immutable
public allows shared caches to store the response. max-age=31536000 is one year (60 × 60 × 24 × 365). immutable removes revalidation within that year. The directive only has an effect alongside a positive max-age, because a response that is never fresh has no window in which to skip revalidation.
What Happens Without immutable?
Without immutable, a browser that revalidates subresources on reload sends a conditional request for every cached file. The browser sends If-None-Match with the stored ETag, or If-Modified-Since with the stored date. The server replies 304 Not Modified, which means “use your copy.”
# Reload without immutable
GET /assets/app.3f9a2c.js
If-None-Match: "abc123"
→ 304 Not Modified
# Reload with Cache-Control: public, max-age=31536000, immutable
(no request; served from cache)
A 304 has no body, but it still costs a full network round trip. On a page with dozens of chunks, those round trips add up to the slow reload you see in the Network panel.
Why Does immutable Only Work With Hashed File Names?
immutable is only safe on URLs whose content can never change. In practice, that means file names with a content hash, such as app.3f9a2c.js. Any edit to the file produces a new hash, so the new content gets a new URL. The old URL keeps pointing at the old bytes forever.
Vite’s production build emits hashed JS, CSS and imported assets into an assets directory, set by build.assetsDir, by default. Files in Vite’s public/ folder are copied as-is with no hash. With webpack, hashing depends on your output template: the webpack Caching guide shows how to add [contenthash] to output.filename. Many framework presets configure this for you, but check your build output before assuming it.
One deploy-side rule follows from this. Keep the previous build’s hashed files available for a while after each deploy. A user whose tab loaded the old HTML will still request old chunk names when lazy routes load.
Never Use immutable on HTML or Fixed-Name Files
Never mark index.html or fixed-name files like logo.svg as immutable. Browsers that honor the directive will keep showing the cached copy without asking the server until max-age runs out, so users stay on the old version for up to a year.
| File type | Example | Header |
|---|---|---|
| Hashed JS/CSS | app.3f9a2c.js | public, max-age=31536000, immutable |
| Hashed fonts and images | inter.8b1e7d.woff2 | public, max-age=31536000, immutable |
| HTML entry point | index.html | no-cache |
| Fixed-name public files | logo.svg, favicon.ico | no-cache or a short max-age |
A stale HTML document causes a recognizable failure. The old HTML references chunks from a previous build. If those chunks were deleted, lazy routes fail with dynamic-import errors. A session replay paired with its network and console timeline shows the whole sequence (old HTML, 404 on a hashed chunk, broken route) along with the Cache-Control header that the HTML actually received.
Setting immutable on Your Server or Host
Apply the header to the directory that holds your hashed output, not by file extension. An extension rule such as “all .js and .png files” also matches unhashed files that do change.
Express
import express from 'express';
const app = express();
app.use('/assets', express.static('dist/assets', {
immutable: true,
maxAge: '365d',
}));
app.use(express.static('dist', {
setHeaders(res, path) {
if (path.endsWith('.html')) res.setHeader('Cache-Control', 'no-cache');
},
}));
The serve-static options take maxAge in milliseconds or as an ms-style string. The docs say immutable should be paired with maxAge to enable caching. Use '365d', not '1y': the ms package counts a year as 365.25 days, which produces max-age=31557600. Only the /assets mount gets immutable. The second mount serves index.html with no-cache.
nginx
location /assets/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
location = /index.html {
add_header Cache-Control "no-cache";
}
A location block that has its own add_header directives does not inherit any from the server level. If you set security headers in the server block, repeat them inside these locations.
Netlify
[[headers]]
for = "/assets/*"
[headers.values]
Cache-Control = "public, max-age=31536000, immutable"
Netlify reads [[headers]] blocks from netlify.toml. They apply to every deploy context.
Vercel
{
"headers": [
{
"source": "/assets/(.*)",
"headers": [
{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }
]
}
]
}
The headers array in vercel.json matches the source pattern against request paths.
To check the result on any host, request one asset and the HTML:
curl -sI https://example.com/assets/app.3f9a2c.js | grep -i cache-control
curl -sI https://example.com/ | grep -i cache-control
The first response should include immutable. The second must not.
Browser Support for Cache-Control immutable
Firefox has supported immutable since Firefox 49, and Safari has supported it since Safari 11 on desktop and iOS, according to MDN compatibility data. Chrome and current Chromium-based Edge do not implement the directive.
In Chrome, the practical outcome is similar anyway, because Chrome changed reload in 2017 so that a normal reload revalidates only the main resource. Under RFC 9111, caches must ignore Cache-Control directives they don’t recognize, so adding immutable carries no compatibility risk in browsers that don’t support it.
Conclusion
Use immutable only where it is true: on URLs whose bytes are fixed by a content hash, scoped to your build’s assets directory, and paired with no-cache on the HTML that points at them. Add the path-scoped rule for your server or host, run the two curl checks, and confirm the 304s are gone on reload in Firefox or Safari.
FAQs
How do I fix a file that was marked immutable by mistake?
Change its URL. Browsers that honor immutable keep serving the cached copy without contacting the server until max-age expires or the user forces a reload, so you cannot push a new version to the same URL. For a stuck HTML page, a Clear-Site-Data: 'cache' header on any response the old page still fetches from your origin asks the browser to clear its HTTP cache, though support varies by browser version.
Is it safe to use immutable with a version query string like app.js?v=2?
Yes, if the version value changes whenever the file content changes. HTTP caches key responses on the full request URI, so app.js?v=2 and app.js?v=3 are separate cache entries. A content hash in the query string is as safe as one in the file name. A hand-edited version number is not, and some CDN configurations ignore query strings in their cache key, so check yours.
Do I still need ETags or Last-Modified when I use immutable?
Yes. Immutable only suppresses revalidation while a response is fresh. Once max-age expires, or in a client that does not implement the directive and chooses to revalidate, the browser sends If-None-Match or If-Modified-Since, and those validators let the server answer 304 Not Modified instead of resending the whole file. Most static servers, including express.static and nginx, send them by default.
What is the difference between immutable and stale-while-revalidate?
Immutable skips revalidation while a response is fresh. Stale-while-revalidate, defined in RFC 5861, applies after freshness ends: the cache may serve the stale copy for a set number of seconds while it fetches a fresh one in the background. Use immutable on content-hashed URLs and stale-while-revalidate on fixed-name files that do change, such as logo.svg, for example max-age=3600, stale-while-revalidate=86400.