# locale routing agent served wrong language after CDN cache

## TL;DR

Fixes the routing agent serving cached pages in the wrong language. Vary the CDN cache key on the locale so each language caches separately. Use when users get a different language than requested. Not for locale detection bugs.

## The error

```text
User requested fr but received the cached en page; the CDN served the wrong language variant.
```

## Fix it

### Step 1: Confirm the cache served it

```bash
node -e "console.log('check the response headers for a cache hit on the wrong-locale request')"
```

Expected: You see a cache hit returning the wrong language.

### Step 2: Vary the cache key on locale

```bash
node -e "console.log('add the locale to the cache key or the Vary header')"
```

Expected: Each locale caches independently.

### Step 3: Purge and re-test

```bash
node -e "console.log('purge the CDN, request fr, confirm French content')"
```

Expected: The right language serves every time.

## When to use this

- Users receive pages in the wrong language
- The CDN caches one variant for all locales

## When NOT to use this

- The app detects the wrong locale, fix detection
- The translation is missing, check the strings

## Tool and version compatibility

- Any CDN with edge caching
- Next.js, Nuxt, and similar SSR frameworks

## Variant phrasings

### wrong language only for logged-out users

Logged-in pages bypass cache; anonymous pages do not. Fix the anonymous cache key.

### correct language but stale translations

The cache TTL outlives the translation deploy. Purge on deploy.

## Why it happens

A CDN cache keyed only on URL serves the first-cached language to everyone. Without the locale in the key or Vary header, language variants collapse into one cached copy.

## Edge cases

- Vary on Accept-Language explodes the cache; prefer the path prefix
- Purging per locale needs the locale-tagged URLs
- Service workers add a second cache layer with the same bug

## Provenance

Resolved from the public thread: https://vectle.com/posts/pst_jXwrmOEbD-zE3naPhFiAmg
