<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 刘立耀</title>
    <description>The latest articles on DEV Community by 刘立耀 (@_abf9d4bd1d1649ac33e5c).</description>
    <link>https://dev.to/_abf9d4bd1d1649ac33e5c</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4250411%2F442b9b11-7833-4b6a-9625-9adfe2e2edf7.png</url>
      <title>DEV Community: 刘立耀</title>
      <link>https://dev.to/_abf9d4bd1d1649ac33e5c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/_abf9d4bd1d1649ac33e5c"/>
    <language>en</language>
    <item>
      <title>HTTP Caching Headers That Actually Work</title>
      <dc:creator>刘立耀</dc:creator>
      <pubDate>Tue, 17 Nov 2026 18:36:57 +0000</pubDate>
      <link>https://dev.to/_abf9d4bd1d1649ac33e5c/http-caching-headers-that-actually-work-55j6</link>
      <guid>https://dev.to/_abf9d4bd1d1649ac33e5c/http-caching-headers-that-actually-work-55j6</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;This article is also maintained as a reference guide at &lt;a href="https://www.jsonmind.com/guides/http-caching-headers" rel="noopener noreferrer"&gt;JsonMind&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A cache bets that a stored response will still be acceptable when the next request arrives. HTTP splits that bet into freshness (reuse without asking the origin) and validation (refresh a stale copy cheaply). Nearly every caching bug is a mistake in one of those decisions. The contract is enforced by systems you do not control — browser, proxy, CDN, service worker — so a wrong header rarely fails in staging; it surfaces as a customer seeing another customer's invoice or an API serving data from before a password change.&lt;/p&gt;

&lt;h2&gt;
  
  
  Freshness and validation: the two questions every cache answers
&lt;/h2&gt;

&lt;p&gt;Freshness is a lifetime, not a flag. The origin grants it with &lt;code&gt;Cache-Control: max-age=N&lt;/code&gt; in seconds, &lt;code&gt;s-maxage=N&lt;/code&gt; for shared caches only, or &lt;code&gt;Expires&lt;/code&gt; as an absolute date. A cache tracks age from the origin's &lt;code&gt;Date&lt;/code&gt; and the &lt;code&gt;Age&lt;/code&gt; added by intermediaries, then reuses the response only while it is below the freshness lifetime. &lt;code&gt;s-maxage&lt;/code&gt; overrides &lt;code&gt;max-age&lt;/code&gt; in a CDN; &lt;code&gt;Cache-Control&lt;/code&gt; overrides &lt;code&gt;Expires&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Validation is a comparison. Once an entry is stale, the cache sends &lt;code&gt;If-None-Match&lt;/code&gt; with the stored &lt;code&gt;ETag&lt;/code&gt;, or &lt;code&gt;If-Modified-Since&lt;/code&gt; with the stored &lt;code&gt;Last-Modified&lt;/code&gt;. A &lt;code&gt;304 Not Modified&lt;/code&gt; refreshes the entry without a body; a &lt;code&gt;200&lt;/code&gt; replaces it. With no validator, the full response is downloaded again.&lt;/p&gt;

&lt;p&gt;Both failure directions hurt: a lifetime that is too long serves replaced or private content, while a lifetime of zero adds a round trip without guaranteeing freshness. A correct policy names each resource class — hashed asset, HTML, public JSON, authenticated JSON, error — and assigns it an explicit mechanism.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cache-Control directives that change behavior
&lt;/h2&gt;

&lt;p&gt;Four distinctions explain most incidents. &lt;code&gt;no-cache&lt;/code&gt; means store but revalidate before every reuse, not "do not cache." &lt;code&gt;no-store&lt;/code&gt; means do not write the response to disk or memory at all. &lt;code&gt;private&lt;/code&gt; lets the browser store it but forbids shared caches. &lt;code&gt;public&lt;/code&gt; makes it storable by shared caches even when it would otherwise be refused, so it must never appear on authenticated data.&lt;/p&gt;

