Skip to content

Commit

Permalink
fix: Fix forwarding of request headers in middleware (#269 by @ARochniak
Browse files Browse the repository at this point in the history
)

fix #266

---------

Co-authored-by: Jan Amann <jan@amann.work>
  • Loading branch information
ARochniak and amannn committed May 12, 2023
1 parent 731bc4c commit 4ecbab5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/next-intl/src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,13 @@ export default function createMiddleware(config: MiddlewareConfig) {
const hasUnknownHost = configWithDefaults.domains != null && !domain;

function getResponseInit() {
// Nothing yet
return undefined;
const responseInit = {
request: {
headers: request.headers
}
};

return responseInit;
}

function rewrite(url: string) {
Expand Down
30 changes: 28 additions & 2 deletions packages/next-intl/test/middleware/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ function createMockRequest(
pathnameWithSearch = '/',
locale = 'en',
host = 'http://localhost:3000',
localeCookieValue?: string
localeCookieValue?: string,
customHeaders?: HeadersInit
) {
const headers = new Headers({
'accept-language': `${locale};q=0.9,en;q=0.8`,
host: new URL(host).host,
...(localeCookieValue && {
cookie: `${COOKIE_LOCALE_NAME}=${localeCookieValue}`
})
}),
...customHeaders
});
const url = host + pathnameWithSearch;

Expand Down Expand Up @@ -184,6 +186,30 @@ describe('prefix-based routing', () => {
value: 'en'
});
});

it('retains request headers for the default locale', () => {
middleware(
createMockRequest('/', 'en', 'http://localhost:3000', undefined, {
'x-test': 'test'
})
);
expect(
MockedNextResponse.rewrite.mock.calls[0][1].request.headers.get(
'x-test'
)
).toBe('test');
});

it('retains request headers for secondary locales', () => {
middleware(
createMockRequest('/de', 'de', 'http://localhost:3000', undefined, {
'x-test': 'test'
})
);
expect(
MockedNextResponse.next.mock.calls[0][0].request.headers.get('x-test')
).toBe('test');
});
});

describe('localePrefix: as-needed, localeDetection: false', () => {
Expand Down

2 comments on commit 4ecbab5

@vercel
Copy link

@vercel vercel bot commented on 4ecbab5 May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

next-intl-docs – ./docs

next-intl-docs-next-intl.vercel.app
next-intl-docs.vercel.app
next-intl-docs-git-main-next-intl.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 4ecbab5 May 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

example-next-13-next-auth – ./examples/example-next-13-next-auth

example-next-13-next-auth-next-intl.vercel.app
example-next-13-next-auth-git-main-next-intl.vercel.app
example-next-13-next-auth.vercel.app

Please sign in to comment.