import { NextResponse, type NextRequest } from "next/server"; import { LOCALE_COOKIE } from "@/src/domain/locale"; import { routeRequest } from "@/src/domain/routes"; const PERMANENT = 308; const TEMPORARY = 307; /** * Picks the language of a request and maps the public addresses onto the app * directory. All decisions are made in src/domain/routes.ts; this only carries * them out. */ export function proxy(request: NextRequest) { const decision = routeRequest({ pathname: request.nextUrl.pathname, cookie: request.cookies.get(LOCALE_COOKIE)?.value, acceptLanguage: request.headers.get("accept-language"), }); const url = request.nextUrl.clone(); url.pathname = decision.to; const response = decision.kind === "redirect" ? NextResponse.redirect(url, decision.permanent ? PERMANENT : TEMPORARY) : NextResponse.rewrite(url); // The answer depends on the visitor's language: caches must keep one per language. if (decision.negotiated) { response.headers.append("Vary", "Accept-Language, Cookie"); } return response; } export const config = { // Pages only: not Next's own files, the analytics and error proxies, or files. matcher: ["/((?!_next/|stats/|errors/|.*\\..*).*)"], };