Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(RSC): Always read header in RSC render #506

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions packages/next-intl/src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,9 @@ export default function createMiddleware<Locales extends AllLocales>(
const hasUnknownHost = configWithDefaults.domains != null && !domain;

function getResponseInit() {
const responseInit = {
request: {
headers: request.headers
}
};

if (hasOutdatedCookie) {
responseInit.request.headers = new Headers(
responseInit.request.headers
);
responseInit.request.headers.set(HEADER_LOCALE_NAME, locale);
}

return responseInit;
const headers = new Headers(request.headers);
headers.set(HEADER_LOCALE_NAME, locale);
return {request: {headers}};
}

function rewrite(url: string) {
Expand Down
13 changes: 3 additions & 10 deletions packages/next-intl/src/server/getLocaleFromHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import {cookies, headers} from 'next/headers';
import {headers} from 'next/headers';
import {cache} from 'react';
import {COOKIE_LOCALE_NAME, HEADER_LOCALE_NAME} from '../shared/constants';
import {HEADER_LOCALE_NAME} from '../shared/constants';

const getLocaleFromHeader = cache(() => {
let locale;

try {
// A header is only set when we're changing the locale,
// otherwise we reuse an existing one from the cookie.
const requestHeaders = headers();
if (requestHeaders.has(HEADER_LOCALE_NAME)) {
locale = requestHeaders.get(HEADER_LOCALE_NAME);
} else {
locale = cookies().get(COOKIE_LOCALE_NAME)?.value;
}
locale = headers().get(HEADER_LOCALE_NAME);
} catch (error) {
if (
process.env.NODE_ENV !== 'production' &&
Expand Down
10 changes: 10 additions & 0 deletions packages/next-intl/test/middleware/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,16 @@ describe('prefix-based routing', () => {
);
});

it('always provides the locale via a request header, even if a cookie exists with the correct value (see https://github.com/amannn/next-intl/discussions/446)', () => {
middleware(createMockRequest('/', 'en', 'http://localhost:3000', 'en'));
expect(MockedNextResponse.rewrite).toHaveBeenCalled();
expect(
MockedNextResponse.rewrite.mock.calls[0][1]?.request?.headers?.get(
'x-next-intl-locale'
)
).toBe('en');
});

describe('localized pathnames', () => {
const middlewareWithPathnames = createIntlMiddleware({
defaultLocale: 'en',
Expand Down
Loading