@kuncat/avoidant 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/AvoidantGame.svelte +1 -2
- package/dist/components/MapControls.svelte +1 -1
- package/dist/components/board.svelte +7 -34
- package/dist/components/label-connectors.d.ts +5 -2
- package/dist/components/label-connectors.js +47 -37
- package/dist/components/map-camera.js +1 -1
- package/dist/paraglide/messages/_index.d.ts +8 -8
- package/dist/paraglide/messages/_index.js +9 -9
- package/dist/paraglide/messages/action_back.js +2 -2
- package/dist/paraglide/messages/action_copy.js +2 -2
- package/dist/paraglide/messages/action_exit.js +2 -2
- package/dist/paraglide/messages/action_invite.js +2 -2
- package/dist/paraglide/messages/action_join.js +2 -2
- package/dist/paraglide/messages/action_join_game.js +2 -2
- package/dist/paraglide/messages/action_new_game.js +2 -2
- package/dist/paraglide/messages/action_random.js +2 -2
- package/dist/paraglide/messages/action_start.js +2 -2
- package/dist/paraglide/messages/aria_close_invitation.js +2 -2
- package/dist/paraglide/messages/aria_score_summary.js +2 -2
- package/dist/paraglide/messages/cells_count.js +2 -2
- package/dist/paraglide/messages/cells_per_face.js +2 -2
- package/dist/paraglide/messages/default_player_name.js +2 -2
- package/dist/paraglide/messages/error_join_connect_failed.js +2 -2
- package/dist/paraglide/messages/error_join_failed.js +2 -2
- package/dist/paraglide/messages/error_join_ticket_invalid.js +2 -2
- package/dist/paraglide/messages/error_join_ticket_required.js +2 -2
- package/dist/paraglide/messages/faces_count.js +2 -2
- package/dist/paraglide/messages/field_cell_density.js +2 -2
- package/dist/paraglide/messages/field_invitation_url.js +2 -2
- package/dist/paraglide/messages/field_map_shape.js +2 -2
- package/dist/paraglide/messages/field_map_size.js +2 -2
- package/dist/paraglide/messages/field_player_name.js +2 -2
- package/dist/paraglide/messages/field_radius.js +2 -2
- package/dist/paraglide/messages/field_radius_x.js +2 -2
- package/dist/paraglide/messages/field_radius_y.js +2 -2
- package/dist/paraglide/messages/field_radius_z.js +2 -2
- package/dist/paraglide/messages/field_relay_servers.js +2 -2
- package/dist/paraglide/messages/field_seed.js +2 -2
- package/dist/paraglide/messages/field_size.js +2 -2
- package/dist/paraglide/messages/field_spikiness.js +2 -2
- package/dist/paraglide/messages/field_subdivisions.js +2 -2
- package/dist/paraglide/messages/field_surface_detail.js +2 -2
- package/dist/paraglide/messages/field_ticket_or_invitation_url.js +2 -2
- package/dist/paraglide/messages/field_tutorial_mode.js +2 -2
- package/dist/paraglide/messages/field_void_fraction.js +2 -2
- package/dist/paraglide/messages/label_advanced_settings.js +2 -2
- package/dist/paraglide/messages/label_players.js +2 -2
- package/dist/paraglide/messages/label_safe_cells_explored.js +2 -2
- package/dist/paraglide/messages/label_safe_cells_remaining.js +2 -2
- package/dist/paraglide/messages/label_score.js +2 -2
- package/dist/paraglide/messages/label_streak.js +2 -2
- package/dist/paraglide/messages/label_voids_discovered.js +2 -2
- package/dist/paraglide/messages/package.json +4 -0
- package/dist/paraglide/messages/preset_custom.js +2 -2
- package/dist/paraglide/messages/preset_large.js +2 -2
- package/dist/paraglide/messages/preset_medium.js +2 -2
- package/dist/paraglide/messages/preset_small.js +2 -2
- package/dist/paraglide/messages/shape_label.js +2 -2
- package/dist/paraglide/messages/status_generating_map.js +2 -2
- package/dist/paraglide/messages/status_initialization_failed.js +2 -2
- package/dist/paraglide/messages/status_joining_game.js +2 -2
- package/dist/paraglide/messages/status_loading.js +2 -2
- package/dist/paraglide/messages/status_preparing.js +2 -2
- package/dist/paraglide/messages/text_avoided.js +2 -2
- package/dist/paraglide/messages/text_density_summary.js +2 -2
- package/dist/paraglide/messages/text_invite_after_opening.js +2 -2
- package/dist/paraglide/messages/text_relay_servers_hint.js +2 -2
- package/dist/paraglide/messages/tutorial_action_dismiss.js +2 -2
- package/dist/paraglide/messages/tutorial_action_next.js +2 -2
- package/dist/paraglide/messages/tutorial_after_safe_deducible.js +2 -2
- package/dist/paraglide/messages/tutorial_after_safe_guess.js +2 -2
- package/dist/paraglide/messages/tutorial_after_void.js +2 -2
- package/dist/paraglide/messages/tutorial_awaiting_first_click.js +2 -2
- package/dist/paraglide/messages/tutorial_intro.d.ts +1 -1
- package/dist/paraglide/messages/tutorial_intro.js +5 -5
- package/dist/paraglide/messages/tutorial_won.js +2 -2
- package/dist/paraglide/registry.d.ts +13 -0
- package/dist/paraglide/registry.js +15 -0
- package/dist/paraglide/runtime.d.ts +90 -26
- package/dist/paraglide/runtime.js +597 -109
- package/dist/paraglide/server.d.ts +1 -1
- package/dist/paraglide/server.js +27 -10
- package/dist/wasm/avoidant_wasm.d.ts +46 -1
- package/dist/wasm/avoidant_wasm.js +164 -28
- package/dist/wasm/avoidant_wasm_bg.wasm +0 -0
- package/dist/wasm/avoidant_wasm_bg.wasm.d.ts +15 -1
- package/package.json +29 -29
|
@@ -69,62 +69,12 @@ export const urlPatterns = [
|
|
|
69
69
|
]
|
|
70
70
|
}
|
|
71
71
|
];
|
|
72
|
-
/** @type {string | undefined} */
|
|
73
|
-
let cachedRouteStrategyUrl;
|
|
74
|
-
/** @type {{ match: string; strategy?: typeof strategy; exclude?: boolean } | undefined} */
|
|
75
|
-
let cachedRouteStrategy;
|
|
76
72
|
/**
|
|
77
|
-
*
|
|
78
|
-
* @returns {{ match: string; strategy?: typeof strategy; exclude?: boolean } | undefined}
|
|
79
|
-
*/
|
|
80
|
-
function findMatchingRouteStrategy(url) {
|
|
81
|
-
if (routeStrategies.length === 0) {
|
|
82
|
-
return undefined;
|
|
83
|
-
}
|
|
84
|
-
const urlString = typeof url === "string" ? url : url.href;
|
|
85
|
-
if (cachedRouteStrategyUrl === urlString) {
|
|
86
|
-
return cachedRouteStrategy;
|
|
87
|
-
}
|
|
88
|
-
const urlObject = new URL(urlString, "http://dummy.com");
|
|
89
|
-
let match;
|
|
90
|
-
for (const routeStrategy of routeStrategies) {
|
|
91
|
-
const pattern = new URLPattern(routeStrategy.match, urlObject.href);
|
|
92
|
-
if (pattern.exec(urlObject.href)) {
|
|
93
|
-
match = routeStrategy;
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
cachedRouteStrategyUrl = urlString;
|
|
98
|
-
cachedRouteStrategy = match;
|
|
99
|
-
return match;
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Returns the strategy to use for a specific URL.
|
|
73
|
+
* Controls trailing slash canonicalization for localized URLs.
|
|
103
74
|
*
|
|
104
|
-
*
|
|
105
|
-
* the global strategy is returned.
|
|
106
|
-
*
|
|
107
|
-
* @param {string | URL} url
|
|
108
|
-
* @returns {typeof strategy}
|
|
75
|
+
* @type {"always" | "never" | undefined}
|
|
109
76
|
*/
|
|
110
|
-
export
|
|
111
|
-
const routeStrategy = findMatchingRouteStrategy(url);
|
|
112
|
-
if (routeStrategy &&
|
|
113
|
-
routeStrategy.exclude !== true &&
|
|
114
|
-
Array.isArray(routeStrategy.strategy)) {
|
|
115
|
-
return routeStrategy.strategy;
|
|
116
|
-
}
|
|
117
|
-
return strategy;
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Returns whether the given URL is excluded from middleware i18n processing.
|
|
121
|
-
*
|
|
122
|
-
* @param {string | URL} url
|
|
123
|
-
* @returns {boolean}
|
|
124
|
-
*/
|
|
125
|
-
export function isExcludedByRouteStrategy(url) {
|
|
126
|
-
return findMatchingRouteStrategy(url)?.exclude === true;
|
|
127
|
-
}
|
|
77
|
+
export const trailingSlash = undefined;
|
|
128
78
|
/**
|
|
129
79
|
* @typedef {{
|
|
130
80
|
* getStore(): {
|
|
@@ -145,6 +95,17 @@ export function isExcludedByRouteStrategy(url) {
|
|
|
145
95
|
* @type {ParaglideAsyncLocalStorage | undefined}
|
|
146
96
|
*/
|
|
147
97
|
export let serverAsyncLocalStorage = undefined;
|
|
98
|
+
/**
|
|
99
|
+
* Returns the current server-side async local storage instance.
|
|
100
|
+
*
|
|
101
|
+
* Accessing the mutable value through a function keeps it observable when
|
|
102
|
+
* module interceptors wrap exported bindings and snapshot their initial value.
|
|
103
|
+
*
|
|
104
|
+
* @returns {ParaglideAsyncLocalStorage | undefined}
|
|
105
|
+
*/
|
|
106
|
+
export function getServerAsyncLocalStorage() {
|
|
107
|
+
return serverAsyncLocalStorage;
|
|
108
|
+
}
|
|
148
109
|
export const disableAsyncLocalStorage = false;
|
|
149
110
|
export const experimentalMiddlewareLocaleSplitting = false;
|
|
150
111
|
export const isServer = typeof window === 'undefined';
|
|
@@ -193,7 +154,7 @@ let localeInitiallySet = false;
|
|
|
193
154
|
* in the order they are defined. In SSR contexts, the locale is retrieved from AsyncLocalStorage
|
|
194
155
|
* which is set by the `paraglideMiddleware()`.
|
|
195
156
|
*
|
|
196
|
-
* @see https://
|
|
157
|
+
* @see https://paraglidejs.com/strategy - Configure locale detection strategies
|
|
197
158
|
*
|
|
198
159
|
* @example
|
|
199
160
|
* if (getLocale() === 'de') {
|
|
@@ -230,7 +191,7 @@ export let getLocale = () => {
|
|
|
230
191
|
}
|
|
231
192
|
return resolved;
|
|
232
193
|
}
|
|
233
|
-
throw new Error("No locale found. Read the docs https://
|
|
194
|
+
throw new Error("No locale found. Read the docs https://paraglidejs.com/errors#no-locale-found");
|
|
234
195
|
};
|
|
235
196
|
/**
|
|
236
197
|
* Resolve locale for a given URL using route-aware strategies.
|
|
@@ -247,7 +208,7 @@ export function getLocaleForUrl(url) {
|
|
|
247
208
|
if (resolved) {
|
|
248
209
|
return resolved;
|
|
249
210
|
}
|
|
250
|
-
throw new Error("No locale found. Read the docs https://
|
|
211
|
+
throw new Error("No locale found. Read the docs https://paraglidejs.com/errors#no-locale-found");
|
|
251
212
|
}
|
|
252
213
|
/**
|
|
253
214
|
* @param {typeof strategy} strategyToUse
|
|
@@ -312,7 +273,7 @@ function resolveLocaleWithStrategies(strategyToUse, urlForUrlStrategy) {
|
|
|
312
273
|
* Use this function to overwrite how the locale is resolved. This is useful
|
|
313
274
|
* for custom locale resolution or advanced use cases like SSG with concurrent rendering.
|
|
314
275
|
*
|
|
315
|
-
* @see https://
|
|
276
|
+
* @see https://paraglidejs.com/strategy
|
|
316
277
|
*
|
|
317
278
|
* @example
|
|
318
279
|
* overwriteGetLocale(() => {
|
|
@@ -392,14 +353,17 @@ const navigateOrReload = (newLocation) => {
|
|
|
392
353
|
* Set the locale.
|
|
393
354
|
*
|
|
394
355
|
* Updates the locale using your configured strategies (cookie, localStorage, URL, etc.).
|
|
395
|
-
* By default, this
|
|
396
|
-
*
|
|
397
|
-
*
|
|
356
|
+
* By default, this navigates the client to the localized URL or reloads the current
|
|
357
|
+
* document to reflect the new locale. `reload: false` is a narrow browser-only escape
|
|
358
|
+
* hatch for a fully client-rendered, non-URL-routed surface that owns its reactive
|
|
359
|
+
* updates and document state. It does not re-render the UI or update the document.
|
|
360
|
+
* Do not use it for normal locale pickers, URL-routed pages, or switching an SSR,
|
|
361
|
+
* SSG, or hydrated document. It is incompatible with per-locale builds.
|
|
398
362
|
*
|
|
399
363
|
* If any custom strategy's `setLocale` function is async, then this function
|
|
400
364
|
* will become async as well.
|
|
401
365
|
*
|
|
402
|
-
* @see https://
|
|
366
|
+
* @see https://paraglidejs.com/strategy
|
|
403
367
|
*
|
|
404
368
|
* @example
|
|
405
369
|
* setLocale('en');
|
|
@@ -414,6 +378,12 @@ export let setLocale = (newLocale, options) => {
|
|
|
414
378
|
reload: true,
|
|
415
379
|
...options,
|
|
416
380
|
};
|
|
381
|
+
if (experimentalStaticLocale !== undefined &&
|
|
382
|
+
newLocale !== experimentalStaticLocale &&
|
|
383
|
+
optionsWithDefaults.reload === false) {
|
|
384
|
+
console.warn(`Paraglide: setLocale(${JSON.stringify(newLocale)}, { reload: false }) cannot switch away from the statically built locale ${JSON.stringify(experimentalStaticLocale)}. A document navigation is required; reload has been forced to true.`);
|
|
385
|
+
optionsWithDefaults.reload = true;
|
|
386
|
+
}
|
|
417
387
|
// locale is already set
|
|
418
388
|
// https://github.com/opral/inlang-paraglide-js/issues/430
|
|
419
389
|
/** @type {Locale | undefined} */
|
|
@@ -450,6 +420,7 @@ export let setLocale = (newLocale, options) => {
|
|
|
450
420
|
document.cookie = cookieDomain
|
|
451
421
|
? `${cookieString}; domain=${cookieDomain}`
|
|
452
422
|
: cookieString;
|
|
423
|
+
clearLocaleCookieCache();
|
|
453
424
|
}
|
|
454
425
|
else if (strat === "baseLocale") {
|
|
455
426
|
// nothing to be set here. baseLocale is only a fallback
|
|
@@ -529,7 +500,7 @@ export const overwriteSetLocale = (fn) => {
|
|
|
529
500
|
/**
|
|
530
501
|
* The origin of the current URL.
|
|
531
502
|
*
|
|
532
|
-
* Defaults to "http://
|
|
503
|
+
* Defaults to "http://example.com" in non-browser environments. If this
|
|
533
504
|
* behavior is not desired, the implementation can be overwritten
|
|
534
505
|
* by `overwriteGetUrlOrigin()`.
|
|
535
506
|
*
|
|
@@ -606,6 +577,58 @@ export function assertIsLocale(input) {
|
|
|
606
577
|
throw new Error(`Invalid locale: ${input}. Expected one of: ${locales.join(", ")}`);
|
|
607
578
|
}
|
|
608
579
|
|
|
580
|
+
/**
|
|
581
|
+
* Applies the configured trailing slash policy to a URL.
|
|
582
|
+
*
|
|
583
|
+
* The root pathname always remains `/`. Query parameters and hashes are not
|
|
584
|
+
* modified.
|
|
585
|
+
*
|
|
586
|
+
* @param {URL} url
|
|
587
|
+
* @returns {URL}
|
|
588
|
+
*/
|
|
589
|
+
function normalizeTrailingSlash(url) {
|
|
590
|
+
if (trailingSlash === undefined || url.pathname === "/") {
|
|
591
|
+
return url;
|
|
592
|
+
}
|
|
593
|
+
if (trailingSlash === "never") {
|
|
594
|
+
url.pathname = url.pathname.replace(/\/+$/, "") || "/";
|
|
595
|
+
}
|
|
596
|
+
else if (trailingSlash === "always") {
|
|
597
|
+
url.pathname = url.pathname.replace(/\/+$/, "") + "/";
|
|
598
|
+
}
|
|
599
|
+
return url;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
/**
|
|
603
|
+
* Matches a canonical URL while allowing configured patterns to retain their
|
|
604
|
+
* existing trailing slash style.
|
|
605
|
+
*
|
|
606
|
+
* @param {URLPattern} pattern
|
|
607
|
+
* @param {URL} url
|
|
608
|
+
* @returns {any}
|
|
609
|
+
*/
|
|
610
|
+
function execUrlPattern(pattern, url) {
|
|
611
|
+
if (trailingSlash === undefined || url.pathname === "/") {
|
|
612
|
+
return pattern.exec(url.href);
|
|
613
|
+
}
|
|
614
|
+
const alias = new URL(url);
|
|
615
|
+
if (trailingSlash === "always") {
|
|
616
|
+
alias.pathname = alias.pathname.replace(/\/+$/, "") || "/";
|
|
617
|
+
// Prefer the slashless alias so the canonical slash does not become part
|
|
618
|
+
// of a terminal wildcard capture.
|
|
619
|
+
return pattern.exec(alias.href) ?? pattern.exec(url.href);
|
|
620
|
+
}
|
|
621
|
+
else if (trailingSlash === "never") {
|
|
622
|
+
const match = pattern.exec(url.href);
|
|
623
|
+
if (match) {
|
|
624
|
+
return match;
|
|
625
|
+
}
|
|
626
|
+
alias.pathname = alias.pathname.replace(/\/+$/, "") + "/";
|
|
627
|
+
return pattern.exec(alias.href);
|
|
628
|
+
}
|
|
629
|
+
return pattern.exec(url.href);
|
|
630
|
+
}
|
|
631
|
+
|
|
609
632
|
/**
|
|
610
633
|
* @typedef {object} ExtractLocaleFromRequestOptions
|
|
611
634
|
* @property {string | URL} [effectiveRequestUrl] - Effective request URL to use for route matching and locale detection with the URL strategy.
|
|
@@ -648,11 +671,13 @@ export const extractLocaleFromRequestWithStrategies = (request, strategies, url
|
|
|
648
671
|
let locale;
|
|
649
672
|
for (const strat of strategies) {
|
|
650
673
|
if (TREE_SHAKE_COOKIE_STRATEGY_USED && strat === "cookie") {
|
|
674
|
+
const cookiePrefix = cookieName + "=";
|
|
651
675
|
locale = request.headers
|
|
652
676
|
.get("cookie")
|
|
653
|
-
?.split(";
|
|
654
|
-
.
|
|
655
|
-
|
|
677
|
+
?.split(";")
|
|
678
|
+
.map((c) => c.trim())
|
|
679
|
+
.find((c) => c.startsWith(cookiePrefix))
|
|
680
|
+
?.slice(cookiePrefix.length);
|
|
656
681
|
}
|
|
657
682
|
else if (TREE_SHAKE_URL_STRATEGY_USED && strat === "url") {
|
|
658
683
|
locale = extractLocaleFromUrl(effectiveRequestUrl);
|
|
@@ -680,7 +705,7 @@ export const extractLocaleFromRequestWithStrategies = (request, strategies, url
|
|
|
680
705
|
return matchedLocale;
|
|
681
706
|
}
|
|
682
707
|
}
|
|
683
|
-
throw new Error("No locale found. There is an error in your strategy. Try adding 'baseLocale' as the very last strategy. Read more here https://
|
|
708
|
+
throw new Error("No locale found. There is an error in your strategy. Try adding 'baseLocale' as the very last strategy. Read more here https://paraglidejs.com/errors#no-locale-found");
|
|
684
709
|
};
|
|
685
710
|
/**
|
|
686
711
|
* @param {Request} request
|
|
@@ -760,6 +785,25 @@ function resolveEffectiveRequestUrlFromRequestAsync(request, effectiveRequestUrl
|
|
|
760
785
|
return new URL(effectiveRequestUrl, request.url);
|
|
761
786
|
}
|
|
762
787
|
|
|
788
|
+
const cookieNamePattern = cookieName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
789
|
+
const localeCookiePattern = new RegExp(`(?:^|;\\s*)${cookieNamePattern}=([^;]*)`);
|
|
790
|
+
const noCachedLocale = Symbol();
|
|
791
|
+
/** @type {Locale | undefined | typeof noCachedLocale} */
|
|
792
|
+
let cachedLocaleFromCookie = noCachedLocale;
|
|
793
|
+
/**
|
|
794
|
+
* Clears the cached locale from `document.cookie`.
|
|
795
|
+
*/
|
|
796
|
+
function clearLocaleCookieCache() {
|
|
797
|
+
cachedLocaleFromCookie = noCachedLocale;
|
|
798
|
+
}
|
|
799
|
+
function scheduleLocaleCookieCacheClear() {
|
|
800
|
+
if (typeof queueMicrotask === "function") {
|
|
801
|
+
queueMicrotask(clearLocaleCookieCache);
|
|
802
|
+
}
|
|
803
|
+
else {
|
|
804
|
+
Promise.resolve().then(clearLocaleCookieCache);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
763
807
|
/**
|
|
764
808
|
* Extracts a cookie from the document.
|
|
765
809
|
*
|
|
@@ -769,12 +813,17 @@ function resolveEffectiveRequestUrlFromRequestAsync(request, effectiveRequestUrl
|
|
|
769
813
|
* @returns {Locale | undefined}
|
|
770
814
|
*/
|
|
771
815
|
export function extractLocaleFromCookie() {
|
|
772
|
-
if (typeof document === "undefined"
|
|
816
|
+
if (typeof document === "undefined") {
|
|
773
817
|
return;
|
|
774
818
|
}
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
819
|
+
if (cachedLocaleFromCookie !== noCachedLocale) {
|
|
820
|
+
return cachedLocaleFromCookie;
|
|
821
|
+
}
|
|
822
|
+
const match = document.cookie.match(localeCookiePattern);
|
|
823
|
+
const locale = match?.[1];
|
|
824
|
+
cachedLocaleFromCookie = toLocale(locale);
|
|
825
|
+
scheduleLocaleCookieCacheClear();
|
|
826
|
+
return cachedLocaleFromCookie;
|
|
778
827
|
}
|
|
779
828
|
|
|
780
829
|
/**
|
|
@@ -881,14 +930,17 @@ export function extractLocaleFromUrl(url) {
|
|
|
881
930
|
/** @type {Locale | undefined} */
|
|
882
931
|
let result;
|
|
883
932
|
if (TREE_SHAKE_DEFAULT_URL_PATTERN_USED) {
|
|
884
|
-
|
|
933
|
+
const urlObj = typeof url === "string"
|
|
934
|
+
? new URL(url, "http://example.com")
|
|
935
|
+
: new URL(url);
|
|
936
|
+
result = defaultUrlPatternExtractLocale(normalizeTrailingSlash(urlObj));
|
|
885
937
|
}
|
|
886
938
|
else {
|
|
887
|
-
const urlObj = typeof url === "string" ? new URL(url) : url;
|
|
939
|
+
const urlObj = normalizeTrailingSlash(typeof url === "string" ? new URL(url) : new URL(url));
|
|
888
940
|
// Iterate over URL patterns
|
|
889
941
|
for (const element of urlPatterns) {
|
|
890
942
|
for (const [locale, localizedPattern] of element.localized) {
|
|
891
|
-
const match = new URLPattern(localizedPattern, urlObj.href)
|
|
943
|
+
const match = execUrlPattern(new URLPattern(localizedPattern, urlObj.href), urlObj);
|
|
892
944
|
if (match) {
|
|
893
945
|
result = locale;
|
|
894
946
|
break;
|
|
@@ -909,7 +961,7 @@ export function extractLocaleFromUrl(url) {
|
|
|
909
961
|
* @returns {Locale | undefined} The extracted locale, or undefined if no locale is found.
|
|
910
962
|
*/
|
|
911
963
|
function defaultUrlPatternExtractLocale(url) {
|
|
912
|
-
const urlObj = new URL(url, "http://
|
|
964
|
+
const urlObj = new URL(url, "http://example.com");
|
|
913
965
|
const pathSegments = urlObj.pathname.split("/").filter(Boolean);
|
|
914
966
|
return toLocale(pathSegments[0]) || baseLocale;
|
|
915
967
|
}
|
|
@@ -924,7 +976,7 @@ function defaultUrlPatternExtractLocale(url) {
|
|
|
924
976
|
* For client-side UI components, use `localizeHref()` instead, which provides
|
|
925
977
|
* a more convenient API with relative paths and automatic locale detection.
|
|
926
978
|
*
|
|
927
|
-
* @see https://
|
|
979
|
+
* @see https://paraglidejs.com/i18n-routing
|
|
928
980
|
*
|
|
929
981
|
* @example
|
|
930
982
|
* ```typescript
|
|
@@ -965,12 +1017,24 @@ export function localizeUrl(url, options) {
|
|
|
965
1017
|
if (TREE_SHAKE_DEFAULT_URL_PATTERN_USED) {
|
|
966
1018
|
return localizeUrlDefaultPattern(url, targetLocale);
|
|
967
1019
|
}
|
|
968
|
-
const
|
|
1020
|
+
const originalUrl = typeof url === "string" ? new URL(url) : url;
|
|
1021
|
+
const urlObj = trailingSlash === undefined
|
|
1022
|
+
? originalUrl
|
|
1023
|
+
: normalizeTrailingSlash(new URL(originalUrl));
|
|
969
1024
|
// Iterate over URL patterns
|
|
970
1025
|
for (const element of urlPatterns) {
|
|
1026
|
+
// Most applications use a locale prefix (and sometimes a fixed domain)
|
|
1027
|
+
// with a trailing catch-all path. Those routes do not need URLPattern's
|
|
1028
|
+
// parser or matcher on every call. Keep the generic matcher below as the
|
|
1029
|
+
// fallback for every other URLPattern shape. This stays inside the outer
|
|
1030
|
+
// loop so specific patterns retain precedence over a later catch-all.
|
|
1031
|
+
const fastPathLocalized = localizeUrlFastPath(urlObj, targetLocale, element);
|
|
1032
|
+
if (fastPathLocalized !== undefined) {
|
|
1033
|
+
return fastPathLocalized;
|
|
1034
|
+
}
|
|
971
1035
|
// match localized patterns
|
|
972
1036
|
for (const [, localizedPattern] of element.localized) {
|
|
973
|
-
const match =
|
|
1037
|
+
const match = execUrlPattern(getUrlPattern(localizedPattern, urlObj), urlObj);
|
|
974
1038
|
if (!match) {
|
|
975
1039
|
continue;
|
|
976
1040
|
}
|
|
@@ -979,19 +1043,19 @@ export function localizeUrl(url, options) {
|
|
|
979
1043
|
continue;
|
|
980
1044
|
}
|
|
981
1045
|
const localizedUrl = fillPattern(targetPattern, aggregateGroups(match), urlObj.origin);
|
|
982
|
-
return fillMissingUrlParts(localizedUrl, match);
|
|
1046
|
+
return normalizeTrailingSlash(fillMissingUrlParts(localizedUrl, match));
|
|
983
1047
|
}
|
|
984
|
-
const unlocalizedMatch =
|
|
1048
|
+
const unlocalizedMatch = execUrlPattern(getUrlPattern(element.pattern, urlObj), urlObj);
|
|
985
1049
|
if (unlocalizedMatch) {
|
|
986
1050
|
const targetPattern = element.localized.find(([locale]) => locale === targetLocale)?.[1];
|
|
987
1051
|
if (targetPattern) {
|
|
988
1052
|
const localizedUrl = fillPattern(targetPattern, aggregateGroups(unlocalizedMatch), urlObj.origin);
|
|
989
|
-
return fillMissingUrlParts(localizedUrl, unlocalizedMatch);
|
|
1053
|
+
return normalizeTrailingSlash(fillMissingUrlParts(localizedUrl, unlocalizedMatch));
|
|
990
1054
|
}
|
|
991
1055
|
}
|
|
992
1056
|
}
|
|
993
1057
|
// If no match found, return the original URL
|
|
994
|
-
return
|
|
1058
|
+
return originalUrl;
|
|
995
1059
|
}
|
|
996
1060
|
/**
|
|
997
1061
|
* https://github.com/opral/inlang-paraglide-js/issues/381
|
|
@@ -1001,13 +1065,14 @@ export function localizeUrl(url, options) {
|
|
|
1001
1065
|
* @returns {URL}
|
|
1002
1066
|
*/
|
|
1003
1067
|
function localizeUrlDefaultPattern(url, locale) {
|
|
1004
|
-
const urlObj = typeof url === "string" ? new URL(url, getUrlOrigin()) : new URL(url);
|
|
1068
|
+
const urlObj = normalizeTrailingSlash(typeof url === "string" ? new URL(url, getUrlOrigin()) : new URL(url));
|
|
1005
1069
|
const currentLocale = extractLocaleFromUrl(urlObj);
|
|
1006
1070
|
// If current locale matches target locale, no change needed
|
|
1007
1071
|
if (currentLocale === locale) {
|
|
1008
|
-
return urlObj;
|
|
1072
|
+
return normalizeTrailingSlash(urlObj);
|
|
1009
1073
|
}
|
|
1010
|
-
|
|
1074
|
+
// Ignore leading separators like locale detection, preserving the remaining path.
|
|
1075
|
+
const pathSegments = urlObj.pathname.replace(/^\/+/, "").split("/");
|
|
1011
1076
|
// If current path starts with a locale, remove it
|
|
1012
1077
|
if (pathSegments.length > 0 && toLocale(pathSegments[0])) {
|
|
1013
1078
|
pathSegments.shift();
|
|
@@ -1020,7 +1085,7 @@ function localizeUrlDefaultPattern(url, locale) {
|
|
|
1020
1085
|
// For other locales, add prefix
|
|
1021
1086
|
urlObj.pathname = "/" + locale + "/" + pathSegments.join("/");
|
|
1022
1087
|
}
|
|
1023
|
-
return urlObj;
|
|
1088
|
+
return normalizeTrailingSlash(urlObj);
|
|
1024
1089
|
}
|
|
1025
1090
|
/**
|
|
1026
1091
|
* Low-level URL de-localization function, primarily used in server contexts.
|
|
@@ -1032,7 +1097,7 @@ function localizeUrlDefaultPattern(url, locale) {
|
|
|
1032
1097
|
* For client-side UI components, use `deLocalizeHref()` instead, which provides
|
|
1033
1098
|
* a more convenient API with relative paths.
|
|
1034
1099
|
*
|
|
1035
|
-
* @see https://
|
|
1100
|
+
* @see https://paraglidejs.com/i18n-routing
|
|
1036
1101
|
*
|
|
1037
1102
|
* @example
|
|
1038
1103
|
* ```typescript
|
|
@@ -1065,28 +1130,35 @@ export function deLocalizeUrl(url) {
|
|
|
1065
1130
|
if (TREE_SHAKE_DEFAULT_URL_PATTERN_USED) {
|
|
1066
1131
|
return deLocalizeUrlDefaultPattern(url);
|
|
1067
1132
|
}
|
|
1068
|
-
const
|
|
1133
|
+
const originalUrl = typeof url === "string" ? new URL(url) : url;
|
|
1134
|
+
const urlObj = trailingSlash === undefined
|
|
1135
|
+
? originalUrl
|
|
1136
|
+
: normalizeTrailingSlash(new URL(originalUrl));
|
|
1069
1137
|
// Iterate over URL patterns
|
|
1070
1138
|
for (const element of urlPatterns) {
|
|
1139
|
+
const fastPathDeLocalized = deLocalizeUrlFastPath(urlObj, element);
|
|
1140
|
+
if (fastPathDeLocalized !== undefined) {
|
|
1141
|
+
return fastPathDeLocalized;
|
|
1142
|
+
}
|
|
1071
1143
|
// Iterate over localized versions
|
|
1072
1144
|
for (const [, localizedPattern] of element.localized) {
|
|
1073
|
-
const match =
|
|
1145
|
+
const match = execUrlPattern(getUrlPattern(localizedPattern, urlObj), urlObj);
|
|
1074
1146
|
if (match) {
|
|
1075
1147
|
// Convert localized URL back to the base pattern
|
|
1076
1148
|
const groups = aggregateGroups(match);
|
|
1077
1149
|
const baseUrl = fillPattern(element.pattern, groups, urlObj.origin);
|
|
1078
|
-
return fillMissingUrlParts(baseUrl, match);
|
|
1150
|
+
return normalizeTrailingSlash(fillMissingUrlParts(baseUrl, match));
|
|
1079
1151
|
}
|
|
1080
1152
|
}
|
|
1081
1153
|
// match unlocalized pattern
|
|
1082
|
-
const unlocalizedMatch =
|
|
1154
|
+
const unlocalizedMatch = execUrlPattern(getUrlPattern(element.pattern, urlObj), urlObj);
|
|
1083
1155
|
if (unlocalizedMatch) {
|
|
1084
1156
|
const baseUrl = fillPattern(element.pattern, aggregateGroups(unlocalizedMatch), urlObj.origin);
|
|
1085
|
-
return fillMissingUrlParts(baseUrl, unlocalizedMatch);
|
|
1157
|
+
return normalizeTrailingSlash(fillMissingUrlParts(baseUrl, unlocalizedMatch));
|
|
1086
1158
|
}
|
|
1087
1159
|
}
|
|
1088
1160
|
// no match found return the original url
|
|
1089
|
-
return
|
|
1161
|
+
return originalUrl;
|
|
1090
1162
|
}
|
|
1091
1163
|
/**
|
|
1092
1164
|
* De-localizes a URL using the default pattern (/:locale/*)
|
|
@@ -1094,13 +1166,14 @@ export function deLocalizeUrl(url) {
|
|
|
1094
1166
|
* @returns {URL}
|
|
1095
1167
|
*/
|
|
1096
1168
|
function deLocalizeUrlDefaultPattern(url) {
|
|
1097
|
-
const urlObj = typeof url === "string" ? new URL(url, getUrlOrigin()) : new URL(url);
|
|
1098
|
-
|
|
1169
|
+
const urlObj = normalizeTrailingSlash(typeof url === "string" ? new URL(url, getUrlOrigin()) : new URL(url));
|
|
1170
|
+
// Ignore leading separators like locale detection, preserving the remaining path.
|
|
1171
|
+
const pathSegments = urlObj.pathname.replace(/^\/+/, "").split("/");
|
|
1099
1172
|
// If first segment is a locale, remove it
|
|
1100
1173
|
if (pathSegments.length > 0 && toLocale(pathSegments[0])) {
|
|
1101
1174
|
urlObj.pathname = "/" + pathSegments.slice(1).join("/");
|
|
1102
1175
|
}
|
|
1103
|
-
return urlObj;
|
|
1176
|
+
return normalizeTrailingSlash(urlObj);
|
|
1104
1177
|
}
|
|
1105
1178
|
/**
|
|
1106
1179
|
* Takes matches of implicit wildcards in the UrlPattern (when a part is missing
|
|
@@ -1225,6 +1298,413 @@ export function aggregateGroups(match) {
|
|
|
1225
1298
|
...match.username.groups,
|
|
1226
1299
|
};
|
|
1227
1300
|
}
|
|
1301
|
+
/** @type {Map<string, URLPattern>} */
|
|
1302
|
+
const urlPatternCache = new Map();
|
|
1303
|
+
const URL_PATTERN_CACHE_LIMIT = 128;
|
|
1304
|
+
const ABSOLUTE_URL_PATTERN = /^(?:[A-Za-z][A-Za-z\d+.-]*|:[A-Za-z][A-Za-z\d_-]*):\/\//;
|
|
1305
|
+
/**
|
|
1306
|
+
* URLPattern's base URL affects relative patterns. Absolute patterns only
|
|
1307
|
+
* depend on the pattern itself, while root-relative patterns also depend on
|
|
1308
|
+
* the URL origin. Other relative patterns are deliberately not cached because
|
|
1309
|
+
* their semantics depend on the complete base URL.
|
|
1310
|
+
*
|
|
1311
|
+
* @param {string} pattern
|
|
1312
|
+
* @param {URL} url
|
|
1313
|
+
* @returns {URLPattern}
|
|
1314
|
+
*/
|
|
1315
|
+
function getUrlPattern(pattern, url) {
|
|
1316
|
+
const isAbsolutePattern = ABSOLUTE_URL_PATTERN.test(pattern);
|
|
1317
|
+
const isRootRelativePattern = pattern.startsWith("/");
|
|
1318
|
+
if (!isAbsolutePattern && !isRootRelativePattern) {
|
|
1319
|
+
return new URLPattern(pattern, url.href);
|
|
1320
|
+
}
|
|
1321
|
+
const key = isAbsolutePattern
|
|
1322
|
+
? pattern
|
|
1323
|
+
: JSON.stringify([url.origin, pattern]);
|
|
1324
|
+
const cached = urlPatternCache.get(key);
|
|
1325
|
+
if (cached !== undefined) {
|
|
1326
|
+
// Refresh the entry so frequently used patterns stay in the bounded cache.
|
|
1327
|
+
urlPatternCache.delete(key);
|
|
1328
|
+
urlPatternCache.set(key, cached);
|
|
1329
|
+
return cached;
|
|
1330
|
+
}
|
|
1331
|
+
const compiled = new URLPattern(pattern, url.href);
|
|
1332
|
+
if (urlPatternCache.size >= URL_PATTERN_CACHE_LIMIT) {
|
|
1333
|
+
const oldestKey = urlPatternCache.keys().next().value;
|
|
1334
|
+
if (oldestKey !== undefined)
|
|
1335
|
+
urlPatternCache.delete(oldestKey);
|
|
1336
|
+
}
|
|
1337
|
+
urlPatternCache.set(key, compiled);
|
|
1338
|
+
return compiled;
|
|
1339
|
+
}
|
|
1340
|
+
/**
|
|
1341
|
+
* A small, deliberately conservative subset of URLPattern routing.
|
|
1342
|
+
*
|
|
1343
|
+
* The compiler emits routes such as `/:path(.*)?`, `/de/:path*`, or
|
|
1344
|
+
* `https://example.com/:path*`. For those routes matching is equivalent to a
|
|
1345
|
+
* pathname prefix check and (optionally) an origin check. Everything that has
|
|
1346
|
+
* a dynamic host, a custom path regexp, or another URLPattern modifier keeps
|
|
1347
|
+
* using the generic implementation above.
|
|
1348
|
+
*
|
|
1349
|
+
* @typedef {{
|
|
1350
|
+
* protocol: string | undefined;
|
|
1351
|
+
* hostname: string | undefined;
|
|
1352
|
+
* port: string | undefined;
|
|
1353
|
+
* pathnamePrefix: string;
|
|
1354
|
+
* pathMode: "segments" | "catch-all-optional" | "catch-all-required";
|
|
1355
|
+
* }} FastPathPattern
|
|
1356
|
+
* @typedef {{
|
|
1357
|
+
* base: FastPathPattern;
|
|
1358
|
+
* localized: Array<{ locale: string; pattern: FastPathPattern }>;
|
|
1359
|
+
* }} FastPathRoute
|
|
1360
|
+
*/
|
|
1361
|
+
/** @type {WeakMap<object, FastPathRoute | null>} */
|
|
1362
|
+
const fastPathRouteCache = new WeakMap();
|
|
1363
|
+
/**
|
|
1364
|
+
* @param {URL} urlObj
|
|
1365
|
+
* @param {string} targetLocale
|
|
1366
|
+
* @param {{ pattern: string; localized: Array<[string, string]> }} element
|
|
1367
|
+
* @returns {URL | undefined}
|
|
1368
|
+
*/
|
|
1369
|
+
function localizeUrlFastPath(urlObj, targetLocale, element) {
|
|
1370
|
+
const route = getFastPathRoute(element);
|
|
1371
|
+
if (route === null)
|
|
1372
|
+
return undefined;
|
|
1373
|
+
// Preserve URLPattern's ordering: localized patterns are checked before
|
|
1374
|
+
// the unlocalized pattern, and the first matching pattern wins.
|
|
1375
|
+
for (const localized of route.localized) {
|
|
1376
|
+
const suffix = matchFastPathPattern(localized.pattern, urlObj);
|
|
1377
|
+
if (suffix === undefined)
|
|
1378
|
+
continue;
|
|
1379
|
+
const target = route.localized.find((candidate) => candidate.locale === targetLocale)?.pattern;
|
|
1380
|
+
if (target === undefined)
|
|
1381
|
+
continue;
|
|
1382
|
+
return applyFastPathPattern(target, suffix, urlObj);
|
|
1383
|
+
}
|
|
1384
|
+
const suffix = matchFastPathPattern(route.base, urlObj);
|
|
1385
|
+
if (suffix !== undefined) {
|
|
1386
|
+
const target = route.localized.find((candidate) => candidate.locale === targetLocale)?.pattern;
|
|
1387
|
+
if (target !== undefined) {
|
|
1388
|
+
return applyFastPathPattern(target, suffix, urlObj);
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
return undefined;
|
|
1392
|
+
}
|
|
1393
|
+
/**
|
|
1394
|
+
* @param {URL} urlObj
|
|
1395
|
+
* @param {{ pattern: string; localized: Array<[string, string]> }} element
|
|
1396
|
+
* @returns {URL | undefined}
|
|
1397
|
+
*/
|
|
1398
|
+
function deLocalizeUrlFastPath(urlObj, element) {
|
|
1399
|
+
const route = getFastPathRoute(element);
|
|
1400
|
+
if (route === null)
|
|
1401
|
+
return undefined;
|
|
1402
|
+
for (const localized of route.localized) {
|
|
1403
|
+
const suffix = matchFastPathPattern(localized.pattern, urlObj);
|
|
1404
|
+
if (suffix !== undefined) {
|
|
1405
|
+
return applyFastPathPattern(route.base, suffix, urlObj);
|
|
1406
|
+
}
|
|
1407
|
+
}
|
|
1408
|
+
const suffix = matchFastPathPattern(route.base, urlObj);
|
|
1409
|
+
if (suffix !== undefined) {
|
|
1410
|
+
return applyFastPathPattern(route.base, suffix, urlObj);
|
|
1411
|
+
}
|
|
1412
|
+
return undefined;
|
|
1413
|
+
}
|
|
1414
|
+
/**
|
|
1415
|
+
* @param {{ pattern: string; localized: Array<[string, string]> }} element
|
|
1416
|
+
* @returns {FastPathRoute | null}
|
|
1417
|
+
*/
|
|
1418
|
+
function getFastPathRoute(element) {
|
|
1419
|
+
const cached = fastPathRouteCache.get(element);
|
|
1420
|
+
if (cached !== undefined)
|
|
1421
|
+
return cached;
|
|
1422
|
+
const base = parseFastPathPattern(element.pattern);
|
|
1423
|
+
if (base === undefined) {
|
|
1424
|
+
fastPathRouteCache.set(element, null);
|
|
1425
|
+
return null;
|
|
1426
|
+
}
|
|
1427
|
+
const localized = [];
|
|
1428
|
+
for (const [locale, pattern] of element.localized) {
|
|
1429
|
+
const parsed = parseFastPathPattern(pattern);
|
|
1430
|
+
if (parsed === undefined || parsed.pathMode !== base.pathMode) {
|
|
1431
|
+
fastPathRouteCache.set(element, null);
|
|
1432
|
+
return null;
|
|
1433
|
+
}
|
|
1434
|
+
localized.push({ locale, pattern: parsed });
|
|
1435
|
+
}
|
|
1436
|
+
const route = { base, localized };
|
|
1437
|
+
fastPathRouteCache.set(element, route);
|
|
1438
|
+
return route;
|
|
1439
|
+
}
|
|
1440
|
+
/**
|
|
1441
|
+
* Parse only catch-all path patterns. In particular, do not treat `:path(.)?`
|
|
1442
|
+
* as a catch-all: URLPattern's `(.)` means exactly one character and cannot be
|
|
1443
|
+
* represented by this prefix matcher without changing routing semantics.
|
|
1444
|
+
*
|
|
1445
|
+
* @param {string} pattern
|
|
1446
|
+
* @returns {FastPathPattern | undefined}
|
|
1447
|
+
*/
|
|
1448
|
+
function parseFastPathPattern(pattern) {
|
|
1449
|
+
const wildcard = pattern.match(/\/:path(?:\(\.\*\)(?:\?)?|\*)$/);
|
|
1450
|
+
if (wildcard === null || wildcard.index === undefined)
|
|
1451
|
+
return undefined;
|
|
1452
|
+
const prefix = pattern.slice(0, wildcard.index);
|
|
1453
|
+
const originAndPath = parseFastPathOriginAndPath(prefix);
|
|
1454
|
+
if (originAndPath === undefined)
|
|
1455
|
+
return undefined;
|
|
1456
|
+
const pathMode = wildcard[0].endsWith("*")
|
|
1457
|
+
? "segments"
|
|
1458
|
+
: wildcard[0].endsWith("?")
|
|
1459
|
+
? "catch-all-optional"
|
|
1460
|
+
: "catch-all-required";
|
|
1461
|
+
return { ...originAndPath, pathMode };
|
|
1462
|
+
}
|
|
1463
|
+
/**
|
|
1464
|
+
* @param {string} prefix
|
|
1465
|
+
* @returns {FastPathPattern | undefined}
|
|
1466
|
+
*/
|
|
1467
|
+
function parseFastPathOriginAndPath(prefix) {
|
|
1468
|
+
if (prefix === "" || prefix.startsWith("/")) {
|
|
1469
|
+
if (hasUrlPatternSyntax(prefix))
|
|
1470
|
+
return undefined;
|
|
1471
|
+
return {
|
|
1472
|
+
protocol: undefined,
|
|
1473
|
+
hostname: undefined,
|
|
1474
|
+
port: undefined,
|
|
1475
|
+
pathnamePrefix: normalizePathPrefix(prefix),
|
|
1476
|
+
pathMode: "catch-all-optional",
|
|
1477
|
+
};
|
|
1478
|
+
}
|
|
1479
|
+
// A `:protocol://host` pattern is common in generated configurations. The
|
|
1480
|
+
// protocol is intentionally left unconstrained, just like URLPattern.
|
|
1481
|
+
const dynamicProtocol = prefix.match(/^:protocol:\/\/([^/]+)(\/.*)?$/);
|
|
1482
|
+
const staticOrigin = prefix.match(/^([A-Za-z][A-Za-z\d+.-]*):\/\/([^/]+)(\/.*)?$/);
|
|
1483
|
+
if (dynamicProtocol !== null) {
|
|
1484
|
+
const dynamicHost = dynamicProtocol[1] ?? "";
|
|
1485
|
+
const hostMatch = dynamicHost.match(/^([^:(){}?*+]+)(?::(\d+))?$/);
|
|
1486
|
+
if (hostMatch === null ||
|
|
1487
|
+
(dynamicProtocol[2] !== undefined &&
|
|
1488
|
+
hasUrlPatternSyntax(dynamicProtocol[2]))) {
|
|
1489
|
+
return undefined;
|
|
1490
|
+
}
|
|
1491
|
+
return {
|
|
1492
|
+
protocol: undefined,
|
|
1493
|
+
hostname: (hostMatch[1] ?? "").toLowerCase(),
|
|
1494
|
+
port: hostMatch[2],
|
|
1495
|
+
pathnamePrefix: normalizePathPrefix(dynamicProtocol[2] ?? ""),
|
|
1496
|
+
pathMode: "catch-all-optional",
|
|
1497
|
+
};
|
|
1498
|
+
}
|
|
1499
|
+
if (staticOrigin === null)
|
|
1500
|
+
return undefined;
|
|
1501
|
+
const host = staticOrigin[2] ?? "";
|
|
1502
|
+
const pathname = staticOrigin[3];
|
|
1503
|
+
const hostMatch = host.match(/^([^:(){}?*+]+)(?::(\d+))?$/);
|
|
1504
|
+
if (hostMatch === null ||
|
|
1505
|
+
(pathname !== undefined && hasUrlPatternSyntax(pathname))) {
|
|
1506
|
+
return undefined;
|
|
1507
|
+
}
|
|
1508
|
+
return {
|
|
1509
|
+
protocol: `${staticOrigin[1]}:`,
|
|
1510
|
+
hostname: (hostMatch[1] ?? "").toLowerCase(),
|
|
1511
|
+
port: normalizePatternPort(hostMatch[2], `${staticOrigin[1]}:`),
|
|
1512
|
+
pathnamePrefix: normalizePathPrefix(pathname ?? ""),
|
|
1513
|
+
pathMode: "catch-all-optional",
|
|
1514
|
+
};
|
|
1515
|
+
}
|
|
1516
|
+
/**
|
|
1517
|
+
* @param {string | undefined} port
|
|
1518
|
+
* @param {string} protocol
|
|
1519
|
+
* @returns {string | undefined}
|
|
1520
|
+
*/
|
|
1521
|
+
function normalizePatternPort(port, protocol) {
|
|
1522
|
+
if (port === "80" && protocol === "http:")
|
|
1523
|
+
return "";
|
|
1524
|
+
if (port === "443" && protocol === "https:")
|
|
1525
|
+
return "";
|
|
1526
|
+
return port;
|
|
1527
|
+
}
|
|
1528
|
+
/**
|
|
1529
|
+
* @param {string} value
|
|
1530
|
+
* @returns {boolean}
|
|
1531
|
+
*/
|
|
1532
|
+
function hasUrlPatternSyntax(value) {
|
|
1533
|
+
return /[:(){}?*+]/.test(value);
|
|
1534
|
+
}
|
|
1535
|
+
/**
|
|
1536
|
+
* @param {string} pathname
|
|
1537
|
+
* @returns {string}
|
|
1538
|
+
*/
|
|
1539
|
+
function normalizePathPrefix(pathname) {
|
|
1540
|
+
if (pathname === "" || pathname === "/")
|
|
1541
|
+
return "/";
|
|
1542
|
+
return pathname.endsWith("/") ? pathname.slice(0, -1) : pathname;
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* @param {FastPathPattern} pattern
|
|
1546
|
+
* @param {URL} urlObj
|
|
1547
|
+
* @returns {string | undefined}
|
|
1548
|
+
*/
|
|
1549
|
+
function matchFastPathPattern(pattern, urlObj) {
|
|
1550
|
+
if (pattern.protocol !== undefined && pattern.protocol !== urlObj.protocol) {
|
|
1551
|
+
return undefined;
|
|
1552
|
+
}
|
|
1553
|
+
if (pattern.hostname !== undefined &&
|
|
1554
|
+
pattern.hostname !== urlObj.hostname.toLowerCase()) {
|
|
1555
|
+
return undefined;
|
|
1556
|
+
}
|
|
1557
|
+
if (pattern.hostname !== undefined) {
|
|
1558
|
+
const expectedPort = pattern.port ?? defaultPortForProtocol(pattern.protocol ?? urlObj.protocol);
|
|
1559
|
+
if (expectedPort !== urlObj.port)
|
|
1560
|
+
return undefined;
|
|
1561
|
+
}
|
|
1562
|
+
const prefix = pattern.pathnamePrefix;
|
|
1563
|
+
if (prefix === "/") {
|
|
1564
|
+
if (urlObj.pathname.startsWith("//"))
|
|
1565
|
+
return undefined;
|
|
1566
|
+
if (pattern.pathMode === "segments") {
|
|
1567
|
+
return isNonEmptyPathSegments(urlObj.pathname)
|
|
1568
|
+
? urlObj.pathname
|
|
1569
|
+
: undefined;
|
|
1570
|
+
}
|
|
1571
|
+
return urlObj.pathname;
|
|
1572
|
+
}
|
|
1573
|
+
if (urlObj.pathname === prefix) {
|
|
1574
|
+
return pattern.pathMode === "catch-all-required" ? undefined : "";
|
|
1575
|
+
}
|
|
1576
|
+
if (urlObj.pathname.startsWith(`${prefix}/`)) {
|
|
1577
|
+
const suffix = urlObj.pathname.slice(prefix.length);
|
|
1578
|
+
if (suffix.startsWith("//"))
|
|
1579
|
+
return undefined;
|
|
1580
|
+
if (pattern.pathMode === "segments" &&
|
|
1581
|
+
!isNonEmptyPathSegments(suffix)) {
|
|
1582
|
+
return undefined;
|
|
1583
|
+
}
|
|
1584
|
+
return suffix;
|
|
1585
|
+
}
|
|
1586
|
+
return undefined;
|
|
1587
|
+
}
|
|
1588
|
+
/**
|
|
1589
|
+
* @param {string} pathname
|
|
1590
|
+
* @returns {boolean}
|
|
1591
|
+
*/
|
|
1592
|
+
function isNonEmptyPathSegments(pathname) {
|
|
1593
|
+
return pathname.length > 1 && !pathname.endsWith("/") && !pathname.includes("//");
|
|
1594
|
+
}
|
|
1595
|
+
/**
|
|
1596
|
+
* URLPattern treats the default port as empty in URL instances.
|
|
1597
|
+
*
|
|
1598
|
+
* @param {string} protocol
|
|
1599
|
+
* @returns {string}
|
|
1600
|
+
*/
|
|
1601
|
+
function defaultPortForProtocol(protocol) {
|
|
1602
|
+
if (protocol === "http:")
|
|
1603
|
+
return "";
|
|
1604
|
+
if (protocol === "https:")
|
|
1605
|
+
return "";
|
|
1606
|
+
return "";
|
|
1607
|
+
}
|
|
1608
|
+
/**
|
|
1609
|
+
* @param {FastPathPattern} pattern
|
|
1610
|
+
* @param {string} suffix
|
|
1611
|
+
* @param {URL} source
|
|
1612
|
+
* @returns {URL}
|
|
1613
|
+
*/
|
|
1614
|
+
function applyFastPathPattern(pattern, suffix, source) {
|
|
1615
|
+
const localized = new URL(source.href);
|
|
1616
|
+
if (pattern.protocol !== undefined)
|
|
1617
|
+
localized.protocol = pattern.protocol;
|
|
1618
|
+
if (pattern.hostname !== undefined) {
|
|
1619
|
+
localized.hostname = pattern.hostname;
|
|
1620
|
+
localized.port =
|
|
1621
|
+
pattern.port ?? defaultPortForProtocol(localized.protocol);
|
|
1622
|
+
}
|
|
1623
|
+
localized.pathname = joinFastPathPrefix(pattern.pathnamePrefix, suffix);
|
|
1624
|
+
return localized;
|
|
1625
|
+
}
|
|
1626
|
+
/**
|
|
1627
|
+
* @param {string} prefix
|
|
1628
|
+
* @param {string} suffix
|
|
1629
|
+
* @returns {string}
|
|
1630
|
+
*/
|
|
1631
|
+
function joinFastPathPrefix(prefix, suffix) {
|
|
1632
|
+
if (prefix === "/")
|
|
1633
|
+
return suffix === "" ? "/" : suffix;
|
|
1634
|
+
if (suffix === "")
|
|
1635
|
+
return prefix;
|
|
1636
|
+
return `${prefix}${suffix.startsWith("/") ? suffix : `/${suffix}`}`;
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
/** @type {string | undefined} */
|
|
1640
|
+
let cachedRouteStrategyUrl;
|
|
1641
|
+
/** @type {{ match: string; strategy?: typeof strategy; exclude?: boolean } | undefined} */
|
|
1642
|
+
let cachedRouteStrategy;
|
|
1643
|
+
/**
|
|
1644
|
+
* Match route policy against both the public URL and its canonical URL.
|
|
1645
|
+
*
|
|
1646
|
+
* The function is deliberately separate from variables.js: configuration is
|
|
1647
|
+
* inert data, while canonicalization and route selection form a routing layer.
|
|
1648
|
+
*
|
|
1649
|
+
* @param {string | URL} url
|
|
1650
|
+
* @returns {{ match: string; strategy?: typeof strategy; exclude?: boolean } | undefined}
|
|
1651
|
+
*/
|
|
1652
|
+
export function findMatchingRouteStrategy(url) {
|
|
1653
|
+
if (routeStrategies.length === 0) {
|
|
1654
|
+
return undefined;
|
|
1655
|
+
}
|
|
1656
|
+
const urlString = typeof url === "string" ? url : url.href;
|
|
1657
|
+
if (cachedRouteStrategyUrl === urlString) {
|
|
1658
|
+
return cachedRouteStrategy;
|
|
1659
|
+
}
|
|
1660
|
+
const publicUrl = normalizeTrailingSlash(new URL(urlString, "http://example.com"));
|
|
1661
|
+
const canonicalUrl = deLocalizeUrl(publicUrl);
|
|
1662
|
+
const candidateUrls = canonicalUrl.href === publicUrl.href
|
|
1663
|
+
? [publicUrl]
|
|
1664
|
+
: [publicUrl, canonicalUrl];
|
|
1665
|
+
let match;
|
|
1666
|
+
for (const candidateUrl of candidateUrls) {
|
|
1667
|
+
for (const routeStrategy of routeStrategies) {
|
|
1668
|
+
const pattern = new URLPattern(routeStrategy.match, candidateUrl.href);
|
|
1669
|
+
if (execUrlPattern(pattern, candidateUrl)) {
|
|
1670
|
+
match = routeStrategy;
|
|
1671
|
+
break;
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1674
|
+
if (match)
|
|
1675
|
+
break;
|
|
1676
|
+
}
|
|
1677
|
+
cachedRouteStrategyUrl = urlString;
|
|
1678
|
+
cachedRouteStrategy = match;
|
|
1679
|
+
return match;
|
|
1680
|
+
}
|
|
1681
|
+
/**
|
|
1682
|
+
* Returns the strategy to use for a specific URL.
|
|
1683
|
+
*
|
|
1684
|
+
* If no route strategy matches (or the matching rule is `exclude: true`),
|
|
1685
|
+
* the global strategy is returned.
|
|
1686
|
+
*
|
|
1687
|
+
* @param {string | URL} url
|
|
1688
|
+
* @returns {typeof strategy}
|
|
1689
|
+
*/
|
|
1690
|
+
export function getStrategyForUrl(url) {
|
|
1691
|
+
const routeStrategy = findMatchingRouteStrategy(url);
|
|
1692
|
+
if (routeStrategy &&
|
|
1693
|
+
routeStrategy.exclude !== true &&
|
|
1694
|
+
Array.isArray(routeStrategy.strategy)) {
|
|
1695
|
+
return routeStrategy.strategy;
|
|
1696
|
+
}
|
|
1697
|
+
return strategy;
|
|
1698
|
+
}
|
|
1699
|
+
/**
|
|
1700
|
+
* Returns whether the given URL is excluded from middleware i18n processing.
|
|
1701
|
+
*
|
|
1702
|
+
* @param {string | URL} url
|
|
1703
|
+
* @returns {boolean}
|
|
1704
|
+
*/
|
|
1705
|
+
export function isExcludedByRouteStrategy(url) {
|
|
1706
|
+
return findMatchingRouteStrategy(url)?.exclude === true;
|
|
1707
|
+
}
|
|
1228
1708
|
|
|
1229
1709
|
/**
|
|
1230
1710
|
* @typedef {object} ShouldRedirectServerInput
|
|
@@ -1253,7 +1733,7 @@ export function aggregateGroups(match) {
|
|
|
1253
1733
|
*
|
|
1254
1734
|
* When called in the browser without arguments, the current `window.location.href` is used.
|
|
1255
1735
|
*
|
|
1256
|
-
* @see https://
|
|
1736
|
+
* @see https://paraglidejs.com/i18n-routing#redirects
|
|
1257
1737
|
*
|
|
1258
1738
|
* @example
|
|
1259
1739
|
* // Client side usage (e.g. TanStack Router beforeLoad hook)
|
|
@@ -1341,10 +1821,12 @@ async function resolveLocale(input, currentUrl) {
|
|
|
1341
1821
|
* @returns {URL}
|
|
1342
1822
|
*/
|
|
1343
1823
|
function resolveUrl(input) {
|
|
1344
|
-
if ("effectiveRequestUrl" in input &&
|
|
1824
|
+
if ("effectiveRequestUrl" in input &&
|
|
1825
|
+
input.effectiveRequestUrl instanceof URL) {
|
|
1345
1826
|
return new URL(input.effectiveRequestUrl.href);
|
|
1346
1827
|
}
|
|
1347
|
-
if ("effectiveRequestUrl" in input &&
|
|
1828
|
+
if ("effectiveRequestUrl" in input &&
|
|
1829
|
+
typeof input.effectiveRequestUrl === "string") {
|
|
1348
1830
|
return new URL(input.effectiveRequestUrl, input.request ? input.request.url : getUrlOrigin());
|
|
1349
1831
|
}
|
|
1350
1832
|
if (input.request) {
|
|
@@ -1369,7 +1851,9 @@ function resolveUrl(input) {
|
|
|
1369
1851
|
*/
|
|
1370
1852
|
function normalizeUrl(url) {
|
|
1371
1853
|
const urlObj = new URL(url);
|
|
1372
|
-
|
|
1854
|
+
if (trailingSlash === undefined) {
|
|
1855
|
+
urlObj.pathname = urlObj.pathname.replace(/\/$/, "");
|
|
1856
|
+
}
|
|
1373
1857
|
return urlObj.href;
|
|
1374
1858
|
}
|
|
1375
1859
|
|
|
@@ -1384,7 +1868,7 @@ function normalizeUrl(url) {
|
|
|
1384
1868
|
* - Automatically detects current locale if not specified
|
|
1385
1869
|
* - Handles string input/output instead of URL objects
|
|
1386
1870
|
*
|
|
1387
|
-
* @see https://
|
|
1871
|
+
* @see https://paraglidejs.com/i18n-routing
|
|
1388
1872
|
*
|
|
1389
1873
|
* @example
|
|
1390
1874
|
* ```typescript
|
|
@@ -1444,7 +1928,7 @@ export function localizeHref(href, options) {
|
|
|
1444
1928
|
* - Returns relative paths when possible
|
|
1445
1929
|
* - Handles string input/output instead of URL objects
|
|
1446
1930
|
*
|
|
1447
|
-
* @see https://
|
|
1931
|
+
* @see https://paraglidejs.com/i18n-routing
|
|
1448
1932
|
*
|
|
1449
1933
|
* @example
|
|
1450
1934
|
* ```typescript
|
|
@@ -1508,7 +1992,7 @@ export function trackMessageCall(safeModuleId, locale) {
|
|
|
1508
1992
|
* The function respects your `urlPatterns` configuration - if you have translated pathnames
|
|
1509
1993
|
* (e.g., `/about` → `/ueber-uns` for German), it will generate the correct localized paths.
|
|
1510
1994
|
*
|
|
1511
|
-
* @see https://
|
|
1995
|
+
* @see https://paraglidejs.com/static-site-generation
|
|
1512
1996
|
*
|
|
1513
1997
|
* @example
|
|
1514
1998
|
* // Basic usage - generate all locale variants for a list of paths
|
|
@@ -1547,9 +2031,12 @@ export function generateStaticLocalizedUrls(urls) {
|
|
|
1547
2031
|
// For default URL pattern, we can optimize the generation
|
|
1548
2032
|
if (TREE_SHAKE_DEFAULT_URL_PATTERN_USED) {
|
|
1549
2033
|
for (const urlInput of urls) {
|
|
1550
|
-
const
|
|
2034
|
+
const originalUrl = urlInput instanceof URL
|
|
1551
2035
|
? urlInput
|
|
1552
2036
|
: new URL(urlInput, "http://localhost");
|
|
2037
|
+
const url = trailingSlash === undefined
|
|
2038
|
+
? originalUrl
|
|
2039
|
+
: normalizeTrailingSlash(new URL(originalUrl));
|
|
1553
2040
|
// Base locale doesn't get a prefix
|
|
1554
2041
|
localizedUrls.add(url);
|
|
1555
2042
|
// Other locales get their code as prefix
|
|
@@ -1557,7 +2044,7 @@ export function generateStaticLocalizedUrls(urls) {
|
|
|
1557
2044
|
if (locale !== baseLocale) {
|
|
1558
2045
|
const localizedPath = `/${locale}${url.pathname}${url.search}${url.hash}`;
|
|
1559
2046
|
const localizedUrl = new URL(localizedPath, url.origin);
|
|
1560
|
-
localizedUrls.add(localizedUrl);
|
|
2047
|
+
localizedUrls.add(normalizeTrailingSlash(localizedUrl));
|
|
1561
2048
|
}
|
|
1562
2049
|
}
|
|
1563
2050
|
}
|
|
@@ -1565,15 +2052,16 @@ export function generateStaticLocalizedUrls(urls) {
|
|
|
1565
2052
|
}
|
|
1566
2053
|
// For custom URL patterns, we need to use localizeUrl for each URL and locale
|
|
1567
2054
|
for (const urlInput of urls) {
|
|
1568
|
-
const
|
|
2055
|
+
const originalUrl = urlInput instanceof URL
|
|
1569
2056
|
? urlInput
|
|
1570
2057
|
: new URL(urlInput, "http://localhost");
|
|
2058
|
+
const url = normalizeTrailingSlash(new URL(originalUrl));
|
|
1571
2059
|
// Try each URL pattern to find one that matches
|
|
1572
2060
|
let patternFound = false;
|
|
1573
2061
|
for (const pattern of urlPatterns) {
|
|
1574
2062
|
try {
|
|
1575
2063
|
// Try to match the unlocalized pattern
|
|
1576
|
-
const unlocalizedMatch = new URLPattern(pattern.pattern, url.href)
|
|
2064
|
+
const unlocalizedMatch = execUrlPattern(new URLPattern(pattern.pattern, url.href), url);
|
|
1577
2065
|
if (!unlocalizedMatch)
|
|
1578
2066
|
continue;
|
|
1579
2067
|
patternFound = true;
|
|
@@ -1604,7 +2092,7 @@ export function generateStaticLocalizedUrls(urls) {
|
|
|
1604
2092
|
}
|
|
1605
2093
|
// If no pattern matched, use the URL as is
|
|
1606
2094
|
if (!patternFound) {
|
|
1607
|
-
localizedUrls.add(
|
|
2095
|
+
localizedUrls.add(originalUrl);
|
|
1608
2096
|
}
|
|
1609
2097
|
}
|
|
1610
2098
|
return Array.from(localizedUrls);
|
|
@@ -1645,7 +2133,7 @@ export function isCustomStrategy(strategy) {
|
|
|
1645
2133
|
/**
|
|
1646
2134
|
* Defines a custom strategy that is executed on the server.
|
|
1647
2135
|
*
|
|
1648
|
-
* @see https://
|
|
2136
|
+
* @see https://paraglidejs.com/strategy#write-your-own-strategy
|
|
1649
2137
|
*
|
|
1650
2138
|
* @param {string} strategy The name of the custom strategy to define. Must follow the pattern custom-name with alphanumeric characters, hyphens, or underscores.
|
|
1651
2139
|
* @param {CustomServerStrategyHandler} handler The handler for the custom strategy, which should implement
|
|
@@ -1661,7 +2149,7 @@ export function defineCustomServerStrategy(strategy, handler) {
|
|
|
1661
2149
|
/**
|
|
1662
2150
|
* Defines a custom strategy that is executed on the client.
|
|
1663
2151
|
*
|
|
1664
|
-
* @see https://
|
|
2152
|
+
* @see https://paraglidejs.com/strategy#write-your-own-strategy
|
|
1665
2153
|
*
|
|
1666
2154
|
* @param {string} strategy The name of the custom strategy to define. Must follow the pattern custom-name with alphanumeric characters, hyphens, or underscores.
|
|
1667
2155
|
* @param {CustomClientStrategyHandler} handler The handler for the custom strategy, which should implement the
|