@bleedingdev/modern-js-runtime-extensions 3.9.0-ultramodern.10 → 3.9.0-ultramodern.12
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/localisedUrls/config.js +49 -0
- package/dist/cjs/localisedUrls/index.js +95 -0
- package/dist/cjs/localisedUrls/normalise.js +87 -0
- package/dist/cjs/localisedUrls/pathname.js +68 -0
- package/dist/cjs/localisedUrls/patterns.js +214 -0
- package/dist/cjs/localisedUrls/redirect.js +100 -0
- package/dist/cjs/localisedUrls/resolve.js +78 -0
- package/dist/cjs/localisedUrls/routeIdentity.js +92 -0
- package/dist/cjs/localisedUrls/routes.js +217 -0
- package/dist/cjs/localisedUrls/types.js +18 -0
- package/dist/cjs/routerState.js +48 -77
- package/dist/cjs/routerStatePlugin.js +36 -9
- package/dist/esm/localisedUrls/config.mjs +11 -0
- package/dist/esm/localisedUrls/index.mjs +8 -0
- package/dist/esm/localisedUrls/normalise.mjs +37 -0
- package/dist/esm/localisedUrls/pathname.mjs +24 -0
- package/dist/esm/localisedUrls/patterns.mjs +152 -0
- package/dist/esm/localisedUrls/redirect.mjs +53 -0
- package/dist/esm/localisedUrls/resolve.mjs +37 -0
- package/dist/esm/localisedUrls/routeIdentity.mjs +54 -0
- package/dist/esm/localisedUrls/routes.mjs +176 -0
- package/dist/esm/localisedUrls/types.mjs +0 -0
- package/dist/esm/routerState.mjs +49 -75
- package/dist/esm/routerStatePlugin.mjs +37 -10
- package/dist/esm-node/localisedUrls/config.mjs +12 -0
- package/dist/esm-node/localisedUrls/index.mjs +9 -0
- package/dist/esm-node/localisedUrls/normalise.mjs +38 -0
- package/dist/esm-node/localisedUrls/pathname.mjs +25 -0
- package/dist/esm-node/localisedUrls/patterns.mjs +153 -0
- package/dist/esm-node/localisedUrls/redirect.mjs +54 -0
- package/dist/esm-node/localisedUrls/resolve.mjs +38 -0
- package/dist/esm-node/localisedUrls/routeIdentity.mjs +55 -0
- package/dist/esm-node/localisedUrls/routes.mjs +177 -0
- package/dist/esm-node/localisedUrls/types.mjs +1 -0
- package/dist/esm-node/routerState.mjs +49 -75
- package/dist/esm-node/routerStatePlugin.mjs +37 -10
- package/dist/types/localisedUrls/config.d.ts +9 -0
- package/dist/types/localisedUrls/index.d.ts +10 -0
- package/dist/types/localisedUrls/normalise.d.ts +10 -0
- package/dist/types/localisedUrls/pathname.d.ts +4 -0
- package/dist/types/localisedUrls/patterns.d.ts +11 -0
- package/dist/types/localisedUrls/redirect.d.ts +19 -0
- package/dist/types/localisedUrls/resolve.d.ts +7 -0
- package/dist/types/localisedUrls/routeIdentity.d.ts +22 -0
- package/dist/types/localisedUrls/routes.d.ts +3 -0
- package/dist/types/localisedUrls/types.d.ts +14 -0
- package/dist/types/routeHydration.d.ts +2 -2
- package/dist/types/routerState.d.ts +5 -6
- package/dist/types/routerStateTypes.d.ts +47 -17
- package/package.json +16 -4
- package/src/localisedUrls/config.ts +18 -0
- package/src/localisedUrls/index.ts +25 -0
- package/src/localisedUrls/normalise.ts +62 -0
- package/src/localisedUrls/pathname.ts +60 -0
- package/src/localisedUrls/patterns.ts +283 -0
- package/src/localisedUrls/redirect.ts +107 -0
- package/src/localisedUrls/resolve.ts +106 -0
- package/src/localisedUrls/routeIdentity.ts +83 -0
- package/src/localisedUrls/routes.ts +451 -0
- package/src/localisedUrls/types.ts +18 -0
- package/src/routeHydration.ts +2 -2
- package/src/routerState.ts +52 -122
- package/src/routerStatePlugin.ts +49 -10
- package/src/routerStateTypes.ts +47 -17
- package/tests/localisedUrls.test.ts +560 -0
- package/tests/routerState.test.ts +7 -8
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { normalisePathname } from './normalise';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
buildPathFromPattern,
|
|
5
|
+
matchPathPattern,
|
|
6
|
+
sortPatternsBySpecificity,
|
|
7
|
+
} from './patterns';
|
|
8
|
+
|
|
9
|
+
import type { LocalisedUrlsMap } from './types';
|
|
10
|
+
|
|
11
|
+
type CanonicalCandidate = {
|
|
12
|
+
pattern: string;
|
|
13
|
+
canonicalPattern: string;
|
|
14
|
+
localisedUrlEntry: LocalisedUrlsMap[string];
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
type ResolveLocalisedUrlPathOptions =
|
|
18
|
+
| {
|
|
19
|
+
mode: 'localised';
|
|
20
|
+
targetLanguage: string;
|
|
21
|
+
}
|
|
22
|
+
| {
|
|
23
|
+
mode: 'canonical';
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const getCanonicalCandidates = (
|
|
27
|
+
localisedUrls: LocalisedUrlsMap,
|
|
28
|
+
): CanonicalCandidate[] =>
|
|
29
|
+
sortPatternsBySpecificity(
|
|
30
|
+
Object.entries(localisedUrls).map(
|
|
31
|
+
([canonicalPattern, localisedUrlEntry]) => ({
|
|
32
|
+
pattern: canonicalPattern,
|
|
33
|
+
canonicalPattern,
|
|
34
|
+
localisedUrlEntry,
|
|
35
|
+
}),
|
|
36
|
+
),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const resolveLocalisedUrlPath = (
|
|
40
|
+
pathname: string,
|
|
41
|
+
languages: string[],
|
|
42
|
+
localisedUrls: LocalisedUrlsMap,
|
|
43
|
+
options: ResolveLocalisedUrlPathOptions,
|
|
44
|
+
): string => {
|
|
45
|
+
const normalizedPathname = normalisePathname(pathname);
|
|
46
|
+
const canonicalCandidates = getCanonicalCandidates(localisedUrls);
|
|
47
|
+
|
|
48
|
+
const candidates = sortPatternsBySpecificity(
|
|
49
|
+
canonicalCandidates.flatMap(({ canonicalPattern, localisedUrlEntry }) => {
|
|
50
|
+
const targetPattern =
|
|
51
|
+
options.mode === 'localised'
|
|
52
|
+
? localisedUrlEntry[options.targetLanguage]
|
|
53
|
+
: canonicalPattern;
|
|
54
|
+
if (!targetPattern) {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const sourcePatterns = new Set([
|
|
59
|
+
canonicalPattern,
|
|
60
|
+
...languages
|
|
61
|
+
.map(language => localisedUrlEntry[language])
|
|
62
|
+
.filter((sourcePattern): sourcePattern is string =>
|
|
63
|
+
Boolean(sourcePattern),
|
|
64
|
+
),
|
|
65
|
+
]);
|
|
66
|
+
return Array.from(sourcePatterns, sourcePattern => ({
|
|
67
|
+
pattern: sourcePattern,
|
|
68
|
+
sourcePattern,
|
|
69
|
+
targetPattern,
|
|
70
|
+
}));
|
|
71
|
+
}),
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
for (const { sourcePattern, targetPattern } of candidates) {
|
|
75
|
+
const params = matchPathPattern(normalizedPathname, sourcePattern);
|
|
76
|
+
if (params) {
|
|
77
|
+
return buildPathFromPattern(targetPattern, params);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return normalizedPathname;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export const resolveLocalisedPath = (
|
|
85
|
+
pathname: string,
|
|
86
|
+
targetLanguage: string,
|
|
87
|
+
languages: string[],
|
|
88
|
+
localisedUrls: LocalisedUrlsMap,
|
|
89
|
+
): string =>
|
|
90
|
+
resolveLocalisedUrlPath(pathname, languages, localisedUrls, {
|
|
91
|
+
mode: 'localised',
|
|
92
|
+
targetLanguage,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Reverse-map language-specific pathnames back to language-agnostic
|
|
97
|
+
* LocalisedUrlsMap keys.
|
|
98
|
+
*/
|
|
99
|
+
export const resolveCanonicalLocalisedPath = (
|
|
100
|
+
pathname: string,
|
|
101
|
+
languages: string[],
|
|
102
|
+
localisedUrls: LocalisedUrlsMap,
|
|
103
|
+
): string =>
|
|
104
|
+
resolveLocalisedUrlPath(pathname, languages, localisedUrls, {
|
|
105
|
+
mode: 'canonical',
|
|
106
|
+
});
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { normalisePathPattern } from './normalise';
|
|
2
|
+
import { buildPathFromPattern, matchPathPattern } from './patterns';
|
|
3
|
+
|
|
4
|
+
interface RouteIdentity {
|
|
5
|
+
id?: string;
|
|
6
|
+
path?: string;
|
|
7
|
+
canonicalPath: string;
|
|
8
|
+
paths?: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface RouteNode {
|
|
12
|
+
id?: string;
|
|
13
|
+
path?: string;
|
|
14
|
+
children?: RouteNode[];
|
|
15
|
+
modernLocalisedRoute?: RouteIdentity;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type PathRewrite = { from: string; to: string };
|
|
19
|
+
|
|
20
|
+
function rewritePath(url: URL, mappings: PathRewrite[]) {
|
|
21
|
+
for (const { from, to } of mappings) {
|
|
22
|
+
const params = matchPathPattern(url.pathname, from);
|
|
23
|
+
if (params) {
|
|
24
|
+
url.pathname = buildPathFromPattern(to, params);
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return url;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Use the router's URL rewrite seam; browser URLs retain their locale spelling. */
|
|
32
|
+
export function createLocalisedRouteRewrite(routes: RouteNode[]) {
|
|
33
|
+
const input: PathRewrite[] = [];
|
|
34
|
+
const output: PathRewrite[] = [];
|
|
35
|
+
const visit = (nodes: RouteNode[], parent: string) => {
|
|
36
|
+
for (const route of nodes) {
|
|
37
|
+
const fullPath = normalisePathPattern(`${parent}/${route.path ?? ''}`);
|
|
38
|
+
const identity = route.modernLocalisedRoute;
|
|
39
|
+
if (identity?.paths && fullPath.endsWith(identity.canonicalPath)) {
|
|
40
|
+
const prefix = fullPath.slice(0, -identity.canonicalPath.length);
|
|
41
|
+
const hasLocale = prefix
|
|
42
|
+
.split('/')
|
|
43
|
+
.some(segment => /^:(?:lang|locale|language)\??$/.test(segment));
|
|
44
|
+
for (const [language, localisedPath] of Object.entries(
|
|
45
|
+
identity.paths,
|
|
46
|
+
)) {
|
|
47
|
+
const localePrefix = prefix
|
|
48
|
+
.split('/')
|
|
49
|
+
.map(segment =>
|
|
50
|
+
/^:(?:lang|locale|language)\??$/.test(segment)
|
|
51
|
+
? language
|
|
52
|
+
: segment,
|
|
53
|
+
)
|
|
54
|
+
.join('/');
|
|
55
|
+
const canonical = normalisePathPattern(
|
|
56
|
+
`${localePrefix}/${identity.canonicalPath}`,
|
|
57
|
+
);
|
|
58
|
+
const localised = normalisePathPattern(
|
|
59
|
+
`${localePrefix}/${localisedPath}`,
|
|
60
|
+
);
|
|
61
|
+
if (canonical !== localised) {
|
|
62
|
+
input.push({ from: localised, to: canonical });
|
|
63
|
+
if (hasLocale) output.push({ from: canonical, to: localised });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
if (route.children) visit(route.children, fullPath);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
visit(routes, '');
|
|
71
|
+
if (input.length === 0) return undefined;
|
|
72
|
+
const specificity = (pattern: string) =>
|
|
73
|
+
pattern
|
|
74
|
+
.split('/')
|
|
75
|
+
.filter(segment => segment && !segment.startsWith(':') && segment !== '*')
|
|
76
|
+
.length;
|
|
77
|
+
for (const mappings of [input, output])
|
|
78
|
+
mappings.sort((a, b) => specificity(b.from) - specificity(a.from));
|
|
79
|
+
return {
|
|
80
|
+
input: ({ url }: { url: URL }) => rewritePath(url, input),
|
|
81
|
+
output: ({ url }: { url: URL }) => rewritePath(url, output),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getLeadingLocaleParam,
|
|
3
|
+
normalisePathPattern,
|
|
4
|
+
normaliseRoutePath,
|
|
5
|
+
stripLeadingLocaleParam,
|
|
6
|
+
} from './normalise';
|
|
7
|
+
import {
|
|
8
|
+
doPathPatternsOverlap,
|
|
9
|
+
getOrderedPathParamSignature,
|
|
10
|
+
getPathParamSignature,
|
|
11
|
+
getPathPatternValidationIssue,
|
|
12
|
+
getPhysicalPathPattern,
|
|
13
|
+
haveEqualPatternSpecificity,
|
|
14
|
+
} from './patterns';
|
|
15
|
+
import type {
|
|
16
|
+
LocalisedRoute,
|
|
17
|
+
LocalisedUrlPathMap,
|
|
18
|
+
LocalisedUrlsMap,
|
|
19
|
+
} from './types';
|
|
20
|
+
|
|
21
|
+
const isLocalisableRoutePath = (path?: string): path is string => {
|
|
22
|
+
const pathWithoutLocale = stripLeadingLocaleParam(path);
|
|
23
|
+
|
|
24
|
+
if (
|
|
25
|
+
!pathWithoutLocale ||
|
|
26
|
+
pathWithoutLocale === '/' ||
|
|
27
|
+
pathWithoutLocale === '*'
|
|
28
|
+
) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return true;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const joinPath = (parentPath: string, routePath?: string): string => {
|
|
36
|
+
if (!isLocalisableRoutePath(routePath)) {
|
|
37
|
+
return parentPath;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const segment = normaliseRoutePath(stripLeadingLocaleParam(routePath) || '');
|
|
41
|
+
return normalisePathPattern(`${parentPath}/${segment}`);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const isFrameworkInternalRoutePath = (canonicalPath: string) =>
|
|
45
|
+
canonicalPath === '/_mf' || canonicalPath.startsWith('/_mf/');
|
|
46
|
+
|
|
47
|
+
const legalRouteIdPart = (value: string): string =>
|
|
48
|
+
value.replace(/[^a-zA-Z0-9_$-]+/g, '_').replace(/^_+|_+$/g, '') || 'index';
|
|
49
|
+
|
|
50
|
+
const formatParamSignature = (signature: string[]): string =>
|
|
51
|
+
signature.length > 0 ? signature.join(', ') : '(none)';
|
|
52
|
+
|
|
53
|
+
const validateLocalisedUrlPatterns = (
|
|
54
|
+
languages: string[],
|
|
55
|
+
localisedUrls: LocalisedUrlsMap,
|
|
56
|
+
) => {
|
|
57
|
+
const physicalPatterns = new Map<
|
|
58
|
+
string,
|
|
59
|
+
{ canonicalPath: string; source: string }
|
|
60
|
+
>();
|
|
61
|
+
const claimedPatterns: Array<{
|
|
62
|
+
canonicalPath: string;
|
|
63
|
+
pattern: string;
|
|
64
|
+
source: string;
|
|
65
|
+
}> = [];
|
|
66
|
+
|
|
67
|
+
const claimPhysicalPattern = (
|
|
68
|
+
sourcePattern: string,
|
|
69
|
+
canonicalPath: string,
|
|
70
|
+
source: string,
|
|
71
|
+
) => {
|
|
72
|
+
const physicalPattern = getPhysicalPathPattern(sourcePattern);
|
|
73
|
+
const existing = physicalPatterns.get(physicalPattern);
|
|
74
|
+
if (existing && existing.canonicalPath !== canonicalPath) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
`localisedUrls["${existing.canonicalPath}"] ${existing.source} and localisedUrls["${canonicalPath}"] ${source} generate the same physical route pattern "${physicalPattern}". Localised route patterns must identify exactly one canonical route.`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const overlapping = claimedPatterns.find(
|
|
81
|
+
claimed =>
|
|
82
|
+
claimed.canonicalPath !== canonicalPath &&
|
|
83
|
+
haveEqualPatternSpecificity(claimed.pattern, sourcePattern) &&
|
|
84
|
+
doPathPatternsOverlap(claimed.pattern, sourcePattern),
|
|
85
|
+
);
|
|
86
|
+
if (overlapping) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`localisedUrls["${overlapping.canonicalPath}"] ${overlapping.source} pattern "${overlapping.pattern}" and localisedUrls["${canonicalPath}"] ${source} pattern "${sourcePattern}" are equal-specificity overlapping route patterns. Localised route patterns must identify exactly one canonical route.`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
physicalPatterns.set(physicalPattern, { canonicalPath, source });
|
|
93
|
+
claimedPatterns.push({ canonicalPath, pattern: sourcePattern, source });
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
for (const [canonicalPath, entry] of Object.entries(localisedUrls).sort(
|
|
97
|
+
([left], [right]) => (left < right ? -1 : left > right ? 1 : 0),
|
|
98
|
+
)) {
|
|
99
|
+
const canonicalIssue = getPathPatternValidationIssue(canonicalPath);
|
|
100
|
+
if (canonicalIssue) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`localisedUrls["${canonicalPath}"] canonical pattern has ${canonicalIssue}.`,
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
const expectedSignature = getPathParamSignature(canonicalPath);
|
|
106
|
+
const expectedOrderedSignature =
|
|
107
|
+
getOrderedPathParamSignature(canonicalPath);
|
|
108
|
+
claimPhysicalPattern(canonicalPath, canonicalPath, 'canonical pattern');
|
|
109
|
+
|
|
110
|
+
for (const language of languages) {
|
|
111
|
+
const localisedPattern = entry[language];
|
|
112
|
+
if (!localisedPattern) {
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const localisedIssue = getPathPatternValidationIssue(localisedPattern);
|
|
117
|
+
if (localisedIssue) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
`localisedUrls["${canonicalPath}"].${language} has ${localisedIssue}.`,
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const actualSignature = getPathParamSignature(localisedPattern);
|
|
124
|
+
if (
|
|
125
|
+
actualSignature.length !== expectedSignature.length ||
|
|
126
|
+
actualSignature.some(
|
|
127
|
+
(parameter, index) => parameter !== expectedSignature[index],
|
|
128
|
+
)
|
|
129
|
+
) {
|
|
130
|
+
throw new Error(
|
|
131
|
+
`localisedUrls["${canonicalPath}"].${language} must use the same parameter signature as "${canonicalPath}": expected ${formatParamSignature(
|
|
132
|
+
expectedSignature,
|
|
133
|
+
)}; received ${formatParamSignature(actualSignature)}.`,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const actualOrderedSignature =
|
|
138
|
+
getOrderedPathParamSignature(localisedPattern);
|
|
139
|
+
if (
|
|
140
|
+
expectedOrderedSignature.some(
|
|
141
|
+
parameter => parameter === '*' || parameter.endsWith('?'),
|
|
142
|
+
) &&
|
|
143
|
+
actualOrderedSignature.some(
|
|
144
|
+
(parameter, index) => parameter !== expectedOrderedSignature[index],
|
|
145
|
+
)
|
|
146
|
+
) {
|
|
147
|
+
throw new Error(
|
|
148
|
+
`localisedUrls["${canonicalPath}"].${language} must keep optional and splat parameters in canonical order: expected ${formatParamSignature(
|
|
149
|
+
expectedOrderedSignature,
|
|
150
|
+
)}; received ${formatParamSignature(actualOrderedSignature)}.`,
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
claimPhysicalPattern(localisedPattern, canonicalPath, `.${language}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const ensureLocalisedUrlsForPath = (
|
|
160
|
+
canonicalPath: string,
|
|
161
|
+
languages: string[],
|
|
162
|
+
localisedUrls: LocalisedUrlsMap,
|
|
163
|
+
): LocalisedUrlPathMap => {
|
|
164
|
+
const entry = localisedUrls[canonicalPath];
|
|
165
|
+
if (!entry) {
|
|
166
|
+
throw new Error(
|
|
167
|
+
`localisedUrls is enabled, but route "${canonicalPath}" does not define localised URLs for languages: ${languages.join(
|
|
168
|
+
', ',
|
|
169
|
+
)}. Add localisedUrls["${canonicalPath}"] or set localeDetection.localisedUrls to false.`,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const missingLanguages = languages.filter(language => !entry[language]);
|
|
174
|
+
if (missingLanguages.length > 0) {
|
|
175
|
+
throw new Error(
|
|
176
|
+
`localisedUrls["${canonicalPath}"] is missing languages: ${missingLanguages.join(
|
|
177
|
+
', ',
|
|
178
|
+
)}. Every configured language must have a localised URL.`,
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return entry;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export const validateLocalisedUrls = (
|
|
186
|
+
routes: LocalisedRoute[],
|
|
187
|
+
languages: string[],
|
|
188
|
+
localisedUrls: LocalisedUrlsMap,
|
|
189
|
+
) => {
|
|
190
|
+
validateLocalisedUrlPatterns(languages, localisedUrls);
|
|
191
|
+
|
|
192
|
+
const visit = (route: LocalisedRoute, parentPath: string) => {
|
|
193
|
+
const canonicalPath = joinPath(parentPath, route.path);
|
|
194
|
+
if (
|
|
195
|
+
isLocalisableRoutePath(route.path) &&
|
|
196
|
+
!isFrameworkInternalRoutePath(canonicalPath)
|
|
197
|
+
) {
|
|
198
|
+
ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if ('children' in route && route.children) {
|
|
202
|
+
route.children.forEach(child => visit(child, canonicalPath));
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
routes.forEach(route => visit(route, ''));
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const getLocalisedRouteIdSuffixes = (
|
|
210
|
+
paths: string[],
|
|
211
|
+
): Array<string | undefined> => {
|
|
212
|
+
const usedSuffixes = new Set<string>();
|
|
213
|
+
|
|
214
|
+
return paths.map((path, index) => {
|
|
215
|
+
if (index === 0) {
|
|
216
|
+
return undefined;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const baseSuffix = legalRouteIdPart(path);
|
|
220
|
+
let suffix = baseSuffix;
|
|
221
|
+
let disambiguator = 2;
|
|
222
|
+
while (usedSuffixes.has(suffix)) {
|
|
223
|
+
suffix = `${baseSuffix}_${disambiguator++}`;
|
|
224
|
+
}
|
|
225
|
+
usedSuffixes.add(suffix);
|
|
226
|
+
return suffix;
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const getLocalisedRoutePaths = (
|
|
231
|
+
canonicalPath: string,
|
|
232
|
+
parentLocalisedPaths: Record<string, string>,
|
|
233
|
+
languages: string[],
|
|
234
|
+
entry: LocalisedUrlPathMap,
|
|
235
|
+
): string[] => {
|
|
236
|
+
const paths = languages.map(language => {
|
|
237
|
+
const fullPath = normalisePathPattern(entry[language]);
|
|
238
|
+
const parentPath = normalisePathPattern(
|
|
239
|
+
parentLocalisedPaths[language] || '/',
|
|
240
|
+
);
|
|
241
|
+
if (parentPath === '/') {
|
|
242
|
+
return normaliseRoutePath(fullPath) || undefined;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
const parentPrefix = `${parentPath}/`;
|
|
246
|
+
if (!fullPath.startsWith(parentPrefix)) {
|
|
247
|
+
throw new Error(
|
|
248
|
+
`localisedUrls["${canonicalPath}"].${language} must be nested under "${parentPath}" because its parent route is localised there.`,
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return normaliseRoutePath(fullPath.slice(parentPath.length));
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
const uniquePaths = Array.from(new Set(paths.filter(Boolean) as string[]));
|
|
256
|
+
return uniquePaths;
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
const transformLocalisedRoute = (
|
|
260
|
+
route: LocalisedRoute,
|
|
261
|
+
parentCanonicalPath: string,
|
|
262
|
+
parentLocalisedPaths: Record<string, string>,
|
|
263
|
+
languages: string[],
|
|
264
|
+
localisedUrls: LocalisedUrlsMap,
|
|
265
|
+
projection: 'physical' | 'canonical',
|
|
266
|
+
): LocalisedRoute[] => {
|
|
267
|
+
const isSplat = stripLeadingLocaleParam(route.path) === '*';
|
|
268
|
+
const canonicalPath = isSplat
|
|
269
|
+
? normalisePathPattern(`${parentCanonicalPath}/*`)
|
|
270
|
+
: joinPath(parentCanonicalPath, route.path);
|
|
271
|
+
const localisedUrlEntry =
|
|
272
|
+
isLocalisableRoutePath(route.path) &&
|
|
273
|
+
!isFrameworkInternalRoutePath(canonicalPath)
|
|
274
|
+
? ensureLocalisedUrlsForPath(canonicalPath, languages, localisedUrls)
|
|
275
|
+
: undefined;
|
|
276
|
+
const routeLocalisedPaths = localisedUrlEntry
|
|
277
|
+
? languages.reduce<Record<string, string>>((acc, language) => {
|
|
278
|
+
acc[language] = normalisePathPattern(localisedUrlEntry[language]);
|
|
279
|
+
return acc;
|
|
280
|
+
}, {})
|
|
281
|
+
: isSplat
|
|
282
|
+
? Object.fromEntries(
|
|
283
|
+
Object.entries(parentLocalisedPaths).map(([language, path]) => [
|
|
284
|
+
language,
|
|
285
|
+
normalisePathPattern(`${path}/*`),
|
|
286
|
+
]),
|
|
287
|
+
)
|
|
288
|
+
: parentLocalisedPaths;
|
|
289
|
+
|
|
290
|
+
const children =
|
|
291
|
+
'children' in route && route.children
|
|
292
|
+
? route.children.flatMap(child =>
|
|
293
|
+
transformLocalisedRoute(
|
|
294
|
+
child,
|
|
295
|
+
canonicalPath,
|
|
296
|
+
routeLocalisedPaths,
|
|
297
|
+
languages,
|
|
298
|
+
localisedUrls,
|
|
299
|
+
projection,
|
|
300
|
+
),
|
|
301
|
+
)
|
|
302
|
+
: undefined;
|
|
303
|
+
|
|
304
|
+
const baseRoute = {
|
|
305
|
+
...route,
|
|
306
|
+
...(!isFrameworkInternalRoutePath(canonicalPath)
|
|
307
|
+
? {
|
|
308
|
+
modernLocalisedRoute: {
|
|
309
|
+
id: route.id,
|
|
310
|
+
path: route.path,
|
|
311
|
+
canonicalPath,
|
|
312
|
+
paths:
|
|
313
|
+
localisedUrlEntry ?? (isSplat ? routeLocalisedPaths : undefined),
|
|
314
|
+
},
|
|
315
|
+
}
|
|
316
|
+
: {}),
|
|
317
|
+
...(children ? { children } : {}),
|
|
318
|
+
} as LocalisedRoute;
|
|
319
|
+
|
|
320
|
+
if (!localisedUrlEntry) {
|
|
321
|
+
return [baseRoute];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const localisedPaths = getLocalisedRoutePaths(
|
|
325
|
+
canonicalPath,
|
|
326
|
+
parentLocalisedPaths,
|
|
327
|
+
languages,
|
|
328
|
+
localisedUrlEntry,
|
|
329
|
+
);
|
|
330
|
+
// Validate nesting for both adapters, but only React Router needs physical
|
|
331
|
+
// variants. TanStack consumes the source identity and native URL rewrites.
|
|
332
|
+
if (projection === 'canonical') return [baseRoute];
|
|
333
|
+
|
|
334
|
+
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
335
|
+
const routeIdPaths = leadingLocaleParam
|
|
336
|
+
? localisedPaths.map(path =>
|
|
337
|
+
normaliseRoutePath(`${leadingLocaleParam}/${path}`),
|
|
338
|
+
)
|
|
339
|
+
: localisedPaths;
|
|
340
|
+
const routeIdSuffixes = getLocalisedRouteIdSuffixes(routeIdPaths);
|
|
341
|
+
|
|
342
|
+
return localisedPaths.map((localisedPath, index) =>
|
|
343
|
+
cloneRouteWithLocalisedPath(
|
|
344
|
+
baseRoute,
|
|
345
|
+
localisedPath,
|
|
346
|
+
routeIdSuffixes[index],
|
|
347
|
+
canonicalPath,
|
|
348
|
+
),
|
|
349
|
+
);
|
|
350
|
+
};
|
|
351
|
+
|
|
352
|
+
const suffixRouteIds = <T extends LocalisedRoute>(
|
|
353
|
+
route: T,
|
|
354
|
+
suffix: string,
|
|
355
|
+
): T => {
|
|
356
|
+
const children =
|
|
357
|
+
'children' in route && route.children
|
|
358
|
+
? route.children.map(child => suffixRouteIds(child, suffix))
|
|
359
|
+
: undefined;
|
|
360
|
+
|
|
361
|
+
return {
|
|
362
|
+
...route,
|
|
363
|
+
...(route.id ? { id: `${route.id}__localised_${suffix}` } : {}),
|
|
364
|
+
...(children ? { children } : {}),
|
|
365
|
+
};
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
const collectRouteIdCounts = (
|
|
369
|
+
routes: LocalisedRoute[],
|
|
370
|
+
counts = new Map<string, number>(),
|
|
371
|
+
): Map<string, number> => {
|
|
372
|
+
for (const route of routes) {
|
|
373
|
+
if (route.id) {
|
|
374
|
+
counts.set(route.id, (counts.get(route.id) ?? 0) + 1);
|
|
375
|
+
}
|
|
376
|
+
if ('children' in route && route.children) {
|
|
377
|
+
collectRouteIdCounts(route.children, counts);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return counts;
|
|
381
|
+
};
|
|
382
|
+
|
|
383
|
+
const validateNoNewRouteIdCollisions = (
|
|
384
|
+
sourceRoutes: LocalisedRoute[],
|
|
385
|
+
localisedRoutes: LocalisedRoute[],
|
|
386
|
+
) => {
|
|
387
|
+
const sourceCounts = collectRouteIdCounts(sourceRoutes);
|
|
388
|
+
const localisedCounts = collectRouteIdCounts(localisedRoutes);
|
|
389
|
+
|
|
390
|
+
for (const [routeId, count] of localisedCounts) {
|
|
391
|
+
if (count > 1 && count > (sourceCounts.get(routeId) ?? 0)) {
|
|
392
|
+
throw new Error(
|
|
393
|
+
`Localised route expansion generated duplicate route ID "${routeId}". Change the source route ID or localised path so every generated route remains identifiable.`,
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
|
|
399
|
+
const cloneRouteWithLocalisedPath = (
|
|
400
|
+
route: LocalisedRoute,
|
|
401
|
+
path: string,
|
|
402
|
+
routeIdSuffix: string | undefined,
|
|
403
|
+
canonicalPath: string,
|
|
404
|
+
): LocalisedRoute => {
|
|
405
|
+
const leadingLocaleParam = getLeadingLocaleParam(route.path);
|
|
406
|
+
const localisedPath = leadingLocaleParam
|
|
407
|
+
? normaliseRoutePath(`${leadingLocaleParam}/${path}`)
|
|
408
|
+
: path;
|
|
409
|
+
const routeWithPath = {
|
|
410
|
+
...route,
|
|
411
|
+
path: localisedPath,
|
|
412
|
+
} as LocalisedRoute;
|
|
413
|
+
// Language-agnostic source pattern; lets downstream codegen collapse the
|
|
414
|
+
// localized physical variants back to one canonical route.
|
|
415
|
+
(routeWithPath as { modernCanonicalPath?: string }).modernCanonicalPath =
|
|
416
|
+
canonicalPath;
|
|
417
|
+
|
|
418
|
+
return routeIdSuffix
|
|
419
|
+
? suffixRouteIds(routeWithPath, routeIdSuffix)
|
|
420
|
+
: routeWithPath;
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
export const applyLocalisedUrlsToRoutes = (
|
|
424
|
+
routes: LocalisedRoute[],
|
|
425
|
+
languages: string[],
|
|
426
|
+
localisedUrls: LocalisedUrlsMap,
|
|
427
|
+
projection: 'physical' | 'canonical' = 'physical',
|
|
428
|
+
): LocalisedRoute[] => {
|
|
429
|
+
const rootLocalisedPaths = languages.reduce<Record<string, string>>(
|
|
430
|
+
(acc, language) => {
|
|
431
|
+
acc[language] = '/';
|
|
432
|
+
return acc;
|
|
433
|
+
},
|
|
434
|
+
{},
|
|
435
|
+
);
|
|
436
|
+
|
|
437
|
+
validateLocalisedUrls(routes, languages, localisedUrls);
|
|
438
|
+
|
|
439
|
+
const localisedRoutes = routes.flatMap(route =>
|
|
440
|
+
transformLocalisedRoute(
|
|
441
|
+
route,
|
|
442
|
+
'',
|
|
443
|
+
rootLocalisedPaths,
|
|
444
|
+
languages,
|
|
445
|
+
localisedUrls,
|
|
446
|
+
projection,
|
|
447
|
+
),
|
|
448
|
+
);
|
|
449
|
+
validateNoNewRouteIdCollisions(routes, localisedRoutes);
|
|
450
|
+
return localisedRoutes;
|
|
451
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type LocalisedRoute = {
|
|
2
|
+
type: 'nested' | 'page';
|
|
3
|
+
path?: string;
|
|
4
|
+
id?: string;
|
|
5
|
+
children?: LocalisedRoute[];
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type LocalisedUrlPathMap = Record<string, string>;
|
|
10
|
+
|
|
11
|
+
export type LocalisedUrlsMap = Record<string, LocalisedUrlPathMap>;
|
|
12
|
+
|
|
13
|
+
export type LocalisedUrlsOption = boolean | LocalisedUrlsMap;
|
|
14
|
+
|
|
15
|
+
export interface ResolvedLocalisedUrlsConfig {
|
|
16
|
+
enabled: boolean;
|
|
17
|
+
map: LocalisedUrlsMap;
|
|
18
|
+
}
|
package/src/routeHydration.ts
CHANGED
|
@@ -71,7 +71,7 @@ const isAsyncEntryScriptChunk = (chunk: ScriptChunkLike, entryName: string) => {
|
|
|
71
71
|
|
|
72
72
|
export function getMatchedRouteChunks<T>(
|
|
73
73
|
routeManifest: RouteManifestLike | undefined,
|
|
74
|
-
matchedRouteIds: string[],
|
|
74
|
+
matchedRouteIds: readonly string[],
|
|
75
75
|
routeAssetToChunk: (asset: string) => T,
|
|
76
76
|
): T[] {
|
|
77
77
|
const routeAssets = routeManifest?.routeAssets;
|
|
@@ -167,7 +167,7 @@ export function replaceChunkJsPlaceholder(
|
|
|
167
167
|
|
|
168
168
|
export function createRouteHydrationScriptTags(
|
|
169
169
|
routeManifest: RouteManifestLike | undefined,
|
|
170
|
-
matchedRouteIds: string[],
|
|
170
|
+
matchedRouteIds: readonly string[],
|
|
171
171
|
entryName: string,
|
|
172
172
|
options: {
|
|
173
173
|
nonce?: string;
|