Caching · Practical guide
Cache-Control explained: no-cache, no-store and private
Use no-store when a response must not be stored, no-cache when stored content must be validated before reuse, and private when shared caches must not keep it. Use max-age for freshness and s-maxage for a separate shared-cache lifetime; public permits shared caching but does not set a lifetime. These directives solve different problems. Start with the exact resource, choose a policy for its content and verify both a first visit and a repeat visit.
Fix shared caching on a sensitive account response
Suppose /account returns personal details, sets a session cookie and sends Cache-Control: public, max-age=600. That combination needs attention: Set-Cookie does not automatically prevent HTTP caching. HeaderScan can flag a combination it receives, but cannot prove that a CDN stored the response or inspect an account that requires login.
For this sensitive output, set no-store in the route middleware or response handler and remove conflicting public cache rules from the web server and CDN. Bypass shared caching for the affected account routes. If older responses were already cached at the edge, invalidate those entries: sending a new no-store header does not erase existing copies.
# Before: inappropriate for this personal account response
Cache-Control: public, max-age=600
# After: instruct browsers and shared caches not to store it
Cache-Control: no-store - Inspect the signed-in account request in DevTools > Network. Confirm which layer supplies public or s-maxage and whether the body contains personal data; keep public assets on a separate policy.
- Deploy the account policy and CDN bypass, invalidate affected edge entries, then test with two separate browser profiles and test accounts. Each profile must receive only its own content; inspect both the returned headers and the body.
- Repeat a visit and check the CDN cache status for those routes. Rescan public URLs after deployment, and verify authenticated responses separately in your browser.
Reference: RFC 9111: caching sensitive information
Separate storage from reuse
Make three decisions: may the response be stored, which caches may store it, and when must they contact the server again? A browser cache belongs to one browser; a CDN or proxy cache can serve multiple visitors. private alone does not force the browser to fetch a fresh response.
must-revalidate allows reuse while fresh, but requires validation once the response becomes stale. Unlike no-cache, it does not require validation before every reuse. Browser history navigation can restore a page snapshot without new validation, so it needs separate testing.
Combinations can be deliberate: private, no-cache allows a browser copy but requires validation; no-store, no-cache is redundant rather than permission to store. Use one coherent policy per resource. Remove contradictory public and private settings instead of appending another header at a different layer. Absence of Cache-Control does not necessarily mean no caching: some responses can be cached heuristically.
# Illustrative final response for a public stylesheet
Cache-Control: public, max-age=600, s-maxage=3600
Age: 120
# Simplified remaining freshness, accounting for Age
# Browser: 600 - 120 = 480 seconds
# Shared cache: 3600 - 120 = 3480 seconds
# Actual freshness also depends on response timing and request directives. | Directive | What it controls | Common mistake |
|---|---|---|
| no-store | Do not store this response in browser or shared HTTP caches. | Treating it as a command to delete previously stored copies. |
| no-cache | Storage is allowed, but reuse requires successful validation. | Assuming the name means that nothing is stored. |
| private | Browser storage is allowed; shared caches must not store the response. | Assuming it also sets a lifetime or requires validation. |
| public | Permit shared caching, subject to the other cache rules. | Using it for personal content or assuming it supplies a freshness lifetime. |
| max-age=300 | A stored response stays fresh for 300 seconds, accounting for its age. | Using a long lifetime for content that changes at the same URL. |
| s-maxage=300 | Set the shared-cache lifetime, overriding max-age there; browsers ignore it. | Allowing CDN reuse of personalized responses. |
| must-revalidate | A stale response requires successful validation before reuse. | Confusing validation after expiry with no-cache validation before every reuse. |
| immutable | The representation will not change while it is fresh. | Overwriting a long-cached file without changing its URL. |
Reference: MDN: Cache-Control
Match the policy to the resource
The freshness example above accounts for 120 seconds already spent in caches: receipt by the browser does not restart the full lifetime. It describes the policy, not proof that a CDN stored the resource. These resource policies make different tradeoffs. Apply them to the relevant response or route group, not as one site-wide setting. For personalized content, use browser storage only when retaining a local copy is acceptable; sensitive output belongs in the no-store case.
A content hash in an asset URL, such as /assets/app.a81f3c.css, allows a long lifetime because every content change produces a different URL. Deploy the new file and update the HTML reference together. Never overwrite an immutable file at its existing URL and expect returning visitors to fetch it immediately.
For public HTML with no-cache, configure the server or framework to generate an ETag or Last-Modified value from the representation. Do not hard-code the same validator for changing content. This lets an unchanged page validate without transferring its body again.
# Sensitive account or invoice response
Cache-Control: no-store
# Personalized HTML where browser storage is acceptable
Cache-Control: private, no-cache
# Public HTML that must validate before reuse
Cache-Control: no-cache
# Public asset with a content hash in its URL
Cache-Control: public, max-age=31536000, immutable Reference: MDN: HTTP caching and versioned resources
Test revalidation with a conditional GET
Use the exact final public URL, such as an article rather than the homepage. If it ends on a login page or CDN challenge, its headers describe that response. Cache-Control can appear in requests as well as responses; HeaderScan reports the final response policy and cannot establish a visitor’s request directives. The first macOS/Linux command below makes a GET request and prints headers while discarding the body. Read Cache-Control and ETag. HeaderScan redacts identifying validator values, so obtain the ETag locally for this check.
Run the second command with the actual ETag you received, preserving its quotes and any W/ prefix. The example value article-v7 is only a placeholder. For an unchanged representation and a supported matching condition, expect 304 Not Modified without a response body. A 200 is not automatically a fault: the content may have changed or the endpoint may not support conditional requests.
In staging, make a visible content change, invalidate or revalidate any applicable CDN entry, then retry with the old ETag. Expect the updated representation and a changed validator; omit -o /dev/null to see the body. If the updated response still incorrectly validates against the old tag, inspect how its validator is generated. An edge cache can otherwise return 304 for its still-fresh copy even after the origin changes.
curl has no built-in HTTP response cache, so running an ordinary GET twice does not simulate browser caching. The explicit If-None-Match condition tests revalidation; use a browser with caching enabled to check actual reuse.
# 1. Read the current response headers
curl -q -sS --max-time 30 -D - -o /dev/null \
'https://example.com/article'
# 2. Replace article-v7 with the ETag from that response
curl -q -sS --max-time 30 -D - -o /dev/null \
-H 'If-None-Match: "article-v7"' \
'https://example.com/article' Reference: MDN: If-None-Match and conditional requests
Check the browser, CDN and response variants
If an old response persists, compare the origin settings with the public response and review CDN rules that override them. Age and vendor cache-status headers can help identify edge reuse, but one header does not prove that every cache entry is correct. Check the exact URL and relevant query parameters, then invalidate older edge entries when necessary.
When a request header selects a response variant, describe that variation with Vary and check the CDN cache key. For example, if Access-Control-Allow-Origin changes with the requesting Origin, evaluate Vary: Origin and test both allowed and disallowed origins. Vary: Cookie is not a substitute for keeping personal responses out of shared caches.
In browser DevTools, leave Disable cache unchecked when testing normal reuse. Load the page, navigate away and return; inspect the document and asset requests. If the header policy is correct but old content persists, also check whether a service worker supplies it. A fresh private window alone does not tell you what an existing visitor receives.
- Confirm the final policy on an anonymous request and, where applicable, in two separate test accounts. No account should receive another account’s content.
- Compare requests for each supported variant, such as language or Origin. Verify the returned body, Vary header and CDN cache-key configuration agree.
- Test a fresh visit and a normal repeat visit with browser caching enabled. Public HTML should validate when required; reusable assets should remain available from cache while fresh.
- Deploy a changed asset under a new URL and confirm an existing browser session loads the new version. Invalidate old CDN responses affected by a policy change.
- Rescan the exact public resource to confirm the deployed headers. Keep authenticated checks and actual cache behaviour as separate browser evidence.
Reference: RFC 9111: response variants and Vary
Keep reading
Related guides
HTTP security headers: what to check first
Fix security header findings with CSP examples, a staged HSTS rollout, framing rules and practical browser and curl checks.
Read guide CookiesSecure, HttpOnly and SameSite cookies explained
Fix SameSite and session-cookie findings with Set-Cookie examples, host-only scope and browser checks for login, embedded content and logout.
Read guide Nginx configurationHow to configure security headers in Nginx
Add Nginx security headers to the correct HTTPS server, fix location inheritance and duplicates, then verify normal pages and error responses.
Read guide LaravelHow to add security headers in Laravel
Add response headers with Laravel middleware, register them correctly, test normal and error responses, and check proxy or CDN overrides.
Read guide CSP implementationHow to roll out Content Security Policy with Report-Only
Build and test a CSP candidate, connect matching nonces and reporting endpoints, then enforce it with a practical rollback plan.
Read guideHTTP response headers reference: security, cookies and caching
Look up common HTTP response headers, their purpose and the checks to make before changing a policy.
Open reference