HTTP Caching Headers: A Practical Guide to Cache-Control, ETags, and CDN Behavior
HTTPCachingCDNPerformanceWeb Standards

HTTP Caching Headers: A Practical Guide to Cache-Control, ETags, and CDN Behavior

WWebDev.cloud Editorial Team
2026-11-28
7 min read

A practical checklist for choosing HTTP caching headers, debugging browser and CDN behavior, and preventing stale or private data from being shared.

HTTP caching becomes easier to manage when each response has a deliberate policy. This guide explains how browsers, CDNs, and origin servers use Cache-Control, ETag, and related headers, then turns that knowledge into reusable checklists for HTML, APIs, JavaScript bundles, images, and other production assets.

Overview

A cached response can be served by several layers: the browser, a shared proxy, a CDN edge, or the origin application. Each layer may make a different decision, so “the cache” is not a single switch. A response can be fresh in a browser but absent from the CDN, or fresh at the CDN while a user still has an older browser copy.

Cache-Control is the main policy header. It defines whether a response may be stored, how long it can be considered fresh, and whether it can be reused without contacting the origin. Common directives include:

  • max-age=<seconds>: how long a browser may use the response while it is fresh.
  • s-maxage=<seconds>: a freshness lifetime for shared caches such as CDNs. When supported by the cache, it can take precedence over max-age.
  • public: indicates that a response may be stored by shared caches. Use it only when the response is safe to share between users.
  • private: restricts storage to a private cache, normally the user’s browser. This is appropriate for personalized responses.
  • no-store: tells caches not to store the response. Use this for genuinely sensitive or highly transient content when storage is not acceptable.
  • no-cache: allows storage, but requires a cache to revalidate before reuse. It does not mean “do not store.”
  • must-revalidate: asks a cache not to serve a stale response after it becomes unusable without successful validation.
  • stale-while-revalidate: permits a cache that supports the directive to serve a stale response while it checks for an updated copy.

Validators make revalidation efficient. An ETag identifies a particular representation, while Last-Modified records a modification time. On a later request, the client can send If-None-Match or If-Modified-Since. If the representation has not changed, the server can return 304 Not Modified without sending the full body again.

In practice, prefer ETag when your application can produce a stable representation identifier. Last-Modified is useful when a trustworthy modification timestamp is available. They can be used together, but the server and intermediary must apply consistent validation rules.

Checklist by scenario

ResourceStarting policyWhat to verify
Personalized HTML or account pagesprivate, no-cache, or no-store when storage is inappropriateCookies, authorization, and user-specific data are never shared through a public cache.
Public HTML shellShort max-age with revalidation, or a carefully selected CDN lifetimeDeployments can reveal new asset references without leaving users on an obsolete shell.
Versioned JavaScript and CSS bundlespublic, max-age=31536000, immutable when filenames change whenever content changesEvery deployment generates a new URL for changed content, and old files remain available as long as older HTML may reference them.
Images, fonts, and other static assetsLong-lived caching for immutable, versioned URLs; shorter caching for URLs whose content can be replacedContent type, compression, range requests, and cross-origin behavior are correct.
Public API responsesA short shared lifetime or no-cache with validators, depending on freshness needsResponses do not vary by user, authorization, locale, or other untracked request inputs.
Private API responsesprivate and validation where usefulCDN rules do not override application intent, and authenticated data is not stored in a shared cache.

HTML documents

HTML is often the entry point that references the rest of an application. A conservative starting point is:

Cache-Control: public, max-age=60, must-revalidate
ETag: "homepage-build-42"

Adjust the lifetime to match how quickly a deployment must become visible. If HTML contains account-specific data, do not make it public merely because the route is technically a document. Use private or no-store according to the sensitivity and product requirements.

Hashed bundles and static assets

Content-hashed filenames, such as app.8f31c.js, solve invalidation by changing the URL when the bytes change. That supports a long lifetime:

Cache-Control: public, max-age=31536000, immutable

The policy is only safe if the URL is truly immutable. Do not apply it to app.js when the server may replace that file in place. Also ensure your deployment keeps old assets available long enough for users with an older HTML document to request them.

APIs and data responses

For a public response that can be briefly stale, a CDN-oriented policy might be:

Cache-Control: public, max-age=30, s-maxage=300
ETag: "products-2026-11-23-v7"

For a response that may be stored in a browser but must be checked before reuse:

Cache-Control: private, no-cache
ETag: "profile-1842-v3"

