Conversation
**Summary** Adds a new [`defineRouting`](https://next-intl-docs-git-feat-v4-routing-next-intl.vercel.app/docs/routing#define-routing) API that returns a `routing` object that can be used to initialize the middleware as well as the navigation APIs. This provides type-safety for your i18n config with a single import and helps to ensure that your i18n config is in sync across your app. ```tsx // routing.ts import {defineRouting} from 'next-intl/routing'; export const routing = defineRouting({ locales: ['en-US', 'en-GB'], defaultLocale: 'en-US', localePrefix: { mode: 'always', prefixes: { 'en-US': '/us', 'en-GB': '/uk' } }, pathnames: { '/': '/', '/organization': { 'en-US': '/organization', 'en-GB': '/organisation' } } }); ``` ```tsx // middleware.ts import createMiddleware from 'next-intl/middleware'; import {routing} from './routing'; export default createMiddleware(routing); export const config = { // Match only internationalized pathnames matcher: ['/', '/(de|en)/:path*'] }; ``` ```tsx // navigation.ts import {createSharedPathnamesNavigation} from 'next-intl/navigation'; import {routing} from './routing'; export const {Link, redirect, usePathname, useRouter} = createSharedPathnamesNavigation(routing); ``` (in the docs we now suggest to create navigation APIs directly in `routing.ts`) **Changes** - feat: Add `defineRouting` API - feat: Allow to pass middleware options (ie. now everything that is not `routing`) as second param to `createMiddleware` - docs: Suggest using `src/routing.ts` instead of `src/navigation.ts` - docs: Suggest setting up navigation APIs in getting started docs - docs: Suggest setting up middleware before `i18n.ts` in getting started docs **TODO:** - [ ] More testing (via a prerelease) - [ ] Update bug repro app router (after release) --------- Co-authored-by: amannn <amannn@users.noreply.github.com>
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
amannn
marked this pull request as ready for review
August 28, 2024 14:41
defineRouting for easier i18n routing setup (#1294)defineRouting for easier i18n routing setup
juanforlizzi
pushed a commit
to juanforlizzi/next-intl
that referenced
this pull request
Jan 16, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a new
defineRoutingAPI that returns aroutingobject that can be used to initialize the middleware as well as the navigation APIs. This provides type-safety for your i18n config with a single import and helps to ensure that your i18n config is in sync across your app.While this change is purely additional, it's encouraged to adopt
defineRoutingin existing code bases and the docs have been updated to reflect this.Migration example: amannn/next-intl-bug-repro-app-router@0a00684
In case you're providing either
alternateLinksorlocaleDetectiontocreateMiddleware, these options can now be passed as a second argument:Changes
defineRoutingAPIrouting) as second param tocreateMiddlewaresrc/routing.tsinstead ofsrc/navigation.tsi18n.tsin getting started docs