@better-i18n/next 0.2.2 → 0.2.4
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.
- package/package.json +2 -2
- package/src/middleware.ts +27 -10
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@better-i18n/next",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "Better-i18n Next.js integration",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"typecheck": "tsc --noEmit"
|
|
58
58
|
},
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@better-i18n/core": "0.1.
|
|
60
|
+
"@better-i18n/core": "0.1.5"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
63
|
"next": ">=15.0.0",
|
package/src/middleware.ts
CHANGED
|
@@ -32,9 +32,13 @@ export const createI18nProxy = createI18nMiddleware;
|
|
|
32
32
|
|
|
33
33
|
/**
|
|
34
34
|
* Modern composable middleware for Better i18n
|
|
35
|
+
*
|
|
36
|
+
* Delegates to next-intl's middleware internally to ensure full compatibility
|
|
37
|
+
* with `getRequestConfig({ requestLocale })` while keeping our compose-friendly
|
|
38
|
+
* API and custom locale detection options.
|
|
35
39
|
*/
|
|
36
40
|
export function createBetterI18nMiddleware(config: I18nMiddlewareConfig) {
|
|
37
|
-
const { project, defaultLocale, detection = {} } = config;
|
|
41
|
+
const { project, defaultLocale, localePrefix = "as-needed", detection = {} } = config;
|
|
38
42
|
|
|
39
43
|
const {
|
|
40
44
|
cookie = true,
|
|
@@ -46,18 +50,32 @@ export function createBetterI18nMiddleware(config: I18nMiddlewareConfig) {
|
|
|
46
50
|
// Create i18n core instance for CDN operations
|
|
47
51
|
const i18nCore = createI18nCore({ project, defaultLocale });
|
|
48
52
|
|
|
53
|
+
// Cache for next-intl middleware instance (recreate only when locales change)
|
|
54
|
+
let cachedMiddleware: ReturnType<typeof createMiddleware> | null = null;
|
|
55
|
+
let cachedLocalesKey: string | null = null;
|
|
56
|
+
|
|
49
57
|
return async (request: NextRequest): Promise<NextResponse> => {
|
|
50
58
|
// 1. Fetch available locales from CDN
|
|
51
59
|
const availableLocales = await i18nCore.getLocales();
|
|
52
60
|
|
|
53
|
-
// 2.
|
|
61
|
+
// 2. Create/reuse next-intl middleware (only recreate if locales changed)
|
|
62
|
+
const localesKey = availableLocales.join(",");
|
|
63
|
+
if (!cachedMiddleware || cachedLocalesKey !== localesKey) {
|
|
64
|
+
cachedMiddleware = createMiddleware({
|
|
65
|
+
locales: availableLocales,
|
|
66
|
+
defaultLocale,
|
|
67
|
+
localePrefix,
|
|
68
|
+
});
|
|
69
|
+
cachedLocalesKey = localesKey;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 3. Our custom locale detection (for cookie logic)
|
|
54
73
|
const pathLocale = request.nextUrl.pathname.split("/")[1];
|
|
55
74
|
const cookieLocale = cookie ? request.cookies.get(cookieName)?.value : null;
|
|
56
75
|
const headerLocale = browserLanguage
|
|
57
76
|
? request.headers.get("accept-language")?.split(",")[0]?.split("-")[0]
|
|
58
77
|
: null;
|
|
59
78
|
|
|
60
|
-
// 3. Detect locale using core logic
|
|
61
79
|
const result = detectLocale({
|
|
62
80
|
project,
|
|
63
81
|
defaultLocale,
|
|
@@ -67,14 +85,13 @@ export function createBetterI18nMiddleware(config: I18nMiddlewareConfig) {
|
|
|
67
85
|
availableLocales,
|
|
68
86
|
});
|
|
69
87
|
|
|
70
|
-
// 4.
|
|
71
|
-
const response =
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
});
|
|
88
|
+
// 4. Call next-intl middleware (sets x-middleware-request-x-next-intl-locale header)
|
|
89
|
+
const response = cachedMiddleware(request);
|
|
90
|
+
|
|
91
|
+
// 5. Add x-locale header for backwards compatibility
|
|
92
|
+
response.headers.set("x-locale", result.locale);
|
|
76
93
|
|
|
77
|
-
//
|
|
94
|
+
// 6. Set our custom cookie if needed
|
|
78
95
|
if (cookie && result.shouldSetCookie) {
|
|
79
96
|
response.cookies.set(cookieName, result.locale, {
|
|
80
97
|
path: "/",
|