Do not cache responses containing secrets, account details, or permission-dependent data in a shared cache. Be especially careful with responses that vary by Authorization, cookies, language, device, or query parameters. If a cache needs to distinguish those inputs, configure the cache key and Vary behavior deliberately rather than assuming it will happen automatically.

CDN and origin behavior

Start with origin headers, then inspect the CDN’s rules. A CDN may honor, override, or combine origin directives according to its configuration. Confirm the actual behavior using response headers such as Age, Via, or a provider-specific cache-status header when available. A header indicating a hit or miss is useful evidence, but its name and meaning vary by platform.

When content must be removed before its normal lifetime ends, use one of three strategies: deploy a new URL, purge the relevant CDN objects, or shorten the policy and wait for expiry. Versioned URLs are usually the simplest strategy for build assets; purges are more relevant for HTML or data that keeps a stable URL.

What to double-check

Inspect a request in browser developer tools

  1. Open the browser’s developer tools and select the Network panel.
  2. Enable a setting such as “Disable cache” only while actively debugging. Remember that this setting commonly applies while developer tools are open and can hide normal caching behavior.
  3. Reload the page and select the document, API request, or asset under investigation.
  4. Review response headers for Cache-Control, ETag, Last-Modified, Age, Vary, and any CDN cache-status header.
  5. Check the request headers for If-None-Match or If-Modified-Since. A 304 response indicates validation succeeded; it does not mean the resource was never stored.
  6. Compare the transferred size and status with a fresh reload. Browser labels such as “from memory cache” or “from disk cache” are useful clues, but response headers and repeatable tests are more reliable.

Repeat the test with curl

Request headers without downloading the body:

curl -I https://example.test/assets/app.8f31c.js

Capture the validator from a first response, then test conditional behavior:

curl -sD headers.txt -o /dev/null https://example.test/data.json
curl -H 'If-None-Match: "copy-the-etag-from-headers.txt"' \
  -D - -o /dev/null https://example.test/data.json

Test more than one URL when a CDN is involved. Different edge locations, query strings, cookies, or request methods can produce different cache outcomes. Also test an authenticated request separately from an anonymous request.

Review deployment and framework configuration

Locate the layer that sets each header: application code, framework middleware, reverse proxy, object storage, CDN, or hosting configuration. A correct application setting can be defeated by a later proxy rule, while a broad CDN rule can make private application responses public. Add header checks to deployment validation where practical, particularly for the HTML entry point, one hashed asset, and one authenticated API route.

Common mistakes

  • Confusing no-cache with no-store. The first permits storage but requires validation; the second is the stronger instruction not to store.
  • Using long caching for mutable filenames. If main.js is replaced in place, a browser or CDN can continue serving the previous bytes until expiry or purge.
  • Making personalized responses public. Cookies, authorization headers, and user-specific output require a deliberate private-cache policy.
  • Purging only one layer. Removing an object at the CDN does not necessarily remove a browser’s stored copy, and a browser refresh does not necessarily purge every edge location.
  • Ignoring query strings and content negotiation. If output changes by query parameter, language, encoding, or another request header, the cache key and Vary policy must reflect that.
  • Assuming a 304 means the origin sent nothing. It means the client’s validator was accepted; the request may still have reached an origin or intermediary.
  • Debugging with cache disabled and stopping there. Reproduce once with normal browser settings and an independent curl request before changing production configuration.

When to revisit

Review caching before a major deployment, a seasonal traffic cycle, a CDN or hosting migration, or a change to authentication and personalization. Recheck it whenever a new asset type or API route is introduced, a build pipeline changes filename handling, or users report stale content.

Keep a short cache inventory with the URL pattern, owner, intended audience, browser lifetime, shared-cache lifetime, validator strategy, and invalidation method. During each review, run this action list:

  1. List the resource classes your application serves.
  2. Assign each class a freshness and privacy requirement.
  3. Verify headers at the origin and at the public CDN URL.
  4. Test anonymous, authenticated, and conditional requests where relevant.
  5. Confirm that a deployment can expose new HTML and assets without breaking users with older cached documents.
  6. Record how to purge or version each resource before an incident occurs.

For related production workflows, pair this review with an API debugging workflow, a JavaScript and TypeScript CI/CD checklist, and a TLS certificate runbook. Caching is most dependable when its policy, deployment process, and observability are reviewed together.

Related Topics

#HTTP#Caching#CDN#Performance#Web Standards
W

WebDev.cloud Editorial Team

Technical Editorial Team

Senior editor and content strategist. Writing about technology, design, and the future of digital media. Follow along for deep dives into the industry's moving parts.