Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle inconsistency in Next.js when using usePathname with custom prefixes, localePrefix: 'as-needed' and static rendering #1571

Merged
merged 3 commits into from
Nov 25, 2024

Conversation

amannn
Copy link
Owner

@amannn amannn commented Nov 22, 2024

Fixes #1568

Copy link

vercel bot commented Nov 22, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 6:41pm
next-intl-example-app-router ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 6:41pm
next-intl-example-app-router-without-i18n-routing ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 25, 2024 6:41pm

if (hasPathnamePrefixed(localeAsPrefix, pathname)) {
unlocalizedPathname = unprefixPathname(pathname, localeAsPrefix);
}
}
Copy link
Owner Author

@amannn amannn Nov 25, 2024

Choose a reason for hiding this comment

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

One rare case where this behavior might not be wanted is this:

const routing = defineRouting({
  locales: ['en'],
  defaultLocale: 'en',
  localePrefix: {
    mode: 'as-needed',
    prefixes: {
      en: '/uk'
    }
  }
});

… and then having a path like /[locale]/en/page.tsx.

In this case, when Next.js prerenders with a pathname set to /en/en, we end up with /en as the unprefixed pathname—that's fine. However, in production, the Next.js pathname will be /en after the rewrite and we'll unprefix this to /.

A potential workaround could be based on useParams(), useSelectedLayoutSegments() etc., but I believe a static path like /en can still not be detected due to only the locale segment being returned from these hooks. So what would be necessary is a hook that returns the actual, internal pathname, but Next.js currently doesn't support that.

So I think for the time being this is a tradeoff that we have to live with.

Therefore to summarize: If you're using a localePrefix mode of as-needed with custom prefixes, make sure you don't use the default locale as a pathname at the root (e.g. /uk/en is unsafe, /uk/english is fine).

@amannn amannn changed the title fix: Handle inconsistency in Next.js when using usePathname with custom prefixes and static rendering fix: Handle inconsistency in Next.js when using usePathname with custom prefixes, localePrefix: 'as-needed and static rendering Nov 25, 2024
@amannn amannn changed the title fix: Handle inconsistency in Next.js when using usePathname with custom prefixes, localePrefix: 'as-needed and static rendering fix: Handle inconsistency in Next.js when using usePathname with custom prefixes, localePrefix: 'as-needed' and static rendering Nov 25, 2024
@amannn amannn merged commit a205821 into canary Nov 25, 2024
9 of 10 checks passed
@amannn amannn deleted the fix/1568-usepathnamessg branch November 25, 2024 18:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Inconsistent behavior between dev and prod for link href
1 participant