Azure Front Door serves a stale pointer file for longer than max-age after you publish
Versioned bundles can be cached forever, but the small file that points at them (versions.json, a release index) has a fixed URL and changes on every publish. Behind Azure Front Door with Cache-Control: max-age=600 it can stay stale for well over 10 minutes, because Front Door caches through more than one layer, may serve expired copies, and keeps a separate copy for each compression variant. Give pointer files a short s-maxage (Front Door prefers it over max-age), and when you test, send the same Accept-Encoding: br the browser sends.
Verzované bundly se smí cachovat napořád, ale malý soubor, který na ně ukazuje (versions.json, index vydání), má pevnou adresu a mění se při každé publikaci. Za Azure Front Doorem s Cache-Control: max-age=600 může zůstat starý i přes 10 minut, protože Front Door cachuje ve víc vrstvách, smí vydat i prošlou kopii a pro každou variantu komprese drží vlastní kopii. Ukazatelům dej krátké s-maxage (Front Door ho upřednostní před max-age) a při testu posílej stejné Accept-Encoding: br jako prohlížeč.
Symptom
- You publish a new build and update the index. For 5–12 minutes the loader in the browser still gets the previous index, doesn't find the new version, and falls back to an older one.
curl https://cdn.example.com/app/index.jsonreturns the new file the whole time, so the CDN "looks fine".- The difference looks like it depends on the
Originheader (browser fetch vs. curl). It doesn't.
Cause
Response headers from Front Door tell the story:
Vary: Accept-Encoding
Cache-Control: max-age=600
X-Cache: TCP_HIT
X-Cache-Info: L1_T2
- TTL comes from the origin. Front Door takes the cache duration from
s-maxage, thenmax-age, thenExpires(Caching with Azure Front Door – Cache expiration). Withmax-age=600every cached copy lives 600 s from when it was stored. - Longer than one
max-age. We measured stale responses for up to ~12 minutes withmax-age=600. The responses carryX-Cache-Info: L1_T2, so they pass through more than one cache layer, and Microsoft states that Front Door "might be able to serve data from the cache even if the cached data has expired". Don't plan on stale data disappearing after exactlymax-age. - One copy per compression variant and per edge node. With
Vary: Accept-Encoding,br,gzipand uncompressed responses are cached separately. Browsers always ask forbr, so that variant is always warm and stale. A plaincurlasks for the uncompressed variant, which nobody requested recently. It getsTCP_MISS, goes to the origin, and returns fresh bytes.Originis not part of the cache key. fetch(url, { cache: 'no-cache' })doesn't help. It makes the browser revalidate with the edge, not with your origin. The edge answers from its own copy.
Fix
Give the files that change on every publish a short shared-cache TTL and keep the browser TTL:
Cache-Control: max-age=600, s-maxage=60
- On Azure Blob Storage (static website) this is the blob's
x-ms-blob-cache-controlproperty, so set it per file in your upload script: pointer files gets-maxage, versioned files keepmax-age. - Only the pointers need it. Versioned bundles and hashed chunks never change, and long caching is what you want there.
- Purging after every deploy is weaker. It can take up to 10 minutes to propagate, and the deploy identity needs permission to purge on the Front Door profile, on top of write access to storage.
no-cache/no-storefrom the origin turns Front Door caching off for that file completely. That works too, but every page load then reaches the origin.
On the client: if the loader finds that the version it wants is missing from the index, it should first fetch the index once more with a cache-busting query (?t=<now>; Front Door includes the query string in the cache key only if the route is configured that way). Only then should it fall back. And it shouldn't cache that fallback for longer than the edge TTL.
How to check
Compare X-Cache, ETag and Last-Modified the way a browser would see them:
curl -s -o /dev/null -D - -H "Accept-Encoding: br" https://cdn.example.com/app/index.json | grep -iE "x-cache|etag|last-modified|cache-control"
Without the Accept-Encoding header you are testing a different cached copy.