From 1167523f01ed6363c3fe3bbb7aa925744eedd055 Mon Sep 17 00:00:00 2001 From: Jan Amann Date: Thu, 1 Feb 2024 09:30:09 +0100 Subject: [PATCH] fix: Keep cookie value in sync when navigating within a locale (#828) Fixes #826 --- packages/next-intl/package.json | 4 ++-- packages/next-intl/src/navigation/shared/syncLocaleCookie.tsx | 2 +- .../test/navigation/react-client/useBaseRouter.test.tsx | 3 +++ 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/next-intl/package.json b/packages/next-intl/package.json index dbfb060da..9063a38b4 100644 --- a/packages/next-intl/package.json +++ b/packages/next-intl/package.json @@ -122,11 +122,11 @@ }, { "path": "dist/production/navigation.react-client.js", - "limit": "2.83 KB" + "limit": "2.84 KB" }, { "path": "dist/production/navigation.react-server.js", - "limit": "2.94 KB" + "limit": "2.95 KB" }, { "path": "dist/production/server.react-client.js", diff --git a/packages/next-intl/src/navigation/shared/syncLocaleCookie.tsx b/packages/next-intl/src/navigation/shared/syncLocaleCookie.tsx index 8cebf57c2..9ca242d8a 100644 --- a/packages/next-intl/src/navigation/shared/syncLocaleCookie.tsx +++ b/packages/next-intl/src/navigation/shared/syncLocaleCookie.tsx @@ -14,7 +14,7 @@ export default function syncLocaleCookie( locale: string, nextLocale?: string ) { - const isSwitchingLocale = nextLocale !== locale; + const isSwitchingLocale = nextLocale !== locale && nextLocale != null; if ( !isSwitchingLocale || diff --git a/packages/next-intl/test/navigation/react-client/useBaseRouter.test.tsx b/packages/next-intl/test/navigation/react-client/useBaseRouter.test.tsx index 581f23cab..adae5051f 100644 --- a/packages/next-intl/test/navigation/react-client/useBaseRouter.test.tsx +++ b/packages/next-intl/test/navigation/react-client/useBaseRouter.test.tsx @@ -95,6 +95,9 @@ describe('unprefixed routing', () => { callRouter((router) => router.push('/about', {locale: 'de'})); expect(document.cookie).toContain('NEXT_LOCALE=de'); + callRouter((router) => router.push('/test')); + expect(document.cookie).toContain('NEXT_LOCALE=de'); + callRouter((router) => router.replace('/about', {locale: 'es'})); expect(document.cookie).toContain('NEXT_LOCALE=es');