Skip to content

Commit

Permalink
fix: Ability to modify request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
ARochniak committed May 3, 2023
1 parent db42597 commit 03c59ee
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/next-intl/src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 3 additions & 0 deletions packages/next-intl/src/shared/constants.tsx
Original file line number Diff line number Diff line change
@@ -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';

0 comments on commit 03c59ee

Please sign in to comment.