A German visitor opens your home page, the CDN stores the German HTML, and the next visitor from Brazil gets German too. Nothing in the logs looks wrong: the origin chose the right language for the first request, and the cache did exactly what it was told. The fix people usually reach for is Vary: Accept-Language, and that header alone is often ignored, too expensive, or both.
This guide is for engineers who ship localized web pages or API responses behind a CDN, a reverse proxy, or a framework route cache. It explains why the language leaks, which design removes most of the problem, how the major CDNs treat Vary: Accept-Language, and how to test the result by reading what the cache actually returns.
The short answer
- Give every language its own URL, such as
/de/pricing, for anything you want cached or indexed. A URL that already names the language needs no negotiation and noVary. - Negotiate language in one place only: the bare root redirect, or an API that truly must read
Accept-Language. - Make that redirect either uncacheable or correctly varied, and never let it be cached under the root URL alone.
- If a cached response must depend on
Accept-Language, normalize the header to your supported locales at the edge and key on that value, not on the raw header. - Avoid
Vary: Cookie. Read the cookie in an edge function and turn it into a redirect or a narrow derived header instead. - Return
Content-Languageon every localized response and test with alternating languages against the public URL.
Why a shared cache serves one language to everyone
A shared cache stores a response under a cache key, usually the method and URL. If the origin decides the language from something that is not in the key, the first answer for that URL wins. RFC 9110 defines Vary as the way an origin lists the request headers that influenced the choice, so a cache can store separate variants. MDN's Vary reference adds a detail that matters later: the same Vary value should be sent on every response for a URL, including 304 Not Modified.
Practitioner reports show how ordinary this failure is. A BookWyrm issue describes an about page cached in the wrong language. A Craft Blitz report traced cached pages in the wrong app language to cache warming. In Mozilla's addons tracker the team found Accept-Language hardcoded into the CDN cache policy, and the proposed change moves it to the origin request policy and relies on Vary instead. Backend teams hit the same thing on JSON: MentorsMind-Backend #1103 adds Vary: Accept-Language to localized endpoints, and Stargate #216 reports that the header was missing whenever the request carried no Accept-Language at all.
That last case is easy to miss. If the origin only adds Vary when the header is present, the first request without one produces a cacheable default-language response with no Vary, and every later visitor can receive it.
Put the language in the URL first
The cheapest cache key is one you already have. When the path carries the locale, /fr/docs/setup and /ja/docs/setup are different objects, the CDN needs no special configuration, and hit rates stay high. Framework routing already pushes in this direction: the Next.js internationalization guide negotiates the locale from Accept-Language in a proxy and redirects to a locale path, and the next-intl middleware resolves the locale from the URL prefix, then a cookie, then Accept-Language, then the default, and redirects / to a prefixed path such as /en.
There is also a search reason. Google's page on locale-adaptive pages says Googlebot crawls mostly from US IP addresses and sends requests without an Accept-Language header, and recommends separate locale URLs annotated with hreflang. Its multi-regional site guidance likewise recommends different URLs for each language version instead of cookies or browser settings. Googlebot sends no Accept-Language header, so a page that picks its language from that header is crawled and indexed in the default language only. If a single URL serves several languages, the cache problem and the indexing problem share one root cause, and fixing the URL fixes both.
Keep header negotiation for three places: the bare root, legacy URLs you cannot change, and APIs whose clients only send Accept-Language. Everything else should name its language.
Treat the root redirect as the riskiest response
The locale redirect on the bare root URL is the response most likely to leak, because it is small, cacheable, and decided by a header and a cookie that the cache key usually ignores. A 302 or 307 from / to /de/ looks harmless, so nobody adds caching headers to it. Permanent redirects are cacheable by default under HTTP rules, and a cache-everything rule on a CDN or framework will store a temporary one too. Once one is cached under /, every visitor goes to German until it expires.
Two reports show the pattern. In directwerk #441 the locale redirect was cacheable without a Vary header, and the proposed fix varies on both Accept-Language and Cookie. In astro-blocks #192 a cached / bypassed both the root locale redirect and the visitor's saved cookie preference, and the in-memory cache ignored Vary: Cookie. This is a synthesis of those reports plus the cache rules above, not a vendor statement: the root redirect combines the two inputs caches handle worst, and it sits on the most visited URL.
Pick one of these policies for / and write it down:
- Do not cache the redirect. Send
Cache-Control: private, no-storeon the redirect only. The locale pages behind it stay fully cacheable, so the cost is one uncached round trip on the first visit. - Decide at the edge. Run the negotiation in an edge function or CDN rule that never reaches the shared cache, then redirect. This keeps the redirect fast without storing it.
- Cache it per normalized locale. Only if your CDN can key on a normalized language value (covered below), cache the redirect with that value in the key.
A minimal edge redirect in a Next.js style proxy looks like this:
import { match } from "@formatjs/intl-localematcher";
import Negotiator from "negotiator";
import { NextResponse, type NextRequest } from "next/server";
const locales = ["en", "de", "pt-BR", "pt-PT", "ja"];
const defaultLocale = "en";
export function proxy(request: NextRequest) {
if (request.nextUrl.pathname !== "/") return NextResponse.next();
const saved = request.cookies.get("locale")?.value;
const headers = { "accept-language": request.headers.get("accept-language") ?? "" };
const requested = new Negotiator({ headers }).languages();
const locale = saved && locales.includes(saved)
? saved
: match(requested, locales, defaultLocale);
const response = NextResponse.redirect(new URL(`/${locale}`, request.url), 307);
response.headers.set("Cache-Control", "private, no-store");
response.headers.set("Vary", "Accept-Language, Cookie");
return response;
}
no-store keeps the redirect out of shared caches. The Vary line records the inputs in case some cache in front of you stores it anyway.
Know what your CDN does with Vary
The header means the same thing everywhere, but CDNs treat it differently, and those differences decide whether a leak happens.
Cloudflare. Its Cache-Control documentation states that by default Cloudflare does not consider Vary values in caching decisions, with exceptions for images, accept-encoding, and Cache Rules that turn the setting on. A July 2026 changelog entry introduced that Cache Rules setting with normalize, passthrough, and bypass actions. Cloudflare's Vary concept page explains what normalize does to Accept-Language: it lowercases values, orders them by quality, strips parameters, and reduces region variants such as en-US to en unless the region appears in an optional languages list. If normalization leaves nothing, the header is removed from the request sent to the origin.
Cloudflare's normalize action drops region subtags unless you list them in languages, so pt-BR and pt-PT, or zh-TW and zh-CN, can share one cache entry. Teams that localize Brazilian and European Portuguese separately, or Traditional and Simplified Chinese, need to list those exact tags. Then confirm that the origin chooses its language from the same normalized value, because an origin that reads the raw header can still pick pt-BR for a request whose cache key says pt. Enterprise plans can also put the header in a custom cache key, where accept-language is a restricted header that requires a short list of specific values.
Amazon CloudFront. The header caching guide says to include Accept-Language in the cache key if you want separate cached objects per language, and the cache policy documentation explains that headers in the cache key are also forwarded to the origin. A Stack Overflow question with about 2,500 views shows the common gap: CloudFront was not forwarding Accept-Language to the origin, so the origin had nothing to choose a language from.
Fastly. Fastly honors Vary, and its Vary best practices post warns that varying on a raw, unnormalized header gives a vanishingly small chance of a cache hit, then recommends normalizing first. Its VCL function accept.language_lookup implements RFC 4647 lookup against a list of supported languages and a default, which is exactly the normalization step a language key needs.
Vercel. The Vercel CDN cache docs say Vary values combine with the cache key, list Accept-Language as a content negotiation use case, and advise choosing the narrowest header. They also say a Vary that names a high-cardinality header such as Cookie means no cache entry is written.
So the same Vary line can leak on one CDN, barely hit on another, and not cache at all on a third. Configure the cache key explicitly for the CDN you run.
Normalize before you key, and key what the origin used
Raw Accept-Language values are long and varied. One browser sends de-DE,de;q=0.9,en;q=0.8, another de,en-US;q=0.7. Varying on the raw string creates a separate entry for each combination, which is why givefood2 #39 measured Vary: Accept-Language multiplying edge cache objects about four times, with cache misses far slower than hits. Without normalization you trade a correctness bug for a performance bug.
The fix is a two-step contract:
- At the edge, map the request header to one of your supported locales using a defined rule, such as RFC 4647 lookup, and put the result in the cache key or in a derived header like
X-Locale. - At the origin, choose the language from that same value, not from a second parse of the raw header.
When the two sides disagree, the cache key says one language and the body is another. The How HTTP Works content negotiation guide makes the same point from the testing side: check the body, not just the headers, because a normalized key only helps if it matches what the origin returned. Hand-written parsers make this worse. hypha #593 describes code that read only the first two letters of the header, ignored quality values, and sent no Vary.
If you also return Content-Language, which MDN describes as the language intended for the audience of the response, you get a cheap way to compare key and body during testing.
Do not vary on Cookie
A saved language preference usually lives in a cookie, so Vary: Cookie looks natural. In practice the cookie header carries session IDs, analytics, and consent values, so every visitor has a different value. Vercel stops caching entirely in that case, some caches ignore it, and others store one entry per visitor. Zeolite #346 reports a cache that honored Vary only for Cookie, and vouchington-platform #495 reports Accept-Language listed in Vary while the cache keys were not partitioned, so one locale was served to everyone.
Use the cookie to choose a URL instead. On /, read the cookie in the edge redirect. On a locale URL, the path already wins, and the cookie only matters when the visitor switches language, which is a navigation to a new URL. Our language selector guide covers the interface side of switching; on the web, the switch itself should navigate to the new locale URL and update the cookie for the next visit to /.
APIs that must read Accept-Language
Some APIs cannot change their URLs because mobile apps already send Accept-Language on every call. For those:
- Send
Vary: Accept-Languageon every response, including the default-language response and304 Not Modified. MixedVaryvalues let an old variant win. - Normalize at the edge, as above, or mark responses
privateif you cannot. - Return
Content-Languageso clients and logs can see which language was served. - Give each language variant its own
ETag. A 2026 Stack Overflow question asks how validators should behave when language variants share one; our recommendation is to keep them distinct, because a sharedETaglets a conditional request confirm the wrong language as fresh.
If a translation is missing and you fall back, the response should still say which language it is in. The fallback rules in our guide to fallback languages apply here: a cached fallback should carry the fallback language in Content-Language, not the requested one.
Decision table
| Situation | Recommended approach |
|---|---|
| Marketing pages, docs, app shells | Locale in the path, no Vary on language |
Bare root / |
Edge redirect with no-store, or a normalized locale key |
| Saved preference in a cookie | Read it in the redirect; never Vary: Cookie |
| Legacy single-URL pages | Normalized Accept-Language key, matching origin logic |
API for clients that send only Accept-Language |
Vary on every response, separate ETag, Content-Language |
Regional variants such as pt-BR and pt-PT |
List them explicitly in the CDN normalization config |
Test the cache, not the origin
Most of these bugs pass local testing because no cache sits between the browser and the dev server. Test against the public hostname, and read the body. Set SITE to your public origin, including the scheme, before running these requests:
curl -s -H "Accept-Language: de-DE,de;q=0.9" "$SITE"/ -o /dev/null -D - | grep -i -E "location|cache|vary|content-language"
curl -s -H "Accept-Language: pt-PT" "$SITE"/ -o /dev/null -D - | grep -i -E "location|cache|vary|content-language"
curl -s "$SITE"/ -o /dev/null -D - | grep -i -E "location|cache|vary|content-language"
curl -s -H "Accept-Language: ja" "$SITE"/api/plans | head -c 300
curl -s -H "Accept-Language: en" "$SITE"/api/plans | head -c 300
Check four things in order:
- Alternate languages within the cache lifetime. Request German, then Portuguese, then no header, and confirm each response points to or contains the right language. A cache hit header on the second request with German content means the key is wrong.
- Send no Accept-Language. This mimics Googlebot and the case Stargate #216 describes. The response should still carry consistent
Varyand cache headers. - Test the regional pairs you ship.
pt-BRagainstpt-PT,zh-TWagainstzh-CN. This is where normalization collapses variants. - Compare
Content-Languageto the body. If the header sayspt-PTand the text is Brazilian, the edge and origin are using different matching rules.
After any change to cache keys, purge the affected URLs. Old entries stored under the previous key keep serving until they expire, which makes a correct fix look broken.
Next step
Run the five requests above against your production root URL and one localized API endpoint today, with two languages inside the cache lifetime. If the second language comes back from the cache with the first language's content, move that page to a locale path or add a normalized locale key on your CDN, mark the root redirect no-store, and purge before you test again.
