Skip to content

Commit

Permalink
fix(navigation): remove locale from path for "as-needed" usecase
Browse files Browse the repository at this point in the history
When localePrefix is set to "as-needed" we shouldn't add the locale to the Link component.
  • Loading branch information
blurrah committed Jan 22, 2024
1 parent f4c8843 commit 85a4f65
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/cool-gorillas-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/next-intl-custom-paths": minor
---

Fix Link component with as-needed localePrefix
4 changes: 4 additions & 0 deletions examples/next-intl/src/app/[locale]/page.tsx
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -15,6 +16,9 @@ export default function IndexPage({ params: { locale } }: Props) {
return (
<PageLayout title={t("title")}>
<p>Hello!</p>
<Link className="inline-block bg-white p-4 rounded-md" href="/pathnames">
Pathnames link
</Link>
<p className="max-w-[590px]">
{t.rich("description", {
code: (chunks) => (
Expand Down
9 changes: 3 additions & 6 deletions src/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down

0 comments on commit 85a4f65

Please sign in to comment.