Skip to content

Commit

Permalink
docs: Use defineRouting in routing docs consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn committed Oct 16, 2024
1 parent 0ea6000 commit cf8f95a
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions docs/pages/docs/routing.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const routing = defineRouting({
});
```

Depending on your routing needs, you may wish to consider further settings.
Depending on your routing needs, you may wish to consider further settings—see below.

<Details id="locales-unknown">
<summary>What if the locales aren't known at build time?</summary>
Expand Down Expand Up @@ -460,22 +460,20 @@ The middleware will [detect a matching locale](/docs/routing/middleware#locale-d

If you want to rely entirely on the URL to resolve the locale, you can set the `localeDetection` property to `false`. This will disable locale detection based on the `accept-language` header and a potentially existing cookie value from a previous visit.

```tsx filename="middleware.ts" {5}
import createMiddleware from 'next-intl/middleware';
import {routing} from './i18n/routing';
```tsx filename="routing.ts" {5}
import {defineRouting} from 'next-intl/routing';

export default createMiddleware(routing, {
export const routing = defineRouting({
// ...
localeDetection: false
});
```

In this case, only the locale prefix and a potentially [matching domain](#domain-based-routing) are used to determine the locale.

Note that by setting this option, the middleware will no longer return a `set-cookie` response header, which can be beneficial for CDN caching (see e.g. [the Cloudflare Cache rules for `set-cookie`](https://developers.cloudflare.com/cache/concepts/cache-behavior/#interaction-of-set-cookie-response-header-with-cache)).
In this case, only the locale prefix and a potentially [matching domain](#domains) are used to determine the locale.

### Locale cookie [#locale-cookie]

By default, the middleware will set a cookie called `NEXT_LOCALE` that contains the most recently detected locale. This is used to remember the user's locale preference for future requests (see [locale detection in the middleware](/docs/routing/middleware#locale-detection)).
By default, the middleware will set a 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 future requests.

By default, the cookie will be configured with the following attributes:

Expand All @@ -485,12 +483,13 @@ By default, the cookie will be configured with the following attributes:

If you have more specific requirements, you can adjust these settings accordingly:

```tsx filename="middleware.ts"
import createMiddleware from 'next-intl/middleware';
import {routing} from './i18n/routing';
```tsx filename="routing.ts"
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
// ...

export default createMiddleware(routing, {
// These options will be merged with the defaults
// Will be merged with the defaults
localeCookie: {
// Custom cookie name
name: 'USER_LOCALE',
Expand All @@ -502,16 +501,17 @@ export default createMiddleware(routing, {

… or turn the cookie off entirely:

```tsx filename="middleware.ts"
import createMiddleware from 'next-intl/middleware';
import {routing} from './i18n/routing';
```tsx filename="routing.ts"
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
// ...

export default createMiddleware(routing, {
localeCookie: false
});
```

Note that the cookie is only set once per detected locale and is not updated on every request.
Note that the cookie is only set when the user switches the locale and is not updated on every request.

### Alternate links [#alternate-links]

Expand All @@ -524,12 +524,13 @@ However, there are cases where you may want to provide these links yourself:

In this case, you can opt-out of this behavior by setting `alternateLinks` to `false`.

```tsx filename="middleware.ts" {5}
import createMiddleware from 'next-intl/middleware';
import {routing} from './i18n/routing';
```tsx filename="routing.ts"
import {defineRouting} from 'next-intl/routing';

export const routing = defineRouting({
// ...

export default createMiddleware(routing, {
alternateLinks: false // Defaults to `true`
alternateLinks: false
});
```

Expand Down

0 comments on commit cf8f95a

Please sign in to comment.