&lt;p&gt;The stale-serving directives buy resilience during outages. &lt;code&gt;stale-while-revalidate=N&lt;/code&gt; lets a cache serve stale for up to N seconds while it refreshes in the background; &lt;code&gt;stale-if-error=N&lt;/code&gt; extends that window when revalidation fails with a 5xx or network error. Both are defined in RFC 5861, widely supported, and capable of hiding origin failures: enable them deliberately and monitor the origin.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Directive&lt;/th&gt;
&lt;th&gt;Applies to&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;th&gt;Common mistake&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;max-age=N&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;every cache&lt;/td&gt;
&lt;td&gt;fresh for N seconds&lt;/td&gt;
&lt;td&gt;expecting a change to invalidate immediately&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;s-maxage=N&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;shared caches&lt;/td&gt;
&lt;td&gt;overrides &lt;code&gt;max-age&lt;/code&gt; in CDNs and proxies&lt;/td&gt;
&lt;td&gt;assuming browsers honor it&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;no-cache&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;every cache&lt;/td&gt;
&lt;td&gt;may store, must revalidate before reuse&lt;/td&gt;
&lt;td&gt;reading it as "do not store"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;no-store&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;every cache&lt;/td&gt;
&lt;td&gt;must not store any part of the response&lt;/td&gt;
&lt;td&gt;using &lt;code&gt;private&lt;/code&gt; where tokens are involved&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;private&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;shared caches&lt;/td&gt;
&lt;td&gt;only the browser may store it&lt;/td&gt;
&lt;td&gt;expecting it to prevent browser storage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;public&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;shared caches&lt;/td&gt;
&lt;td&gt;storable even with &lt;code&gt;Authorization&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;adding it to authenticated responses&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;must-revalidate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;every cache&lt;/td&gt;
&lt;td&gt;no stale reuse after expiry&lt;/td&gt;
&lt;td&gt;omitting it on data that cannot be stale&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;stale-while-revalidate=N&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;every cache&lt;/td&gt;
&lt;td&gt;serves stale while refreshing in the background&lt;/td&gt;
&lt;td&gt;enabling it without a validator&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;stale-if-error=N&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;every cache&lt;/td&gt;
&lt;td&gt;serves stale when the origin fails&lt;/td&gt;
&lt;td&gt;masking a prolonged outage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;immutable&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;browsers&lt;/td&gt;
&lt;td&gt;skips revalidation while fresh&lt;/td&gt;
&lt;td&gt;using it on unversioned URLs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A public marketing page can combine browser revalidation with a short shared lifetime:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="no"&gt;HTTP&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="mf"&gt;1.1&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt; &lt;span class="no"&gt;OK&lt;/span&gt;
&lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Tue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt; &lt;span class="nc"&gt;Nov&lt;/span&gt; &lt;span class="mi"&gt;2026&lt;/span&gt; &lt;span class="mi"&gt;08&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;00&lt;/span&gt; &lt;span class="no"&gt;GMT&lt;/span&gt;
&lt;span class="na"&gt;Content-Type: text/html; charset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;utf&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;
&lt;span class="na"&gt;Cache-Control: public, max-age&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;maxage&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;stale&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;while&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;revalidate&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;
&lt;span class="nc"&gt;ETag&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"7c3a1f9d9b2e"&lt;/span&gt;
&lt;span class="nc"&gt;Vary&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Accept&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nc"&gt;Encoding&lt;/span&gt;
&lt;span class="nc"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;37&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Browsers treat the document as stale immediately and revalidate; the CDN serves it fresh for 60 seconds and may serve a stale copy for another 300 seconds while revalidating. &lt;code&gt;Age: 37&lt;/code&gt; says the copy is 37 seconds old, which is the number to watch when a deploy seems slow. The &lt;a href="https://dev.to/tools/http-headers"&gt;HTTP headers analyzer&lt;/a&gt; parses a captured response and shows which directives a specific layer added.&lt;/p&gt;

