Skip to main content

How a request is served

An app declares its locales, its preferences and its flags once, in lib/indicators.ts.

Everything else follows from that declaration: the rewrites in next.config.ts, the params of the static build, the links, and what a layout reads.

What a request goes through

Take GET /login with a theme~dark cookie, from a browser in New York.

  1. The proxy matches only the site root, so it does not run.
  2. Redirects. /login is already canonical. /en/login would have been sent to /login, since the default locale stays out of public URLs.
  3. Rewrites. In beforeFiles, one small step at a time, the path becomes /en/theme~dark/tz~EST/login: the default locale, then two empty segments, then each matching cookie or header appends its pair. See Rewrites.
  4. The route. app/[locale]/[prefs]/[flags]/login/page.tsx renders, or rather was rendered at build time, because generateStaticParams listed this combination. The browser's URL is still /login.

The layout reads locale, prefs and flags from its params — not from cookies or headers, which would make it dynamic — and renders <html lang="en" data-theme="dark">.

The three segments

/[locale]/[prefs]/[flags]/...route
SegmentHoldsComes fromExample
[locale]one localethe URL, or negotiation at the rootfr-CA
[prefs]choices the user madecookiestheme~dark.tz~EST
[flags]facts about the requestheaderslang~fr.tz~EST

A segment with nothing in it is -. The pairs in a segment are sorted by key, which is what makes the path a stable cache key — see Paths.

A tree need not have all three. segments: ['locale'] gives app/[locale]/…, for a site whose pages are too heavy to copy per preference; its preferences are then expressed through stylesheets instead. A site in one language leaves locales out too, and segments: [] is a plain app/….

What the app has to do

What runs, and what does not

Every step above is routing: beforeFiles rewrites, redirects and headers in next.config.ts, which the routing layer applies before any page code, plus static files in public/. So a page's variant is decided with:

  • No inline script. The theme is data-theme in the HTML the browser receives and color-scheme in a stylesheet it loads before painting. A script is never in the path to the first frame, so there is no frame in the wrong state. A script would also see less than the routing layer does. A header added on the way to the server never reaches the browser; the country an edge network works out from the IP is an example. An HttpOnly cookie is hidden from scripts. A rewrite can match on either, and the page only ever sees the name of the value that matched.
  • No serverless function. Every variant is prerendered, or rendered once on its first request and static from then on. A stylesheet preference is a static file the request is rewritten to. A hard flag's page is a static page in another directory.
  • No middleware, with one exception: the proxy that negotiates the locale at the site root, matched on / and nothing else. A site in one language has no proxy at all. The Pages Router's i18n config did this in the routing layer — localeDetection redirected the root by Accept-Language, and a NEXT_LOCALE cookie overrode it, the same two inputs in the same order — but the App Router does not support it, and that is the proxy's only reason to exist. Nothing here stands in the way of it coming back; if it does, the proxy goes and the rest is unchanged.

Scripts do run after the page. usePref writes a cookie and refreshes, or fetches a stylesheet again, and what comes back is served the same way the page was.

What it does not do

It does not translate anything. The locale is a segment and a param; what an app does with it is its own business.

It does not read a cookie or header at render time, and nothing here calls cookies() or headers(). If a page needs a value that is not in the path, that value is not an indicator yet — declare it, and it becomes one.