You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/src/pages/docs/routing.mdx
+19-15
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ import Card from '@/components/Card';
3
3
importCardsfrom'@/components/Cards';
4
4
importCalloutfrom'@/components/Callout';
5
5
6
-
# Next.js internationalized routing
6
+
# Routing
7
7
8
8
<Callout>
9
9
Routing APIs are only needed when you're using [i18n
@@ -17,7 +17,7 @@ import Callout from '@/components/Callout';
17
17
18
18
This enables you to express your app in terms of APIs like `<Link href="/about">`, while aspects like the locale and user-facing pathnames are automatically handled behind the scenes (e.g. `/de/ueber-uns`).
19
19
20
-
## Define routing
20
+
## `defineRouting`
21
21
22
22
The routing configuration that is shared between the [middleware](/docs/routing/middleware) and [the navigation APIs](/docs/routing/navigation) can be defined with the `defineRouting` function.
23
23
@@ -46,11 +46,11 @@ Still, in case you're defining other routing config, make sure to keep them in s
46
46
47
47
</Details>
48
48
49
-
### Locale prefix
49
+
### `localePrefix`
50
50
51
51
By default, the pathnames of your app will be available under a prefix that matches your directory structure (e.g. `/en/about` → `app/[locale]/about/page.tsx`). You can however adapt the routing to optionally remove the prefix or customize it per locale by configuring the `localePrefix` setting.
52
52
53
-
#### Always use a locale prefix (default) [#locale-prefix-always]
1. If you use this routing strategy, make sure that your [middleware matcher](/docs/routing/middleware#matcher-config) detects unprefixed pathnames.
82
82
2. If a superfluous locale prefix like `/en/about` is requested, the middleware will automatically redirect to the unprefixed version `/about`. This can be helpful in case you're redirecting from another locale and you want to update a potential cookie value first (e.g. [`<Link />`](/docs/routing/navigation#link) relies on this mechanism).
83
83
84
-
#### Never use a locale prefix[#locale-prefix-never]
If you'd like to provide a locale to `next-intl`, e.g. based on user settings, you can consider setting up `next-intl`[without i18n routing](/docs/getting-started/app-router/without-i18n-routing). This way, you don't need to use the routing integration in the first place.
87
87
@@ -158,7 +158,7 @@ If you'd like to encode custom information in the locale, you can use arbitrary
158
158
159
159
</Details>
160
160
161
-
### Localized pathnames [#pathnames]
161
+
### `pathnames`[#pathnames]
162
162
163
163
Many apps choose to localize pathnames, especially when search engine optimization is relevant, e.g.:
164
164
@@ -312,7 +312,7 @@ Furthermore, in case you provide a locale switcher, it might require special car
312
312
313
313
</Details>
314
314
315
-
### Domains
315
+
### `domains`
316
316
317
317
If you want to serve your localized content based on different domains, you can provide a list of mappings between domains and locales via the `domains` setting.
### Turning off locale detection[#locale-detection]
424
+
### `localeDetection`[#locale-detection]
425
425
426
426
The middleware will [detect a matching locale](/docs/routing/middleware#locale-detection) based on your routing configuration & the incoming request and will either pass the request through for a matching locale or redirect to one that matches.
In this case, only the locale prefix and a potentially [matching domain](#domains) are used to determine the locale.
440
440
441
-
### Locale cookie [#locale-cookie]
441
+
### `localeCookie`
442
442
443
443
If a user changes the locale to a value that doesn't match the `accept-language` header, `next-intl` will set a session cookie called `NEXT_LOCALE` that contains the most recently detected locale. This is used to [remember the user's locale](/docs/routing/middleware#locale-detection) preference for subsequent requests.
444
444
445
445
By default, the cookie will be configured with the following attributes:
446
446
447
447
2.[**`sameSite`**](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#samesitesamesite-value): This value is set to `lax` so that the cookie can be set when coming from an external site.
448
-
3.[**`path`**](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#pathpath-value): This value is not set by default, but will use the value of your [`basePath`](#base-path) if configured.
448
+
3.[**`path`**](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie#pathpath-value): This value is not set by default, but will use the value of your [`basePath`](#basepath) if configured.
449
449
450
450
If you have more specific requirements, you can adjust these settings accordingly:
451
451
@@ -492,7 +492,7 @@ Please note that legal requirements may vary by region, so it's advisable to ver
492
492
493
493
</Details>
494
494
495
-
### Alternate links[#alternate-links]
495
+
### `alternateLinks`[#alternate-links]
496
496
497
497
The middleware automatically sets [the `link` header](https://developers.google.com/search/docs/specialty/international/localized-versions#http) to inform search engines that your content is available in different languages. Note that this automatically integrates with your routing strategy and will generate the correct links based on your configuration.
Apart from your routing configuration, `next-intl` will also incorporate settings from [`next.config.ts`](https://nextjs.org/docs/pages/api-reference/config/next-config-js).
565
+
566
+
### `basePath`
563
567
564
568
The `next-intl` middleware as well as [the navigation APIs](/docs/routing/navigation) will automatically pick up a [`basePath`](https://nextjs.org/docs/app/api-reference/next-config-js/basePath) that you might have configured in your `next.config.js`.
565
569
@@ -578,8 +582,8 @@ export const config = {
578
582
};
579
583
```
580
584
581
-
### Trailing slash
585
+
### `trailingSlash`
582
586
583
587
If you have [`trailingSlash`](https://nextjs.org/docs/app/api-reference/next-config-js/trailingSlash) set to `true` in your Next.js config, this setting will be taken into account by the middleware and the navigation APIs.
584
588
585
-
Note that if you're using [localized pathnames](#pathnames), your internal and external pathnames can be defined either with or without a trailing slash as they will be normalized internally.
589
+
Note that if you're using [`pathnames`](#pathnames), your internal and external pathnames can be defined either with or without a trailing slash as they will be normalized internally.
Copy file name to clipboardexpand all lines: docs/src/pages/docs/routing/middleware.mdx
+4-13
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,9 @@ import Details from '@/components/Details';
8
8
routing](/docs/getting-started/app-router).
9
9
</Callout>
10
10
11
-
The middleware receives a [`routing`](/docs/routing#define-routing) configuration and takes care of:
11
+
The middleware can be created via `createMiddleware`.
12
+
13
+
It receives a [`routing`](/docs/routing#define-routing) configuration and takes care of:
12
14
13
15
1. Locale negotiation
14
16
2. Applying relevant redirects & rewrites
@@ -45,7 +47,7 @@ In this case, the locale is detected based on these priorities:
45
47
3. A locale can be matched based on the [`accept-language` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language)
46
48
4. As a last resort, the `defaultLocale` is used
47
49
48
-
To change the locale, users can visit a prefixed route. This will take precedence over a previously matched locale that is saved in a cookie or the `accept-language` header and will update the previous cookie value.
50
+
To change the locale, users can visit a prefixed route. This will take precedence over a previously matched locale that is saved in a cookie or the `accept-language` header and will update a previous cookie value.
49
51
50
52
**Example workflow:**
51
53
@@ -92,17 +94,6 @@ Since the middleware is aware of all your domains, if a domain receives a reques
92
94
3. When the link is clicked, a request to `us.example.com/fr` is initiated.
93
95
4. The middleware recognizes that the user wants to switch to another domain and responds with a redirect to `ca.example.com/fr`.
94
96
95
-
<Detailsid="domain-matching">
96
-
<summary>How is the best-matching domain for a given locale detected?</summary>
97
-
98
-
The best-matching domain is detected based on these priorities:
99
-
100
-
1. Stay on the current domain if the locale is supported here
101
-
2. Use an alternative domain where the locale is configured as the `defaultLocale`
102
-
3. Use an alternative domain that supports the locale
103
-
104
-
</Details>
105
-
106
97
## Matcher config
107
98
108
99
The middleware is intended to only run on pages, not on arbitrary files that you serve independently of the user locale (e.g. `/favicon.ico`).
0 commit comments