diff --git a/packages/next-intl/src/middleware/middleware.tsx b/packages/next-intl/src/middleware/middleware.tsx index c0af0facc..d44f318a7 100644 --- a/packages/next-intl/src/middleware/middleware.tsx +++ b/packages/next-intl/src/middleware/middleware.tsx @@ -1,5 +1,5 @@ import {NextRequest, NextResponse} from 'next/server'; -import {COOKIE_LOCALE_NAME} from '../shared/constants'; +import {COOKIE_LOCALE_NAME, HEADER_LOCALE_NAME} from '../shared/constants'; import MiddlewareConfig, { MiddlewareConfigWithDefaults } from './NextIntlMiddlewareConfig'; @@ -58,8 +58,16 @@ export default function createMiddleware(config: MiddlewareConfig) { const hasUnknownHost = configWithDefaults.domains != null && !domain; function getResponseInit() { - // Nothing yet - return undefined; + if (hasOutdatedCookie) { + request.headers.set(HEADER_LOCALE_NAME, locale); + } + const responseInit = { + request: { + headers: request.headers + } + }; + + return responseInit; } function rewrite(url: string) { diff --git a/packages/next-intl/src/shared/constants.tsx b/packages/next-intl/src/shared/constants.tsx index 745c0c6bf..c509bca9f 100644 --- a/packages/next-intl/src/shared/constants.tsx +++ b/packages/next-intl/src/shared/constants.tsx @@ -1,3 +1,6 @@ // Reuse the legacy cookie name // https://nextjs.org/docs/advanced-features/i18n-routing#leveraging-the-next_locale-cookie export const COOKIE_LOCALE_NAME = 'NEXT_LOCALE'; + +// Should take precedence over the cookie +export const HEADER_LOCALE_NAME = 'X-NEXT-INTL-LOCALE';