Cache-Control headers determine how browsers, CDNs, and other shared caches store and reuse your responses. This practical guide explains which directives to use for HTML, images, CSS, JavaScript, and APIs, then provides a reusable checklist for TTLs, ETags, revalidation, deployment changes, and troubleshooting stale content.
Overview
HTTP caching works by allowing a client or intermediary to reuse a response instead of requesting it from the origin every time. The result can be lower bandwidth use, fewer origin requests, and faster page delivery. However, an aggressive browser cache TTL is only helpful when you have a reliable way to change URLs or purge shared caches when content changes.
The central header is Cache-Control. Its directives describe who may cache a response, how long it may be considered fresh, and what should happen when it becomes stale. Browser caching and CDN caching overlap, but they are not identical. A browser usually follows the response it receives for one user, while a CDN may serve one cached response to many users. Use s-maxage and CDN-specific controls when you need to separate those policies.
| Directive | Practical meaning | Typical use |
|---|---|---|
max-age=seconds | Allows a browser or cache to treat the response as fresh for the stated period. | Static assets, HTML with a controlled deployment process, or short-lived API data. |
s-maxage=seconds | Sets freshness for shared caches such as CDNs; it can take precedence over max-age there. | Public CDN caching with a different policy from browser caching. |
public | Signals that a response may be stored by shared caches, subject to other rules. | Public pages and versioned static assets when no user-specific data is present. |
private | Restricts storage to a private cache, such as the user’s browser. | Responses containing user-specific information that still benefits from browser caching. |
no-cache | Permits storage, but requires validation before reuse. | Content that may be cached but must be checked for changes. |
no-store | Requests that the response not be stored. | Highly sensitive or unsuitable-to-cache responses. |
immutable | Indicates that a fresh response will not change during its freshness lifetime. | Versioned files whose URL changes whenever their content changes. |
stale-while-revalidate | Allows a cache to serve a stale response temporarily while it checks for an updated one. | Public content where a brief stale window is preferable to a blocking request. |
The distinction between no-cache and no-store is especially important. no-cache does not mean “do not cache”; it means “validate before reuse.” Validation commonly uses an ETag or Last-Modified response header. If the content has not changed, the server can return 304 Not Modified without sending the full representation again.
Checklist by scenario
HTML pages
Start with a short browser TTL for frequently changing HTML, or require validation:
Cache-Control: public, max-age=0, must-revalidateThis permits caching while encouraging a freshness check. For a CDN, you may set an explicit shared-cache policy:
Cache-Control: public, max-age=0, s-maxage=300, must-revalidateUse a longer s-maxage only when the page is genuinely public and you have a purge or deployment strategy. Do not cache account pages, checkout pages, cart responses, or pages whose output depends on cookies or authentication unless the cache key and privacy rules are deliberately designed. For more on this boundary, see caching dynamic websites and cache-key design.
Images, fonts, CSS, and JavaScript
For fingerprinted or versioned assets such as app.8f31c.js, a long browser TTL is usually appropriate:
Cache-Control: public, max-age=31536000, immutableThis pattern depends on a strict rule: changing the file must also change its URL. If your deployment overwrites the same filename, use a shorter TTL or remove immutable; otherwise users can retain an old file until the TTL expires.
For assets without versioned URLs, choose a shorter TTL and plan invalidation. A CDN purge can help shared caches, but it cannot instantly remove a file already stored in every visitor’s browser. A naming strategy that produces new URLs is often more predictable than relying on repeated purges.
Public APIs
For a public endpoint whose data can tolerate a brief delay, define a clear freshness period:
Cache-Control: public, max-age=30, s-maxage=60, stale-while-revalidate=30The exact values should reflect the data’s business and operational requirements. If the response varies by language, device, authorization state, or another request property, ensure the cache key accounts for that variation. The Vary response header may be necessary, but adding it without understanding the cache key can reduce cache efficiency or leave an incomplete policy.
Authenticated and sensitive responses
Responses containing private account information should normally use a private policy, for example:
Cache-Control: private, no-cacheThat permits a browser to retain the response while requiring validation. For information that should not be stored by a browser or intermediary, use:
Cache-Control: no-storeConfirm that your application, reverse proxy, hosting platform, and CDN all honor the intended policy. A cache plugin, reverse proxy, or edge rule can change behavior if it is configured independently from the application.
Revalidation with ETags and Last-Modified
An ETag identifies a particular representation, often with a generated token. A Last-Modified header communicates a modification time. On a later request, the client can send If-None-Match or If-Modified-Since. The server returns the full response if the representation changed, or 304 Not Modified if it did not.
Use one or both mechanisms consistently. ETags can behave unexpectedly when different origin servers generate different values for the same content, so check your load-balancing and compression setup. Last-Modified is straightforward for file-backed content but may have limited precision for rapid changes. Neither mechanism replaces a suitable TTL or a purge strategy; they reduce transfer work when validation occurs.
What to double-check
- Inspect the actual response. Use browser developer tools or a command such as
curl -I https://example.com/path. CheckCache-Control,ETag,Last-Modified,Age,Via, and any CDN cache-status header provided by your platform. - Separate browser and shared-cache policy. Decide whether
max-ageands-maxageshould be different. A CDN’s dashboard rule may override or supplement origin headers, so document which layer is authoritative. - Check cacheability. Review
Set-Cookie, authorization headers, request cookies, redirects, query strings, andVary. These can change whether a response is safe or useful to share. - Test a cold and warm request. Compare the first request with a repeat request from the same browser and with a request routed through the CDN. A fast browser hit does not prove that the CDN is serving the response from its edge cache.
- Test deployment behavior. Publish a changed CSS or JavaScript file, then verify that new visitors receive it while existing cached copies follow the intended policy. Also test rollback and purge procedures.
- Review framework and hosting defaults. Web servers, WordPress caching plugins, reverse proxies such as NGINX or Varnish, and CDN rules can each add headers. Avoid assuming that an application-level setting is the final response.
For a broader operational review, use the cache invalidation patterns checklist. If a changed response still appears stale, work through the browser, CDN, reverse proxy, page cache, and origin layers rather than clearing everything at random.
Common mistakes
- Using a long TTL for unversioned assets. This creates stale CSS, JavaScript, or images after deployment. Version the URL or shorten the TTL.
- Treating
no-cacheasno-store. Choose validation or complete non-storage deliberately. - Caching personalized HTML. A page that is safe for one user may expose data when served to another. Exclude authenticated and cookie-dependent responses unless the design explicitly supports them.
- Purging only the CDN. Browser caches may still hold the old response, and an upstream reverse proxy may have its own copy.
- Applying
immutableto mutable URLs. This makes accidental overwrites particularly difficult to recover from. - Copying a policy between asset types. HTML, public APIs, images, and account responses have different freshness and privacy requirements.
- Ignoring headers added later in the stack. Inspect the final response at the public URL, not only the application configuration.
Cache headers are one part of website speed optimization. They work alongside page caching, object caching, server caching, and CDN delivery; they do not replace an appropriate hosting or application architecture. See page cache versus object cache for the distinction between response reuse and database or application-data reuse.
When to revisit
Review cache headers before a seasonal traffic period, a major redesign, a migration between hosting or CDN providers, or a change to asset build tooling. Revisit them when you introduce personalization, authentication, a new API, WooCommerce or another transactional workflow, or a new deployment process. Also check them after changing a WordPress cache plugin, reverse proxy, CDN cache rule, or web server configuration.
Keep a short policy record for each response category: its intended audience, browser TTL, shared-cache TTL, invalidation method, and rollback procedure. During every release, verify one HTML page, one versioned asset, one API response, and one authenticated response. Record the observed headers and cache status. This small recurring audit catches policy drift before it becomes a stale-content or data-exposure incident.
When a problem appears, start by capturing the public response headers, identify every caching layer, and compare the observed behavior with the policy record. Then change one layer at a time and retest with both cold and warm requests. This makes cache behavior understandable, repeatable, and safe to maintain.