import { internationalization, log, routing } from '@intlayer/config/built';
import { ROUTING_MODE } from '@intlayer/config/defaultValues';
import { getAppLogger } from '@intlayer/config/logger';
import {
formatProxyEnabledMessage,
getCanonicalPath,
getDomainHostname,
getDomainOrigin,
getInternalPath,
getLocaleFromDomain,
getRewriteRules,
isProxyStorageLocaleEnabled,
type LocaleDomainMap,
resolveLocalizedPath,
resolveProxyMode,
} from '@intlayer/core/localization';
import {
getLocaleFromStorageServer,
setLocaleInStorageServer,
} from '@intlayer/core/utils';
import type { Locale } from '@intlayer/types/allLocales';
import {
type NextFetchEvent,
type NextRequest,
NextResponse,
} from 'next/server';
import { localeDetector } from './localeDetector';
/**
* Controls whether locale detection occurs during Next.js prefetch requests
* - true: Detect and apply locale during prefetch
* - false: Use default locale during prefetch (recommended)
*
* This setting affects how Next.js handles locale prefetching:
*
* Example scenario:
* - User's browser language is 'fr'
* - Current page is /fr/about
* - Link prefetches /about
*
* With `detectLocaleOnPrefetchNoPrefix:true`
* - Prefetch detects 'fr' locale from browser
* - Redirects prefetch to /fr/about
*
* With `detectLocaleOnPrefetchNoPrefix:false` (default)
* - Prefetch uses default locale
* - Redirects prefetch to /en/about (assuming 'en' is default)
*
* When to use true:
* - Your app uses non-localized internal links (e.g. )
* - You want consistent locale detection behavior between regular and prefetch requests
*
* When to use false (default):
* - Your app uses locale-prefixed links (e.g. )
* - You want to optimize prefetching performance
* - You want to avoid potential redirect loops
*/
const DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX = false;
const { locales, defaultLocale } = internationalization ?? {};
const { basePath, mode, rewrite, domains, enableProxy } = routing ?? {};
// Resolved behaviour of the locale-routing proxy. `disabled` turns
// `intlayerProxy` into a pass-through so apps can handle routing themselves.
// The env var backing this is injected at build time so bundlers can tree-shake
// the guarded branches.
const proxyMode = resolveProxyMode(enableProxy);
// Next.js inlines NODE_ENV into every bundle, edge middleware included, so this
// is both reliable and statically eliminable. `next build` is the only command
// that injects `INTLAYER_ROUTING_ENABLE_PROXY`, meaning a dev server always
// reaches `resolveProxyMode` through the configuration value.
//
// Matched against `development` rather than "not production" on purpose: only
// `next dev` runs a dev server. Any other value (`test`, a custom staging env)
// has no dev server in play and must keep the full production behaviour.
const isDevServer = process.env.NODE_ENV === 'development';
// In auto mode, a dev server keeps locale routing URL-driven: the stored locale
// is not used as a redirect source, so a stale cookie cannot keep pulling every
// navigation to another locale while developing.
const canUseStorageLocale = isProxyStorageLocaleEnabled(proxyMode, isDevServer);
// Announce the proxy the way the Vite plugin does on `configureServer`. Next
// has no server-start hook for middleware, so this runs when the middleware
// module is first evaluated — at the first request `next dev` routes through
// it. Restricted to the dev server on purpose: in production this module is
// re-evaluated on every edge cold start, and the line would be pure noise.
if (isDevServer && proxyMode !== 'disabled') {
getAppLogger({ log })(formatProxyEnabledMessage(!canUseStorageLocale), {
level: 'info',
});
}
// Note: cookie names are resolved inside LocaleStorage based on configuration
// Derived flags from routing.mode
const effectiveMode = mode ?? ROUTING_MODE;
const noPrefix =
(!(
process.env.INTLAYER_ROUTING_MODE &&
process.env.INTLAYER_ROUTING_MODE !== 'no-prefix'
) &&
effectiveMode === 'no-prefix') ||
(!(
process.env.INTLAYER_ROUTING_MODE &&
process.env.INTLAYER_ROUTING_MODE !== 'search-params'
) &&
effectiveMode === 'search-params');
const prefixDefault =
!(
process.env.INTLAYER_ROUTING_MODE &&
process.env.INTLAYER_ROUTING_MODE !== 'prefix-all'
) && effectiveMode === 'prefix-all';
const internalPrefix = !noPrefix;
const rewriteRules =
process.env.INTLAYER_ROUTING_REWRITE_RULES !== 'false'
? getRewriteRules(rewrite, 'url')
: undefined;
/**
* Resolves the locale a localized ("pretty") path belongs to.
*
* A path such as `/a-propos` only exists because a rewrite rule maps it to the
* French locale, so it declares its locale just as explicitly as an `/fr`
* prefix would. Without this, a visitor whose `Accept-Language` (or stored
* locale) says `en` gets `/a-propos` canonicalized against the English rules
* only, which never matches — the proxy then treats it as an unlocalized path
* and redirects to `/en/a-propos`, a URL no locale owns.
*
* Only patterns that differ from their canonical form are considered: a rule
* whose localized path equals the canonical one (`en: '/about'`) carries no
* locale signal, and matching on it would hijack the default locale's
* unprefixed URLs.
*
* @param pathname - The request pathname, without any locale prefix.
* @returns The locale declared by the path, or `undefined` when it declares none.
*/
const getRewriteLocale = (pathname: string): Locale | undefined =>
rewriteRules
? (locales?.find(
(candidate) =>
getCanonicalPath(pathname, candidate as Locale, rewriteRules) !==
pathname
) as Locale | undefined)
: undefined;
/**
* Detects if the request is a prefetch request from Next.js.
*
* Next.js prefetch requests are identified by:
* - purpose: 'prefetch' (standard prefetch header)
* - next-router-prefetch: '1' (Next.js app-router prefetch)
*
* Note: `next-url` and `x-nextjs-data` are intentionally NOT used here.
* Both are also sent on real client-side navigations (RSC navigation
* requests and pages-router data requests respectively), so treating them
* as prefetch would force such navigations to the default locale instead
* of the user's stored locale.
*
* During prefetch, we should ignore cookie-based locale detection
* to prevent unwanted redirects when users are switching locales.
*
* @param request - The incoming Next.js request object.
* @returns - True if the request is a prefetch request, false otherwise.
*/
const isPrefetchRequest = (request: NextRequest): boolean => {
const purpose = request.headers.get('purpose');
const nextRouterPrefetch = request.headers.get('next-router-prefetch');
return purpose === 'prefetch' || nextRouterPrefetch === '1';
};
// Ensure locale is reflected in search params when routing mode is 'search-params'
const appendLocaleSearchIfNeeded = (
search: string | undefined,
locale: Locale
): string | undefined => {
if (
(process.env.INTLAYER_ROUTING_MODE &&
process.env.INTLAYER_ROUTING_MODE !== 'search-params') ||
effectiveMode !== 'search-params'
)
return search;
const params = new URLSearchParams(search ?? '');
params.set('locale', locale);
return `?${params.toString()}`;
};
/**
* Proxy that handles the internationalization layer
*
* Usage:
*
* ```ts
* // ./src/proxy.ts
*
* export { intlayerProxy as proxy } from '@intlayer/next/proxy';
*
* // applies this proxy only to files in the app directory
* export const config = {
* matcher: '/((?!api|static|.*\\..*|_next).*)',
* };
* ```
*
* Main proxy function for handling internationalization.
*
* @param request - The incoming Next.js request object.
* @param event - The Next.js fetch event (optional).
* @param response - The Next.js response object (optional).
* @returns - The response to be returned to the client.
*
*/
export const intlayerProxy = (
request: NextRequest,
_event?: NextFetchEvent,
_response?: NextResponse
): NextResponse => {
// When the proxy is disabled, pass the request through untouched.
if (proxyMode === 'disabled') {
return NextResponse.next();
}
const pathname = request.nextUrl.pathname;
const localLocale = getLocalLocale(request);
if (noPrefix) {
return handleNoPrefix(request, localLocale, pathname);
}
const pathLocale = getPathLocale(pathname);
// Domain routing: if the path locale is mapped to a different domain, redirect there.
// e.g. intlayer.org/zh/about → https://intlayer.zh/about
if (
process.env.INTLAYER_ROUTING_DOMAINS !== 'false' &&
pathLocale &&
domains
) {
const localeDomain = domains[pathLocale];
if (localeDomain) {
const domainHost = getDomainHostname(localeDomain);
if (domainHost !== request.nextUrl.hostname) {
const rawPath = pathname.slice(`/${pathLocale}`.length) || '/';
const targetOrigin = getDomainOrigin(localeDomain);
return NextResponse.redirect(
new URL(`${rawPath}${request.nextUrl.search}`, targetOrigin)
);
}
}
}
// Domain routing: if the current hostname is exclusively mapped to one locale,
// treat it as that locale's domain — no URL prefix needed.
// e.g. intlayer.zh/about → internally rewrite to /zh/about
if (process.env.INTLAYER_ROUTING_DOMAINS !== 'false' && !pathLocale) {
const domainLocale = getLocaleFromDomain(
request.nextUrl.hostname,
domains as LocaleDomainMap
);
if (domainLocale) {
const canonicalPath = getCanonicalPath(
pathname,
domainLocale,
rewriteRules
);
// Never emit a trailing slash (`/zh/`): Next.js trailing-slash
// normalisation would redirect it back and forth with this proxy.
const internalPath = getInternalPath(canonicalPath, domainLocale);
return rewriteUrl(
request,
internalPath + (request.nextUrl.search ?? ''),
domainLocale
);
}
}
return handlePrefix(request, localLocale, pathLocale, pathname);
};
/**
* Retrieves the locale from the request cookies if available and valid.
*
* Returns `undefined` when the stored locale is not allowed to drive locale
* resolution (auto mode on a dev server), which makes every caller fall through
* to `Accept-Language` detection and then the default locale.
*
* @param request - The incoming Next.js request object.
* @returns - The locale found in the cookies, or undefined if not found or invalid.
*/
const getLocalLocale = (request: NextRequest): Locale | undefined => {
if (!canUseStorageLocale) return undefined;
return getLocaleFromStorageServer({
getCookie: (name: string) => request.cookies.get(name)?.value ?? null,
getHeader: (name: string) => request.headers.get(name) ?? null,
});
};
/**
* Handles the case where URLs do not have locale prefixes.
*/
const handleNoPrefix = (
request: NextRequest,
localLocale: Locale | undefined,
pathname: string
): NextResponse => {
const pathLocale = getPathLocale(pathname);
if (pathLocale) {
const pathWithoutLocale = pathname.slice(`/${pathLocale}`.length) || '/';
const canonicalPath = getCanonicalPath(
pathWithoutLocale,
pathLocale,
rewriteRules
);
const search = appendLocaleSearchIfNeeded(
request.nextUrl.search,
pathLocale
);
const redirectPath = search
? `${canonicalPath}${search}`
: `${canonicalPath}${request.nextUrl.search ?? ''}`;
// Persist the explicitly-requested locale: stripping the prefix drops the
// only locale signal from the URL, so without this the follow-up request
// would fall back to cookie / Accept-Language detection and could resolve
// a different locale.
return redirectUrl(request, redirectPath, pathLocale);
}
if (
!(
process.env.INTLAYER_ROUTING_MODE &&
process.env.INTLAYER_ROUTING_MODE !== 'search-params'
) &&
effectiveMode === 'search-params'
) {
const existingSearchParams = new URLSearchParams(request.nextUrl.search);
const existingLocale = existingSearchParams.get('locale');
const isExistingValid = locales?.includes(existingLocale as Locale);
let locale = (localLocale ??
(isExistingValid ? (existingLocale as Locale) : undefined) ??
localeDetector?.(request) ??
defaultLocale) as Locale;
if (!locales?.includes(locale as Locale)) {
locale = defaultLocale as Locale;
}
const canonicalPath = getCanonicalPath(
pathname,
locale as Locale,
rewriteRules
);
if (existingLocale === locale) {
const internalPath = internalPrefix
? getInternalPath(canonicalPath, locale as Locale)
: canonicalPath;
const rewritePath = `${internalPath}${request.nextUrl.search ?? ''}`;
return rewriteUrl(request, rewritePath, locale as Locale);
}
const search = appendLocaleSearchIfNeeded(
request.nextUrl.search,
locale as Locale
);
// Use original pathname for redirect to preserve user's URL input, just adding params
const redirectPath = search
? `${pathname}${search}`
: `${pathname}${request.nextUrl.search ?? ''}`;
return redirectUrl(request, redirectPath);
}
// effectiveMode === 'no-prefix'
let locale = (localLocale ??
localeDetector?.(request) ??
defaultLocale) as Locale;
if (!locales?.includes(locale as Locale)) {
locale = defaultLocale as Locale;
}
// Without a prefix, a localized path is the only locale signal the URL
// carries, so it takes precedence over storage / `Accept-Language`.
locale = getRewriteLocale(pathname) ?? locale;
const canonicalPath = getCanonicalPath(
pathname,
locale as Locale,
rewriteRules
);
const internalPath = internalPrefix
? getInternalPath(canonicalPath, locale as Locale)
: canonicalPath;
const search = appendLocaleSearchIfNeeded(
request.nextUrl.search,
locale as Locale
);
const rewritePath = search
? `${internalPath}${search}`
: `${internalPath}${request.nextUrl.search ?? ''}`;
return rewriteUrl(request, rewritePath, locale as Locale);
};
/**
* Checks whether a pathname starts with the given locale as a full path
* segment (`/fr` or `/fr/...`). A bare `startsWith('/fr')` would also match
* unrelated paths like `/friends`, causing wrong prefix stripping and
* self-redirect loops.
*
* @param pathname - The pathname to test.
* @param locale - The locale to look for as the first path segment.
* @returns - True if the first path segment is exactly the locale.
*/
const hasLocaleSegmentPrefix = (pathname: string, locale: Locale): boolean =>
pathname === `/${locale}` || pathname.startsWith(`/${locale}/`);
/**
* Extracts the locale from the URL pathname if present.
*
* @param pathname - The pathname from the request URL.
* @returns - The locale found in the pathname, or undefined if not found.
*/
const getPathLocale = (pathname: string): Locale | undefined =>
(locales as Locale[] | undefined)?.find((locale) =>
hasLocaleSegmentPrefix(pathname, locale)
);
/**
* Handles the case where URLs have locale prefixes.
*
* @param request - The incoming Next.js request object.
* @param localLocale - The locale from the cookie.
* @param pathLocale - The locale extracted from the pathname.
* @param pathname - The pathname from the request URL.
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
* @returns - The response to be returned to the client.
*/
const handlePrefix = (
request: NextRequest,
localLocale: Locale | undefined,
pathLocale: Locale | undefined,
pathname: string
): NextResponse => {
if (!pathLocale) {
const isPrefetch = isPrefetchRequest(request);
if (isPrefetch && !DEFAULT_DETECT_LOCALE_ON_PREFETCH_NO_PREFIX) {
return handleMissingPathLocale(
request,
defaultLocale as Locale,
pathname
);
}
return handleMissingPathLocale(request, localLocale, pathname);
}
return handleExistingPathLocale(request, pathLocale, pathname);
};
/**
* Handles requests where the locale is missing from the URL pathname.
*
* @param request - The incoming Next.js request object.
* @param localLocale - The locale from the cookie.
* @param pathname - The pathname from the request URL.
* @param basePathTrailingSlash - Indicates if the basePath ends with a slash.
* @returns - The response to be returned to the client.
*/
const handleMissingPathLocale = (
request: NextRequest,
localLocale: Locale | undefined,
pathname: string
): NextResponse => {
let locale = (localLocale ??
localeDetector?.(request) ??
defaultLocale) as Locale;
if (!locales?.includes(locale as Locale)) {
locale = defaultLocale as Locale;
}
// A localized path names its own locale, and the URL always outranks the
// stored / negotiated one — otherwise /a-propos read as 'en' resolves to no
// rule and gets redirected to the ownerless /en/a-propos.
locale = getRewriteLocale(pathname) ?? locale;
// Resolve to canonical path.
// If user visits /a-propos (implied 'fr'), we resolve to /about
const canonicalPath = getCanonicalPath(pathname, locale, rewriteRules);
// Determine target localized path for redirection
// /about + 'fr' -> /a-propos
const { path: targetLocalizedPath } = resolveLocalizedPath(
canonicalPath,
locale,
rewriteRules
);
const newPath = constructPath(
locale,
targetLocalizedPath,
basePath as string,
appendLocaleSearchIfNeeded(request.nextUrl.search, locale)
);
// Never emit a trailing slash (`/en/` for canonicalPath `/`): Next.js
// trailing-slash normalisation would redirect it back and forth with this
// proxy. `getInternalPath` collapses the root path to `/${locale}`.
return prefixDefault || locale !== defaultLocale
? redirectUrl(request, newPath)
: rewriteUrl(
request,
internalPrefix ? getInternalPath(canonicalPath, locale) : canonicalPath,
locale
); // Rewrite must use Canonical
};
/**
* Handles requests where the locale exists in the URL pathname.
*
* @param request - The incoming Next.js request object.
* @param localLocale - The locale from the cookie.
* @param pathLocale - The locale extracted from the pathname.
* @param pathname - The pathname from the request URL.
* @returns - The response to be returned to the client.
*/
const handleExistingPathLocale = (
request: NextRequest,
pathLocale: Locale,
pathname: string
): NextResponse => {
const rawPath = pathname.slice(`/${pathLocale}`.length) || '/';
// 1. Identify the Canonical Path (Internal Next.js path)
// Ex: /a-propos (from URL) -> /about (Canonical)
const canonicalPath = getCanonicalPath(rawPath, pathLocale, rewriteRules);
// By skipping the forced localLocale check, we allow the explicit pathLocale
// to take precedence, which correctly updates the header/cookie when navigating.
// Rewrite Logic
// We must rewrite to the Next.js internal structure: /[locale]/[canonicalPath]
// Ex: Rewrite /fr/a-propos -> /fr/about
// 2. Redirect to localized path if needed (Canonical -> Localized)
// Ex: /fr/about -> /fr/a-propos
const { path: targetLocalizedPath, isRewritten } = resolveLocalizedPath(
canonicalPath,
pathLocale,
rewriteRules
);
if (isRewritten && targetLocalizedPath !== rawPath) {
const newPath = constructPath(
pathLocale,
targetLocalizedPath,
basePath as string,
appendLocaleSearchIfNeeded(request.nextUrl.search, pathLocale)
);
return redirectUrl(request, newPath);
}
// Never emit a trailing slash (`/fr/` for the bare `/fr` URL): rewriting
// `/fr` to `/fr/` makes Next.js issue a trailing-slash normalisation
// redirect back to `/fr`, which this proxy rewrites again — an infinite
// redirect loop. `getInternalPath` collapses the root path to `/${locale}`.
const internalUrl = internalPrefix
? getInternalPath(canonicalPath, pathLocale)
: canonicalPath;
// Only handle redirect if we are strictly managing default locale prefixing
// Fix: pass `canonicalPath` (the path *without* the locale prefix, e.g. /pricing)
// instead of `pathname` (the full path including prefix, e.g. /en/pricing).
// Previously this caused an infinite redirect loop in prefix-no-default mode
// because handleDefaultLocaleRedirect built the redirect target from its third
// argument, which reproduced the same URL on every response.
if (!prefixDefault && pathLocale === defaultLocale) {
return handleDefaultLocaleRedirect(request, pathLocale, canonicalPath);
}
const search = request.nextUrl.search;
return rewriteUrl(request, internalUrl + (search ?? ''), pathLocale);
};
/**
* Handles the scenario where the locale in the cookie does not match the locale in the URL pathname.
*
* @param request - The incoming Next.js request object.
* @param pathname - The pathname from the request URL.
* @param pathLocale - The locale extracted from the pathname.
* @param localLocale - The locale from the cookie.
* @param basePath - The base path of the application.
* @returns - The new URL path with the correct locale.
*/
// Function handleCookieLocaleMismatch was removed because the URL locale should take precedence over the stored locale.
/**
* The key fix for 404s without [locale] folders
*/
const handleDefaultLocaleRedirect = (
request: NextRequest,
pathLocale: Locale,
canonicalPath: string // Internal path (e.g. /about)
): NextResponse => {
// Always called with !prefixDefault && pathLocale === defaultLocale (pre-validated by caller).
// Redirect to strip the default-locale prefix from the URL.
const { path: targetLocalizedPath } = resolveLocalizedPath(
canonicalPath,
pathLocale,
rewriteRules
);
const basePathValue = (basePath as string) || '';
const basePathTrailingSlash = basePathValue.endsWith('/');
let finalPath = targetLocalizedPath;
if (finalPath.startsWith('/')) finalPath = finalPath.slice(1);
const fullPath = `${basePathValue}${basePathTrailingSlash ? '' : '/'}${finalPath}`;
const searchWithLocale = appendLocaleSearchIfNeeded(
request.nextUrl.search,
pathLocale
);
// Persist the explicitly-requested default locale. Stripping the prefix
// (e.g. /es → /) drops the only locale signal from the URL, so without this
// the follow-up request to the canonical path would fall back to
// Accept-Language detection and could resolve a different locale (e.g. /en).
return redirectUrl(
request,
fullPath + (searchWithLocale ?? request.nextUrl.search ?? ''),
pathLocale
);
};
/**
* Constructs a new path by combining the locale, path, basePath, and search parameters.
*
* @param locale - The locale to include in the path.
* @param path - The original path from the request.
* @param basePath - The base path of the application.
* @param [search] - The query string from the request URL (optional).
* @returns - The constructed new path.
*/
const constructPath = (
locale: Locale,
path: string,
basePath: string,
search?: string
): string => {
// Remove existing locale prefix from path if it was passed by mistake,
// though we usually pass localized paths here now.
const pathWithoutPrefix = hasLocaleSegmentPrefix(path, locale)
? path.slice(`/${locale}`.length) || '/'
: path;
if (
(!(
process.env.INTLAYER_ROUTING_MODE &&
process.env.INTLAYER_ROUTING_MODE !== 'no-prefix'
) &&
effectiveMode === 'no-prefix') ||
(!(
process.env.INTLAYER_ROUTING_MODE &&
process.env.INTLAYER_ROUTING_MODE !== 'search-params'
) &&
effectiveMode === 'search-params')
) {
// `search` is either undefined or already has a leading '?' (from
// appendLocaleSearchIfNeeded / request.nextUrl.search), so append as-is.
return `${pathWithoutPrefix}${search ?? ''}`;
}
// Prefix handling
const pathWithLocalePrefix = hasLocaleSegmentPrefix(path, locale)
? path
: `${locale}${path.startsWith('/') ? '' : '/'}${path}`;
const basePathValue = basePath || '';
const basePathTrailingSlash = basePathValue.endsWith('/');
const newPath = `${basePathValue}${basePathTrailingSlash ? '' : '/'}${pathWithLocalePrefix}`;
// Clean double slashes
const cleanPath = newPath.replace(/\/+/g, '/');
// Never emit a trailing slash (`/fr/` for the root path): the framework's
// trailing-slash normalisation would redirect it back and forth with this
// proxy, creating an infinite redirect loop.
return cleanPath !== '/' && cleanPath.endsWith('/')
? cleanPath.slice(0, -1)
: cleanPath;
};
/**
* This handles the internal path Next.js sees.
* To support optional [locale] folders, we need to decide if we
* keep the locale prefix or strip it.
*/
const rewriteUrl = (
request: NextRequest,
newPath: string,
locale: Locale
): NextResponse => {
const search = request.nextUrl.search;
// Next.js strips `basePath` from `request.nextUrl.pathname` before the
// middleware runs, so every path computed from it (e.g. `/en/about`) lacks
// the basePath prefix. When we pass that as an absolute path to `new URL`,
// it replaces the entire path after the origin, silently discarding the
// basePath (e.g. `new URL('/en/', 'http://host/weather/')` →
// `http://host/en/`). Prepending the configured basePath restores the
// correct mount-point so rewrites resolve under the app root.
const basePathValue = (basePath as string) || '';
const pathWithBase =
basePathValue && !newPath.startsWith(basePathValue)
? `${basePathValue}${newPath}`
: newPath;
const pathWithSearch =
search && !pathWithBase.includes('?')
? `${pathWithBase}${search}`
: pathWithBase;
const requestHeaders = new Headers(request.headers);
setLocaleInStorageServer(locale, {
setHeader: (name: string, value: string) => {
requestHeaders.set(name, value);
},
});
const targetUrl = new URL(pathWithSearch, request.url);
// If the target URL is exactly the current request URL,
// we just want to `next()` to avoid losing headers on a redundant rewrite.
const response =
targetUrl.href === request.nextUrl.href
? NextResponse.next({
request: {
headers: requestHeaders,
},
})
: NextResponse.rewrite(targetUrl, {
request: {
headers: requestHeaders,
},
});
setLocaleInStorageServer(locale, {
setHeader: (name: string, value: string) => {
response.headers.set(name, value);
},
});
return response;
};
/**
* Redirects the request to the new path.
*
* @param request - The incoming Next.js request object.
* @param newPath - The new path to redirect to.
* @param persistLocale - When provided, the locale is written to storage
* (cookie/header, per config) on the redirect response so the follow-up
* request resolves the same locale instead of re-running detection.
* @returns - The redirect response.
*/
const redirectUrl = (
request: NextRequest,
newPath: string,
persistLocale?: Locale
): NextResponse => {
const search = request.nextUrl.search;
const pathWithSearch =
search && !newPath.includes('?') ? `${newPath}${search}` : newPath;
const target = new URL(pathWithSearch, request.url);
// Prevent open redirect: if the resolved origin differs from the request
// origin, strip it back to a same-origin URL using only the path/search/hash.
const safeTarget =
target.origin === request.nextUrl.origin
? target
: new URL(
`${target.pathname}${target.search}${target.hash}`,
request.url
);
const response = NextResponse.redirect(safeTarget);
if (persistLocale) {
persistLocaleOnResponse(response, persistLocale);
}
return response;
};
/**
* Writes the resolved locale to the outgoing response's storage (cookie and/or
* header, according to `routing.storage`). Only the cookie survives a client
* redirect, so this is what carries an explicitly-selected locale across a
* prefix-stripping redirect. Enabled cookie/header targets are resolved by
* {@link setLocaleInStorageServer} from the config; disabled ones are no-ops.
*
* @param response - The outgoing Next.js response to attach storage to.
* @param locale - The locale to persist.
*/
const persistLocaleOnResponse = (
response: NextResponse,
locale: Locale
): void => {
setLocaleInStorageServer(locale, {
setCookieStore: (name, value, attributes) => {
response.cookies.set(name, value, {
path: attributes.path,
domain: attributes.domain,
expires:
typeof attributes.expires === 'number'
? new Date(attributes.expires)
: attributes.expires,
secure: attributes.secure,
sameSite: attributes.sameSite,
httpOnly: attributes.httpOnly,
});
},
setHeader: (name, value) => {
response.headers.set(name, value);
},
});
};