Skip to content

Commit

Permalink
Refactor middleware (non-breaking)
Browse files Browse the repository at this point in the history
  • Loading branch information
amannn committed Aug 27, 2024
1 parent b1e118b commit 0d89722
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 212 deletions.
4 changes: 2 additions & 2 deletions docs/pages/docs/routing/middleware.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ With this, your domain config for this particular domain will be used.

Apart from the [`routing` configuration](/docs/routing#shared-configuration) that is shared with the [navigation APIs](/docs/routing/navigation), the middleware accepts a few additional options that can be used for customization.

### Turning off locale detection [#locale-detection-false]
### Turning off locale detection [#locale-detection]

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.

Expand Down Expand Up @@ -539,7 +539,7 @@ If you're using the [static export](https://nextjs.org/docs/app/building-your-ap
**Static export limitations:**

1. There's no default locale that can be used without a prefix (same as [`localePrefix: 'always'`](/docs/routing#locale-prefix-always))
2. The locale can't be negotiated at runtime (same as [`localeDetection: false`](#locale-detection-false))
2. The locale can't be negotiated at runtime (same as [`localeDetection: false`](#locale-detection))
3. You can't use [pathname localization](/docs/routing#pathnames)
4. This requires [static rendering](/docs/getting-started/app-router/with-i18n-routing#static-rendering)
5. You need to add a redirect for the root of the app
Expand Down
43 changes: 2 additions & 41 deletions packages/next-intl/src/middleware/config.tsx
Original file line number Diff line number Diff line change
@@ -1,48 +1,9 @@
import {
RoutingBaseConfigInput,
receiveLocalePrefixConfig
} from '../routing/config';
import {Locales, LocalePrefixConfigVerbose, Pathnames} from '../routing/types';

export type MiddlewareRoutingConfigInput<
AppLocales extends Locales,
AppPathnames extends Pathnames<AppLocales>
> = RoutingBaseConfigInput<AppLocales> & {
locales: AppLocales;
defaultLocale: AppLocales[number];

export type MiddlewareOptions = {
/** Sets the `Link` response header to notify search engines about content in other languages (defaults to `true`). See https://developers.google.com/search/docs/specialty/international/localized-versions#http */
alternateLinks?: boolean;

/** By setting this to `false`, the cookie as well as the `accept-language` header will no longer be used for locale detection. */
localeDetection?: boolean;

/** Maps internal pathnames to external ones which can be localized per locale. */
pathnames?: AppPathnames;
};

export type MiddlewareRoutingConfig<
AppLocales extends Locales,
AppPathnames extends Pathnames<AppLocales>
> = Omit<
MiddlewareRoutingConfigInput<AppLocales, AppPathnames>,
'alternateLinks' | 'localeDetection' | 'localePrefix'
> & {
alternateLinks: boolean;
localeDetection: boolean;
localePrefix: LocalePrefixConfigVerbose<AppLocales>;
};

export function receiveConfig<
AppLocales extends Locales,
AppPathnames extends Pathnames<AppLocales>
>(
input: MiddlewareRoutingConfigInput<AppLocales, AppPathnames>
): MiddlewareRoutingConfig<AppLocales, AppPathnames> {
return {
...input,
alternateLinks: input?.alternateLinks ?? true,
localeDetection: input?.localeDetection ?? true,
localePrefix: receiveLocalePrefixConfig(input?.localePrefix)
};
}
export type ResolvedMiddlewareOptions = Required<MiddlewareOptions>;
Loading

0 comments on commit 0d89722

Please sign in to comment.