@inlang/paraglide-js 2.0.12 → 2.0.13
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/cli/steps/initialize-inlang-project.d.ts.map +1 -1
- package/dist/cli/utils.d.ts.map +1 -1
- package/dist/compiler/compile-bundle.d.ts.map +1 -1
- package/dist/compiler/compile-message.d.ts.map +1 -1
- package/dist/compiler/compile-pattern.d.ts.map +1 -1
- package/dist/compiler/compile-project.d.ts.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-url.d.ts.map +1 -1
- package/dist/compiler/runtime/extract-locale-from-url.js +35 -13
- package/dist/compiler/runtime/localize-href.d.ts.map +1 -1
- package/dist/compiler/runtime/localize-href.js +7 -4
- package/dist/compiler/server/middleware.d.ts +4 -2
- package/dist/compiler/server/middleware.d.ts.map +1 -1
- package/dist/compiler/server/middleware.js +5 -3
- package/dist/compiler/server/middleware.test.js +78 -0
- package/dist/services/codegen/quotes.d.ts.map +1 -1
- package/dist/services/env-variables/index.js +1 -1
- package/dist/services/telemetry/capture.d.ts.map +1 -1
- package/dist/utilities/detect-json-formatting.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"initialize-inlang-project.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/initialize-inlang-project.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,aAAa,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,eAAO,MAAM,uBAAuB,EAAE,OAAO,CAC5C;IACC,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;IACvB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACb,EACD;IACC,OAAO,EAAE,aAAa,CAAC;IACvB,mCAAmC;IACnC,WAAW,EAAE,MAAM,CAAC;CACpB,CA2BD,CAAC;AAgHF,eAAO,MAAM,oBAAoB,
|
|
1
|
+
{"version":3,"file":"initialize-inlang-project.d.ts","sourceRoot":"","sources":["../../../src/cli/steps/initialize-inlang-project.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,KAAK,aAAa,EAClB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAG3C,OAAO,EAAE,MAAM,SAAS,CAAC;AAGzB,eAAO,MAAM,uBAAuB,EAAE,OAAO,CAC5C;IACC,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;IACvB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;CACb,EACD;IACC,OAAO,EAAE,aAAa,CAAC;IACvB,mCAAmC;IACnC,WAAW,EAAE,MAAM,CAAC;CACpB,CA2BD,CAAC;AAgHF,eAAO,MAAM,oBAAoB,GAAU,KAAK;IAC/C,EAAE,EAAE,OAAO,EAAE,CAAC,QAAQ,CAAC;IACvB,MAAM,EAAE,OAAO,EAAE,CAAC;IAClB,MAAM,EAAE,MAAM,CAAC;CACf,KAAG,OAAO,CAAC;IACX,OAAO,EAAE,aAAa,CAAC;IACvB,8CAA8C;IAC9C,WAAW,EAAE,MAAM,CAAC;CACpB,CAwEA,CAAC;AAEF;;;;;;;;;GASG;AACH,wBAAgB,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,MAAM,CAazD;AAcD;;GAEG;AACH,eAAO,MAAM,OAAO,sbACga,CAAC"}
|
package/dist/cli/utils.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/cli/utils.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,SAAS,EAAE,EAC5D,GAAG,EAAE,GAAG,KACJ,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;AAExB;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,OAAO,OAAO,CAAC,MAMnC,CAAC;AAEF,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,MAAM,
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/cli/utils.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAE9B;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,SAAS,EAAE,EAC5D,GAAG,EAAE,GAAG,KACJ,OAAO,CAAC,GAAG,GAAG,GAAG,CAAC,CAAC;AAExB;;GAEG;AACH,eAAO,MAAM,MAAM,EAAE,OAAO,OAAO,CAAC,MAMnC,CAAC;AAEF,eAAO,MAAM,eAAe,GAAU,CAAC,SAAS,MAAM,EACrD,SAAS,MAAM,EACf,UAAS;IAAE,OAAO,CAAC,EAAE,CAAC,CAAC;IAAC,OAAO,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,CAAC,CAAA;KAAE,EAAE,CAAA;CAE7D,KACC,OAAO,CAAC,CAAC,CAKX,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile-bundle.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-bundle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C,MAAM,MAAM,0BAA0B,GAAG;IACxC,kDAAkD;IAClD,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACzB,gDAAgD;IAChD,QAAQ,EAAE;QACT,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;KACpC,CAAC;CACF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,
|
|
1
|
+
{"version":3,"file":"compile-bundle.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-bundle.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,YAAY,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C,MAAM,MAAM,0BAA0B,GAAG;IACxC,kDAAkD;IAClD,MAAM,EAAE,QAAQ,CAAC,MAAM,CAAC,CAAC;IACzB,gDAAgD;IAChD,QAAQ,EAAE;QACT,CAAC,MAAM,EAAE,MAAM,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC;KACpC,CAAC;CACF,CAAC;AAEF;;GAEG;AACH,eAAO,MAAM,aAAa,GAAI,MAAM;IACnC,MAAM,EAAE,YAAY,CAAC;IACrB,WAAW,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;IAChD,0BAA0B,EAAE,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,KAAK,MAAM,CAAC;CACzE,KAAG,0BA0BH,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile-message.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C;;;GAGG;AACH,eAAO,MAAM,cAAc,
|
|
1
|
+
{"version":3,"file":"compile-message.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-message.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAEjE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAK3C;;;GAGG;AACH,eAAO,MAAM,cAAc,GAC1B,cAAc,WAAW,EAAE,EAC3B,SAAS,OAAO,EAChB,UAAU,OAAO,EAAE,KACjB,QAAQ,CAAC,OAAO,CAUlB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile-pattern.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAqB,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,
|
|
1
|
+
{"version":3,"file":"compile-pattern.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-pattern.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,OAAO,EAAqB,MAAM,aAAa,CAAC;AAC3E,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAG3C;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,GAAI,MAAM;IACpC,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,WAAW,EAAE,CAAC;CAC5B,KAAG,QAAQ,CAAC,OAAO,CA4BnB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compile-project.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-project.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAIrE,OAAO,EAEN,KAAK,eAAe,EACpB,MAAM,uBAAuB,CAAC;AAU/B;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,
|
|
1
|
+
{"version":3,"file":"compile-project.d.ts","sourceRoot":"","sources":["../../src/compiler/compile-project.ts"],"names":[],"mappings":"AACA,OAAO,EAAsB,KAAK,aAAa,EAAE,MAAM,aAAa,CAAC;AAIrE,OAAO,EAEN,KAAK,eAAe,EACpB,MAAM,uBAAuB,CAAC;AAU/B;;;;;;;;;GASG;AACH,eAAO,MAAM,cAAc,GAAU,MAAM;IAC1C,OAAO,EAAE,aAAa,CAAC;IACvB,eAAe,CAAC,EAAE,IAAI,CAAC,eAAe,EAAE,IAAI,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC;CACrE,KAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAsEjC,CAAC;AAEF,wBAAgB,cAAc,CAAC,CAAC,SAAS,MAAM,EAC9C,OAAO,EAAE,CAAC,EAAE,EACZ,UAAU,EAAE,OAAO,CAAC,CAAC,CAAC,GACpB,MAAM,CAAC,CAAC,EAAE,CAAC,GAAG,SAAS,CAAC,CAY1B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"extract-locale-from-url.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-url.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"extract-locale-from-url.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/extract-locale-from-url.js"],"names":[],"mappings":"AAmBA;;;;;GAKG;AACH,0CAHW,GAAG,GAAC,MAAM,GACR,MAAM,GAAC,SAAS,CAuC5B"}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { assertIsLocale } from "./assert-is-locale.js";
|
|
2
2
|
import { isLocale } from "./is-locale.js";
|
|
3
3
|
import { baseLocale, TREE_SHAKE_DEFAULT_URL_PATTERN_USED, urlPatterns, } from "./variables.js";
|
|
4
|
+
/**
|
|
5
|
+
* If extractLocaleFromUrl is called many times on the same page and the URL
|
|
6
|
+
* hasn't changed, we don't need to recompute it every time which can get expensive.
|
|
7
|
+
* We might use a LRU cache if needed, but for now storing only the last result is enough.
|
|
8
|
+
* https://github.com/opral/monorepo/pull/3575#discussion_r2066731243
|
|
9
|
+
*/
|
|
10
|
+
/** @type {string|undefined} */
|
|
11
|
+
let cachedUrl;
|
|
12
|
+
/** @type {Locale|undefined} */
|
|
13
|
+
let cachedLocale;
|
|
4
14
|
/**
|
|
5
15
|
* Extracts the locale from a given URL using native URLPattern.
|
|
6
16
|
*
|
|
@@ -8,24 +18,36 @@ import { baseLocale, TREE_SHAKE_DEFAULT_URL_PATTERN_USED, urlPatterns, } from ".
|
|
|
8
18
|
* @returns {Locale|undefined} The extracted locale, or undefined if no locale is found.
|
|
9
19
|
*/
|
|
10
20
|
export function extractLocaleFromUrl(url) {
|
|
21
|
+
const urlString = typeof url === "string" ? url : url.href;
|
|
22
|
+
if (cachedUrl === urlString) {
|
|
23
|
+
return cachedLocale;
|
|
24
|
+
}
|
|
25
|
+
let result;
|
|
11
26
|
if (TREE_SHAKE_DEFAULT_URL_PATTERN_USED) {
|
|
12
|
-
|
|
27
|
+
result = defaultUrlPatternExtractLocale(url);
|
|
13
28
|
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
for (const
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
29
|
+
else {
|
|
30
|
+
const urlObj = typeof url === "string" ? new URL(url) : url;
|
|
31
|
+
// Iterate over URL patterns
|
|
32
|
+
for (const element of urlPatterns) {
|
|
33
|
+
for (const [locale, localizedPattern] of element.localized) {
|
|
34
|
+
const match = new URLPattern(localizedPattern, urlObj.href).exec(urlObj.href);
|
|
35
|
+
if (!match) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
// Check if the locale is valid
|
|
39
|
+
if (assertIsLocale(locale)) {
|
|
40
|
+
result = locale;
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
25
43
|
}
|
|
44
|
+
if (result)
|
|
45
|
+
break;
|
|
26
46
|
}
|
|
27
47
|
}
|
|
28
|
-
|
|
48
|
+
cachedUrl = urlString;
|
|
49
|
+
cachedLocale = result;
|
|
50
|
+
return result;
|
|
29
51
|
}
|
|
30
52
|
/**
|
|
31
53
|
* https://github.com/opral/inlang-paraglide-js/issues/381
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"localize-href.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/localize-href.js"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,mCALW,MAAM,YAEd;IAAyB,MAAM;CAC/B,GAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"localize-href.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/localize-href.js"],"names":[],"mappings":"AAIA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,mCALW,MAAM,YAEd;IAAyB,MAAM;CAC/B,GAAU,MAAM,CAyBlB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,qCAJW,MAAM,GACJ,MAAM,CAelB"}
|
|
@@ -40,15 +40,18 @@ import { deLocalizeUrl, localizeUrl } from "./localize-url.js";
|
|
|
40
40
|
* @returns {string} The localized href, relative if input was relative
|
|
41
41
|
*/
|
|
42
42
|
export function localizeHref(href, options) {
|
|
43
|
-
const
|
|
43
|
+
const currentLocale = getLocale();
|
|
44
|
+
const locale = options?.locale ?? currentLocale;
|
|
44
45
|
const url = new URL(href, getUrlOrigin());
|
|
45
|
-
const localized = localizeUrl(url,
|
|
46
|
+
const localized = localizeUrl(url, { locale });
|
|
46
47
|
// if the origin is identical and the href is relative,
|
|
47
48
|
// return the relative path
|
|
48
49
|
if (href.startsWith("/") && url.origin === localized.origin) {
|
|
49
50
|
// check for cross origin localization in which case an absolute URL must be returned.
|
|
50
|
-
if (locale !==
|
|
51
|
-
const localizedCurrentLocale = localizeUrl(url, {
|
|
51
|
+
if (locale !== currentLocale) {
|
|
52
|
+
const localizedCurrentLocale = localizeUrl(url, {
|
|
53
|
+
locale: currentLocale,
|
|
54
|
+
});
|
|
52
55
|
if (localizedCurrentLocale.origin !== localized.origin) {
|
|
53
56
|
return localized.href;
|
|
54
57
|
}
|
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
*
|
|
18
18
|
* @param {Request} request - The incoming request object
|
|
19
19
|
* @param {(args: { request: Request, locale: import("./runtime.js").Locale }) => T | Promise<T>} resolve - Function to handle the request
|
|
20
|
-
*
|
|
20
|
+
* @param {{ onRedirect:(response: Response) => void }} [callbacks] - Callbacks to handle events from middleware
|
|
21
21
|
* @returns {Promise<Response>}
|
|
22
22
|
*
|
|
23
23
|
* @example
|
|
@@ -62,5 +62,7 @@
|
|
|
62
62
|
export function paraglideMiddleware<T>(request: Request, resolve: (args: {
|
|
63
63
|
request: Request;
|
|
64
64
|
locale: import("./runtime.js").Locale;
|
|
65
|
-
}) => T | Promise<T
|
|
65
|
+
}) => T | Promise<T>, callbacks?: {
|
|
66
|
+
onRedirect: (response: Response) => void;
|
|
67
|
+
}): Promise<Response>;
|
|
66
68
|
//# sourceMappingURL=middleware.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../../src/compiler/server/middleware.js"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,oCA9Ca,CAAC,WAEH,OAAO,WACP,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,cAAc,EAAE,MAAM,CAAA;CAAE,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../../src/compiler/server/middleware.js"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,oCA9Ca,CAAC,WAEH,OAAO,WACP,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,cAAc,EAAE,MAAM,CAAA;CAAE,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,cACrF;IAAE,UAAU,EAAC,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAA;CAAE,GACzC,OAAO,CAAC,QAAQ,CAAC,CA4H7B"}
|
|
@@ -18,7 +18,7 @@ import * as runtime from "./runtime.js";
|
|
|
18
18
|
*
|
|
19
19
|
* @param {Request} request - The incoming request object
|
|
20
20
|
* @param {(args: { request: Request, locale: import("./runtime.js").Locale }) => T | Promise<T>} resolve - Function to handle the request
|
|
21
|
-
*
|
|
21
|
+
* @param {{ onRedirect:(response: Response) => void }} [callbacks] - Callbacks to handle events from middleware
|
|
22
22
|
* @returns {Promise<Response>}
|
|
23
23
|
*
|
|
24
24
|
* @example
|
|
@@ -60,7 +60,7 @@ import * as runtime from "./runtime.js";
|
|
|
60
60
|
* };
|
|
61
61
|
* ```
|
|
62
62
|
*/
|
|
63
|
-
export async function paraglideMiddleware(request, resolve) {
|
|
63
|
+
export async function paraglideMiddleware(request, resolve, callbacks) {
|
|
64
64
|
if (!runtime.disableAsyncLocalStorage && !runtime.serverAsyncLocalStorage) {
|
|
65
65
|
const { AsyncLocalStorage } = await import("async_hooks");
|
|
66
66
|
runtime.overwriteServerAsyncLocalStorage(new AsyncLocalStorage());
|
|
@@ -76,7 +76,9 @@ export async function paraglideMiddleware(request, resolve) {
|
|
|
76
76
|
runtime.strategy.includes("url")) {
|
|
77
77
|
const localizedUrl = runtime.localizeUrl(request.url, { locale });
|
|
78
78
|
if (normalizeURL(localizedUrl.href) !== normalizeURL(request.url)) {
|
|
79
|
-
|
|
79
|
+
const response = Response.redirect(localizedUrl, 307);
|
|
80
|
+
callbacks?.onRedirect(response);
|
|
81
|
+
return response;
|
|
80
82
|
}
|
|
81
83
|
}
|
|
82
84
|
// If the strategy includes "url", we need to de-localize the URL
|
|
@@ -109,6 +109,84 @@ test("redirects to localized URL when non-URL strategy determines locale", async
|
|
|
109
109
|
expect(response.status).toBe(307); // Redirect status code
|
|
110
110
|
expect(response.headers.get("Location")).toBe("https://example.com/fr/some-path");
|
|
111
111
|
});
|
|
112
|
+
test("call onRedirect callback when redirecting to new url", async () => {
|
|
113
|
+
const runtime = await createParaglide({
|
|
114
|
+
blob: await newProject({
|
|
115
|
+
settings: {
|
|
116
|
+
baseLocale: "en",
|
|
117
|
+
locales: ["en", "fr"],
|
|
118
|
+
},
|
|
119
|
+
}),
|
|
120
|
+
strategy: ["cookie", "url"],
|
|
121
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
122
|
+
urlPatterns: [
|
|
123
|
+
{
|
|
124
|
+
pattern: "https://example.com/:path(.*)?",
|
|
125
|
+
localized: [
|
|
126
|
+
["en", "https://example.com/en/:path(.*)?"],
|
|
127
|
+
["fr", "https://example.com/fr/:path(.*)?"],
|
|
128
|
+
],
|
|
129
|
+
},
|
|
130
|
+
],
|
|
131
|
+
});
|
|
132
|
+
// Request to URL in en with cookie specifying French
|
|
133
|
+
const request = new Request("https://example.com/en/some-path", {
|
|
134
|
+
headers: {
|
|
135
|
+
cookie: `PARAGLIDE_LOCALE=fr`,
|
|
136
|
+
"Sec-Fetch-Dest": "document",
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
let response;
|
|
140
|
+
await runtime.paraglideMiddleware(request, () => {
|
|
141
|
+
// This shouldn't be called since we should redirect
|
|
142
|
+
throw new Error("Should not reach here");
|
|
143
|
+
}, {
|
|
144
|
+
onRedirect: (res) => {
|
|
145
|
+
response = res;
|
|
146
|
+
},
|
|
147
|
+
});
|
|
148
|
+
expect(response instanceof Response).toBe(true);
|
|
149
|
+
// needs to be 307 status code https://github.com/opral/inlang-paraglide-js/issues/416
|
|
150
|
+
expect(response.status).toBe(307); // Redirect status code
|
|
151
|
+
expect(response.headers.get("Location")).toBe("https://example.com/fr/some-path");
|
|
152
|
+
});
|
|
153
|
+
test("does not call onRedirect callback when there is no redirecting", async () => {
|
|
154
|
+
const runtime = await createParaglide({
|
|
155
|
+
blob: await newProject({
|
|
156
|
+
settings: {
|
|
157
|
+
baseLocale: "en",
|
|
158
|
+
locales: ["en", "fr"],
|
|
159
|
+
},
|
|
160
|
+
}),
|
|
161
|
+
strategy: ["cookie", "url"],
|
|
162
|
+
cookieName: "PARAGLIDE_LOCALE",
|
|
163
|
+
urlPatterns: [
|
|
164
|
+
{
|
|
165
|
+
pattern: "https://example.com/:path(.*)?",
|
|
166
|
+
localized: [
|
|
167
|
+
["en", "https://example.com/en/:path(.*)?"],
|
|
168
|
+
["fr", "https://example.com/fr/:path(.*)?"],
|
|
169
|
+
],
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
});
|
|
173
|
+
// Request to URL in en with cookie specifying French
|
|
174
|
+
const request = new Request("https://example.com/fr/some-path", {
|
|
175
|
+
headers: {
|
|
176
|
+
cookie: `PARAGLIDE_LOCALE=fr`,
|
|
177
|
+
"Sec-Fetch-Dest": "document",
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
let response = null;
|
|
181
|
+
await runtime.paraglideMiddleware(request, () => {
|
|
182
|
+
return new Response("Hello World");
|
|
183
|
+
}, {
|
|
184
|
+
onRedirect: (res) => {
|
|
185
|
+
response = res;
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
expect(response).toBe(null);
|
|
189
|
+
});
|
|
112
190
|
test("does not redirect if URL already matches determined locale", async () => {
|
|
113
191
|
const runtime = await createParaglide({
|
|
114
192
|
blob: await newProject({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"quotes.d.ts","sourceRoot":"","sources":["../../../src/services/codegen/quotes.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,eAAO,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"quotes.d.ts","sourceRoot":"","sources":["../../../src/services/codegen/quotes.ts"],"names":[],"mappings":"AAAA,6CAA6C;AAC7C,eAAO,MAAM,WAAW,GAAI,KAAK,MAAM,WAAoC,CAAC;AAC5E,yCAAyC;AACzC,eAAO,MAAM,QAAQ,GAAI,KAAK,MAAM,WAAiB,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../../src/services/telemetry/capture.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGnD;;;;GAIG;AACH,eAAO,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"capture.d.ts","sourceRoot":"","sources":["../../../src/services/telemetry/capture.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGnD;;;;GAIG;AACH,eAAO,MAAM,OAAO,GACnB,OAAO,+BAA+B,EACtC,MAAM;IACL,SAAS,EAAE,MAAM,CAAC;IAClB;;OAEG;IACH,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAChC,QAAQ,EAAE,IAAI,CAAC,eAAe,EAAE,WAAW,CAAC,CAAC;CAC7C,kBA8BD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"detect-json-formatting.d.ts","sourceRoot":"","sources":["../../src/utilities/detect-json-formatting.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,
|
|
1
|
+
{"version":3,"file":"detect-json-formatting.d.ts","sourceRoot":"","sources":["../../src/utilities/detect-json-formatting.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AACH,eAAO,MAAM,oBAAoB,GAChC,MAAM,MAAM,KACV,CAAC,CACH,KAAK,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,EAC3C,QAAQ,CAAC,EAAE,UAAU,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,KAE3C,MAAM,CAOV,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inlang/paraglide-js",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.
|
|
4
|
+
"version": "2.0.13",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
7
7
|
"access": "public",
|
|
@@ -44,14 +44,14 @@
|
|
|
44
44
|
"memfs": "4.17.0",
|
|
45
45
|
"prettier": "^3.4.2",
|
|
46
46
|
"rolldown": "1.0.0-beta.1",
|
|
47
|
-
"typedoc": "
|
|
48
|
-
"typedoc-plugin-markdown": "^4.
|
|
49
|
-
"typedoc-plugin-missing-exports": "
|
|
47
|
+
"typedoc": "0.28.3",
|
|
48
|
+
"typedoc-plugin-markdown": "^4.6.0",
|
|
49
|
+
"typedoc-plugin-missing-exports": "4.0.0",
|
|
50
50
|
"typescript": "^5.7.3",
|
|
51
51
|
"typescript-eslint": "^8.20.0",
|
|
52
52
|
"vitest": "2.1.8",
|
|
53
|
-
"@inlang/paraglide-js": "2.0.12",
|
|
54
53
|
"@inlang/plugin-message-format": "4.0.0",
|
|
54
|
+
"@inlang/paraglide-js": "2.0.13",
|
|
55
55
|
"@opral/tsconfig": "1.1.0"
|
|
56
56
|
},
|
|
57
57
|
"keywords": [
|