&lt;h2&gt;
  
  
  ETag, weak validators, and the 304 response
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;ETag&lt;/code&gt; is an opaque validator chosen by the origin: a hash of the representation bytes, or a version identifier that changes with them. A process-local counter or file modification time breaks on multiple nodes, because each node produces a different value and conditional requests bounce between &lt;code&gt;304&lt;/code&gt; and &lt;code&gt;200&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Strong ETags compare byte for byte; weak ETags, written &lt;code&gt;W/"..."&lt;/code&gt;, promise only semantic equivalence, enough for &lt;code&gt;If-None-Match&lt;/code&gt; but not for range requests or &lt;code&gt;If-Range&lt;/code&gt;. &lt;code&gt;If-None-Match&lt;/code&gt; uses weak comparison even for strong tags and takes precedence over &lt;code&gt;If-Modified-Since&lt;/code&gt;. That combination is the right default: ETag first, date as a fallback.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;304&lt;/code&gt; is not an empty success. It must not carry a body, should repeat &lt;code&gt;ETag&lt;/code&gt;, &lt;code&gt;Cache-Control&lt;/code&gt;, &lt;code&gt;Date&lt;/code&gt;, and &lt;code&gt;Vary&lt;/code&gt;, and updates the stored metadata: a cache receiving a new &lt;code&gt;max-age&lt;/code&gt; on &lt;code&gt;304&lt;/code&gt; must adopt it. &lt;code&gt;Last-Modified&lt;/code&gt; has one-second resolution, so two edits in the same second look identical; keep it at least one second older than &lt;code&gt;Date&lt;/code&gt; or heuristic caches may refuse to store the response.&lt;/p&gt;

&lt;h2&gt;
  
  
  Vary, cache keys, and normalization
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;Vary&lt;/code&gt; lists the request headers that were part of the response's identity. If the body changes with &lt;code&gt;Accept-Encoding&lt;/code&gt;, the response must say &lt;code&gt;Vary: Accept-Encoding&lt;/code&gt;; otherwise a CDN can hand a Brotli body to a client that only advertised gzip, and the user sees corrupted text. The same applies to &lt;code&gt;Accept-Language&lt;/code&gt; and any custom negotiation header.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Vary&lt;/code&gt; is also a performance switch. &lt;code&gt;Vary: User-Agent&lt;/code&gt; creates a variant per user agent string and shreds the hit ratio. &lt;code&gt;Vary: Cookie&lt;/code&gt; creates a variant per cookie value, which usually means no shared caching at all, and it is not a security boundary: sloppy key normalization can still collide. &lt;code&gt;Vary: *&lt;/code&gt; marks the response as unreusable without revalidation. Prefer a cookie-free host for static assets and keep personalized responses out of shared caches.&lt;/p&gt;

&lt;p&gt;Cache keys are not only the URL. CDNs normalize query order, strip tracking parameters, lowercase the host, and sometimes ignore the scheme or a trailing slash. If that normalization differs from the origin's interpretation, two resources share one key — the mechanism behind cache poisoning and cache deception. Document the key for each route, confirm it includes every input that changes the body, and test with parameters in different orders.&lt;/p&gt;

&lt;h2&gt;
  
  
  Shared caches, CDNs, Age, and clock skew
&lt;/h2&gt;

&lt;p&gt;A private cache belongs to one user; a shared cache serves many. RFC 9111 forbids a shared cache from reusing a stored response to a request with &lt;code&gt;Authorization&lt;/code&gt; unless the response carries &lt;code&gt;public&lt;/code&gt;, &lt;code&gt;s-maxage&lt;/code&gt;, or &lt;code&gt;must-revalidate&lt;/code&gt;. That is a floor, not a recommendation: authenticated responses should normally carry &lt;code&gt;private&lt;/code&gt; or &lt;code&gt;no-store&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Set-Cookie&lt;/code&gt; deserves its own warning. Its presence does not prevent a shared cache from storing the response, so a CDN can cache a page that sets a session cookie and replay that header to other users. Strip &lt;code&gt;Set-Cookie&lt;/code&gt; at the edge or mark the response &lt;code&gt;private&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Age&lt;/code&gt; and &lt;code&gt;Date&lt;/code&gt; report time. Apparent age is roughly the cache's receive time minus &lt;code&gt;Date&lt;/code&gt;, plus the &lt;code&gt;Age&lt;/code&gt; it received. Clock skew corrupts that estimate: a fast origin clock triggers early revalidation, a slow one keeps responses fresh too long. Keep nodes on NTP and treat &lt;code&gt;Age&lt;/code&gt; as the truth for how long a copy has existed.&lt;/p&gt;

