@tanstack/router-core 1.120.3 → 1.120.4-alpha.10
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/fileRoute.d.cts +6 -2
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +6 -6
- package/dist/cjs/link.cjs.map +1 -1
- package/dist/cjs/link.d.cts +17 -1
- package/dist/cjs/path.cjs +130 -16
- package/dist/cjs/path.cjs.map +1 -1
- package/dist/cjs/path.d.cts +17 -0
- package/dist/cjs/redirect.cjs +19 -14
- package/dist/cjs/redirect.cjs.map +1 -1
- package/dist/cjs/redirect.d.cts +10 -7
- package/dist/cjs/route.cjs +12 -1
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +15 -14
- package/dist/cjs/router.cjs +227 -155
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +45 -3
- package/dist/cjs/typePrimitives.d.cts +2 -2
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +2 -0
- package/dist/esm/fileRoute.d.ts +6 -2
- package/dist/esm/index.d.ts +6 -6
- package/dist/esm/index.js +7 -3
- package/dist/esm/link.d.ts +17 -1
- package/dist/esm/link.js.map +1 -1
- package/dist/esm/path.d.ts +17 -0
- package/dist/esm/path.js +130 -16
- package/dist/esm/path.js.map +1 -1
- package/dist/esm/redirect.d.ts +10 -7
- package/dist/esm/redirect.js +20 -15
- package/dist/esm/redirect.js.map +1 -1
- package/dist/esm/route.d.ts +15 -14
- package/dist/esm/route.js +12 -1
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/router.d.ts +45 -3
- package/dist/esm/router.js +230 -158
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/typePrimitives.d.ts +2 -2
- package/dist/esm/utils.d.ts +2 -0
- package/dist/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/fileRoute.ts +90 -1
- package/src/index.ts +15 -6
- package/src/link.ts +96 -11
- package/src/path.ts +181 -16
- package/src/redirect.ts +37 -22
- package/src/route.ts +104 -35
- package/src/router.ts +326 -209
- package/src/typePrimitives.ts +2 -2
- package/src/utils.ts +14 -0
package/dist/cjs/router.cjs
CHANGED
|
@@ -74,10 +74,7 @@ class RouterCore {
|
|
|
74
74
|
this.basepath = `/${path.trimPath(newOptions.basepath)}`;
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
|
-
if (
|
|
78
|
-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
79
|
-
!this.history || this.options.history && this.options.history !== this.history
|
|
80
|
-
) {
|
|
77
|
+
if (!this.history || this.options.history && this.options.history !== this.history) {
|
|
81
78
|
this.history = this.options.history ?? (this.isServer ? history.createMemoryHistory({
|
|
82
79
|
initialEntries: [this.basepath || "/"]
|
|
83
80
|
}) : history.createBrowserHistory());
|
|
@@ -100,16 +97,25 @@ class RouterCore {
|
|
|
100
97
|
});
|
|
101
98
|
scrollRestoration.setupScrollRestoration(this);
|
|
102
99
|
}
|
|
103
|
-
if (typeof window !== "undefined" && "CSS" in window &&
|
|
104
|
-
typeof ((_a = window.CSS) == null ? void 0 : _a.supports) === "function") {
|
|
100
|
+
if (typeof window !== "undefined" && "CSS" in window && typeof ((_a = window.CSS) == null ? void 0 : _a.supports) === "function") {
|
|
105
101
|
this.isViewTransitionTypesSupported = window.CSS.supports(
|
|
106
102
|
"selector(:active-view-transition-type(a)"
|
|
107
103
|
);
|
|
108
104
|
}
|
|
109
105
|
};
|
|
110
106
|
this.buildRouteTree = () => {
|
|
111
|
-
|
|
112
|
-
|
|
107
|
+
const { routesById, routesByPath, flatRoutes } = processRouteTree({
|
|
108
|
+
routeTree: this.routeTree,
|
|
109
|
+
initRoute: (route, i) => {
|
|
110
|
+
route.init({
|
|
111
|
+
originalIndex: i,
|
|
112
|
+
defaultSsr: this.options.defaultSsr
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
});
|
|
116
|
+
this.routesById = routesById;
|
|
117
|
+
this.routesByPath = routesByPath;
|
|
118
|
+
this.flatRoutes = flatRoutes;
|
|
113
119
|
const notFoundRoute = this.options.notFoundRoute;
|
|
114
120
|
if (notFoundRoute) {
|
|
115
121
|
notFoundRoute.init({
|
|
@@ -118,77 +124,6 @@ class RouterCore {
|
|
|
118
124
|
});
|
|
119
125
|
this.routesById[notFoundRoute.id] = notFoundRoute;
|
|
120
126
|
}
|
|
121
|
-
const recurseRoutes = (childRoutes) => {
|
|
122
|
-
childRoutes.forEach((childRoute, i) => {
|
|
123
|
-
childRoute.init({
|
|
124
|
-
originalIndex: i,
|
|
125
|
-
defaultSsr: this.options.defaultSsr
|
|
126
|
-
});
|
|
127
|
-
const existingRoute = this.routesById[childRoute.id];
|
|
128
|
-
invariant(
|
|
129
|
-
!existingRoute,
|
|
130
|
-
`Duplicate routes found with id: ${String(childRoute.id)}`
|
|
131
|
-
);
|
|
132
|
-
this.routesById[childRoute.id] = childRoute;
|
|
133
|
-
if (!childRoute.isRoot && childRoute.path) {
|
|
134
|
-
const trimmedFullPath = path.trimPathRight(childRoute.fullPath);
|
|
135
|
-
if (!this.routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith("/")) {
|
|
136
|
-
this.routesByPath[trimmedFullPath] = childRoute;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
const children = childRoute.children;
|
|
140
|
-
if (children == null ? void 0 : children.length) {
|
|
141
|
-
recurseRoutes(children);
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
};
|
|
145
|
-
recurseRoutes([this.routeTree]);
|
|
146
|
-
const scoredRoutes = [];
|
|
147
|
-
const routes = Object.values(this.routesById);
|
|
148
|
-
routes.forEach((d, i) => {
|
|
149
|
-
var _a;
|
|
150
|
-
if (d.isRoot || !d.path) {
|
|
151
|
-
return;
|
|
152
|
-
}
|
|
153
|
-
const trimmed = path.trimPathLeft(d.fullPath);
|
|
154
|
-
const parsed = path.parsePathname(trimmed);
|
|
155
|
-
while (parsed.length > 1 && ((_a = parsed[0]) == null ? void 0 : _a.value) === "/") {
|
|
156
|
-
parsed.shift();
|
|
157
|
-
}
|
|
158
|
-
const scores = parsed.map((segment) => {
|
|
159
|
-
if (segment.value === "/") {
|
|
160
|
-
return 0.75;
|
|
161
|
-
}
|
|
162
|
-
if (segment.type === "param") {
|
|
163
|
-
return 0.5;
|
|
164
|
-
}
|
|
165
|
-
if (segment.type === "wildcard") {
|
|
166
|
-
return 0.25;
|
|
167
|
-
}
|
|
168
|
-
return 1;
|
|
169
|
-
});
|
|
170
|
-
scoredRoutes.push({ child: d, trimmed, parsed, index: i, scores });
|
|
171
|
-
});
|
|
172
|
-
this.flatRoutes = scoredRoutes.sort((a, b) => {
|
|
173
|
-
const minLength = Math.min(a.scores.length, b.scores.length);
|
|
174
|
-
for (let i = 0; i < minLength; i++) {
|
|
175
|
-
if (a.scores[i] !== b.scores[i]) {
|
|
176
|
-
return b.scores[i] - a.scores[i];
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
if (a.scores.length !== b.scores.length) {
|
|
180
|
-
return b.scores.length - a.scores.length;
|
|
181
|
-
}
|
|
182
|
-
for (let i = 0; i < minLength; i++) {
|
|
183
|
-
if (a.parsed[i].value !== b.parsed[i].value) {
|
|
184
|
-
return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return a.index - b.index;
|
|
188
|
-
}).map((d, i) => {
|
|
189
|
-
d.child.rank = i;
|
|
190
|
-
return d.child;
|
|
191
|
-
});
|
|
192
127
|
};
|
|
193
128
|
this.subscribe = (eventType, fn) => {
|
|
194
129
|
const listener = {
|
|
@@ -261,37 +196,16 @@ class RouterCore {
|
|
|
261
196
|
return this.matchRoutesInternal(pathnameOrNext, locationSearchOrOpts);
|
|
262
197
|
}
|
|
263
198
|
};
|
|
264
|
-
this.getMatchedRoutes = (
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
};
|
|
275
|
-
let foundRoute = (dest == null ? void 0 : dest.to) !== void 0 ? this.routesByPath[dest.to] : void 0;
|
|
276
|
-
if (foundRoute) {
|
|
277
|
-
routeParams = getMatchedParams(foundRoute);
|
|
278
|
-
} else {
|
|
279
|
-
foundRoute = this.flatRoutes.find((route) => {
|
|
280
|
-
const matchedParams = getMatchedParams(route);
|
|
281
|
-
if (matchedParams) {
|
|
282
|
-
routeParams = matchedParams;
|
|
283
|
-
return true;
|
|
284
|
-
}
|
|
285
|
-
return false;
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
let routeCursor = foundRoute || this.routesById[root.rootRouteId];
|
|
289
|
-
const matchedRoutes = [routeCursor];
|
|
290
|
-
while (routeCursor.parentRoute) {
|
|
291
|
-
routeCursor = routeCursor.parentRoute;
|
|
292
|
-
matchedRoutes.unshift(routeCursor);
|
|
293
|
-
}
|
|
294
|
-
return { matchedRoutes, routeParams, foundRoute };
|
|
199
|
+
this.getMatchedRoutes = (pathname, routePathname) => {
|
|
200
|
+
return getMatchedRoutes({
|
|
201
|
+
pathname,
|
|
202
|
+
routePathname,
|
|
203
|
+
basepath: this.basepath,
|
|
204
|
+
caseSensitive: this.options.caseSensitive,
|
|
205
|
+
routesByPath: this.routesByPath,
|
|
206
|
+
routesById: this.routesById,
|
|
207
|
+
flatRoutes: this.flatRoutes
|
|
208
|
+
});
|
|
295
209
|
};
|
|
296
210
|
this.cancelMatch = (id) => {
|
|
297
211
|
const match = this.getMatch(id);
|
|
@@ -504,10 +418,16 @@ class RouterCore {
|
|
|
504
418
|
maskedNext = build(maskedDest);
|
|
505
419
|
}
|
|
506
420
|
}
|
|
507
|
-
const nextMatches = this.getMatchedRoutes(
|
|
421
|
+
const nextMatches = this.getMatchedRoutes(
|
|
422
|
+
next.pathname,
|
|
423
|
+
dest.to
|
|
424
|
+
);
|
|
508
425
|
const final = build(dest, nextMatches);
|
|
509
426
|
if (maskedNext) {
|
|
510
|
-
const maskedMatches = this.getMatchedRoutes(
|
|
427
|
+
const maskedMatches = this.getMatchedRoutes(
|
|
428
|
+
maskedNext.pathname,
|
|
429
|
+
maskedDest == null ? void 0 : maskedDest.to
|
|
430
|
+
);
|
|
511
431
|
const maskedFinal = build(maskedDest, maskedMatches);
|
|
512
432
|
final.maskedLocation = maskedFinal;
|
|
513
433
|
}
|
|
@@ -618,6 +538,13 @@ class RouterCore {
|
|
|
618
538
|
});
|
|
619
539
|
};
|
|
620
540
|
this.navigate = ({ to, reloadDocument, href, ...rest }) => {
|
|
541
|
+
if (!reloadDocument && href) {
|
|
542
|
+
try {
|
|
543
|
+
new URL(`${href}`);
|
|
544
|
+
reloadDocument = true;
|
|
545
|
+
} catch {
|
|
546
|
+
}
|
|
547
|
+
}
|
|
621
548
|
if (reloadDocument) {
|
|
622
549
|
if (!href) {
|
|
623
550
|
const location = this.buildLocation({ to, ...rest });
|
|
@@ -636,8 +563,23 @@ class RouterCore {
|
|
|
636
563
|
to
|
|
637
564
|
});
|
|
638
565
|
};
|
|
639
|
-
this.
|
|
566
|
+
this.beforeLoad = () => {
|
|
567
|
+
this.cancelMatches();
|
|
640
568
|
this.latestLocation = this.parseLocation(this.latestLocation);
|
|
569
|
+
const pendingMatches = this.matchRoutes(this.latestLocation);
|
|
570
|
+
this.__store.setState((s) => ({
|
|
571
|
+
...s,
|
|
572
|
+
status: "pending",
|
|
573
|
+
isLoading: true,
|
|
574
|
+
location: this.latestLocation,
|
|
575
|
+
pendingMatches,
|
|
576
|
+
// If a cached moved to pendingMatches, remove it from cachedMatches
|
|
577
|
+
cachedMatches: s.cachedMatches.filter((d) => {
|
|
578
|
+
return !pendingMatches.find((e) => e.id === d.id);
|
|
579
|
+
})
|
|
580
|
+
}));
|
|
581
|
+
};
|
|
582
|
+
this.load = async (opts) => {
|
|
641
583
|
let redirect$1;
|
|
642
584
|
let notFound$1;
|
|
643
585
|
let loadPromise;
|
|
@@ -645,24 +587,9 @@ class RouterCore {
|
|
|
645
587
|
this.startTransition(async () => {
|
|
646
588
|
var _a;
|
|
647
589
|
try {
|
|
590
|
+
this.beforeLoad();
|
|
648
591
|
const next = this.latestLocation;
|
|
649
592
|
const prevLocation = this.state.resolvedLocation;
|
|
650
|
-
this.cancelMatches();
|
|
651
|
-
let pendingMatches;
|
|
652
|
-
store.batch(() => {
|
|
653
|
-
pendingMatches = this.matchRoutes(next);
|
|
654
|
-
this.__store.setState((s) => ({
|
|
655
|
-
...s,
|
|
656
|
-
status: "pending",
|
|
657
|
-
isLoading: true,
|
|
658
|
-
location: next,
|
|
659
|
-
pendingMatches,
|
|
660
|
-
// If a cached moved to pendingMatches, remove it from cachedMatches
|
|
661
|
-
cachedMatches: s.cachedMatches.filter((d) => {
|
|
662
|
-
return !pendingMatches.find((e) => e.id === d.id);
|
|
663
|
-
})
|
|
664
|
-
}));
|
|
665
|
-
});
|
|
666
593
|
if (!this.state.redirect) {
|
|
667
594
|
this.emit({
|
|
668
595
|
type: "onBeforeNavigate",
|
|
@@ -681,7 +608,7 @@ class RouterCore {
|
|
|
681
608
|
});
|
|
682
609
|
await this.loadMatches({
|
|
683
610
|
sync: opts == null ? void 0 : opts.sync,
|
|
684
|
-
matches: pendingMatches,
|
|
611
|
+
matches: this.state.pendingMatches,
|
|
685
612
|
location: next,
|
|
686
613
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
687
614
|
onReady: async () => {
|
|
@@ -730,11 +657,11 @@ class RouterCore {
|
|
|
730
657
|
}
|
|
731
658
|
});
|
|
732
659
|
} catch (err) {
|
|
733
|
-
if (redirect.
|
|
660
|
+
if (redirect.isRedirect(err)) {
|
|
734
661
|
redirect$1 = err;
|
|
735
662
|
if (!this.isServer) {
|
|
736
663
|
this.navigate({
|
|
737
|
-
...redirect$1,
|
|
664
|
+
...redirect$1.options,
|
|
738
665
|
replace: true,
|
|
739
666
|
ignoreBlocker: true
|
|
740
667
|
});
|
|
@@ -744,7 +671,7 @@ class RouterCore {
|
|
|
744
671
|
}
|
|
745
672
|
this.__store.setState((s) => ({
|
|
746
673
|
...s,
|
|
747
|
-
statusCode: redirect$1 ? redirect$1.
|
|
674
|
+
statusCode: redirect$1 ? redirect$1.status : notFound$1 ? 404 : s.matches.some((d) => d.status === "error") ? 500 : 200,
|
|
748
675
|
redirect: redirect$1
|
|
749
676
|
}));
|
|
750
677
|
}
|
|
@@ -842,12 +769,14 @@ class RouterCore {
|
|
|
842
769
|
};
|
|
843
770
|
const handleRedirectAndNotFound = (match, err) => {
|
|
844
771
|
var _a, _b, _c, _d;
|
|
845
|
-
if (redirect.isResolvedRedirect(err)) {
|
|
846
|
-
if (!err.reloadDocument) {
|
|
847
|
-
throw err;
|
|
848
|
-
}
|
|
849
|
-
}
|
|
850
772
|
if (redirect.isRedirect(err) || notFound.isNotFound(err)) {
|
|
773
|
+
if (redirect.isRedirect(err)) {
|
|
774
|
+
if (err.redirectHandled) {
|
|
775
|
+
if (!err.options.reloadDocument) {
|
|
776
|
+
throw err;
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
}
|
|
851
780
|
updateMatch(match.id, (prev) => ({
|
|
852
781
|
...prev,
|
|
853
782
|
status: redirect.isRedirect(err) ? "redirected" : notFound.isNotFound(err) ? "notFound" : "error",
|
|
@@ -864,7 +793,9 @@ class RouterCore {
|
|
|
864
793
|
(_c = match.loadPromise) == null ? void 0 : _c.resolve();
|
|
865
794
|
if (redirect.isRedirect(err)) {
|
|
866
795
|
rendered = true;
|
|
867
|
-
err =
|
|
796
|
+
err.options._fromLocation = location;
|
|
797
|
+
err.redirectHandled = true;
|
|
798
|
+
err = this.resolveRedirect(err);
|
|
868
799
|
throw err;
|
|
869
800
|
} else if (notFound.isNotFound(err)) {
|
|
870
801
|
this._handleNotFound(matches, err, {
|
|
@@ -1170,8 +1101,8 @@ class RouterCore {
|
|
|
1170
1101
|
loaderPromise: void 0
|
|
1171
1102
|
}));
|
|
1172
1103
|
} catch (err) {
|
|
1173
|
-
if (redirect.
|
|
1174
|
-
await this.navigate(err);
|
|
1104
|
+
if (redirect.isRedirect(err)) {
|
|
1105
|
+
await this.navigate(err.options);
|
|
1175
1106
|
}
|
|
1176
1107
|
}
|
|
1177
1108
|
})();
|
|
@@ -1235,10 +1166,13 @@ class RouterCore {
|
|
|
1235
1166
|
});
|
|
1236
1167
|
return this.load({ sync: opts == null ? void 0 : opts.sync });
|
|
1237
1168
|
};
|
|
1238
|
-
this.resolveRedirect = (
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
redirect2.
|
|
1169
|
+
this.resolveRedirect = (redirect2) => {
|
|
1170
|
+
if (!redirect2.options.href) {
|
|
1171
|
+
redirect2.options.href = this.buildLocation(redirect2.options).href;
|
|
1172
|
+
redirect2.headers.set("Location", redirect2.options.href);
|
|
1173
|
+
}
|
|
1174
|
+
if (!redirect2.headers.get("Location")) {
|
|
1175
|
+
redirect2.headers.set("Location", redirect2.options.href);
|
|
1242
1176
|
}
|
|
1243
1177
|
return redirect2;
|
|
1244
1178
|
};
|
|
@@ -1340,11 +1274,11 @@ class RouterCore {
|
|
|
1340
1274
|
return matches;
|
|
1341
1275
|
} catch (err) {
|
|
1342
1276
|
if (redirect.isRedirect(err)) {
|
|
1343
|
-
if (err.reloadDocument) {
|
|
1277
|
+
if (err.options.reloadDocument) {
|
|
1344
1278
|
return void 0;
|
|
1345
1279
|
}
|
|
1346
1280
|
return await this.preloadRoute({
|
|
1347
|
-
...err,
|
|
1281
|
+
...err.options,
|
|
1348
1282
|
_fromLocation: next
|
|
1349
1283
|
});
|
|
1350
1284
|
}
|
|
@@ -1448,9 +1382,10 @@ class RouterCore {
|
|
|
1448
1382
|
return this.routesById;
|
|
1449
1383
|
}
|
|
1450
1384
|
matchRoutesInternal(next, opts) {
|
|
1385
|
+
var _a;
|
|
1451
1386
|
const { foundRoute, matchedRoutes, routeParams } = this.getMatchedRoutes(
|
|
1452
|
-
next,
|
|
1453
|
-
opts == null ? void 0 : opts.dest
|
|
1387
|
+
next.pathname,
|
|
1388
|
+
(_a = opts == null ? void 0 : opts.dest) == null ? void 0 : _a.to
|
|
1454
1389
|
);
|
|
1455
1390
|
let isGlobalNotFound = false;
|
|
1456
1391
|
if (
|
|
@@ -1481,9 +1416,9 @@ class RouterCore {
|
|
|
1481
1416
|
return root.rootRouteId;
|
|
1482
1417
|
})();
|
|
1483
1418
|
const parseErrors = matchedRoutes.map((route) => {
|
|
1484
|
-
var
|
|
1419
|
+
var _a2;
|
|
1485
1420
|
let parsedParamsError;
|
|
1486
|
-
const parseParams = ((
|
|
1421
|
+
const parseParams = ((_a2 = route.options.params) == null ? void 0 : _a2.parse) ?? route.options.parseParams;
|
|
1487
1422
|
if (parseParams) {
|
|
1488
1423
|
try {
|
|
1489
1424
|
const parsedParams = parseParams(routeParams);
|
|
@@ -1507,7 +1442,7 @@ class RouterCore {
|
|
|
1507
1442
|
return parentContext;
|
|
1508
1443
|
};
|
|
1509
1444
|
matchedRoutes.forEach((route, index) => {
|
|
1510
|
-
var
|
|
1445
|
+
var _a2, _b;
|
|
1511
1446
|
const parentMatch = matches[index - 1];
|
|
1512
1447
|
const [preMatchSearch, strictMatchSearch, searchError] = (() => {
|
|
1513
1448
|
const parentSearch = (parentMatch == null ? void 0 : parentMatch.search) ?? next.search;
|
|
@@ -1535,7 +1470,7 @@ class RouterCore {
|
|
|
1535
1470
|
return [parentSearch, {}, searchParamError];
|
|
1536
1471
|
}
|
|
1537
1472
|
})();
|
|
1538
|
-
const loaderDeps = ((_b = (
|
|
1473
|
+
const loaderDeps = ((_b = (_a2 = route.options).loaderDeps) == null ? void 0 : _b.call(_a2, {
|
|
1539
1474
|
search: preMatchSearch
|
|
1540
1475
|
})) ?? "";
|
|
1541
1476
|
const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : "";
|
|
@@ -1613,7 +1548,7 @@ class RouterCore {
|
|
|
1613
1548
|
matches.push(match);
|
|
1614
1549
|
});
|
|
1615
1550
|
matches.forEach((match, index) => {
|
|
1616
|
-
var
|
|
1551
|
+
var _a2, _b, _c, _d, _e, _f, _g, _h;
|
|
1617
1552
|
const route = this.looseRoutesById[match.routeId];
|
|
1618
1553
|
const existingMatch = this.getMatch(match.id);
|
|
1619
1554
|
if (!existingMatch && (opts == null ? void 0 : opts._buildLocation) !== true) {
|
|
@@ -1631,7 +1566,7 @@ class RouterCore {
|
|
|
1631
1566
|
preload: !!match.preload,
|
|
1632
1567
|
matches
|
|
1633
1568
|
};
|
|
1634
|
-
match.__routeContext = ((_b = (
|
|
1569
|
+
match.__routeContext = ((_b = (_a2 = route.options).context) == null ? void 0 : _b.call(_a2, contextFnContext)) ?? {};
|
|
1635
1570
|
match.context = {
|
|
1636
1571
|
...parentContext,
|
|
1637
1572
|
...match.__routeContext,
|
|
@@ -1717,6 +1652,141 @@ function routeNeedsPreload(route) {
|
|
|
1717
1652
|
}
|
|
1718
1653
|
return false;
|
|
1719
1654
|
}
|
|
1655
|
+
function processRouteTree({
|
|
1656
|
+
routeTree,
|
|
1657
|
+
initRoute
|
|
1658
|
+
}) {
|
|
1659
|
+
const routesById = {};
|
|
1660
|
+
const routesByPath = {};
|
|
1661
|
+
const recurseRoutes = (childRoutes) => {
|
|
1662
|
+
childRoutes.forEach((childRoute, i) => {
|
|
1663
|
+
initRoute == null ? void 0 : initRoute(childRoute, i);
|
|
1664
|
+
const existingRoute = routesById[childRoute.id];
|
|
1665
|
+
invariant(
|
|
1666
|
+
!existingRoute,
|
|
1667
|
+
`Duplicate routes found with id: ${String(childRoute.id)}`
|
|
1668
|
+
);
|
|
1669
|
+
routesById[childRoute.id] = childRoute;
|
|
1670
|
+
if (!childRoute.isRoot && childRoute.path) {
|
|
1671
|
+
const trimmedFullPath = path.trimPathRight(childRoute.fullPath);
|
|
1672
|
+
if (!routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith("/")) {
|
|
1673
|
+
routesByPath[trimmedFullPath] = childRoute;
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
const children = childRoute.children;
|
|
1677
|
+
if (children == null ? void 0 : children.length) {
|
|
1678
|
+
recurseRoutes(children);
|
|
1679
|
+
}
|
|
1680
|
+
});
|
|
1681
|
+
};
|
|
1682
|
+
recurseRoutes([routeTree]);
|
|
1683
|
+
const scoredRoutes = [];
|
|
1684
|
+
const routes = Object.values(routesById);
|
|
1685
|
+
routes.forEach((d, i) => {
|
|
1686
|
+
var _a;
|
|
1687
|
+
if (d.isRoot || !d.path) {
|
|
1688
|
+
return;
|
|
1689
|
+
}
|
|
1690
|
+
const trimmed = path.trimPathLeft(d.fullPath);
|
|
1691
|
+
const parsed = path.parsePathname(trimmed);
|
|
1692
|
+
while (parsed.length > 1 && ((_a = parsed[0]) == null ? void 0 : _a.value) === "/") {
|
|
1693
|
+
parsed.shift();
|
|
1694
|
+
}
|
|
1695
|
+
const scores = parsed.map((segment) => {
|
|
1696
|
+
if (segment.value === "/") {
|
|
1697
|
+
return 0.75;
|
|
1698
|
+
}
|
|
1699
|
+
if (segment.type === "param" && segment.prefixSegment && segment.suffixSegment) {
|
|
1700
|
+
return 0.55;
|
|
1701
|
+
}
|
|
1702
|
+
if (segment.type === "param" && segment.prefixSegment) {
|
|
1703
|
+
return 0.52;
|
|
1704
|
+
}
|
|
1705
|
+
if (segment.type === "param" && segment.suffixSegment) {
|
|
1706
|
+
return 0.51;
|
|
1707
|
+
}
|
|
1708
|
+
if (segment.type === "param") {
|
|
1709
|
+
return 0.5;
|
|
1710
|
+
}
|
|
1711
|
+
if (segment.type === "wildcard" && segment.prefixSegment && segment.suffixSegment) {
|
|
1712
|
+
return 0.3;
|
|
1713
|
+
}
|
|
1714
|
+
if (segment.type === "wildcard" && segment.prefixSegment) {
|
|
1715
|
+
return 0.27;
|
|
1716
|
+
}
|
|
1717
|
+
if (segment.type === "wildcard" && segment.suffixSegment) {
|
|
1718
|
+
return 0.26;
|
|
1719
|
+
}
|
|
1720
|
+
if (segment.type === "wildcard") {
|
|
1721
|
+
return 0.25;
|
|
1722
|
+
}
|
|
1723
|
+
return 1;
|
|
1724
|
+
});
|
|
1725
|
+
scoredRoutes.push({ child: d, trimmed, parsed, index: i, scores });
|
|
1726
|
+
});
|
|
1727
|
+
const flatRoutes = scoredRoutes.sort((a, b) => {
|
|
1728
|
+
const minLength = Math.min(a.scores.length, b.scores.length);
|
|
1729
|
+
for (let i = 0; i < minLength; i++) {
|
|
1730
|
+
if (a.scores[i] !== b.scores[i]) {
|
|
1731
|
+
return b.scores[i] - a.scores[i];
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
if (a.scores.length !== b.scores.length) {
|
|
1735
|
+
return b.scores.length - a.scores.length;
|
|
1736
|
+
}
|
|
1737
|
+
for (let i = 0; i < minLength; i++) {
|
|
1738
|
+
if (a.parsed[i].value !== b.parsed[i].value) {
|
|
1739
|
+
return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
return a.index - b.index;
|
|
1743
|
+
}).map((d, i) => {
|
|
1744
|
+
d.child.rank = i;
|
|
1745
|
+
return d.child;
|
|
1746
|
+
});
|
|
1747
|
+
return { routesById, routesByPath, flatRoutes };
|
|
1748
|
+
}
|
|
1749
|
+
function getMatchedRoutes({
|
|
1750
|
+
pathname,
|
|
1751
|
+
routePathname,
|
|
1752
|
+
basepath,
|
|
1753
|
+
caseSensitive,
|
|
1754
|
+
routesByPath,
|
|
1755
|
+
routesById,
|
|
1756
|
+
flatRoutes
|
|
1757
|
+
}) {
|
|
1758
|
+
let routeParams = {};
|
|
1759
|
+
const trimmedPath = path.trimPathRight(pathname);
|
|
1760
|
+
const getMatchedParams = (route) => {
|
|
1761
|
+
var _a;
|
|
1762
|
+
const result = path.matchPathname(basepath, trimmedPath, {
|
|
1763
|
+
to: route.fullPath,
|
|
1764
|
+
caseSensitive: ((_a = route.options) == null ? void 0 : _a.caseSensitive) ?? caseSensitive,
|
|
1765
|
+
fuzzy: true
|
|
1766
|
+
});
|
|
1767
|
+
return result;
|
|
1768
|
+
};
|
|
1769
|
+
let foundRoute = routePathname !== void 0 ? routesByPath[routePathname] : void 0;
|
|
1770
|
+
if (foundRoute) {
|
|
1771
|
+
routeParams = getMatchedParams(foundRoute);
|
|
1772
|
+
} else {
|
|
1773
|
+
foundRoute = flatRoutes.find((route) => {
|
|
1774
|
+
const matchedParams = getMatchedParams(route);
|
|
1775
|
+
if (matchedParams) {
|
|
1776
|
+
routeParams = matchedParams;
|
|
1777
|
+
return true;
|
|
1778
|
+
}
|
|
1779
|
+
return false;
|
|
1780
|
+
});
|
|
1781
|
+
}
|
|
1782
|
+
let routeCursor = foundRoute || routesById[root.rootRouteId];
|
|
1783
|
+
const matchedRoutes = [routeCursor];
|
|
1784
|
+
while (routeCursor.parentRoute) {
|
|
1785
|
+
routeCursor = routeCursor.parentRoute;
|
|
1786
|
+
matchedRoutes.unshift(routeCursor);
|
|
1787
|
+
}
|
|
1788
|
+
return { matchedRoutes, routeParams, foundRoute };
|
|
1789
|
+
}
|
|
1720
1790
|
exports.PathParamError = PathParamError;
|
|
1721
1791
|
exports.RouterCore = RouterCore;
|
|
1722
1792
|
exports.SearchParamError = SearchParamError;
|
|
@@ -1724,5 +1794,7 @@ exports.componentTypes = componentTypes;
|
|
|
1724
1794
|
exports.defaultSerializeError = defaultSerializeError;
|
|
1725
1795
|
exports.getInitialRouterState = getInitialRouterState;
|
|
1726
1796
|
exports.getLocationChangeInfo = getLocationChangeInfo;
|
|
1797
|
+
exports.getMatchedRoutes = getMatchedRoutes;
|
|
1727
1798
|
exports.lazyFn = lazyFn;
|
|
1799
|
+
exports.processRouteTree = processRouteTree;
|
|
1728
1800
|
//# sourceMappingURL=router.cjs.map
|