A website can be slow because it downloads the same files on every visit.
It can also appear broken because a browser keeps using an old file after a deployment.
Both problems involve caching. The difference is whether the caching policy matches the resource.
A stylesheet with a unique filename for every build can often be stored for a long time. An HTML page that points to the current build needs a different policy. A response containing a customer's account details needs a different one again.
HTTP caching works best when those decisions are made per response, based on who can reuse it and how quickly it must change.
What HTTP caching actually does
An HTTP cache stores a response so it can potentially reuse it for a later request.
A private cache, such as a browser cache, serves one user. A shared cache, such as a CDN cache, may serve many users. A response suitable for one is not automatically suitable for the other.
When a stored response is fresh and otherwise matches the request, the cache can reuse it without contacting the origin server. When it needs validation, the cache can ask whether the stored version is still current.
The server communicates the policy through response headers, particularly Cache-Control. HTTP also allows caches to apply heuristic caching in some circumstances when explicit instructions are missing. Omitting the header is therefore not a reliable way to disable caching.
A useful starting question for every response is:
Can this exact response be reused, by whom, and for how long?
Choose a policy by resource, not by file extension alone
The following examples are starting points. They are not headers to copy across an entire site.
| Resource | Typical concern | Example direction |
|---|---|---|
| Public, content-hashed CSS or JavaScript | Avoid downloading an unchanged file repeatedly | Long freshness lifetime and immutable |
| Public HTML at a stable URL | Show the latest references after deployment | Store, but revalidate before reuse |
| Public data shared by all visitors | Balance freshness with origin load | Explicit lifetime chosen for the data |
| Personalized account page | Prevent shared caches from storing one user's response for another | private, with an appropriate freshness policy |
| Highly sensitive response | Avoid storage by compliant HTTP caches | no-store, alongside other security controls |
The same .html extension can describe a public article, a customer invoice or an admin dashboard. The extension cannot tell the cache which of those responses is safe to share.
Before choosing a lifetime, establish:
- Whether the content is the same for every visitor.
- How quickly an update must become visible.
- Whether the URL changes when the content changes.
- Which caches sit between the browser and the application.
- What happens when an old version is still available.
Cache versioned assets for longer
A file whose URL changes whenever its contents change is a good candidate for a long cache lifetime.
For example:
/assets/app.8c41d2.js
/assets/site.2f7a90.css
When a new build changes the JavaScript, it produces a new URL:
/assets/app.4e31b9.js
The new HTML points to the new file. A browser that already has the old file can keep it without confusing it with the new one.
An illustrative response for a public, content-hashed asset is:
Cache-Control: public, max-age=31536000, immutable
Here, max-age specifies a freshness lifetime of one year in seconds. immutable tells clients that recognize it that the response will not change while fresh. This approach depends on the URL actually changing when the content changes. If the file at that URL is overwritten, visitors can continue using the old version during its freshness lifetime.
A deployment should also retain old versioned assets for an appropriate period. An open browser tab, an older cached HTML response or a delayed request may still refer to them. Removing them immediately can turn a cache strategy into missing scripts and styles.
Do not apply a year-long policy to every .js or .css file merely because it is a static asset. An unversioned /app.js may change while keeping the same URL.
Treat HTML as the map to the current build
An HTML document often contains references to the CSS, JavaScript and other resources that make up a page.
Even if those assets have versioned URLs, visitors need sufficiently current HTML to discover the latest URLs.
For a public page at a stable address, one possible policy is:
Cache-Control: no-cache
ETag: "page-version-42"
Despite its name, no-cache does not mean “do not store.” It allows storage but requires successful validation before a stored response is reused for a subsequent request.
If the page has not changed, the server can respond with 304 Not Modified and the client can reuse its stored body. If it has changed, the server returns the new representation, normally with 200 OK.
For a public site that can tolerate a short delay before updates appear, a short freshness lifetime may be a better trade-off than validating on every ordinary request. That is a product decision: a menu price, a legal notice and an archived article may have different freshness requirements.
Understand no-cache, no-store and private
These directives solve different problems.
no-cache
Cache-Control: no-cache
A cache may store the response, but it must validate it before reuse. This can work well with validators such as ETag or Last-Modified.
no-store
Cache-Control: no-store
Compliant HTTP caches must not store the response. This may be appropriate for sensitive information when the application requires that treatment.
It is not a complete privacy or security mechanism. It does not replace access control, encrypted connections, careful application storage or protection against compromised systems. It also does not delete an older response already stored for the same URL.
private
Cache-Control: private, no-cache
private tells shared caches not to store the response. A private browser cache may still store it, subject to the other directives. The example also requires validation before reuse.
That may be appropriate for some personalized pages, but the right choice depends on the sensitivity of the content and the application's behavior on shared devices.
A common mistake is assuming that a logged-in page is safe to cache publicly because it has the same URL for everyone. The URL may be identical while the response body is different for each account.
Also check CDN rules and application configuration. A platform-specific override can change the behavior you expected from the origin headers.
Use validators to avoid sending an unchanged body
A validator identifies a representation so a cache can check whether its stored copy still matches.
Two common validators are:
ETag, an identifier for a particular representation.Last-Modified, a timestamp indicating when the server believes it last changed.
For example, the server returns:
HTTP/1.1 200 OK
Cache-Control: no-cache
ETag: "article-v7"
On a later request, the client can send:
GET /articles/example/ HTTP/1.1
If-None-Match: "article-v7"
If the representation is unchanged, the server can reply:
HTTP/1.1 304 Not Modified
ETag: "article-v7"
Cache-Control: no-cache
The response does not need to resend the full page body. If the representation has changed, the server returns the updated body with a new validator.
The validator needs to reflect the actual representation. An ETag that remains unchanged after the content changes can cause a cache to keep reusing the wrong version.
Account for variations in the response
A single URL may return different representations based on request headers.
For example, a server might select a language using Accept-Language. If the response is cacheable, it can indicate that variation:
Vary: Accept-Language
A compliant cache uses the nominated request header when deciding whether a stored response matches a later request. Without the correct variation, a cache may reuse a representation that was selected for a different request.
Vary is not a substitute for a privacy policy. Do not assume that adding a cookie header to Vary makes a personalized response safe for shared caching. Use explicit cache controls, understand the CDN's cache key and test the actual behavior.
Where practical, distinct URLs such as /en/ and /de/ can also make language variants easier to reason about. That is an architecture choice, not an HTTP requirement.
Plan how content becomes fresh after an update
Cache lifetime and invalidation are part of the publishing workflow.
Consider a public product page with a ten-minute shared-cache lifetime. If the price changes at the origin, visitors may continue to receive the previous cached page until it expires or the relevant cache entry is invalidated.
Before choosing that policy, decide whether the product can tolerate the delay. If it cannot, use a different strategy, such as revalidation, a shorter lifetime or an invalidation mechanism supported by the actual CDN.
A purge may clear the CDN and still leave a fresh copy in a visitor's browser cache. Conversely, changing a versioned asset URL gives browsers a new resource to request, but only after they receive HTML that references it.
A deployment checklist should therefore include:
- Publish new versioned assets.
- Publish HTML that points to those assets.
- Apply any required CDN invalidation.
- Keep old assets available long enough for existing references.
- Verify the page from a normal browser session.
The exact release order depends on the hosting platform. The objective is to avoid HTML referring to an asset that is not available yet, or an older page referring to an asset that has already been removed.
Test the response people actually receive
Headers configured at the origin are not necessarily the headers a visitor sees after a CDN, reverse proxy or platform rule has processed the response.
Start by inspecting a public response:
curl -sS -D - -o /dev/null https://example.com/
For a versioned asset, inspect its exact URL:
curl -sS -D - -o /dev/null https://example.com/assets/app.8c41d2.js
Look for headers such as:
Cache-Control
ETag
Last-Modified
Age
Vary
Their presence alone does not prove the behavior is correct. Compare the values with the policy you intended.
If the response includes an ETag, test validation using the exact value returned by the server:
curl -sS -D - -o /dev/null \
-H 'If-None-Match: "page-version-42"' \
https://example.com/
Replace the example value with the real validator. An unchanged representation may return 304; a changed representation should return the current content.
Then check in a browser:
- Load the page normally and inspect the network requests and response headers.
- Reload and observe which resources are reused or revalidated.
- Deploy a change and confirm that the new HTML references the new asset URLs.
- Check with two test accounts that personalized data never appears in the other account.
- Test logout and account switching, especially on a shared device.
Browser developer tools, service workers and CDN configuration can affect what you observe. Disabling cache in developer tools is useful for some tests, but it cannot demonstrate normal repeat-visit behavior.
Watch for caches outside the HTTP cache
A service worker can intercept requests and manage its own stored responses. Changing Cache-Control alone may not fix an outdated page if the service worker continues returning an old response from its own cache.
The browser back/forward cache is also different from ordinary HTTP caching. It may restore a page snapshot during history navigation, so a normal HTTP revalidation test does not cover every back-button scenario.
If a user reports stale content, identify the layer before purging anything:
- Browser HTTP cache
- Service worker cache
- CDN or reverse proxy
- Application or database cache
- Browser history restoration
Each layer has its own storage and invalidation behavior. A CDN purge cannot remove a response stored by a service worker on a user's device.
Write the caching policy alongside the feature
Caching becomes easier to maintain when it is documented as behavior rather than hidden in infrastructure configuration.
For each important response, record:
| Decision | Example question |
|---|---|
| Audience | Is this response identical for all visitors? |
| Freshness | How long can an older version remain visible? |
| URL strategy | Does the URL change when the content changes? |
| Storage | Can a shared cache store it? Can a browser store it? |
| Validation | Which validator changes when the representation changes? |
| Update path | How does a published change reach visitors? |
| Verification | How will the team test real responses after deployment? |
A sensible caching policy can reduce repeat downloads and origin requests. It can also prevent a deployment from leaving visitors with an inconsistent mix of old HTML and new assets.
The goal is not to cache everything for as long as possible. It is to make reuse predictable.
Conversation
Discussion
Loading comments…