&lt;p&gt;Without an explicit expiration, caches may apply heuristic freshness, commonly 10% of the interval since &lt;code&gt;Last-Modified&lt;/code&gt;. That is why an unconfigured static file can be cached for hours and an unconfigured API response for minutes. Heuristics are a guess; explicit &lt;code&gt;Cache-Control&lt;/code&gt; is a decision. Check &lt;code&gt;x-cache&lt;/code&gt;, &lt;code&gt;cf-cache-status&lt;/code&gt;, or &lt;code&gt;x-served-by&lt;/code&gt; to see which layer answered and whether it was a hit, a revalidation, or a bypass.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hashed assets, HTML, and safe deployment
&lt;/h2&gt;

&lt;p&gt;Hashed assets and HTML need opposite policies. &lt;code&gt;app.9f2c1a.css&lt;/code&gt; gets a new URL whenever its bytes change, so it can be cached for a year: &lt;code&gt;Cache-Control: public, max-age=31536000, immutable&lt;/code&gt;. &lt;code&gt;immutable&lt;/code&gt; tells browsers to skip even a reload revalidation while fresh, removing a conditional request from every page load. The guarantee depends on the URL changing with the content; &lt;code&gt;immutable&lt;/code&gt; on an unversioned path is a cache you cannot clear.&lt;/p&gt;

&lt;p&gt;HTML names those hashed URLs, so it must be revalidated frequently: &lt;code&gt;Cache-Control: no-cache&lt;/code&gt; plus a strong &lt;code&gt;ETag&lt;/code&gt;, or a short shared TTL such as &lt;code&gt;s-maxage=60, stale-while-revalidate=300&lt;/code&gt;. Long-lived HTML is the classic deploy failure: the browser keeps an old document that references bundles the new release deleted, and users get 404s until they hard-refresh.&lt;/p&gt;

&lt;p&gt;Deployments should be atomic in the cache's terms. Upload new hashed assets before the HTML that references them, keep the previous release available for at least one cache lifetime, and only then remove it. Fonts and CSS imported by old bundles need the same grace period, and a rollback must still find its assets.&lt;/p&gt;

&lt;h2&gt;
  
  
  Invalidation, purge, and versioned URLs
&lt;/h2&gt;

&lt;p&gt;Purging only reaches caches you control, and that set is smaller than you think. A CDN purge API clears edge copies by URL, tag, surrogate key, or wildcard; propagation takes seconds to minutes across points of presence. It does not clear browser caches, corporate proxies, or service worker storage. Users who already hold a copy keep it until their TTL expires or they revalidate.&lt;/p&gt;

&lt;p&gt;Changing &lt;code&gt;Cache-Control&lt;/code&gt; does not purge anything. The new header applies to the next fetch; existing copies keep their old lifetime. Teams routinely "fix" a stale asset by editing headers, redeploy, and see no change because no cache re-fetched the object.&lt;/p&gt;

&lt;p&gt;Versioned URLs are the only invalidation mechanism that works across every cache. Change the URL when the content changes, and old copies become irrelevant rather than wrong. Use purge for mistakes and for content that cannot change its URL: a pricing page, an image with a fixed path, a legal document. Prefer tag-based purge over URL lists because a page and its fragments change together, and treat wildcard purges as an outage risk: they can empty a CDN and send every user to the origin at once. A &lt;code&gt;301&lt;/code&gt; can be cached indefinitely, so temporary moves need &lt;code&gt;302&lt;/code&gt; or &lt;code&gt;307&lt;/code&gt; with a short lifetime.&lt;/p&gt;

&lt;h2&gt;
  
  
  Tokens, PII, and no-store
&lt;/h2&gt;

&lt;p&gt;Anything that identifies a user, grants access, or carries personal data belongs outside every shared cache, and usually outside the browser's disk cache too. A response to an authenticated request should carry &lt;code&gt;Cache-Control: no-store&lt;/code&gt;. &lt;code&gt;private&lt;/code&gt; prevents CDN storage but still lets the browser keep the body on disk, the wrong default for a bearer token, a one-time download link, or an account profile.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Vary: Authorization&lt;/code&gt; is not a substitute. It fragments the cache by credential, still stores every variant, and a cache that mishandles the key can serve one user's response to another. &lt;code&gt;Set-Cookie&lt;/code&gt; is not a substitute either. The safe pattern is explicit: &lt;code&gt;no-store&lt;/code&gt; on the response, no caching of &lt;code&gt;401&lt;/code&gt; and &lt;code&gt;403&lt;/code&gt; bodies that echo request details, and a CDN rule that bypasses authenticated paths rather than trusting origin headers alone.&lt;/p&gt;

