Skip to content

Commit

Permalink
fix: Don't set cookie on response if localeDetection: false (#654)
Browse files Browse the repository at this point in the history
Ref #609
  • Loading branch information
amannn committed Nov 22, 2023
1 parent 11a754e commit e85149c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<hr />

📣 [Support for Next.js 13 and the App Router has arrived →](https://next-intl-docs.vercel.app/docs/next-13)
📣 [Support for the App Router and Server Components has arrived →](https://next-intl-docs.vercel.app/docs/getting-started/app-router)

<hr />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type MiddlewareConfig<Locales extends AllLocales> =
/** Sets the `Link` response header to notify search engines about content in other languages (defaults to `true`). See https://developers.google.com/search/docs/specialty/international/localized-versions#http */
alternateLinks?: boolean;

/** By setting this to `false`, the `accept-language` header will no longer be used for locale detection. */
/** By setting this to `false`, the cookie as well as the `accept-language` header will no longer be used for locale detection. */
localeDetection?: boolean;

/** Maps internal pathnames to external ones which can be localized per locale. */
Expand Down
2 changes: 2 additions & 0 deletions packages/next-intl/src/middleware/middleware.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export default function createMiddleware<Locales extends AllLocales>(
);

const hasOutdatedCookie =
configWithDefaults.localeDetection &&
request.cookies.get(COOKIE_LOCALE_NAME)?.value !== locale;

const hasMatchedDefaultLocale = domain
? domain.defaultLocale === locale
: locale === configWithDefaults.defaultLocale;
Expand Down
7 changes: 7 additions & 0 deletions packages/next-intl/test/middleware/middleware.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,13 @@ describe('prefix-based routing', () => {
'http://localhost:3000/en'
);
});

it("doesn't set a cookie", () => {
const response = middleware(
createMockRequest('/', 'de', 'http://localhost:3000', undefined)
);
expect(response.cookies.getAll()).toEqual([]);
});
});

describe('localePrefix: always', () => {
Expand Down

2 comments on commit e85149c

@vercel
Copy link

@vercel vercel bot commented on e85149c Nov 22, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on e85149c Nov 22, 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.vercel.app
next-intl-docs-git-main-next-intl.vercel.app
next-intl-docs-next-intl.vercel.app

Please sign in to comment.