HTTP Caching Headers Explained: Cache-Control, ETag, and Expires
A fast website is not only about a powerful server. Browsers, proxies, and content delivery networks can store copies of files and reuse them instead of downloading the same data for every visit. HTTP caching headers tell those systems what may be stored, for how long, and when a cached response must be checked again.
Used well, caching reduces bandwidth, improves page speed, and lowers server load. Used carelessly, it can leave visitors seeing outdated pages or expose personalized content to a shared cache.
Cache-Control Sets the Main Rules
Cache-Control is the primary header for modern HTTP caching. A response such as Cache-Control: public, max-age=3600 allows a cache to reuse the response for one hour without contacting the origin server. The public directive permits shared caches to store it, while private limits storage to a user’s browser.
The names of two directives often cause confusion. no-cache does not mean “never store this response.” It means the cached copy must be validated before reuse. no-store tells caches not to store the response at all and is more appropriate for highly sensitive data.
ETag and Last-Modified Enable Validation
When a cached response becomes stale, the browser does not always need the complete file again. An ETag identifies a particular version of a resource. The browser can send that value in an If-None-Match request, allowing the server to reply with 304 Not Modified when the file has not changed.
Last-Modified and If-Modified-Since provide a similar time-based mechanism. These validators save bandwidth because a 304 response has no representation body. If you want to understand the other responses that may appear during testing, see our guide to HTTP status codes for website owners.
Use Different Policies for HTML and Static Files
One caching policy rarely fits an entire website. Versioned CSS, JavaScript, fonts, and images can usually use a long lifetime because a changed filename or query version creates a new URL. A year-long max-age combined with immutable can work well for assets whose URLs change whenever their contents change.
HTML pages require more care because their URLs usually remain stable. A short lifetime or no-cache with validators helps visitors receive current content while still benefiting from conditional requests. Logged-in pages and personalized responses should normally be marked private to prevent shared caches from serving one user’s content to another.
Test the Headers Your Server Sends
Do not assume a plugin or server configuration produces the intended result. Inspect response headers for the homepage, an article, and several static assets. Our practical guide to testing a website with curl shows how to retrieve headers and check response behaviour from the command line.
After making changes, test in a private browser session and verify both the first response and a repeat request. Watch for unexpected Age headers, stale HTML, missing validators, or private pages marked public. Also confirm that deployment changes create new asset URLs when long-lived caching is enabled.
A Practical Caching Strategy
Start conservatively: cache versioned static assets for a long period, require validation for frequently updated HTML, and keep private responses out of shared caches. Then measure performance and adjust lifetimes based on how often each resource changes. Caching should be explicit, testable, and matched to the content rather than applied as one universal switch.
For detailed examples and the precise behaviour of the directives and validators described here, consult MDN’s official HTTP caching guide.