vite-intlayer 6.1.6 → 7.0.0-canary.3
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/dist/cjs/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/cjs/index.cjs +11 -27
- package/dist/cjs/intlayerMiddlewarePlugin.cjs +217 -224
- package/dist/cjs/intlayerMiddlewarePlugin.cjs.map +1 -1
- package/dist/cjs/intlayerPlugin.cjs +85 -100
- package/dist/cjs/intlayerPlugin.cjs.map +1 -1
- package/dist/cjs/intlayerPrunePlugin.cjs +85 -123
- package/dist/cjs/intlayerPrunePlugin.cjs.map +1 -1
- package/dist/esm/_virtual/rolldown_runtime.mjs +8 -0
- package/dist/esm/index.mjs +5 -4
- package/dist/esm/intlayerMiddlewarePlugin.mjs +210 -197
- package/dist/esm/intlayerMiddlewarePlugin.mjs.map +1 -1
- package/dist/esm/intlayerPlugin.mjs +78 -63
- package/dist/esm/intlayerPlugin.mjs.map +1 -1
- package/dist/esm/intlayerPrunePlugin.mjs +77 -87
- package/dist/esm/intlayerPrunePlugin.mjs.map +1 -1
- package/dist/types/index.d.ts +4 -4
- package/dist/types/intlayerMiddlewarePlugin.d.ts +9 -4
- package/dist/types/intlayerMiddlewarePlugin.d.ts.map +1 -1
- package/dist/types/intlayerPlugin.d.ts +10 -4
- package/dist/types/intlayerPlugin.d.ts.map +1 -1
- package/dist/types/intlayerPrunePlugin.d.ts +7 -3
- package/dist/types/intlayerPrunePlugin.d.ts.map +1 -1
- package/package.json +45 -44
- package/LICENSE +0 -202
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
|
|
25
|
+
exports.__toESM = __toESM;
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -1,27 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
15
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
16
|
-
var index_exports = {};
|
|
17
|
-
module.exports = __toCommonJS(index_exports);
|
|
18
|
-
__reExport(index_exports, require('./intlayerMiddlewarePlugin.cjs'), module.exports);
|
|
19
|
-
__reExport(index_exports, require('./intlayerPlugin.cjs'), module.exports);
|
|
20
|
-
__reExport(index_exports, require('./intlayerPrunePlugin.cjs'), module.exports);
|
|
21
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
22
|
-
0 && (module.exports = {
|
|
23
|
-
...require('./intlayerMiddlewarePlugin.cjs'),
|
|
24
|
-
...require('./intlayerPlugin.cjs'),
|
|
25
|
-
...require('./intlayerPrunePlugin.cjs')
|
|
26
|
-
});
|
|
27
|
-
//# sourceMappingURL=index.cjs.map
|
|
1
|
+
const require_intlayerMiddlewarePlugin = require('./intlayerMiddlewarePlugin.cjs');
|
|
2
|
+
const require_intlayerPrunePlugin = require('./intlayerPrunePlugin.cjs');
|
|
3
|
+
const require_intlayerPlugin = require('./intlayerPlugin.cjs');
|
|
4
|
+
|
|
5
|
+
exports.intLayerMiddleware = require_intlayerMiddlewarePlugin.intLayerMiddleware;
|
|
6
|
+
exports.intLayerPlugin = require_intlayerPlugin.intLayerPlugin;
|
|
7
|
+
exports.intlayer = require_intlayerPlugin.intlayer;
|
|
8
|
+
exports.intlayerMiddleware = require_intlayerMiddlewarePlugin.intlayerMiddleware;
|
|
9
|
+
exports.intlayerMiddlewarePlugin = require_intlayerMiddlewarePlugin.intlayerMiddlewarePlugin;
|
|
10
|
+
exports.intlayerPlugin = require_intlayerPlugin.intlayerPlugin;
|
|
11
|
+
exports.intlayerPrune = require_intlayerPrunePlugin.intlayerPrune;
|
|
@@ -1,244 +1,237 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var intlayerMiddlewarePlugin_exports = {};
|
|
20
|
-
__export(intlayerMiddlewarePlugin_exports, {
|
|
21
|
-
intLayerMiddleware: () => intLayerMiddleware,
|
|
22
|
-
intlayerMiddleware: () => intlayerMiddleware,
|
|
23
|
-
intlayerMiddlewarePlugin: () => intlayerMiddlewarePlugin
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(intlayerMiddlewarePlugin_exports);
|
|
26
|
-
var import_config = require("@intlayer/config");
|
|
27
|
-
var import_core = require("@intlayer/core");
|
|
28
|
-
var import_url = require("url");
|
|
29
|
-
const intlayerConfig = (0, import_config.getConfiguration)();
|
|
30
|
-
const { internationalization, middleware } = intlayerConfig;
|
|
1
|
+
const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
|
|
2
|
+
let node_url = require("node:url");
|
|
3
|
+
node_url = require_rolldown_runtime.__toESM(node_url);
|
|
4
|
+
let __intlayer_config = require("@intlayer/config");
|
|
5
|
+
__intlayer_config = require_rolldown_runtime.__toESM(__intlayer_config);
|
|
6
|
+
let __intlayer_core = require("@intlayer/core");
|
|
7
|
+
__intlayer_core = require_rolldown_runtime.__toESM(__intlayer_core);
|
|
8
|
+
|
|
9
|
+
//#region src/intlayerMiddlewarePlugin.ts
|
|
10
|
+
const { internationalization, routing } = (0, __intlayer_config.getConfiguration)();
|
|
31
11
|
const { locales: supportedLocales, defaultLocale } = internationalization;
|
|
32
|
-
const {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
12
|
+
const { headerName, basePath = "", mode } = routing;
|
|
13
|
+
const noPrefix = mode === "no-prefix" || mode === "search-params";
|
|
14
|
+
const prefixDefault = mode === "prefix-all";
|
|
15
|
+
/**
|
|
16
|
+
* @deprecated Rename to intlayerMiddleware instead
|
|
17
|
+
*
|
|
18
|
+
* A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.
|
|
19
|
+
*
|
|
20
|
+
* // Example usage of the plugin in a Vite configuration
|
|
21
|
+
* export default defineConfig({
|
|
22
|
+
* plugins: [ intlayerMiddleware() ],
|
|
23
|
+
* });
|
|
24
|
+
*/
|
|
40
25
|
const intlayerMiddlewarePlugin = () => {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
26
|
+
return {
|
|
27
|
+
name: "vite-intlayer-middleware-plugin",
|
|
28
|
+
configureServer: (server) => {
|
|
29
|
+
server.middlewares.use((req, res, next) => {
|
|
30
|
+
if (req.url?.startsWith("/node_modules") || req.url?.startsWith("/@") || req.url?.split("?")[0].match(/\.[a-z]+$/i)) return next();
|
|
31
|
+
const parsedUrl = (0, node_url.parse)(req.url ?? "/", true);
|
|
32
|
+
const originalPath = parsedUrl.pathname ?? "/";
|
|
33
|
+
const searchParams = parsedUrl.search ?? "";
|
|
34
|
+
const storageLocale = getStorageLocale(req);
|
|
35
|
+
const pathLocale = getPathLocale(originalPath);
|
|
36
|
+
if (noPrefix) {
|
|
37
|
+
handleNoPrefix({
|
|
38
|
+
req,
|
|
39
|
+
res,
|
|
40
|
+
next,
|
|
41
|
+
originalPath,
|
|
42
|
+
searchParams,
|
|
43
|
+
storageLocale
|
|
44
|
+
});
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
handlePrefix({
|
|
48
|
+
req,
|
|
49
|
+
res,
|
|
50
|
+
next,
|
|
51
|
+
originalPath,
|
|
52
|
+
searchParams,
|
|
53
|
+
pathLocale,
|
|
54
|
+
storageLocale
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
74
59
|
};
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
60
|
+
/**
|
|
61
|
+
* Retrieves the locale from storage (cookies, localStorage, sessionStorage).
|
|
62
|
+
*/
|
|
63
|
+
const getStorageLocale = (req) => {
|
|
64
|
+
return (0, __intlayer_core.getLocaleFromStorage)({ getCookie: (name) => {
|
|
65
|
+
return (req.headers.cookie ?? "").split(";").reduce((acc, cookie) => {
|
|
66
|
+
const [key, val] = cookie.trim().split("=");
|
|
67
|
+
acc[key] = val;
|
|
68
|
+
return acc;
|
|
69
|
+
}, {})[name] ?? null;
|
|
70
|
+
} });
|
|
84
71
|
};
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Appends locale to search params when routing mode is 'search-params'.
|
|
74
|
+
*/
|
|
75
|
+
const appendLocaleSearchIfNeeded = (search, locale) => {
|
|
76
|
+
if (mode !== "search-params") return search;
|
|
77
|
+
const params = new URLSearchParams(search ?? "");
|
|
78
|
+
params.set("locale", locale);
|
|
79
|
+
return `?${params.toString()}`;
|
|
90
80
|
};
|
|
81
|
+
/**
|
|
82
|
+
* Extracts the locale from the URL pathname if present as the first segment.
|
|
83
|
+
*/
|
|
91
84
|
const getPathLocale = (pathname) => {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
if (firstSegment && supportedLocales.includes(firstSegment)) {
|
|
95
|
-
return firstSegment;
|
|
96
|
-
}
|
|
97
|
-
return void 0;
|
|
85
|
+
const firstSegment = pathname.split("/").filter(Boolean)[0];
|
|
86
|
+
if (firstSegment && supportedLocales.includes(firstSegment)) return firstSegment;
|
|
98
87
|
};
|
|
88
|
+
/**
|
|
89
|
+
* Writes a 301 redirect response with the given new URL.
|
|
90
|
+
*/
|
|
99
91
|
const redirectUrl = (res, newUrl) => {
|
|
100
|
-
|
|
101
|
-
|
|
92
|
+
res.writeHead(301, { Location: newUrl });
|
|
93
|
+
return res.end();
|
|
102
94
|
};
|
|
95
|
+
/**
|
|
96
|
+
* "Rewrite" the request internally by adjusting req.url;
|
|
97
|
+
* we also set the locale in the response header if needed.
|
|
98
|
+
*/
|
|
103
99
|
const rewriteUrl = (req, res, newUrl, locale) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
res.setHeader(headerName, locale);
|
|
107
|
-
}
|
|
100
|
+
req.url = newUrl;
|
|
101
|
+
if (locale && headerName) res.setHeader(headerName, locale);
|
|
108
102
|
};
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
103
|
+
/**
|
|
104
|
+
* Constructs a new path string, optionally including a locale prefix, basePath, and search parameters.
|
|
105
|
+
* - basePath: (e.g., '/myapp')
|
|
106
|
+
* - locale: (e.g., 'en')
|
|
107
|
+
* - currentPath:(e.g., '/products/shoes')
|
|
108
|
+
* - search: (e.g., '?foo=bar')
|
|
109
|
+
*/
|
|
110
|
+
const constructPath = (locale, currentPath, search) => {
|
|
111
|
+
const cleanBasePath = basePath.startsWith("/") ? basePath : `/${basePath}`;
|
|
112
|
+
const normalizedBasePath = cleanBasePath === "/" ? "" : cleanBasePath;
|
|
113
|
+
let newPath = `${normalizedBasePath}${mode === "search-params" ? currentPath : `/${locale}${currentPath}`}`;
|
|
114
|
+
if (!prefixDefault && locale === defaultLocale && mode !== "search-params") newPath = `${normalizedBasePath}${currentPath}`;
|
|
115
|
+
if (search) newPath += search;
|
|
116
|
+
return newPath;
|
|
117
117
|
};
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
req.headers,
|
|
129
|
-
supportedLocales,
|
|
130
|
-
defaultLocale
|
|
131
|
-
);
|
|
132
|
-
locale = detectedLocale;
|
|
133
|
-
}
|
|
134
|
-
rewriteUrl(req, res, originalPath, locale);
|
|
135
|
-
return next();
|
|
118
|
+
/**
|
|
119
|
+
* If `noPrefix` is true, we never prefix the locale in the URL.
|
|
120
|
+
* We simply rewrite the request to the same path, but with the best-chosen locale
|
|
121
|
+
* in a header or search params if desired.
|
|
122
|
+
*/
|
|
123
|
+
const handleNoPrefix = ({ req, res, next, originalPath, searchParams, storageLocale }) => {
|
|
124
|
+
let locale = storageLocale ?? defaultLocale;
|
|
125
|
+
if (!storageLocale) locale = (0, __intlayer_core.localeDetector)(req.headers, supportedLocales, defaultLocale);
|
|
126
|
+
rewriteUrl(req, res, constructPath(locale, originalPath, appendLocaleSearchIfNeeded(searchParams, locale)), locale);
|
|
127
|
+
return next();
|
|
136
128
|
};
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
129
|
+
/**
|
|
130
|
+
* The main prefix logic:
|
|
131
|
+
* - If there's no pathLocale in the URL, we might want to detect & redirect or rewrite
|
|
132
|
+
* - If there is a pathLocale, handle storage mismatch or default locale special cases
|
|
133
|
+
*/
|
|
134
|
+
const handlePrefix = ({ req, res, next, originalPath, searchParams, pathLocale, storageLocale }) => {
|
|
135
|
+
if (!pathLocale) {
|
|
136
|
+
handleMissingPathLocale({
|
|
137
|
+
req,
|
|
138
|
+
res,
|
|
139
|
+
next,
|
|
140
|
+
originalPath,
|
|
141
|
+
searchParams,
|
|
142
|
+
storageLocale
|
|
143
|
+
});
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
handleExistingPathLocale({
|
|
147
|
+
req,
|
|
148
|
+
res,
|
|
149
|
+
next,
|
|
150
|
+
originalPath,
|
|
151
|
+
searchParams,
|
|
152
|
+
pathLocale,
|
|
153
|
+
storageLocale
|
|
154
|
+
});
|
|
163
155
|
};
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
let locale = cookieLocale ?? (0, import_core.localeDetector)(
|
|
186
|
-
req.headers,
|
|
187
|
-
supportedLocales,
|
|
188
|
-
defaultLocale
|
|
189
|
-
);
|
|
190
|
-
if (!supportedLocales.includes(locale)) {
|
|
191
|
-
locale = defaultLocale;
|
|
192
|
-
}
|
|
193
|
-
const newPath = constructPath(locale, originalPath);
|
|
194
|
-
if (prefixDefault || locale !== defaultLocale) {
|
|
195
|
-
return redirectUrl(res, newPath);
|
|
196
|
-
}
|
|
197
|
-
rewriteUrl(req, res, newPath, locale);
|
|
198
|
-
return next();
|
|
156
|
+
/**
|
|
157
|
+
* Handles requests where the locale is missing from the URL pathname.
|
|
158
|
+
* We detect a locale from storage / headers / default, then either redirect or rewrite.
|
|
159
|
+
*/
|
|
160
|
+
const handleMissingPathLocale = ({ req, res, next, originalPath, searchParams, storageLocale }) => {
|
|
161
|
+
const referer = req.headers.referer || req.headers.referrer;
|
|
162
|
+
if (referer) try {
|
|
163
|
+
const refererUrl = new URL(referer);
|
|
164
|
+
const host = req.headers.host;
|
|
165
|
+
if (host && refererUrl.host === host) {
|
|
166
|
+
const locale$1 = defaultLocale;
|
|
167
|
+
rewriteUrl(req, res, constructPath(locale$1, originalPath, appendLocaleSearchIfNeeded(searchParams, locale$1)), locale$1);
|
|
168
|
+
return next();
|
|
169
|
+
}
|
|
170
|
+
} catch {}
|
|
171
|
+
let locale = storageLocale ?? (0, __intlayer_core.localeDetector)(req.headers, supportedLocales, defaultLocale);
|
|
172
|
+
if (!supportedLocales.includes(locale)) locale = defaultLocale;
|
|
173
|
+
const newPath = constructPath(locale, originalPath, appendLocaleSearchIfNeeded(searchParams, locale));
|
|
174
|
+
if (prefixDefault || locale !== defaultLocale) return redirectUrl(res, newPath);
|
|
175
|
+
rewriteUrl(req, res, newPath, locale);
|
|
176
|
+
return next();
|
|
199
177
|
};
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
req,
|
|
215
|
-
res,
|
|
216
|
-
next,
|
|
217
|
-
originalPath,
|
|
218
|
-
pathLocale
|
|
219
|
-
});
|
|
178
|
+
/**
|
|
179
|
+
* Handles requests where the locale prefix is present in the pathname.
|
|
180
|
+
* We verify if the storage locale differs from the path locale; if so, handle.
|
|
181
|
+
*/
|
|
182
|
+
const handleExistingPathLocale = ({ req, res, next, originalPath, searchParams, pathLocale, storageLocale }) => {
|
|
183
|
+
if (storageLocale && storageLocale !== pathLocale) return redirectUrl(res, constructPath(storageLocale, originalPath.replace(`/${pathLocale}`, `/${storageLocale}`).replace(/^\/+/, "/"), appendLocaleSearchIfNeeded(searchParams, storageLocale)));
|
|
184
|
+
handleDefaultLocaleRedirect({
|
|
185
|
+
req,
|
|
186
|
+
res,
|
|
187
|
+
next,
|
|
188
|
+
originalPath,
|
|
189
|
+
searchParams,
|
|
190
|
+
pathLocale
|
|
191
|
+
});
|
|
220
192
|
};
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
193
|
+
/**
|
|
194
|
+
* If the path locale is the default locale but we don't want to prefix the default, remove it.
|
|
195
|
+
*/
|
|
196
|
+
const handleDefaultLocaleRedirect = ({ req, res, next, originalPath, searchParams, pathLocale }) => {
|
|
197
|
+
if (!prefixDefault && pathLocale === defaultLocale) {
|
|
198
|
+
let newPath = originalPath.replace(`/${defaultLocale}`, "") || "/";
|
|
199
|
+
const searchWithLocale$1 = appendLocaleSearchIfNeeded(searchParams, pathLocale);
|
|
200
|
+
if (searchWithLocale$1) newPath += searchWithLocale$1;
|
|
201
|
+
rewriteUrl(req, res, newPath, pathLocale);
|
|
202
|
+
return next();
|
|
203
|
+
}
|
|
204
|
+
const searchWithLocale = appendLocaleSearchIfNeeded(searchParams, pathLocale);
|
|
205
|
+
rewriteUrl(req, res, searchWithLocale ? `${originalPath}${searchWithLocale}` : originalPath, pathLocale);
|
|
206
|
+
return next();
|
|
235
207
|
};
|
|
208
|
+
/**
|
|
209
|
+
* A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.
|
|
210
|
+
*
|
|
211
|
+
* ```ts
|
|
212
|
+
* // Example usage of the plugin in a Vite configuration
|
|
213
|
+
* export default defineConfig({
|
|
214
|
+
* plugins: [ intlayerMiddleware() ],
|
|
215
|
+
* });
|
|
216
|
+
* ```
|
|
217
|
+
*/
|
|
236
218
|
const intlayerMiddleware = intlayerMiddlewarePlugin;
|
|
219
|
+
/**
|
|
220
|
+
* @deprecated Rename to intlayerMiddleware instead
|
|
221
|
+
*
|
|
222
|
+
* A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.
|
|
223
|
+
|
|
224
|
+
* ```ts
|
|
225
|
+
* // Example usage of the plugin in a Vite configuration
|
|
226
|
+
* export default defineConfig({
|
|
227
|
+
* plugins: [ intlayerMiddleware() ],
|
|
228
|
+
* });
|
|
229
|
+
* ```
|
|
230
|
+
*/
|
|
237
231
|
const intLayerMiddleware = intlayerMiddlewarePlugin;
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
});
|
|
232
|
+
|
|
233
|
+
//#endregion
|
|
234
|
+
exports.intLayerMiddleware = intLayerMiddleware;
|
|
235
|
+
exports.intlayerMiddleware = intlayerMiddleware;
|
|
236
|
+
exports.intlayerMiddlewarePlugin = intlayerMiddlewarePlugin;
|
|
244
237
|
//# sourceMappingURL=intlayerMiddlewarePlugin.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import { getConfiguration, type Locales } from '@intlayer/config';\nimport { localeDetector } from '@intlayer/core';\nimport type { IncomingMessage, ServerResponse } from 'http';\nimport { parse } from 'url';\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 * @deprecated Rename to intlayerMiddleware instead\n *\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n *\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\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 as Locales;\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 // If navigation comes from the same origin (e.g., via an in-app language switcher),\n // treat unprefixed paths as an explicit intent to view the default locale.\n // This avoids redirecting back to the cookie locale (e.g., '/tr/') when user selects '/'.\n const referer = (req.headers.referer || req.headers.referrer) as\n | string\n | undefined;\n if (referer) {\n try {\n const refererUrl = new URL(referer);\n const host = req.headers.host;\n if (host && refererUrl.host === host) {\n const locale = defaultLocale as Locales;\n const newPath = constructPath(locale, originalPath);\n rewriteUrl(req, res, newPath, locale);\n return next();\n }\n } catch {\n // ignore invalid referer\n }\n }\n\n // 1. Choose the best locale\n let locale = (cookieLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n )) as Locales;\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\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intlayerMiddleware = intlayerMiddlewarePlugin;\n\n/**\n * @deprecated Rename to intlayerMiddleware instead\n * \n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intLayerMiddleware = intlayerMiddlewarePlugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAA+C;AAC/C,kBAA+B;AAE/B,iBAAsB;AAetB,MAAM,qBAAiB,gCAAiB;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;AAYG,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,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,EACd;AAGA,aAAW,KAAK,KAAK,cAAc,UAAU;AAC7C,SAAO,KAAK;AACd;AAYO,MAAM,qBAAqB;AAc3B,MAAM,qBAAqB;","names":["locale","newPath"]}
|
|
1
|
+
{"version":3,"file":"intlayerMiddlewarePlugin.cjs","names":["locale","searchWithLocale"],"sources":["../../src/intlayerMiddlewarePlugin.ts"],"sourcesContent":["import type { IncomingMessage, ServerResponse } from 'node:http';\nimport { parse } from 'node:url';\nimport { getConfiguration } from '@intlayer/config';\nimport { getLocaleFromStorage, localeDetector } from '@intlayer/core';\nimport type { Locale } from '@intlayer/types';\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// - routing.mode\n// - routing.storage\n// - routing.headerName\n// - routing.basePath\n// - etc.\nconst intlayerConfig = getConfiguration();\nconst { internationalization, routing } = intlayerConfig;\nconst { locales: supportedLocales, defaultLocale } = internationalization;\n\nconst { headerName, basePath = '', mode } = routing;\n\n// Derived flags from routing.mode\nconst noPrefix = mode === 'no-prefix' || mode === 'search-params';\nconst prefixDefault = mode === 'prefix-all';\n\n/**\n * @deprecated Rename to intlayerMiddleware instead\n *\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n *\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\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 const searchParams = parsedUrl.search ?? '';\n\n // 3. Attempt to read the locale from storage (cookies, localStorage, etc.)\n const storageLocale = getStorageLocale(req);\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 searchParams,\n storageLocale,\n });\n return;\n }\n\n // 6. Otherwise, handle prefix logic\n handlePrefix({\n req,\n res,\n next,\n originalPath,\n searchParams,\n pathLocale,\n storageLocale,\n });\n });\n },\n };\n};\n\n/* --------------------------------------------------------------------\n * Helper & Utility Functions\n * --------------------------------------------------------------------\n */\n\n/**\n * Retrieves the locale from storage (cookies, localStorage, sessionStorage).\n */\nconst getStorageLocale = (req: IncomingMessage): Locale | undefined => {\n const locale = getLocaleFromStorage({\n getCookie: (name: string) => {\n const cookieHeader = req.headers.cookie ?? '';\n const cookies = 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 return cookies[name] ?? null;\n },\n });\n return locale;\n};\n\n/**\n * Appends locale to search params when routing mode is 'search-params'.\n */\nconst appendLocaleSearchIfNeeded = (\n search: string | undefined,\n locale: Locale\n): string | undefined => {\n if (mode !== 'search-params') return search;\n\n const params = new URLSearchParams(search ?? '');\n params.set('locale', locale);\n return `?${params.toString()}`;\n};\n\n/**\n * Extracts the locale from the URL pathname if present as the first segment.\n */\nconst getPathLocale = (pathname: string): Locale | 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 Locale)) {\n return firstSegment as Locale;\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?: Locale\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, basePath, and search parameters.\n * - basePath: (e.g., '/myapp')\n * - locale: (e.g., 'en')\n * - currentPath:(e.g., '/products/shoes')\n * - search: (e.g., '?foo=bar')\n */\nconst constructPath = (\n locale: Locale,\n currentPath: string,\n search?: string\n) => {\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 // In 'search-params' mode, we do not prefix the path with the locale\n const pathWithLocalePrefix =\n mode === 'search-params' ? currentPath : `/${locale}${currentPath}`;\n\n // Combine basePath + locale prefix + the rest of the path\n let newPath = `${normalizedBasePath}${pathWithLocalePrefix}`;\n\n // Special case: if prefixDefault is false and locale is defaultLocale, remove the locale prefix\n if (!prefixDefault && locale === defaultLocale && mode !== 'search-params') {\n newPath = `${normalizedBasePath}${currentPath}`;\n }\n\n // Append search parameters if provided\n if (search) {\n newPath += search;\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 search params if desired.\n */\nconst handleNoPrefix = ({\n req,\n res,\n next,\n originalPath,\n searchParams,\n storageLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n searchParams: string;\n storageLocale?: Locale;\n}) => {\n // Determine the best locale\n let locale = storageLocale ?? defaultLocale;\n\n // Use fallback to localeDetector if no storage locale\n if (!storageLocale) {\n const detectedLocale = localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n );\n locale = detectedLocale as Locale;\n }\n\n // Construct the new path with locale in search params if needed\n const newPath = constructPath(\n locale,\n originalPath,\n appendLocaleSearchIfNeeded(searchParams, locale)\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, newPath, 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 storage mismatch or default locale special cases\n */\nconst handlePrefix = ({\n req,\n res,\n next,\n originalPath,\n searchParams,\n pathLocale,\n storageLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n searchParams: string;\n pathLocale?: Locale;\n storageLocale?: Locale;\n}) => {\n // 1. If pathLocale is missing, handle\n if (!pathLocale) {\n handleMissingPathLocale({\n req,\n res,\n next,\n originalPath,\n searchParams,\n storageLocale,\n });\n return;\n }\n\n // 2. If pathLocale exists, handle possible mismatch with storage\n handleExistingPathLocale({\n req,\n res,\n next,\n originalPath,\n searchParams,\n pathLocale,\n storageLocale,\n });\n};\n\n/**\n * Handles requests where the locale is missing from the URL pathname.\n * We detect a locale from storage / headers / default, then either redirect or rewrite.\n */\nconst handleMissingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n searchParams,\n storageLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n searchParams: string;\n storageLocale?: Locale;\n}) => {\n // If navigation comes from the same origin (e.g., via an in-app language switcher),\n // treat unprefixed paths as an explicit intent to view the default locale.\n // This avoids redirecting back to the storage locale (e.g., '/tr/') when user selects '/'.\n const referer = (req.headers.referer || req.headers.referrer) as\n | string\n | undefined;\n if (referer) {\n try {\n const refererUrl = new URL(referer);\n const host = req.headers.host;\n if (host && refererUrl.host === host) {\n const locale = defaultLocale as Locale;\n const newPath = constructPath(\n locale,\n originalPath,\n appendLocaleSearchIfNeeded(searchParams, locale)\n );\n rewriteUrl(req, res, newPath, locale);\n return next();\n }\n } catch {\n // ignore invalid referer\n }\n }\n\n // 1. Choose the best locale\n let locale = (storageLocale ??\n localeDetector(\n req.headers as Record<string, string>,\n supportedLocales,\n defaultLocale\n )) as Locale;\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(\n locale,\n originalPath,\n appendLocaleSearchIfNeeded(searchParams, locale)\n );\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 storage locale differs from the path locale; if so, handle.\n */\nconst handleExistingPathLocale = ({\n req,\n res,\n next,\n originalPath,\n searchParams,\n pathLocale,\n storageLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n searchParams: string;\n pathLocale: Locale;\n storageLocale?: Locale;\n}) => {\n // 1. If the storage locale is set and differs from the path locale, redirect\n if (storageLocale && storageLocale !== pathLocale) {\n // We want to swap out the pathLocale with the storageLocale\n const newPath = originalPath.replace(`/${pathLocale}`, `/${storageLocale}`);\n const finalPath = constructPath(\n storageLocale,\n newPath.replace(/^\\/+/, '/'),\n appendLocaleSearchIfNeeded(searchParams, storageLocale)\n );\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 searchParams,\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 searchParams,\n pathLocale,\n}: {\n req: Connect.IncomingMessage;\n res: ServerResponse<IncomingMessage>;\n next: Connect.NextFunction;\n originalPath: string;\n searchParams: string;\n pathLocale: Locale;\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 let newPath = originalPath.replace(`/${defaultLocale}`, '') || '/';\n const searchWithLocale = appendLocaleSearchIfNeeded(\n searchParams,\n pathLocale\n );\n if (searchWithLocale) {\n newPath += searchWithLocale;\n }\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 const searchWithLocale = appendLocaleSearchIfNeeded(searchParams, pathLocale);\n const newPath = searchWithLocale\n ? `${originalPath}${searchWithLocale}`\n : originalPath;\n rewriteUrl(req, res, newPath, pathLocale);\n return next();\n};\n\n/**\n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n *\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intlayerMiddleware = intlayerMiddlewarePlugin;\n\n/**\n * @deprecated Rename to intlayerMiddleware instead\n * \n * A Vite plugin that integrates a logic similar to the Next.js intlayer middleware.\n\n * ```ts\n * // Example usage of the plugin in a Vite configuration\n * export default defineConfig({\n * plugins: [ intlayerMiddleware() ],\n * });\n * ```\n */\nexport const intLayerMiddleware = intlayerMiddlewarePlugin;\n"],"mappings":";;;;;;;;;AAkBA,MAAM,EAAE,sBAAsB,qDADW;AAEzC,MAAM,EAAE,SAAS,kBAAkB,kBAAkB;AAErD,MAAM,EAAE,YAAY,WAAW,IAAI,SAAS;AAG5C,MAAM,WAAW,SAAS,eAAe,SAAS;AAClD,MAAM,gBAAgB,SAAS;;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