Geo-based locale detection¶
We use Cloudflare IP Geolocation (cf.country) to route users to the correct locale. This happens across two separate layers that work independently of each other.
The two files and their roles¶
server/middleware/geo-locale.ts — Nitro level (routing)¶
Runs in Nitro, before Nuxt and nuxt-i18n start. Handles server-side redirects and cookie injection based on CF country.
Purpose: - Solves a timing problem where nuxt-i18n's detectBrowserLanguage uses the Accept-Language header (browser language) rather than the user's actual geographic country. - Ensures first-time visitors land directly on the correct locale without going through a client-side modal.
URL handling: - Uses getRequestURL(event) and splits pathname from search before any matching. In Nitro, getRequestPath() returns the raw request path including the query string, which would cause /?utm_source=google to miss the root check entirely. - For first-time visitors on root, we issue the redirect ourselves rather than injecting a cookie and relying on nuxt-i18n. nuxt-i18n's own root redirect does not reliably carry query parameters, which would silently drop UTM and other campaign tracking. Owning the redirect means we control the full target URL including search. - For returning visitors (existing locale cookie), we skip and let nuxt-i18n handle the root redirect. This is the same behaviour as before this middleware existed — UTM preservation on root for returning visitors depends on nuxt-i18n.
app/middleware/cf-country.global.server.ts — Nuxt level (state hydration)¶
Runs as a global Nuxt route middleware on the server. Sets useState("cfCountry") which is hydrated down to the client.
Purpose: - Makes the CF country available to useLocaleDetection on the client. - Acts as a fallback mechanism: shows a modal if the user is on the wrong locale anyway (e.g. returning visitors with an old, incorrect locale cookie).
The two files do not conflict. The Nitro middleware makes the routing decision early. The Nuxt middleware ensures the CF country is available for the subsequent client-side check.
Cookie hierarchy¶
| Cookie | Set by | Meaning |
|---|---|---|
user_selected_locale | useLocaleSwitcher.confirmSelection() | The user has manually confirmed their country. Always respected — no redirect, no modal. |
locale | nuxt-i18n automatically | Set on redirect from root (/). May be incorrect if set via Accept-Language detection rather than CF country. |
Scenarios¶
1. First visit — no cookies, correct CF country¶
User from Italy visits / or /?utm_source=google.
geo-locale.ts:
Parses pathname="/" (query string stripped before matching)
No locale cookie, no user_selected_locale
→ 302 redirect to /en-it/?utm_source=google
nuxt-i18n:
Receives /en-it/ with locale prefix → sets locale=en-it cookie
Result: User lands on /en-it/?utm_source=google ✅
Modal: not shown
2. First visit — no cookies, direct link to wrong locale¶
User from Italy clicks a link to /da-dk/products?ref=newsletter.
geo-locale.ts:
Parses pathname="/da-dk/products", search="?ref=newsletter"
No locale cookie, CF=IT, currentLocale=da-dk ≠ en-it
→ 302 redirect to /en-it/products?ref=newsletter
Result: User lands on /en-it/products?ref=newsletter ✅
Modal: not shown
3. Returning visitor — wrong locale cookie, not confirmed¶
User has locale=da-dk (set by old Accept-Language detection). No user_selected_locale. CF country is IT.
This applies whether they visit / or /da-dk/ directly — the locale cookie check runs before any redirect in both cases.
geo-locale.ts:
Sees locale cookie → skips redirect (respects returning visitors)
cf-country.global.server.ts:
Sets cfCountry="it" in state
Page renders as /da-dk/
Client (useLocaleDetection):
user_selected_locale? No → continue
cfCountry="it", currentRegion="dk" → mismatch
→ Shows locale selection modal
Result: User sees modal and can switch to /en-it/ ✅
4. Returning visitor — confirmed selection¶
User has locale=da-dk and user_selected_locale=true (confirmed via modal). CF country is IT (e.g. travelling abroad).
geo-locale.ts:
Sees user_selected_locale → skips everything
cf-country.global.server.ts:
Sets cfCountry="it" in state
Client (useLocaleDetection):
user_selected_locale? Yes → stops
Result: User stays on /da-dk/ ✅
Modal: not shown
5. CF country not in our mapping¶
User from the US visits /.
geo-locale.ts:
CF=US → not in COUNTRY_TO_LOCALE → skips everything
nuxt-i18n:
No locale cookie → Accept-Language detection → redirects to defaultLocale (da-dk)
cf-country.global.server.ts:
Sets cfCountry="us"
Client (useLocaleDetection):
cfCountry="us", currentRegion="dk" → mismatch
→ Shows locale selection modal
Result: Modal is shown, user can select manually ✅
6. CF country unavailable (local dev, proxy, etc.)¶
geo-locale.ts:
cfCountry=undefined → skips everything
nuxt-i18n:
Runs normal Accept-Language detection
cf-country.global.server.ts:
cfCountry=null → useState("cfCountry")=null
Client (useLocaleDetection):
cfCountry=null → stops, no modal
Result: nuxt-i18n's standard browser detection is used ✅
Testing in development¶
Use the query parameter ?cfcountry=IT to simulate a CF country in the dev environment. This overrides useState("cfCountry") in useLocaleDetection and triggers the modal flow without requiring a real CF header.
The Nitro middleware (geo-locale.ts) only responds to the real CF context — it is not affected by ?cfcountry.