react-intlayer 3.5.5 → 3.5.6
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/README.md +1 -1
- package/dist/cjs/vite/intlayerMiddlewarePlugin.cjs +185 -59
- package/dist/cjs/vite/intlayerMiddlewarePlugin.cjs.map +1 -1
- package/dist/esm/vite/intlayerMiddlewarePlugin.mjs +186 -63
- package/dist/esm/vite/intlayerMiddlewarePlugin.mjs.map +1 -1
- package/dist/types/vite/intlayerMiddlewarePlugin.d.ts +3 -10
- package/dist/types/vite/intlayerMiddlewarePlugin.d.ts.map +1 -1
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -21,75 +21,201 @@ __export(intlayerMiddlewarePlugin_exports, {
|
|
|
21
21
|
intLayerMiddlewarePlugin: () => intLayerMiddlewarePlugin
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(intlayerMiddlewarePlugin_exports);
|
|
24
|
+
var import_url = require("url");
|
|
24
25
|
var import_config = require("@intlayer/config");
|
|
25
26
|
var import_core = require("@intlayer/core");
|
|
26
27
|
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
27
28
|
const { internationalization, middleware } = intlayerConfig;
|
|
28
29
|
const { locales: supportedLocales, defaultLocale } = internationalization;
|
|
29
|
-
const {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
const firstPart = pathParts[0];
|
|
44
|
-
const pathLocale = pathParts[0] && supportedLocales.includes(firstPart) ? pathParts[0] : void 0;
|
|
45
|
-
if (!pathLocale) {
|
|
46
|
-
let locale;
|
|
47
|
-
if (!locale && cookieLocale && supportedLocales.includes(cookieLocale)) {
|
|
48
|
-
locale = cookieLocale;
|
|
49
|
-
}
|
|
50
|
-
if (!locale && headerLocale && supportedLocales.includes(headerLocale)) {
|
|
51
|
-
locale = headerLocale;
|
|
52
|
-
}
|
|
53
|
-
if (!locale) {
|
|
54
|
-
const detectedLocale = (0, import_core.localeDetector)(
|
|
55
|
-
headers,
|
|
56
|
-
supportedLocales,
|
|
57
|
-
defaultLocale
|
|
58
|
-
);
|
|
59
|
-
locale = detectedLocale;
|
|
60
|
-
}
|
|
61
|
-
req.url = formatUrlWithLocale(originalUrl, locale);
|
|
62
|
-
if (req.url === originalUrl) {
|
|
30
|
+
const {
|
|
31
|
+
cookieName,
|
|
32
|
+
headerName,
|
|
33
|
+
prefixDefault,
|
|
34
|
+
noPrefix,
|
|
35
|
+
serverSetCookie,
|
|
36
|
+
basePath = ""
|
|
37
|
+
} = middleware;
|
|
38
|
+
const intLayerMiddlewarePlugin = () => {
|
|
39
|
+
return {
|
|
40
|
+
name: "vite-intlayer-middleware-plugin",
|
|
41
|
+
configureServer: (server) => {
|
|
42
|
+
server.middlewares.use((req, res, next) => {
|
|
43
|
+
if (req.url?.startsWith("/node_modules") || req.url?.startsWith("/@") || req.url?.split("?")[0].match(/\.[a-z]+$/i)) {
|
|
63
44
|
return next();
|
|
64
45
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
46
|
+
const parsedUrl = (0, import_url.parse)(req.url ?? "/", true);
|
|
47
|
+
const originalPath = parsedUrl.pathname ?? "/";
|
|
48
|
+
const cookies = parseCookies(req.headers.cookie ?? "");
|
|
49
|
+
const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);
|
|
50
|
+
const pathLocale = getPathLocale(originalPath);
|
|
51
|
+
if (noPrefix) {
|
|
52
|
+
handleNoPrefix({
|
|
53
|
+
req,
|
|
54
|
+
res,
|
|
55
|
+
next,
|
|
56
|
+
originalPath,
|
|
57
|
+
cookieLocale
|
|
58
|
+
});
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
handlePrefix({
|
|
62
|
+
req,
|
|
63
|
+
res,
|
|
64
|
+
next,
|
|
65
|
+
originalPath,
|
|
66
|
+
pathLocale,
|
|
67
|
+
cookieLocale
|
|
68
|
+
});
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
const parseCookies = (cookieHeader) => {
|
|
74
|
+
return cookieHeader.split(";").reduce(
|
|
75
|
+
(acc, cookie) => {
|
|
76
|
+
const [key, val] = cookie.trim().split("=");
|
|
77
|
+
acc[key] = val;
|
|
78
|
+
return acc;
|
|
79
|
+
},
|
|
80
|
+
{}
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
const getValidLocaleFromCookie = (locale) => {
|
|
84
|
+
if (locale && supportedLocales.includes(locale)) {
|
|
85
|
+
return locale;
|
|
86
|
+
}
|
|
87
|
+
return void 0;
|
|
88
|
+
};
|
|
89
|
+
const getPathLocale = (pathname) => {
|
|
90
|
+
const segments = pathname.split("/").filter(Boolean);
|
|
91
|
+
const firstSegment = segments[0];
|
|
92
|
+
if (firstSegment && supportedLocales.includes(firstSegment)) {
|
|
93
|
+
return firstSegment;
|
|
94
|
+
}
|
|
95
|
+
return void 0;
|
|
96
|
+
};
|
|
97
|
+
const redirectUrl = (res, newUrl) => {
|
|
98
|
+
res.writeHead(301, { Location: newUrl });
|
|
99
|
+
return res.end();
|
|
100
|
+
};
|
|
101
|
+
const rewriteUrl = (req, res, newUrl, locale) => {
|
|
102
|
+
req.url = newUrl;
|
|
103
|
+
if (locale && headerName) {
|
|
104
|
+
res.setHeader(headerName, locale);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
const constructPath = (locale, currentPath) => {
|
|
108
|
+
const cleanBasePath = basePath.startsWith("/") ? basePath : `/${basePath}`;
|
|
109
|
+
const normalizedBasePath = cleanBasePath === "/" ? "" : cleanBasePath;
|
|
110
|
+
let newPath = `${normalizedBasePath}/${locale}${currentPath}`;
|
|
111
|
+
if (!prefixDefault && locale === defaultLocale) {
|
|
112
|
+
newPath = `${normalizedBasePath}${currentPath}`;
|
|
113
|
+
}
|
|
114
|
+
return newPath;
|
|
115
|
+
};
|
|
116
|
+
const handleNoPrefix = ({
|
|
117
|
+
req,
|
|
118
|
+
res,
|
|
119
|
+
next,
|
|
120
|
+
originalPath,
|
|
121
|
+
cookieLocale
|
|
122
|
+
}) => {
|
|
123
|
+
let locale = cookieLocale ?? defaultLocale;
|
|
124
|
+
if (!cookieLocale) {
|
|
125
|
+
const detectedLocale = (0, import_core.localeDetector)(
|
|
126
|
+
req.headers,
|
|
127
|
+
supportedLocales,
|
|
128
|
+
defaultLocale
|
|
129
|
+
);
|
|
130
|
+
locale = detectedLocale;
|
|
131
|
+
}
|
|
132
|
+
rewriteUrl(req, res, originalPath, locale);
|
|
133
|
+
return next();
|
|
134
|
+
};
|
|
135
|
+
const handlePrefix = ({
|
|
136
|
+
req,
|
|
137
|
+
res,
|
|
138
|
+
next,
|
|
139
|
+
originalPath,
|
|
140
|
+
pathLocale,
|
|
141
|
+
cookieLocale
|
|
142
|
+
}) => {
|
|
143
|
+
if (!pathLocale) {
|
|
144
|
+
handleMissingPathLocale({
|
|
145
|
+
req,
|
|
146
|
+
res,
|
|
147
|
+
next,
|
|
148
|
+
originalPath,
|
|
149
|
+
cookieLocale
|
|
74
150
|
});
|
|
151
|
+
return;
|
|
75
152
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
153
|
+
handleExistingPathLocale({
|
|
154
|
+
req,
|
|
155
|
+
res,
|
|
156
|
+
next,
|
|
157
|
+
originalPath,
|
|
158
|
+
pathLocale,
|
|
159
|
+
cookieLocale
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
const handleMissingPathLocale = ({
|
|
163
|
+
req,
|
|
164
|
+
res,
|
|
165
|
+
next,
|
|
166
|
+
originalPath,
|
|
167
|
+
cookieLocale
|
|
168
|
+
}) => {
|
|
169
|
+
let locale = cookieLocale ?? (0, import_core.localeDetector)(
|
|
170
|
+
req.headers,
|
|
171
|
+
supportedLocales,
|
|
172
|
+
defaultLocale
|
|
173
|
+
);
|
|
174
|
+
if (!supportedLocales.includes(locale)) {
|
|
175
|
+
locale = defaultLocale;
|
|
176
|
+
}
|
|
177
|
+
const newPath = constructPath(locale, originalPath);
|
|
178
|
+
if (prefixDefault || locale !== defaultLocale) {
|
|
179
|
+
return redirectUrl(res, newPath);
|
|
180
|
+
}
|
|
181
|
+
rewriteUrl(req, res, newPath, locale);
|
|
182
|
+
return next();
|
|
183
|
+
};
|
|
184
|
+
const handleExistingPathLocale = ({
|
|
185
|
+
req,
|
|
186
|
+
res,
|
|
187
|
+
next,
|
|
188
|
+
originalPath,
|
|
189
|
+
pathLocale,
|
|
190
|
+
cookieLocale
|
|
191
|
+
}) => {
|
|
192
|
+
if (cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always") {
|
|
193
|
+
const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);
|
|
194
|
+
const finalPath = constructPath(cookieLocale, newPath.replace(/^\/+/, "/"));
|
|
195
|
+
return redirectUrl(res, finalPath);
|
|
196
|
+
}
|
|
197
|
+
handleDefaultLocaleRedirect({
|
|
198
|
+
req,
|
|
199
|
+
res,
|
|
200
|
+
next,
|
|
201
|
+
originalPath,
|
|
202
|
+
pathLocale
|
|
203
|
+
});
|
|
204
|
+
};
|
|
205
|
+
const handleDefaultLocaleRedirect = ({
|
|
206
|
+
req,
|
|
207
|
+
res,
|
|
208
|
+
next,
|
|
209
|
+
originalPath,
|
|
210
|
+
pathLocale
|
|
211
|
+
}) => {
|
|
212
|
+
if (!prefixDefault && pathLocale === defaultLocale) {
|
|
213
|
+
const newPath = originalPath.replace(`/${defaultLocale}`, "") ?? "/";
|
|
214
|
+
rewriteUrl(req, res, newPath, pathLocale);
|
|
215
|
+
return next();
|
|
91
216
|
}
|
|
92
|
-
|
|
217
|
+
rewriteUrl(req, res, originalPath, pathLocale);
|
|
218
|
+
return next();
|
|
93
219
|
};
|
|
94
220
|
// Annotate the CommonJS export names for ESM import in node:
|
|
95
221
|
0 && (module.exports = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/vite/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config';\nimport {\n getPathWithoutLocale,\n localeDetector as localeDetector,\n} from '@intlayer/core';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { type Plugin } from 'vite';\n\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\n\nconst { locales: supportedLocales, defaultLocale } = internationalization;\nconst { cookieName, headerName, prefixDefault } = middleware;\n\n/**\n * A Vite plugin that integrates IntLayer middleware into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intLayerMiddlewarePlugin() ],\n * });\n * ```\n */\nexport const intLayerMiddlewarePlugin = (_pluginOptions = {}): Plugin => ({\n name: 'vite-intlayer-middleware-plugin',\n\n configureServer: (server) => {\n // server.middlewares is a Connect instance; you can add middleware here\n server.middlewares.use((req, res, next) => {\n // Skip if request looks like a static asset or internal Vite request\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks if URL has a file extension\n ) {\n return next();\n }\n\n const originalUrl = req.url ?? '/';\n\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = cookies[cookieName];\n\n const headers = req.headers;\n const headerLocale = headers[headerName];\n\n const pathParts = originalUrl.split('?')[0].split('/').filter(Boolean);\n\n const firstPart = pathParts[0] as unknown as Locales;\n\n const pathLocale =\n pathParts[0] && supportedLocales.includes(firstPart)\n ? pathParts[0]\n : undefined;\n\n if (!pathLocale) {\n let locale;\n\n // Try to get the locale from the request cookies\n if (\n !locale &&\n cookieLocale &&\n supportedLocales.includes(cookieLocale as Locales)\n ) {\n locale = cookieLocale as Locales;\n }\n\n // Try to get the locale from the request headers\n if (\n !locale &&\n headerLocale &&\n supportedLocales.includes(headerLocale as Locales)\n ) {\n locale = headerLocale as Locales;\n }\n\n // Get the locale from the negotiator\n if (!locale) {\n const detectedLocale = localeDetector(\n headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale;\n }\n\n // Instead of redirecting, try rewriting internally:\n req.url = formatUrlWithLocale(originalUrl, locale);\n\n if (req.url === originalUrl) {\n return next();\n }\n\n res.writeHead(301, { Location: req.url });\n return res.end();\n }\n\n if (\n pathLocale.toString() === defaultLocale.toString() &&\n !prefixDefault\n ) {\n req.url = getPathWithoutLocale(originalUrl, supportedLocales);\n\n res.writeHead(301, { Location: req.url });\n return res.end();\n }\n\n return next();\n });\n },\n});\n\n// Simple cookie parser:\nconst parseCookies = (cookieHeader: string) =>\n cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key as keyof typeof acc] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n\nconst formatUrlWithLocale = (url: string, locale: Locales) => {\n if (locale.toString() === defaultLocale.toString()) {\n if (prefixDefault) {\n return `/${locale}${url}`;\n }\n\n return url;\n }\n\n return `/${locale}${url}`;\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA0C;AAC1C,kBAGO;AAIP,MAAM,qBAAiB,gCAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAE7C,MAAM,EAAE,SAAS,kBAAkB,cAAc,IAAI;AACrD,MAAM,EAAE,YAAY,YAAY,cAAc,IAAI;AAY3C,MAAM,2BAA2B,CAAC,iBAAiB,CAAC,OAAe;AAAA,EACxE,MAAM;A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,UAAU,IAAI,IAAI,CAAC;AACxC,eAAO,IAAI,IAAI;AAAA,MACjB;AAEA,aAAO,KAAK;AAAA,IACd,CAAC;AAAA,EACH;AACF;AAGA,MAAM,eAAe,CAAC,iBACpB,aAAa,MAAM,GAAG,EAAE;AAAA,EACtB,CAAC,KAAK,WAAW;AACf,UAAM,CAAC,KAAK,GAAG,IAAI,OAAO,KAAK,EAAE,MAAM,GAAG;AAC1C,QAAI,GAAuB,IAAI;AAC/B,WAAO;AAAA,EACT;AAAA,EACA,CAAC;AACH;AAEF,MAAM,sBAAsB,CAAC,KAAa,WAAoB;AAC5D,MAAI,OAAO,SAAS,MAAM,cAAc,SAAS,GAAG;AAClD,QAAI,eAAe;AACjB,aAAO,IAAI,MAAM,GAAG,GAAG;AAAA,IACzB;AAEA,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,MAAM,GAAG,GAAG;AACzB;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/vite/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\n/* @ts-ignore - Vite types error */\nimport type { Connect, Plugin } from 'vite';\n\n// Grab all the config you need.\n// Make sure your config includes the following fields if you want to replicate Next.js logic:\n// - internationalization.locales\n// - internationalization.defaultLocale\n// - middleware.cookieName\n// - middleware.headerName\n// - middleware.prefixDefault\n// - middleware.noPrefix\n// - middleware.serverSetCookie\n// - middleware.basePath\n// - etc.\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\nconst { locales: supportedLocales, defaultLocale } = internationalization;\n\nconst {\n cookieName,\n headerName,\n prefixDefault,\n noPrefix,\n serverSetCookie,\n basePath = '',\n} = middleware;\n\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n */\nexport const intLayerMiddlewarePlugin = (): Plugin => {\n return {\n name: 'vite-intlayer-middleware-plugin',\n configureServer: (server) => {\n server.middlewares.use((req, res, next) => {\n // 1. Bypass assets and special Vite endpoints\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks for file extensions\n ) {\n return next();\n }\n\n // 2. Parse original URL for path and query\n const parsedUrl = parse(req.url ?? '/', true);\n const originalPath = parsedUrl.pathname ?? '/';\n\n // 3. Attempt to read the cookie locale\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);\n\n // 4. Check if there's a locale prefix in the path\n const pathLocale = getPathLocale(originalPath);\n\n // 5. If noPrefix is true, we skip prefix logic altogether\n if (noPrefix) {\n handleNoPrefix({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 6. Otherwise, handle prefix logic\n handlePrefix({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n });\n },\n };\n};\n\n/* --------------------------------------------------------------------\n * Helper & Utility Functions\n * --------------------------------------------------------------------\n */\n\n/**\n * Parses cookies from the Cookie header string into an object.\n */\nconst parseCookies = (cookieHeader: string) => {\n return cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n};\n\n/**\n * Checks if the cookie locale is valid and is included in the supported locales.\n */\nconst getValidLocaleFromCookie = (\n locale: string | undefined\n): Locales | undefined => {\n if (locale && supportedLocales.includes(locale as Locales)) {\n return locale as Locales;\n }\n return undefined;\n};\n\n/**\n * Extracts the locale from the URL pathname if present as the first segment.\n */\nconst getPathLocale = (pathname: string): Locales | undefined => {\n // e.g. if pathname is /en/some/page or /en\n // we check if \"en\" is in your supportedLocales\n const segments = pathname.split('/').filter(Boolean);\n const firstSegment = segments[0];\n if (firstSegment && supportedLocales.includes(firstSegment as Locales)) {\n return firstSegment as Locales;\n }\n return undefined;\n};\n\n/**\n * Writes a 301 redirect response with the given new URL.\n */\nconst redirectUrl = (res: ServerResponse<IncomingMessage>, newUrl: string) => {\n res.writeHead(301, { Location: newUrl });\n return res.end();\n};\n\n/**\n * \"Rewrite\" the request internally by adjusting req.url;\n * we also set the locale in the response header if needed.\n */\nconst rewriteUrl = (\n req: Connect.IncomingMessage,\n res: ServerResponse<IncomingMessage>,\n newUrl: string,\n locale?: Locales\n) => {\n req.url = newUrl;\n // If you want to mimic Next.js's behavior of setting a header for the locale:\n if (locale && headerName) {\n res.setHeader(headerName, locale);\n }\n};\n\n/**\n * Constructs a new path string, optionally including a locale prefix and basePath.\n * - basePath: (e.g., '/myapp')\n * - locale: (e.g., 'en')\n * - currentPath:(e.g., '/products/shoes')\n */\nconst constructPath = (locale: Locales, currentPath: string) => {\n // Ensure basePath always starts with '/', and remove trailing slash if needed\n const cleanBasePath = basePath.startsWith('/') ? basePath : `/${basePath}`;\n // If basePath is '/', no trailing slash is needed\n const normalizedBasePath = cleanBasePath === '/' ? '' : cleanBasePath;\n\n // Combine basePath + locale + the rest of the path\n // Example: basePath = '/myapp', locale = 'en', currentPath = '/products' => '/myapp/en/products'\n let newPath = `${normalizedBasePath}/${locale}${currentPath}`;\n\n // Special case: if prefixDefault is false and locale is defaultLocale, remove the locale prefix\n if (!prefixDefault && locale === defaultLocale) {\n newPath = `${normalizedBasePath}${currentPath}`;\n }\n\n return newPath;\n};\n\n/* --------------------------------------------------------------------\n * Handlers that mirror Next.js style logic\n * --------------------------------------------------------------------\n */\n\n/**\n * If `noPrefix` is true, we never prefix the locale in the URL.\n * We simply rewrite the request to the same path, but with the best-chosen locale\n * in a header or cookie if desired.\n */\nconst handleNoPrefix = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // Determine the best locale\n let locale = cookieLocale ?? defaultLocale;\n\n // Use fallback to localeDetector if no cookie\n if (!cookieLocale) {\n const detectedLocale = localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale;\n }\n\n // Just rewrite the URL in-place (no prefix). We do NOT redirect because we do not want to alter the URL.\n rewriteUrl(req, res, originalPath, locale);\n return next();\n};\n\n/**\n * The main prefix logic:\n * - If there's no pathLocale in the URL, we might want to detect & redirect or rewrite\n * - If there is a pathLocale, handle cookie mismatch or default locale special cases\n */\nconst handlePrefix = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale?: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If pathLocale is missing, handle\n if (!pathLocale) {\n handleMissingPathLocale({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 2. If pathLocale exists, handle possible mismatch with cookie\n handleExistingPathLocale({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n * We detect a locale from cookie / headers / default, then either redirect or rewrite.\n */\nconst handleMissingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // 1. Choose the best locale\n let locale =\n cookieLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n\n // 2. If still invalid, fallback\n if (!supportedLocales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // 3. Construct new path\n const newPath = constructPath(locale, originalPath);\n\n // If we always prefix default or if this is not the default locale, do a 301 redirect\n // so that the user sees the locale in the URL.\n if (prefixDefault || locale !== defaultLocale) {\n return redirectUrl(res, newPath);\n }\n\n // If we do NOT prefix the default locale, just rewrite in place\n rewriteUrl(req, res, newPath, locale);\n return next();\n};\n\n/**\n * Handles requests where the locale prefix is present in the pathname.\n * We verify if the cookie locale differs from the path locale; if so, handle.\n */\nconst handleExistingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If the cookie locale is set and differs from the path locale,\n // and we're not forcing the cookie to always override\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n // We want to swap out the pathLocale with the cookieLocale\n const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);\n const finalPath = constructPath(cookieLocale, newPath.replace(/^\\/+/, '/'));\n return redirectUrl(res, finalPath);\n }\n\n // 2. Otherwise, handle default-locale prefix if needed\n handleDefaultLocaleRedirect({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n });\n};\n\n/**\n * If the path locale is the default locale but we don't want to prefix the default, remove it.\n */\nconst handleDefaultLocaleRedirect = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n}) => {\n // If we don't prefix default AND the path locale is the default locale -> remove it\n if (!prefixDefault && pathLocale === defaultLocale) {\n // Remove the default locale part from the path\n const newPath = originalPath.replace(`/${defaultLocale}`, '') ?? '/';\n rewriteUrl(req, res, newPath, pathLocale);\n return next();\n }\n\n // If we do prefix default or pathLocale != default, keep as is, but rewrite headers\n rewriteUrl(req, res, originalPath, pathLocale);\n return next();\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,iBAAsB;AACtB,oBAA+C;AAC/C,kBAA+B;AAe/B,MAAM,qBAAiB,gCAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,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ACjE,eAAW,KAAK,KAAK,SAAS,UAAU;AACxC,WAAO,KAAK;AAAA,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;","names":[]}
|
|
@@ -1,76 +1,199 @@
|
|
|
1
1
|
import "../chunk-ZD7AOCMD.mjs";
|
|
2
|
+
import { parse } from "url";
|
|
2
3
|
import { getConfiguration } from "@intlayer/config";
|
|
3
|
-
import {
|
|
4
|
-
getPathWithoutLocale,
|
|
5
|
-
localeDetector
|
|
6
|
-
} from "@intlayer/core";
|
|
4
|
+
import { localeDetector } from "@intlayer/core";
|
|
7
5
|
const intlayerConfig = getConfiguration();
|
|
8
6
|
const { internationalization, middleware } = intlayerConfig;
|
|
9
7
|
const { locales: supportedLocales, defaultLocale } = internationalization;
|
|
10
|
-
const {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
const firstPart = pathParts[0];
|
|
25
|
-
const pathLocale = pathParts[0] && supportedLocales.includes(firstPart) ? pathParts[0] : void 0;
|
|
26
|
-
if (!pathLocale) {
|
|
27
|
-
let locale;
|
|
28
|
-
if (!locale && cookieLocale && supportedLocales.includes(cookieLocale)) {
|
|
29
|
-
locale = cookieLocale;
|
|
30
|
-
}
|
|
31
|
-
if (!locale && headerLocale && supportedLocales.includes(headerLocale)) {
|
|
32
|
-
locale = headerLocale;
|
|
33
|
-
}
|
|
34
|
-
if (!locale) {
|
|
35
|
-
const detectedLocale = localeDetector(
|
|
36
|
-
headers,
|
|
37
|
-
supportedLocales,
|
|
38
|
-
defaultLocale
|
|
39
|
-
);
|
|
40
|
-
locale = detectedLocale;
|
|
41
|
-
}
|
|
42
|
-
req.url = formatUrlWithLocale(originalUrl, locale);
|
|
43
|
-
if (req.url === originalUrl) {
|
|
8
|
+
const {
|
|
9
|
+
cookieName,
|
|
10
|
+
headerName,
|
|
11
|
+
prefixDefault,
|
|
12
|
+
noPrefix,
|
|
13
|
+
serverSetCookie,
|
|
14
|
+
basePath = ""
|
|
15
|
+
} = middleware;
|
|
16
|
+
const intLayerMiddlewarePlugin = () => {
|
|
17
|
+
return {
|
|
18
|
+
name: "vite-intlayer-middleware-plugin",
|
|
19
|
+
configureServer: (server) => {
|
|
20
|
+
server.middlewares.use((req, res, next) => {
|
|
21
|
+
if (req.url?.startsWith("/node_modules") || req.url?.startsWith("/@") || req.url?.split("?")[0].match(/\.[a-z]+$/i)) {
|
|
44
22
|
return next();
|
|
45
23
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
24
|
+
const parsedUrl = parse(req.url ?? "/", true);
|
|
25
|
+
const originalPath = parsedUrl.pathname ?? "/";
|
|
26
|
+
const cookies = parseCookies(req.headers.cookie ?? "");
|
|
27
|
+
const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);
|
|
28
|
+
const pathLocale = getPathLocale(originalPath);
|
|
29
|
+
if (noPrefix) {
|
|
30
|
+
handleNoPrefix({
|
|
31
|
+
req,
|
|
32
|
+
res,
|
|
33
|
+
next,
|
|
34
|
+
originalPath,
|
|
35
|
+
cookieLocale
|
|
36
|
+
});
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
handlePrefix({
|
|
40
|
+
req,
|
|
41
|
+
res,
|
|
42
|
+
next,
|
|
43
|
+
originalPath,
|
|
44
|
+
pathLocale,
|
|
45
|
+
cookieLocale
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
const parseCookies = (cookieHeader) => {
|
|
52
|
+
return cookieHeader.split(";").reduce(
|
|
53
|
+
(acc, cookie) => {
|
|
54
|
+
const [key, val] = cookie.trim().split("=");
|
|
55
|
+
acc[key] = val;
|
|
56
|
+
return acc;
|
|
57
|
+
},
|
|
58
|
+
{}
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
const getValidLocaleFromCookie = (locale) => {
|
|
62
|
+
if (locale && supportedLocales.includes(locale)) {
|
|
63
|
+
return locale;
|
|
64
|
+
}
|
|
65
|
+
return void 0;
|
|
66
|
+
};
|
|
67
|
+
const getPathLocale = (pathname) => {
|
|
68
|
+
const segments = pathname.split("/").filter(Boolean);
|
|
69
|
+
const firstSegment = segments[0];
|
|
70
|
+
if (firstSegment && supportedLocales.includes(firstSegment)) {
|
|
71
|
+
return firstSegment;
|
|
72
|
+
}
|
|
73
|
+
return void 0;
|
|
74
|
+
};
|
|
75
|
+
const redirectUrl = (res, newUrl) => {
|
|
76
|
+
res.writeHead(301, { Location: newUrl });
|
|
77
|
+
return res.end();
|
|
78
|
+
};
|
|
79
|
+
const rewriteUrl = (req, res, newUrl, locale) => {
|
|
80
|
+
req.url = newUrl;
|
|
81
|
+
if (locale && headerName) {
|
|
82
|
+
res.setHeader(headerName, locale);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const constructPath = (locale, currentPath) => {
|
|
86
|
+
const cleanBasePath = basePath.startsWith("/") ? basePath : `/${basePath}`;
|
|
87
|
+
const normalizedBasePath = cleanBasePath === "/" ? "" : cleanBasePath;
|
|
88
|
+
let newPath = `${normalizedBasePath}/${locale}${currentPath}`;
|
|
89
|
+
if (!prefixDefault && locale === defaultLocale) {
|
|
90
|
+
newPath = `${normalizedBasePath}${currentPath}`;
|
|
91
|
+
}
|
|
92
|
+
return newPath;
|
|
93
|
+
};
|
|
94
|
+
const handleNoPrefix = ({
|
|
95
|
+
req,
|
|
96
|
+
res,
|
|
97
|
+
next,
|
|
98
|
+
originalPath,
|
|
99
|
+
cookieLocale
|
|
100
|
+
}) => {
|
|
101
|
+
let locale = cookieLocale ?? defaultLocale;
|
|
102
|
+
if (!cookieLocale) {
|
|
103
|
+
const detectedLocale = localeDetector(
|
|
104
|
+
req.headers,
|
|
105
|
+
supportedLocales,
|
|
106
|
+
defaultLocale
|
|
107
|
+
);
|
|
108
|
+
locale = detectedLocale;
|
|
109
|
+
}
|
|
110
|
+
rewriteUrl(req, res, originalPath, locale);
|
|
111
|
+
return next();
|
|
112
|
+
};
|
|
113
|
+
const handlePrefix = ({
|
|
114
|
+
req,
|
|
115
|
+
res,
|
|
116
|
+
next,
|
|
117
|
+
originalPath,
|
|
118
|
+
pathLocale,
|
|
119
|
+
cookieLocale
|
|
120
|
+
}) => {
|
|
121
|
+
if (!pathLocale) {
|
|
122
|
+
handleMissingPathLocale({
|
|
123
|
+
req,
|
|
124
|
+
res,
|
|
125
|
+
next,
|
|
126
|
+
originalPath,
|
|
127
|
+
cookieLocale
|
|
55
128
|
});
|
|
129
|
+
return;
|
|
56
130
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
131
|
+
handleExistingPathLocale({
|
|
132
|
+
req,
|
|
133
|
+
res,
|
|
134
|
+
next,
|
|
135
|
+
originalPath,
|
|
136
|
+
pathLocale,
|
|
137
|
+
cookieLocale
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
const handleMissingPathLocale = ({
|
|
141
|
+
req,
|
|
142
|
+
res,
|
|
143
|
+
next,
|
|
144
|
+
originalPath,
|
|
145
|
+
cookieLocale
|
|
146
|
+
}) => {
|
|
147
|
+
let locale = cookieLocale ?? localeDetector(
|
|
148
|
+
req.headers,
|
|
149
|
+
supportedLocales,
|
|
150
|
+
defaultLocale
|
|
151
|
+
);
|
|
152
|
+
if (!supportedLocales.includes(locale)) {
|
|
153
|
+
locale = defaultLocale;
|
|
154
|
+
}
|
|
155
|
+
const newPath = constructPath(locale, originalPath);
|
|
156
|
+
if (prefixDefault || locale !== defaultLocale) {
|
|
157
|
+
return redirectUrl(res, newPath);
|
|
158
|
+
}
|
|
159
|
+
rewriteUrl(req, res, newPath, locale);
|
|
160
|
+
return next();
|
|
161
|
+
};
|
|
162
|
+
const handleExistingPathLocale = ({
|
|
163
|
+
req,
|
|
164
|
+
res,
|
|
165
|
+
next,
|
|
166
|
+
originalPath,
|
|
167
|
+
pathLocale,
|
|
168
|
+
cookieLocale
|
|
169
|
+
}) => {
|
|
170
|
+
if (cookieLocale && cookieLocale !== pathLocale && serverSetCookie !== "always") {
|
|
171
|
+
const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);
|
|
172
|
+
const finalPath = constructPath(cookieLocale, newPath.replace(/^\/+/, "/"));
|
|
173
|
+
return redirectUrl(res, finalPath);
|
|
174
|
+
}
|
|
175
|
+
handleDefaultLocaleRedirect({
|
|
176
|
+
req,
|
|
177
|
+
res,
|
|
178
|
+
next,
|
|
179
|
+
originalPath,
|
|
180
|
+
pathLocale
|
|
181
|
+
});
|
|
182
|
+
};
|
|
183
|
+
const handleDefaultLocaleRedirect = ({
|
|
184
|
+
req,
|
|
185
|
+
res,
|
|
186
|
+
next,
|
|
187
|
+
originalPath,
|
|
188
|
+
pathLocale
|
|
189
|
+
}) => {
|
|
190
|
+
if (!prefixDefault && pathLocale === defaultLocale) {
|
|
191
|
+
const newPath = originalPath.replace(`/${defaultLocale}`, "") ?? "/";
|
|
192
|
+
rewriteUrl(req, res, newPath, pathLocale);
|
|
193
|
+
return next();
|
|
72
194
|
}
|
|
73
|
-
|
|
195
|
+
rewriteUrl(req, res, originalPath, pathLocale);
|
|
196
|
+
return next();
|
|
74
197
|
};
|
|
75
198
|
export {
|
|
76
199
|
intLayerMiddlewarePlugin
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/vite/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { getConfiguration, Locales } from '@intlayer/config';\nimport {\n getPathWithoutLocale,\n localeDetector as localeDetector,\n} from '@intlayer/core';\n// @ts-ignore - Fix error Module '\"vite\"' has no exported member\nimport { type Plugin } from 'vite';\n\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\n\nconst { locales: supportedLocales, defaultLocale } = internationalization;\nconst { cookieName, headerName, prefixDefault } = middleware;\n\n/**\n * A Vite plugin that integrates IntLayer middleware into the build process\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intLayerMiddlewarePlugin() ],\n * });\n * ```\n */\nexport const intLayerMiddlewarePlugin = (_pluginOptions = {}): Plugin => ({\n name: 'vite-intlayer-middleware-plugin',\n\n configureServer: (server) => {\n // server.middlewares is a Connect instance; you can add middleware here\n server.middlewares.use((req, res, next) => {\n // Skip if request looks like a static asset or internal Vite request\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks if URL has a file extension\n ) {\n return next();\n }\n\n const originalUrl = req.url ?? '/';\n\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = cookies[cookieName];\n\n const headers = req.headers;\n const headerLocale = headers[headerName];\n\n const pathParts = originalUrl.split('?')[0].split('/').filter(Boolean);\n\n const firstPart = pathParts[0] as unknown as Locales;\n\n const pathLocale =\n pathParts[0] && supportedLocales.includes(firstPart)\n ? pathParts[0]\n : undefined;\n\n if (!pathLocale) {\n let locale;\n\n // Try to get the locale from the request cookies\n if (\n !locale &&\n cookieLocale &&\n supportedLocales.includes(cookieLocale as Locales)\n ) {\n locale = cookieLocale as Locales;\n }\n\n // Try to get the locale from the request headers\n if (\n !locale &&\n headerLocale &&\n supportedLocales.includes(headerLocale as Locales)\n ) {\n locale = headerLocale as Locales;\n }\n\n // Get the locale from the negotiator\n if (!locale) {\n const detectedLocale = localeDetector(\n headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale;\n }\n\n // Instead of redirecting, try rewriting internally:\n req.url = formatUrlWithLocale(originalUrl, locale);\n\n if (req.url === originalUrl) {\n return next();\n }\n\n res.writeHead(301, { Location: req.url });\n return res.end();\n }\n\n if (\n pathLocale.toString() === defaultLocale.toString() &&\n !prefixDefault\n ) {\n req.url = getPathWithoutLocale(originalUrl, supportedLocales);\n\n res.writeHead(301, { Location: req.url });\n return res.end();\n }\n\n return next();\n });\n },\n});\n\n// Simple cookie parser:\nconst parseCookies = (cookieHeader: string) =>\n cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key as keyof typeof acc] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n\nconst formatUrlWithLocale = (url: string, locale: Locales) => {\n if (locale.toString() === defaultLocale.toString()) {\n if (prefixDefault) {\n return `/${locale}${url}`;\n }\n\n return url;\n }\n\n return `/${locale}${url}`;\n};\n"],"mappings":";AAAA,SAAS,wBAAiC;AAC1C;AAAA,EACE;AAAA,EACA;AAAA,OACK;AAIP,MAAM,iBAAiB,iBAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAE7C,MAAM,EAAE,SAAS,kBAAkB,cAAc,IAAI;AACrD,MAAM,EAAE,YAAY,YAAY,cAAc,IAAI;AAY3C,MAAM,2BAA2B,CAAC,iBAAiB,CAAC,OAAe;AAAA,EACxE,MAAM;AAAA,EAEN,iBAAiB,CAAC,WAAW;AAE3B,WAAO,YAAY,IAAI,CAAC,KAAK,KAAK,SAAS;AAEzC,UACE,IAAI,KAAK,WAAW,eAAe,KACnC,IAAI,KAAK,WAAW,IAAI,KACxB,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,YAAY,GACzC;AACA,eAAO,KAAK;AAAA,MACd;AAEA,YAAM,cAAc,IAAI,OAAO;AAE/B,YAAM,UAAU,aAAa,IAAI,QAAQ,UAAU,EAAE;AACrD,YAAM,eAAe,QAAQ,UAAU;AAEvC,YAAM,UAAU,IAAI;AAC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,MAAI,OAAO,SAAS,MAAM,cAAc,SAAS,GAAG;AAClD,QAAI,eAAe;AACjB,aAAO,IAAI,MAAM,GAAG,GAAG;AAAA,IACzB;AAEA,WAAO;AAAA,EACT;AAEA,SAAO,IAAI,MAAM,GAAG,GAAG;AACzB;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../../src/vite/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\nimport { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\n/* @ts-ignore - Vite types error */\nimport type { Connect, Plugin } from 'vite';\n\n// Grab all the config you need.\n// Make sure your config includes the following fields if you want to replicate Next.js logic:\n// - internationalization.locales\n// - internationalization.defaultLocale\n// - middleware.cookieName\n// - middleware.headerName\n// - middleware.prefixDefault\n// - middleware.noPrefix\n// - middleware.serverSetCookie\n// - middleware.basePath\n// - etc.\nconst intlayerConfig = getConfiguration();\nconst { internationalization, middleware } = intlayerConfig;\nconst { locales: supportedLocales, defaultLocale } = internationalization;\n\nconst {\n cookieName,\n headerName,\n prefixDefault,\n noPrefix,\n serverSetCookie,\n basePath = '',\n} = middleware;\n\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n */\nexport const intLayerMiddlewarePlugin = (): Plugin => {\n return {\n name: 'vite-intlayer-middleware-plugin',\n configureServer: (server) => {\n server.middlewares.use((req, res, next) => {\n // 1. Bypass assets and special Vite endpoints\n if (\n req.url?.startsWith('/node_modules') ||\n req.url?.startsWith('/@') ||\n req.url?.split('?')[0].match(/\\.[a-z]+$/i) // checks for file extensions\n ) {\n return next();\n }\n\n // 2. Parse original URL for path and query\n const parsedUrl = parse(req.url ?? '/', true);\n const originalPath = parsedUrl.pathname ?? '/';\n\n // 3. Attempt to read the cookie locale\n const cookies = parseCookies(req.headers.cookie ?? '');\n const cookieLocale = getValidLocaleFromCookie(cookies[cookieName]);\n\n // 4. Check if there's a locale prefix in the path\n const pathLocale = getPathLocale(originalPath);\n\n // 5. If noPrefix is true, we skip prefix logic altogether\n if (noPrefix) {\n handleNoPrefix({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 6. Otherwise, handle prefix logic\n handlePrefix({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n });\n },\n };\n};\n\n/* --------------------------------------------------------------------\n * Helper & Utility Functions\n * --------------------------------------------------------------------\n */\n\n/**\n * Parses cookies from the Cookie header string into an object.\n */\nconst parseCookies = (cookieHeader: string) => {\n return cookieHeader.split(';').reduce(\n (acc, cookie) => {\n const [key, val] = cookie.trim().split('=');\n acc[key] = val;\n return acc;\n },\n {} as Record<string, string>\n );\n};\n\n/**\n * Checks if the cookie locale is valid and is included in the supported locales.\n */\nconst getValidLocaleFromCookie = (\n locale: string | undefined\n): Locales | undefined => {\n if (locale && supportedLocales.includes(locale as Locales)) {\n return locale as Locales;\n }\n return undefined;\n};\n\n/**\n * Extracts the locale from the URL pathname if present as the first segment.\n */\nconst getPathLocale = (pathname: string): Locales | undefined => {\n // e.g. if pathname is /en/some/page or /en\n // we check if \"en\" is in your supportedLocales\n const segments = pathname.split('/').filter(Boolean);\n const firstSegment = segments[0];\n if (firstSegment && supportedLocales.includes(firstSegment as Locales)) {\n return firstSegment as Locales;\n }\n return undefined;\n};\n\n/**\n * Writes a 301 redirect response with the given new URL.\n */\nconst redirectUrl = (res: ServerResponse<IncomingMessage>, newUrl: string) => {\n res.writeHead(301, { Location: newUrl });\n return res.end();\n};\n\n/**\n * \"Rewrite\" the request internally by adjusting req.url;\n * we also set the locale in the response header if needed.\n */\nconst rewriteUrl = (\n req: Connect.IncomingMessage,\n res: ServerResponse<IncomingMessage>,\n newUrl: string,\n locale?: Locales\n) => {\n req.url = newUrl;\n // If you want to mimic Next.js's behavior of setting a header for the locale:\n if (locale && headerName) {\n res.setHeader(headerName, locale);\n }\n};\n\n/**\n * Constructs a new path string, optionally including a locale prefix and basePath.\n * - basePath: (e.g., '/myapp')\n * - locale: (e.g., 'en')\n * - currentPath:(e.g., '/products/shoes')\n */\nconst constructPath = (locale: Locales, currentPath: string) => {\n // Ensure basePath always starts with '/', and remove trailing slash if needed\n const cleanBasePath = basePath.startsWith('/') ? basePath : `/${basePath}`;\n // If basePath is '/', no trailing slash is needed\n const normalizedBasePath = cleanBasePath === '/' ? '' : cleanBasePath;\n\n // Combine basePath + locale + the rest of the path\n // Example: basePath = '/myapp', locale = 'en', currentPath = '/products' => '/myapp/en/products'\n let newPath = `${normalizedBasePath}/${locale}${currentPath}`;\n\n // Special case: if prefixDefault is false and locale is defaultLocale, remove the locale prefix\n if (!prefixDefault && locale === defaultLocale) {\n newPath = `${normalizedBasePath}${currentPath}`;\n }\n\n return newPath;\n};\n\n/* --------------------------------------------------------------------\n * Handlers that mirror Next.js style logic\n * --------------------------------------------------------------------\n */\n\n/**\n * If `noPrefix` is true, we never prefix the locale in the URL.\n * We simply rewrite the request to the same path, but with the best-chosen locale\n * in a header or cookie if desired.\n */\nconst handleNoPrefix = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // Determine the best locale\n let locale = cookieLocale ?? defaultLocale;\n\n // Use fallback to localeDetector if no cookie\n if (!cookieLocale) {\n const detectedLocale = localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale;\n }\n\n // Just rewrite the URL in-place (no prefix). We do NOT redirect because we do not want to alter the URL.\n rewriteUrl(req, res, originalPath, locale);\n return next();\n};\n\n/**\n * The main prefix logic:\n * - If there's no pathLocale in the URL, we might want to detect & redirect or rewrite\n * - If there is a pathLocale, handle cookie mismatch or default locale special cases\n */\nconst handlePrefix = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale?: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If pathLocale is missing, handle\n if (!pathLocale) {\n handleMissingPathLocale({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n });\n return;\n }\n\n // 2. If pathLocale exists, handle possible mismatch with cookie\n handleExistingPathLocale({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n });\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n * We detect a locale from cookie / headers / default, then either redirect or rewrite.\n */\nconst handleMissingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n cookieLocale?: Locales;\n}) => {\n // 1. Choose the best locale\n let locale =\n cookieLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n\n // 2. If still invalid, fallback\n if (!supportedLocales.includes(locale)) {\n locale = defaultLocale;\n }\n\n // 3. Construct new path\n const newPath = constructPath(locale, originalPath);\n\n // If we always prefix default or if this is not the default locale, do a 301 redirect\n // so that the user sees the locale in the URL.\n if (prefixDefault || locale !== defaultLocale) {\n return redirectUrl(res, newPath);\n }\n\n // If we do NOT prefix the default locale, just rewrite in place\n rewriteUrl(req, res, newPath, locale);\n return next();\n};\n\n/**\n * Handles requests where the locale prefix is present in the pathname.\n * We verify if the cookie locale differs from the path locale; if so, handle.\n */\nconst handleExistingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n cookieLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n cookieLocale?: Locales;\n}) => {\n // 1. If the cookie locale is set and differs from the path locale,\n // and we're not forcing the cookie to always override\n if (\n cookieLocale &&\n cookieLocale !== pathLocale &&\n serverSetCookie !== 'always'\n ) {\n // We want to swap out the pathLocale with the cookieLocale\n const newPath = originalPath.replace(`/${pathLocale}`, `/${cookieLocale}`);\n const finalPath = constructPath(cookieLocale, newPath.replace(/^\\/+/, '/'));\n return redirectUrl(res, finalPath);\n }\n\n // 2. Otherwise, handle default-locale prefix if needed\n handleDefaultLocaleRedirect({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n });\n};\n\n/**\n * If the path locale is the default locale but we don't want to prefix the default, remove it.\n */\nconst handleDefaultLocaleRedirect = ({\n req,\n res,\n next,\n originalPath,\n pathLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n pathLocale: Locales;\n}) => {\n // If we don't prefix default AND the path locale is the default locale -> remove it\n if (!prefixDefault && pathLocale === defaultLocale) {\n // Remove the default locale part from the path\n const newPath = originalPath.replace(`/${defaultLocale}`, '') ?? '/';\n rewriteUrl(req, res, newPath, pathLocale);\n return next();\n }\n\n // If we do prefix default or pathLocale != default, keep as is, but rewrite headers\n rewriteUrl(req, res, originalPath, pathLocale);\n return next();\n};\n"],"mappings":";AACA,SAAS,aAAa;AACtB,SAAS,wBAAsC;AAC/C,SAAS,sBAAsB;AAe/B,MAAM,iBAAiB,iBAAiB;AACxC,MAAM,EAAE,sBAAsB,WAAW,IAAI;AAC7C,MAAM,EAAE,SAAS,kBAAkB,cAAc,IAAI;AAErD,MAAM;AAAA,EACJ;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA,WAAW;AACb,IAAI;AAKG,MAAM,2BAA2B,MAAc;AACpD,SAAO;AAAA,IACL,MAAM;AAAA,IACN,iBAAiB,CAAC,WAAW;AAC3B,aAAO,YAAY,IAAI,CAAC,KAAK,KAAK,SAAS;AAEzC,YACE,IAAI,KAAK,WAAW,eAAe,KACnC,IAAI,KAAK,WAAW,IAAI,KACxB,IAAI,KAAK,MAAM,GAAG,EAAE,CAAC,EAAE,MAAM,YAAY,GACzC;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GAAG;AACtC,aAAS;AAAA,EACX;AAGA,QAAM,UAAU,cAAc,QAAQ,YAAY;AAIlD,MAAI,iBAAiB,WAAW,eAAe;AAC7C,WAAO,YAAY,KAAK,OAAO;AAAA,EACjC;AAGA,aAAW,KAAK,KAAK,SAAS,MAAM;AACpC,SAAO,KAAK;AACd;AAMA,MAAM,2BAA2B,CAAC;AAAA,EAChC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAOM;AAGJ,MACE,gBACA,iBAAiB,cACjB,oBAAoB,UACpB;AAEA,UAAM,UAAU,aAAa,QAAQ,IAAI,UAAU,IAAI,IAAI,YAAY,EAAE;AACzE,UAAM,YAAY,cAAc,cAAc,QAAQ,QAAQ,QAAQ,GAAG,CAAC;AAC1E,WAAO,YAAY,KAAK,SAAS;AAAA,EACnC;AAGA,8BAA4B;AAAA,IAC1B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAKA,MAAM,8BAA8B,CAAC;AAAA,EACnC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAMM;AAEJ,MAAI,CAAC,iBAAiB,eAAe,eAAe;AAElD,UAAM,UAAU,aAAa,QAAQ,IAAI,aAAa,IAAI,EAAE,KAAK;AACjE,eAAW,KAAK,KAAK,SAAS,UAAU;AACxC,WAAO,KAAK;AAAA,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;","names":[]}
|
|
@@ -1,13 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { Plugin } from '../vite';
|
|
2
2
|
/**
|
|
3
|
-
* A Vite plugin that integrates
|
|
4
|
-
*
|
|
5
|
-
* ```ts
|
|
6
|
-
* // Example usage of the plugin in a Vite configuration
|
|
7
|
-
* export default defineConfig({
|
|
8
|
-
* plugins: [ intLayerMiddlewarePlugin() ],
|
|
9
|
-
* });
|
|
10
|
-
* ```
|
|
3
|
+
* A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.
|
|
11
4
|
*/
|
|
12
|
-
export declare const intLayerMiddlewarePlugin: (
|
|
5
|
+
export declare const intLayerMiddlewarePlugin: () => Plugin;
|
|
13
6
|
//# sourceMappingURL=intlayerMiddlewarePlugin.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"intlayerMiddlewarePlugin.d.ts","sourceRoot":"","sources":["../../../src/vite/intlayerMiddlewarePlugin.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"intlayerMiddlewarePlugin.d.ts","sourceRoot":"","sources":["../../../src/vite/intlayerMiddlewarePlugin.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAW,MAAM,EAAE,MAAM,MAAM,CAAC;AA0B5C;;GAEG;AACH,eAAO,MAAM,wBAAwB,QAAO,MAiD3C,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-intlayer",
|
|
3
|
-
"version": "3.5.
|
|
3
|
+
"version": "3.5.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Internationalization layer for React applications. Declare your multilingual contant in the same lever than your component. Powered by TypeScript, declaration files.",
|
|
6
6
|
"keywords": [
|
|
@@ -92,11 +92,11 @@
|
|
|
92
92
|
"react-cookie": "^7.2.2",
|
|
93
93
|
"vite": "^6.0.3",
|
|
94
94
|
"webpack": "^5.96.1",
|
|
95
|
-
"@intlayer/chokidar": "^3.5.5",
|
|
96
95
|
"@intlayer/config": "^3.5.5",
|
|
96
|
+
"@intlayer/core": "^3.5.5",
|
|
97
97
|
"@intlayer/dictionaries-entry": "^3.5.5",
|
|
98
|
-
"@intlayer/
|
|
99
|
-
"@intlayer/
|
|
98
|
+
"@intlayer/chokidar": "^3.5.5",
|
|
99
|
+
"@intlayer/webpack": "^3.5.5"
|
|
100
100
|
},
|
|
101
101
|
"devDependencies": {
|
|
102
102
|
"@craco/types": "^7.1.0",
|
|
@@ -114,21 +114,21 @@
|
|
|
114
114
|
"tsc-alias": "^1.8.10",
|
|
115
115
|
"tsup": "^8.3.5",
|
|
116
116
|
"typescript": "^5.7.2",
|
|
117
|
-
"@utils/eslint-config": "^1.0.4",
|
|
118
117
|
"@utils/ts-config": "^1.0.4",
|
|
118
|
+
"@utils/tsup-config": "^1.0.4",
|
|
119
119
|
"@utils/ts-config-types": "^1.0.4",
|
|
120
|
-
"@utils/
|
|
120
|
+
"@utils/eslint-config": "^1.0.4"
|
|
121
121
|
},
|
|
122
122
|
"peerDependencies": {
|
|
123
123
|
"react": ">=16.0.0 <19.0.0",
|
|
124
124
|
"react-dom": ">=16.0.0 <19.0.0",
|
|
125
125
|
"vite": ">=4.0.0",
|
|
126
|
-
"@intlayer/config": "^3.5.5",
|
|
127
|
-
"@intlayer/chokidar": "^3.5.5",
|
|
128
126
|
"@intlayer/core": "^3.5.5",
|
|
127
|
+
"@intlayer/dictionaries-entry": "^3.5.5",
|
|
129
128
|
"@intlayer/webpack": "^3.5.5",
|
|
130
|
-
"intlayer": "^3.5.
|
|
131
|
-
"@intlayer/
|
|
129
|
+
"intlayer": "^3.5.6",
|
|
130
|
+
"@intlayer/config": "^3.5.5",
|
|
131
|
+
"@intlayer/chokidar": "^3.5.5"
|
|
132
132
|
},
|
|
133
133
|
"engines": {
|
|
134
134
|
"node": ">=14.18"
|