From 85a4f65b98de721f46d7bb119a828ddb86aa75c7 Mon Sep 17 00:00:00 2001 From: Boris Besemer Date: Mon, 22 Jan 2024 16:12:18 +0100 Subject: [PATCH] fix(navigation): remove locale from path for "as-needed" usecase When localePrefix is set to "as-needed" we shouldn't add the locale to the Link component. --- .changeset/cool-gorillas-exercise.md | 5 +++++ examples/next-intl/src/app/[locale]/page.tsx | 4 ++++ src/navigation.tsx | 9 +++------ 3 files changed, 12 insertions(+), 6 deletions(-) create mode 100644 .changeset/cool-gorillas-exercise.md diff --git a/.changeset/cool-gorillas-exercise.md b/.changeset/cool-gorillas-exercise.md new file mode 100644 index 0000000..a66229c --- /dev/null +++ b/.changeset/cool-gorillas-exercise.md @@ -0,0 +1,5 @@ +--- +"@labdigital/next-intl-custom-paths": minor +--- + +Fix Link component with as-needed localePrefix diff --git a/examples/next-intl/src/app/[locale]/page.tsx b/examples/next-intl/src/app/[locale]/page.tsx index 560bed4..e46ed71 100644 --- a/examples/next-intl/src/app/[locale]/page.tsx +++ b/examples/next-intl/src/app/[locale]/page.tsx @@ -1,4 +1,5 @@ import PageLayout from "components/PageLayout"; +import { Link } from "navigation"; import { useTranslations } from "next-intl"; import { unstable_setRequestLocale } from "next-intl/server"; @@ -15,6 +16,9 @@ export default function IndexPage({ params: { locale } }: Props) { return (

Hello!

+ + Pathnames link +

{t.rich("description", { code: (chunks) => ( diff --git a/src/navigation.tsx b/src/navigation.tsx index 8a3098e..92dbf29 100644 --- a/src/navigation.tsx +++ b/src/navigation.tsx @@ -83,12 +83,9 @@ export function createLocalizedNavigation< // If the `localePrefixForRoot` is set to "as-needed" then we don't want to // include the locale path in the URL for the root page. So erase it - if ( - localePrefixForRoot === "as-needed" && - wantLocale === defaultLocale && - href === "/" - ) { - localePath = undefined; + // Also has the side usecase of not having the localePrefix on "always". + if (localePrefixForRoot === "as-needed" && wantLocale === defaultLocale) { + if (href === "/" || localePrefix !== "always") localePath = undefined; } return (