INNER CODE UNIT · TypeScript
persistLanguage
i18next/next-i18next · src/appRouter/proxy/index.ts:33
const persistLanguage = (req: NextRequest, response: NextResponse, lng: string) => {
if (!config.persistCookie) return
if (req.cookies.get(config.cookieName)?.value === lng) return
response.cookies.set(config.cookieName, lng, {
path: '/',
maxAge: config.cookieMaxAge,
sameSite: 'lax',
...config.cookieOptions,
})
}
return function middleware(req: NextRequest): NextResponse {
// nextUrl.pathname has Next's own basePath (next.config) stripped; String(nextUrl)
// adds it back. Redirect/rewrite targets are therefore built from nextUrl.clone(),
// never from `new URL(path, req.url)`, which would drop that basePath (and trailingSlash).
const { pathname } = req.nextUrl
// When basePath is set, only handle requests under that prefix