Performance and accessibility 3 min read Updated
CDN edge caching: Vary, cookies and safe purging
In short
An unconfigured CDN either misses HTML and XML sitemaps completely or accidentally caches another user’s session cookie. Understand cache keys, Vary headers and targeted purging before turning on edge caching.
Adding a CDN in front of an origin server does not automatically cache HTML pages, API endpoints or sitemap.xml. Worse, forcing aggressive edge caching without checking cookies and headers can leak a user’s CSRF token or serve a Czech redirect to an English visitor. Understanding how shared caches treat Cache-Control, Vary and cookies makes edge caching predictable and safe.
Why CDNs bypass HTML and XML by default
As documented in Cloudflare’s default cache behavior, edge networks typically cache static asset extensions (CSS, JS, images, fonts) automatically while bypassing HTML and XML routes unless a dedicated cache rule says otherwise. Even when your origin sends Cache-Control: public, max-age=3600 on /sitemap.xml or public articles, the CDN status stays DYNAMIC until an explicit rule allows edge caching for that path.
See HTTP caching for how browser and shared-cache directives work together.
Public edge rules vs. private and cookie-bearing routes
| Route type | Recommended origin header | CDN edge rule |
|---|---|---|
| Versioned static assets (app.v12.css) | Cache-Control: public, max-age=31536000, immutable | Cache by default; no purge needed when filenames are hashed |
| Public articles & sitemap.xml | Cache-Control: public, max-age=3600, stale-while-revalidate=60 | Eligible for edge cache; ensure no Set-Cookie is emitted |
| Language redirect on / | Vary: Accept-Language and short or no shared cache | Never cache one fixed 302 target for every visitor ignoring Accept-Language |
| Forms, CSRF endpoints & admin panels | Cache-Control: private, no-store | Bypass cache completely |
Never cache a response that includes a Set-Cookie header or user-specific HTML. If an edge node stores a page containing an active session cookie or form stamp, the next visitor receives the previous user’s state.
Using Vary and stale-while-revalidate properly
Two HTTP headers documented on MDN decide how well an edge cache behaves under real traffic:
- Vary: tells shared caches which request headers split the cache key. If root / redirects based on Accept-Language, omit shared caching or include Vary: Accept-Language; better yet, link directly to canonical language URLs (/en/, /cs/, /sk/) where HTML never varies by request headers.
- stale-while-revalidate: lets the edge serve a recently expired cached copy immediately while refreshing it asynchronously in the background—keeping Time to First Byte and LCP low during traffic spikes or slow origin queries.
Targeted purging after deploys
Purging an entire CDN zone on every deploy flushes unaffected assets and sends a thundering herd straight to your origin database. Instead:
- Put content hashes in CSS/JS filenames so static files carry one-year immutable caching and never require purging.
- On deploy, purge by exact hostnames and changed URLs (for example, updated article paths, index pages and /sitemap.xml) rather than clearing unrelated subdomains on the same account.
- Verify response headers (cf-cache-status / age / cache-control) in smoke checks right after deployment.
Edge cache rules vary between CDN providers, and free/pro plans sometimes ignore certain Vary headers unless explicitly enabled. Always test both cached anonymous requests and authenticated/form routes from outside your local network after changing CDN rules.
Tuning CDN rules or origin TTFB for a multi-language site? Share your setup through our enquiry form.
Common questions
- Why is a CDN returning DYNAMIC or MISS on public HTML or sitemap.xml?
- Many CDNs cache only static asset extensions (CSS, JS, images) by default and bypass HTML or XML unless explicit cache rules match the path and no Set-Cookie or Cache-Control: no-store header blocks caching.
- What happens if you cache a page that sets a session cookie?
- Caching a response containing Set-Cookie or personalised user state can leak one visitor’s CSRF token or session identifier to subsequent visitors. Strip cookies on public routes or mark personalised responses Cache-Control: private, no-store.
- How does Vary: Accept-Language work with CDN caches?
- Vary tells shared caches which request headers change the response. Redirects or pages negotiated by Accept-Language must send Vary: Accept-Language (or route to distinct language URLs like /en/ and /cs/) so one language is not served to everyone.
Sources
- Cache-Control header, MDN Web Docs. Accessed 24 November 2026
- Vary header, MDN Web Docs. Accessed 24 November 2026
- Default Cache Behavior, Cloudflare Docs. Accessed 24 November 2026
Further reading
-
How HTTP caching decides what is reused
Caching is safe to use when each response carries an explicit Cache-Control header that matches how often its content changes.
-
Fixing Largest Contentful Paint one delay at a time
Split the LCP delay into four parts, then fix the largest one first instead of guessing at image tweaks.
-
XML sitemaps that help search engines
An XML sitemap helps search engines find the pages you want considered, within the protocol limits of 50,000 URLs and 50 MB per file.
Have an idea that should exist? Or a working product that needs more customers, a new website or a complete rework? Bring us anything. Apply with your idea