-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
Conversation
…stom prefixes and static rendering
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
if (hasPathnamePrefixed(localeAsPrefix, pathname)) { | ||
unlocalizedPathname = unprefixPathname(pathname, localeAsPrefix); | ||
} | ||
} |
There was a problem hiding this comment.
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).
…s and the default locale matching
usePathname
with custom prefixes and static renderingusePathname
with custom prefixes, localePrefix: 'as-needed
and static rendering
usePathname
with custom prefixes, localePrefix: 'as-needed
and static renderingusePathname
with custom prefixes, localePrefix: 'as-needed'
and static rendering
Fixes #1568