Browser Cache
Browser cache is a copy of assets stored on the visitor's device. When the visitor returns, the browser can use that copy without downloading it again. Correct caching reduces requests, saves bandwidth, and speeds up return visits.
The headers involved
| Header | Purpose |
|---|---|
Cache-Control | Sets how long and by whom a response may be cached |
ETag | Gives the file version an identity for validation |
Last-Modified | Shows when the file last changed |
Expires | An older expiry header still used by some servers |
For static assets with a hash in the filename, `Cache
- Control: public, max-age=31536000, immutable
makes sense. For HTML, useno-cacheormax-age=0, must-revalidate` so the browser always checks for a new version. Do not cache HTML for a year, because visitors can end up with links to assets that no longer exist.
Cache busting
Every deploy should produce a new asset filename, such as app.8f3c2.js. The new name forces the browser to download the new version without clearing its cache. Modern frameworks add the hash automatically. If you use WordPress, an optimization plugin usually handles asset versioning.
Do not rely on a query string such as ?v=1 if a CDN or proxy ignores it. A hash in the filename is safer.
What to watch for
Do not mix public and private caching. Responses with user data must use `Cache
- Control: private
orno-store`. If a CDN stores a private response, another user could see the wrong data.
Test after deployment with Incognito and DevTools. Check whether the HTML document loads the new asset version. For assets, check for status 200 or 304. A 304 means the browser is using its cache and the server confirmed the file has not changed.
For server-side caching, see Web Cache. For asset delivery, see CDN. For images, see Image Optimization. For compression, see Gzip and Brotli.
stale-while-revalidate lets the browser serve an old copy while it fetches a new one in the background. That fits assets that do not need to change instantly, but it is a poor fit for prices or stock. With a CDN, make sure origin and CDN cache rules do not conflict. After deployment, test with curl -I and check whether cache-control and age match your intent.
For HTML that changes often, no-cache is safer than a long max-age. If unsure, start with max-age=0, must-revalidate and increase it once you are confident. Test in staging and check whether the browser really loads the new assets after deployment.
FAQ
Does browser caching delay content updates?
It can, if HTML is cached for too long. Use no-cache for HTML and hashed asset names so updates appear immediately.
What is the difference between max-age and s-maxage?
max-age applies to browsers and shared caches. s-maxage applies only to shared caches such as a CDN, and overrides max-age at that layer.
Does 304 mean the file is missing?
No. 304 means the file has not changed since the version the browser holds. The browser uses its local copy.
Disclaimer: Hosting Wiki articles are prepared for educational and reference purposes. Hosting technology keeps evolving, so some technical details may change over time.