Caching headers are HTTP response headers, mainly Cache-Control, that tell browsers and CDNs whether they may keep a copy of a file and for how long. Long lifetimes for versioned images, scripts and styles make repeat visits faster, while HTML pages usually need a short lifetime or revalidation so updates appear.
Why it matters
Without caching instructions, a browser may download the same logo and stylesheet on every page view. With them, it reuses the stored copy. That saves bandwidth and speeds up repeat visits. The cost is that a cached file can go stale, so lifetimes must match how often the file changes.
How to do it
The values you will meet
max-age=31536000keeps a file for a year (the number is seconds).publicallows shared caches such as CDNs to store it;privatelimits it to the visitor's own browser.immutablesays the file will never change at that address, so the browser need not check again.no-cachelets the browser store the file but forces it to check with the server before reuse. Despite the name, it does not mean "do not store".no-storetells caches not to keep the response at all, which suits sensitive pages.s-maxagesets a separate lifetime for shared caches such as CDNs.
Related validators, ETag and Last-Modified, let the browser ask "has this changed?" and receive a short "not modified" answer.
Steps
- Give files whose names change when their content changes (for example
app.3f9a1c.css) a long lifetime:
Cache-Control: public, max-age=31536000, immutable
- Give HTML pages a short lifetime or revalidation:
Cache-Control: no-cache
- For files with fixed names such as
logo.png, use a moderate lifetime, such as a day or a week, unless you can rename them on change. - Check the result:
curl -sI https://example.com/css/site.css | grep -i -E "cache-control|etag|last-modified"
- Confirm in Chrome DevTools: reload and see
(disk cache)in the Size column of the Network panel.
Common mistakes
- Giving a year-long lifetime to files with fixed names, so visitors keep old versions after you update them.
- Caching HTML for too long so new content takes ages to show.
- Using
no-cacheand expecting it to stop storage; useno-storefor that. - Caching logged-in or personal pages in a shared cache.
- Forgetting that a CDN can have its own cache rules, separate from browser headers.
Frequently asked questions
What is a good cache lifetime?
For fingerprinted static assets, a year is common. For HTML, use no-cache or a short value. In between, choose a lifetime that matches how often the file changes.
How do I update a cached file?
Change its file name or add a version to its address, then update the references. Visitors then fetch the new file immediately.
Does caching affect SEO?
Fast repeat visits help the experience. Stale caching of HTML can delay updates, so keep page lifetimes short.
Related guides
- Browser caching and Cache-Control
- CDN setup for SEO and performance
- Gzip and Brotli compression
- Improve your Lighthouse performance score
Crawvix's scan checks that static files have cache headers. To see yours, Try the free website sample.