amannn / next-intl

🌐 Internationalization (i18n) for Next.js
https://next-intl-docs.vercel.app
MIT License
2.58k stars 236 forks source link

`Link` references the wrong locale, and all `prefetch=true` causes errors when using `createNavigation`. #1462

Closed varmil closed 3 weeks ago

varmil commented 3 weeks ago

Description

OK

// routing.ts
export const { Link, getPathname, redirect, usePathname, useRouter } =
  createLocalizedPathnamesNavigation({
    locales: routing.locales,
    pathnames: { '/': '/' }
  })

---

// component.tsx
<Link href={`/${group}/channels/${basicInfo.id}`} prefetch={true}>
  ...
</Link>

NG

// routing.ts
export const { Link, getPathname, redirect, usePathname, useRouter } =
  createNavigation(routing)

---

// component.tsx
<Link href={`/${group}/channels/${basicInfo.id}`} prefetch={true}>
  ...
</Link>

Error Message

The `prefetch` prop is currently not supported when using the `locale` prop on `Link` to switch the locale.`

Hypothesis after code reading

Hypothesis after reading the code

When using createLocalizedPathnamesNavigation, the locale is ultimately set here. The same is true for the Client.

const finalLocale = locale || (await getLocale());

On the other hand, when using createNavigation, it appears that isChangingLocale is checked here without calculating finalLocale.

As a result, locale in createNavigation is probably undefined and isChangingLocale is always true.

versions

This is the version in which the bug was confirmed to occur.

"next-intl": "^3.23.3",
"next": "15.0.1",

Verifications

Mandatory reproduction URL

https://github.com/varmil/noxi/pull/626/files#diff-ee810f949a204706e7360b0af570aea479f0efb7103a3abaaceb8c0acfb8c9bdL5-R5

Reproduction description

Steps to reproduce:

  1. Clone the repo
  2. npm run dev in ./web dir. Go browser.
  3. See error on browser and server

Expected behaviour

Since I don't use a locale prop, we expect prefetch to proceed without error.

amannn commented 3 weeks ago

Oh you're absolutely right, thanks for the bug report and the great analysis! Should have a fix ready soon.