&lt;p&gt;Apply the same rule to errors. A &lt;code&gt;500&lt;/code&gt; page with a stack trace, an email address, or an internal hostname should never be stored by a shared cache; &lt;code&gt;no-store&lt;/code&gt; on errors costs nothing and prevents a small incident from becoming a data exposure. The back-forward cache is separate from HTTP caching but also keeps rendered pages in memory, so logout should invalidate server-side sessions rather than rely on headers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Verification checklist with concrete pass criteria
&lt;/h2&gt;

&lt;p&gt;Verify from the outside, in the same order a request travels. Start with curl against production, not a local server with caching disabled:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# 1. Inspect the headers a CDN actually returns&lt;/span&gt;
curl -sSI https://www.example.com/pricing &lt;span class="o"&gt;|&lt;/span&gt; grep -iE &lt;span class="s1"&gt;'^(HTTP/|cache-control:|etag:|vary:|age:|date:)'&lt;/span&gt;

&lt;span class="c"&gt;# 2. Capture the ETag, then ask for the same URL conditionally&lt;/span&gt;
&lt;span class="nv"&gt;etag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;curl -sSI https://www.example.com/pricing &lt;span class="o"&gt;|&lt;/span&gt; awk -F&lt;span class="s1"&gt;': '&lt;/span&gt; &lt;span class="s1"&gt;'tolower($1)=="etag"{print $2}'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; tr -d &lt;span class="s1"&gt;'\r'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
curl -sS -o /dev/null -w &lt;span class="s1"&gt;'status=%{http_code} bytes=%{size_download}\n'&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"If-None-Match: $etag"&lt;/span&gt; https://www.example.com/pricing

&lt;span class="c"&gt;# 3. Compare the variants a cache must keep apart&lt;/span&gt;
curl -sSI -H &lt;span class="s1"&gt;'Accept-Encoding: gzip'&lt;/span&gt; https://www.example.com/app.js &lt;span class="o"&gt;|&lt;/span&gt; grep -i &lt;span class="s1"&gt;'^vary:'&lt;/span&gt;
curl -sSI -H &lt;span class="s1"&gt;'Accept-Encoding: br'&lt;/span&gt; https://www.example.com/app.js &lt;span class="o"&gt;|&lt;/span&gt; grep -i &lt;span class="s1"&gt;'^vary:'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then repeat the requests in browser DevTools with "Disable cache" off. That checkbox makes the browser send &lt;code&gt;Cache-Control: no-cache&lt;/code&gt; on every request, so a freshness bug disappears while it is enabled. Read the Size column for "from disk cache" or "from memory cache", and use the Timing panel to confirm that no round trip occurred. The &lt;a href="https://dev.to/tools/http-request"&gt;HTTP request workbench&lt;/a&gt; reproduces a request with exact headers, and the &lt;a href="https://dev.to/tools/curl-converter"&gt;cURL converter&lt;/a&gt; turns a copied browser request into an equivalent command.&lt;/p&gt;

