Locale redirects and Proxy
What you need to know
A locale redirect sends a visitor from a known unlocalized entry URL to a supported language-specific path. It should have a fixed destination policy and a terminating condition.
By the end, you can: validate supported locale; avoid redirect loops.
Redirect only the library entry point
import { NextResponse, type NextRequest } from 'next/server';export function proxy(request: NextRequest) { if (!['GET', 'HEAD'].includes(request.method)) return NextResponse.next(); const preferred = request.cookies.get('reading-locale')?.value; const destination = preferred === 'en-GB' ? '/en-GB/library' : '/en/library'; const response = NextResponse.redirect(new URL(destination, request.url), 307); response.headers.set("Cache-Control", "private, no-store"); return response;}export const config = { matcher: ['/library'] };Expected behavior: GET /library with the exact preference en-GB should redirect to /en-GB/library; other preferences go to /en/library. Query parameters are deliberately discarded.
How it works
Use a small allowlist instead of treating a cookie, header or query parameter as a URL. This English-only teaching fixture uses en and en-GB as hypothetical English language/region variants with identical content, not additional ByteMind lesson locales. Match only /library and redirect to one of two explicit destinations; those destinations are not matched again. The cookie is a display preference, not evidence of identity. Restrict the example to GET and HEAD so an unrelated mutation is not redirected with method preservation. Construct the URL using the request URL in a trusted loopback deployment. In production review allowed hosts and forwarded-header configuration; do not infer a trusted origin from arbitrary client headers. No need to parse Accept-Language for this first policy; real negotiation would need quality weights and fallback rules. A temporary 307 is appropriate for preference-dependent selection. A permanent redirect would encourage caching a preference that may change. The example sends private, no-store to prevent storing a cookie-dependent redirect; verify that the hosting cache honors this policy. A 307 alone is not a cache-isolation guarantee.
Check the termination condition
GET /library, preference absent -> 307 /en/libraryGET /library, preference en-GB -> 307 /en-GB/libraryGET /en/library -> no redirectPOST /library -> continueExpected behavior: After one redirect, the target is outside the matcher. No user-controlled redirect target is accepted.
Common mistake
Do not prepend a locale to every path on every visit; that can generate repeated redirects. Avoid using the cookie value directly as a destination or letting a locale decision stand in for access control.
Practice and verification
Extend the test matrix with missing preference, unsupported preference and a direct visit to each destination. Keep the destination allowlist fixed and prove that the second request continues.
Scope: Proposed Next.js 16.x, React 19, Node runtime. Root-mounted Pages and mixed-router fixtures with Cache Components disabled; the basePath example is separate. Framework examples are staged, not executed. Exact patched dependencies and platform contracts pending.