cloudflare-next-intl 0.2.0 → 0.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -9,10 +9,10 @@ export declare const localesSet: Set<string>;
|
|
|
9
9
|
* @param request The incoming request (pass through from your `middleware.ts`).
|
|
10
10
|
* @param options.middlewareHandler Your own logic (auth, feature flags, etc.),
|
|
11
11
|
* run alongside locale routing — see {@link MiddlewareCustomHandler} for the
|
|
12
|
-
* full contract (
|
|
13
|
-
* @param options.runHandlerOnRedirect By default, `middlewareHandler`
|
|
14
|
-
*
|
|
15
|
-
*
|
|
12
|
+
* full contract (`rewriteUrl` / `redirectUrl` and what to return).
|
|
13
|
+
* @param options.runHandlerOnRedirect By default, `middlewareHandler` does
|
|
14
|
+
* NOT run for the locale-redirect case (so it never receives a
|
|
15
|
+
* `redirectUrl`). Set to `true` to also run it on redirects.
|
|
16
16
|
* Defaults to `false`.
|
|
17
17
|
*/
|
|
18
18
|
export default function intlMiddleware(request: NextRequest, options?: {
|
|
@@ -26,10 +26,10 @@ export const localesSet = new Set(config.locales);
|
|
|
26
26
|
* @param request The incoming request (pass through from your `middleware.ts`).
|
|
27
27
|
* @param options.middlewareHandler Your own logic (auth, feature flags, etc.),
|
|
28
28
|
* run alongside locale routing — see {@link MiddlewareCustomHandler} for the
|
|
29
|
-
* full contract (
|
|
30
|
-
* @param options.runHandlerOnRedirect By default, `middlewareHandler`
|
|
31
|
-
*
|
|
32
|
-
*
|
|
29
|
+
* full contract (`rewriteUrl` / `redirectUrl` and what to return).
|
|
30
|
+
* @param options.runHandlerOnRedirect By default, `middlewareHandler` does
|
|
31
|
+
* NOT run for the locale-redirect case (so it never receives a
|
|
32
|
+
* `redirectUrl`). Set to `true` to also run it on redirects.
|
|
33
33
|
* Defaults to `false`.
|
|
34
34
|
*/
|
|
35
35
|
export default async function intlMiddleware(request, options) {
|
|
@@ -67,16 +67,19 @@ export default async function intlMiddleware(request, options) {
|
|
|
67
67
|
const effectiveLocaleForRequest = urlLocale ?? initialChosenLocale;
|
|
68
68
|
let response;
|
|
69
69
|
let isRedirect = false;
|
|
70
|
-
let
|
|
70
|
+
let rewriteUrl;
|
|
71
|
+
let redirectUrl;
|
|
71
72
|
if (!urlLocale) {
|
|
72
73
|
const targetPath = `/${effectiveLocaleForRequest}${pathWithoutLocale === '/' ? '' : pathWithoutLocale}`;
|
|
73
|
-
|
|
74
|
+
const localeUrl = new URL(`${targetPath}${search}${hash}`, request.url);
|
|
74
75
|
if (initialChosenLocale === config.defaultLocale) {
|
|
75
|
-
|
|
76
|
+
rewriteUrl = localeUrl;
|
|
77
|
+
response = NextResponse.rewrite(localeUrl, { request });
|
|
76
78
|
}
|
|
77
79
|
else {
|
|
78
80
|
isRedirect = true;
|
|
79
|
-
|
|
81
|
+
redirectUrl = localeUrl;
|
|
82
|
+
response = NextResponse.redirect(localeUrl, request);
|
|
80
83
|
}
|
|
81
84
|
}
|
|
82
85
|
else {
|
|
@@ -85,7 +88,7 @@ export default async function intlMiddleware(request, options) {
|
|
|
85
88
|
});
|
|
86
89
|
}
|
|
87
90
|
if (options?.middlewareHandler && (!isRedirect || options.runHandlerOnRedirect)) {
|
|
88
|
-
const customResponse = await options.middlewareHandler(
|
|
91
|
+
const customResponse = await options.middlewareHandler(effectiveLocaleForRequest, rewriteUrl, redirectUrl);
|
|
89
92
|
if (customResponse) {
|
|
90
93
|
response = customResponse;
|
|
91
94
|
}
|
|
@@ -4,6 +4,7 @@ import { localeCookieName } from "../../config/cookie_key";
|
|
|
4
4
|
import { getLocaleCache, getMessageCache, setLocaleCache, setMessageForLocaleCache } from "../../general/cache_variables";
|
|
5
5
|
import { cache } from "react";
|
|
6
6
|
import { localesSet } from "../../config/middleware";
|
|
7
|
+
const isDev = process.env.NODE_ENV === 'development';
|
|
7
8
|
/**
|
|
8
9
|
* Loads and caches messages for a specific locale using dynamic import.
|
|
9
10
|
* Prevents redundant file loads and handles import errors gracefully.
|
|
@@ -11,7 +12,10 @@ import { localesSet } from "../../config/middleware";
|
|
|
11
12
|
* @returns A promise that resolves to the TranslationObject for the given locale.
|
|
12
13
|
*/
|
|
13
14
|
async function iGetMessage(locale) {
|
|
14
|
-
|
|
15
|
+
// In dev, always re-import so editing a messages/*.json file takes effect
|
|
16
|
+
// on the next request without a full server restart. loadedTranslations
|
|
17
|
+
// is a module-level cache that otherwise persists for the whole process.
|
|
18
|
+
const message = isDev ? undefined : getMessageCache(locale);
|
|
15
19
|
if (message) {
|
|
16
20
|
return message;
|
|
17
21
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NextResponse } from 'next/server';
|
|
2
2
|
import type { Languages } from 'next/dist/lib/metadata/types/alternative-urls-types';
|
|
3
3
|
import type { Videos } from 'next/dist/lib/metadata/types/metadata-types';
|
|
4
4
|
/**
|
|
@@ -6,44 +6,37 @@ import type { Videos } from 'next/dist/lib/metadata/types/metadata-types';
|
|
|
6
6
|
* (e.g. auth, feature flags, A/B tests) — on top of the library's own
|
|
7
7
|
* locale routing (locale-prefix rewrite/redirect).
|
|
8
8
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* -
|
|
12
|
-
* locale
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* - If `initialChosenLocale === defaultLocale`, the library rewrites to
|
|
20
|
-
* `targetUrl` (URL bar unchanged).
|
|
21
|
-
* - Otherwise, the library redirects to `targetUrl` (only when
|
|
22
|
-
* `runHandlerOnRedirect: true` is also passed — by default this
|
|
23
|
-
* handler does NOT run on redirects).
|
|
9
|
+
* STRICT RULE — at most ONE of `rewriteUrl` / `redirectUrl` is ever set, and
|
|
10
|
+
* whichever is set tells you exactly what to do:
|
|
11
|
+
* - `rewriteUrl` set: apply `NextResponse.rewrite(rewriteUrl, { request })`
|
|
12
|
+
* (locale matches the default locale — URL bar stays unchanged).
|
|
13
|
+
* - `redirectUrl` set: apply `NextResponse.redirect(redirectUrl, request)`
|
|
14
|
+
* (locale differs from the URL — visible redirect). The handler only runs
|
|
15
|
+
* for this case when `runHandlerOnRedirect: true` is passed.
|
|
16
|
+
* - BOTH undefined: no locale routing needed (URL already has the right
|
|
17
|
+
* locale prefix). This is where your own logic belongs — return
|
|
18
|
+
* `NextResponse.next({ request })`, or your own redirect (e.g. auth).
|
|
24
19
|
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* (rewrite/redirect/`next()`) as-is — this is the common case.
|
|
20
|
+
* Returning `null` in any case makes the library apply its own default,
|
|
21
|
+
* which is the same rewrite/redirect/`next()` described above.
|
|
22
|
+
*
|
|
23
|
+
* @param locale The resolved locale for this request (e.g. `"en"`).
|
|
24
|
+
* @param rewriteUrl URL to rewrite to, or `undefined`.
|
|
25
|
+
* @param redirectUrl URL to redirect to, or `undefined`.
|
|
26
|
+
* @returns A `NextResponse` to use for this request, or `null` to
|
|
27
|
+
* let the library build the default one.
|
|
34
28
|
*
|
|
35
29
|
* @example
|
|
36
30
|
* ```ts
|
|
37
|
-
* middlewareHandler: (
|
|
38
|
-
*
|
|
39
|
-
* if (
|
|
40
|
-
*
|
|
41
|
-
* }
|
|
42
|
-
* return null; // keep default locale routing
|
|
31
|
+
* middlewareHandler: (locale, rewriteUrl, redirectUrl) => {
|
|
32
|
+
* if (rewriteUrl) return NextResponse.rewrite(rewriteUrl, { request });
|
|
33
|
+
* if (redirectUrl) return NextResponse.redirect(redirectUrl, request);
|
|
34
|
+
* // No locale routing needed — your own logic goes here.
|
|
35
|
+
* return NextResponse.next({ request });
|
|
43
36
|
* }
|
|
44
37
|
* ```
|
|
45
38
|
*/
|
|
46
|
-
export type MiddlewareCustomHandler = (
|
|
39
|
+
export type MiddlewareCustomHandler = (locale: string, rewriteUrl: URL | undefined, redirectUrl: URL | undefined) => NextResponse<unknown> | null | Promise<NextResponse<unknown> | null>;
|
|
47
40
|
/** Your app's list of supported locale codes, e.g. `["en", "de"] as const`. */
|
|
48
41
|
export type Locales = readonly string[];
|
|
49
42
|
/**
|