&lt;p&gt;Treat a check as passed only when all of the following hold:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Check&lt;/th&gt;
&lt;th&gt;How to test&lt;/th&gt;
&lt;th&gt;Pass criteria&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Hashed asset&lt;/td&gt;
&lt;td&gt;Two requests for the same hashed URL&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Cache-Control: public, max-age=31536000, immutable&lt;/code&gt;; second response is a hit with a larger &lt;code&gt;Age&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTML revalidation&lt;/td&gt;
&lt;td&gt;Conditional GET with the stored ETag&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;304&lt;/code&gt;, zero-byte body, same &lt;code&gt;ETag&lt;/code&gt;, refreshed &lt;code&gt;Cache-Control&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Authenticated JSON&lt;/td&gt;
&lt;td&gt;Request with a real session cookie&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Cache-Control: no-store&lt;/code&gt;, no &lt;code&gt;Age&lt;/code&gt;, CDN status &lt;code&gt;BYPASS&lt;/code&gt; or &lt;code&gt;DYNAMIC&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Compression variants&lt;/td&gt;
&lt;td&gt;Same URL with &lt;code&gt;Accept-Encoding: gzip&lt;/code&gt; and &lt;code&gt;br&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;Vary: Accept-Encoding&lt;/code&gt; present and both bodies decode correctly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CDN hit behavior&lt;/td&gt;
&lt;td&gt;Repeated requests through one point of presence&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;x-cache: HIT&lt;/code&gt; or &lt;code&gt;cf-cache-status: HIT&lt;/code&gt; with &lt;code&gt;Age&lt;/code&gt; below the configured TTL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deploy consistency&lt;/td&gt;
&lt;td&gt;Load the site immediately after a release&lt;/td&gt;
&lt;td&gt;HTML references new hashed URLs and every referenced asset returns &lt;code&gt;200&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Failure modes and production rules
&lt;/h2&gt;

&lt;p&gt;The expensive failures are predictable:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A long &lt;code&gt;max-age&lt;/code&gt; on HTML pins an old document that references deleted bundles.&lt;/li&gt;
&lt;li&gt;A missing &lt;code&gt;Vary: Accept-Encoding&lt;/code&gt; serves the wrong compression to the wrong client.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;no-cache&lt;/code&gt; is read as &lt;code&gt;no-store&lt;/code&gt;, so tokens or personal data land on disk.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;private&lt;/code&gt; is used where &lt;code&gt;no-store&lt;/code&gt; is required, leaving a token in the browser cache.&lt;/li&gt;
&lt;li&gt;ETags generated per process or from file mtimes cause &lt;code&gt;200&lt;/code&gt;/&lt;code&gt;304&lt;/code&gt; ping-pong across nodes.&lt;/li&gt;
&lt;li&gt;A CDN purge leaves every browser copy untouched, so support keeps hearing about stale pages.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;5xx&lt;/code&gt; or a personalized &lt;code&gt;404&lt;/code&gt; is cached and replayed to other users.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;immutable&lt;/code&gt; is applied to a URL whose bytes can change.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Expires&lt;/code&gt; in the past fights &lt;code&gt;Cache-Control&lt;/code&gt; and confuses older proxies.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Vary: User-Agent&lt;/code&gt; or &lt;code&gt;Vary: Cookie&lt;/code&gt; destroys the hit ratio while providing no safety.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rules that prevent them are short. Default to &lt;code&gt;no-store&lt;/code&gt; for anything authenticated or personalized, and opt into caching explicitly per route. Put &lt;code&gt;Vary&lt;/code&gt; next to the code that performs content negotiation, not in a global config someone forgets. Give hashed assets a year and &lt;code&gt;immutable&lt;/code&gt;, give HTML &lt;code&gt;no-cache&lt;/code&gt; and a validator, and give public JSON a short &lt;code&gt;s-maxage&lt;/code&gt; with &lt;code&gt;stale-while-revalidate&lt;/code&gt;. Version URLs for invalidation, use purge only for mistakes, and monitor &lt;code&gt;Age&lt;/code&gt;, hit ratio, and origin traffic after every deploy. Verify with curl and DevTools: a CDN dashboard reports what the edge believes, not what the user received.&lt;/p&gt;




&lt;p&gt;If you want to inspect a real response, the &lt;a href="https://www.jsonmind.com/tools/http-headers" rel="noopener noreferrer"&gt;HTTP Headers Analyzer&lt;/a&gt; parses &lt;code&gt;Cache-Control&lt;/code&gt;, &lt;code&gt;ETag&lt;/code&gt;, &lt;code&gt;Vary&lt;/code&gt;, and &lt;code&gt;Age&lt;/code&gt;, and the &lt;a href="https://www.jsonmind.com/tools/http-request" rel="noopener noreferrer"&gt;HTTP Request Workbench&lt;/a&gt; lets you replay conditional requests without installing anything.&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>http</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
