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