-
-
Notifications
You must be signed in to change notification settings - Fork 236
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
feat: createNavigation
#1316
feat: createNavigation
#1316
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
packages/next-intl/src/navigation/shared/createSharedNavigationFns.tsx
Outdated
Show resolved
Hide resolved
As far as I understand, the prefix is important for the feature related to locale detection for "x-middleware-rewrite". Then I'm
then i went into the code and found the function ClientLink.js
utils.js
locale || defaultLocale - no chance 😀😀😀
UPD Before using next-intl - worked with custom i18n and encountered a similar problem, I solved it by off localeDetecting and reworked the function
some tests on the topic
to catch 404, added
|
Thanks for including a link to your Stack Overflow question! The reason why a prefix is rendered, is because you're linking to the default locale from a locale switcher (i.e. you're providing the Instead, in this case the user should navigate to Here's a test for the relevant implementation: next-intl/packages/next-intl/src/navigation/createNavigation.test.tsx Lines 643 to 651 in 6b20873
If you want to avoid this, you could for example implement a locale switcher based on a select and use Does this help? I should probably mention this in the docs … |
This PR provides a new
createNavigation
function that supersedes the previously available APIs:createSharedPathnamesNavigation
createLocalizedPathnamesNavigation
This new function is a reimplementation of the existing navigation functionality that unifies the API for both use cases and also fixes a few quirks in the previous APIs.
Usage
(see the updated navigation docs)
Improvements
Link
can now be composed seamlessly into another component with itshref
prop without having to add a generic type argument.getPathname
is now available for both shared as well as localized pathnames (fixes Managing canonical links #785)router.push
andredirect
now accept search params consistently via the object form (e.g.router.push({pathname: '/users', query: {sortBy: 'name'})
)—regardless of if you're using shared or localized pathnames. You can still userouter.push('/users?sortBy=name')
if you prefer though.localePrefix: 'as-needed'
, the initial render ofLink
now uses the correct pathname immediately during SSR (fixes #444). Previously, a prefix for the default locale was added during SSR and removed during hydration. Alsoredirect
now gets the final pathname right without having to add a superfluous prefix (fixes #1335). The only exception is when you uselocalePrefix: 'as-needed'
in combination withdomains
(see Special case: Usingdomains
withlocalePrefix: 'as-needed'
)createNavigation
weighs 2.98 kB (createSharedPathnamesNavigation
was 3.01 kB,createLocalizedPathnamesNavigation
was 3.27 kB)next-intl
for Next.js 15 to run without warnings.Migrating to
createNavigation
createNavigation
is generally considered a drop-in replacement, but a few changes might be necessary:createNavigation
is expected to receive your complete routing configuration. Ideally, you define this via thedefineRouting
function and pass the result tocreateNavigation
.createLocalizedPathnamesNavigation
and have composed theLink
with itshref
prop, you should no longer provide the genericPathname
type argument (see updated docs).redirect
, you now have to provide an explicit locale (even if it's just the current locale). The previously passed href (whether it was a string or an object) now needs to be wrapped in an object and assigned to thehref
prop. This change was necessary for an upcoming change in Next.js 15 whereheaders()
turns into a promise (see #1375 for details).getPathname
and have previously manually prepended a locale prefix, you should no longer do so—getPathname
now takes care of this depending on your routing strategy.localePrefix: 'as-needed'
anddomains
and you're usinggetPathname
, you now need to provide adomain
argument (see Special case: Usingdomains
withlocalePrefix: 'as-needed'
)