@tanstack/router-core 1.132.0-alpha.4 → 1.132.0
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/Matches.cjs +2 -1
- package/dist/cjs/Matches.cjs.map +1 -1
- package/dist/cjs/Matches.d.cts +2 -2
- package/dist/cjs/config.cjs +10 -0
- package/dist/cjs/config.cjs.map +1 -0
- package/dist/cjs/config.d.cts +17 -0
- package/dist/cjs/fileRoute.d.cts +3 -2
- package/dist/cjs/index.cjs +9 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +10 -5
- package/dist/cjs/load-matches.cjs +5 -3
- package/dist/cjs/load-matches.cjs.map +1 -1
- package/dist/cjs/location.d.cts +38 -0
- package/dist/cjs/path.cjs +27 -64
- package/dist/cjs/path.cjs.map +1 -1
- package/dist/cjs/path.d.cts +6 -7
- package/dist/cjs/process-route-tree.cjs +144 -0
- package/dist/cjs/process-route-tree.cjs.map +1 -0
- package/dist/cjs/process-route-tree.d.cts +10 -0
- package/dist/cjs/redirect.cjs +1 -1
- package/dist/cjs/redirect.cjs.map +1 -1
- package/dist/cjs/rewrite.cjs +63 -0
- package/dist/cjs/rewrite.cjs.map +1 -0
- package/dist/cjs/rewrite.d.cts +22 -0
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +62 -44
- package/dist/cjs/router.cjs +102 -210
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +81 -44
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.d.cts +9 -0
- package/dist/cjs/ssr/createRequestHandler.cjs +4 -1
- package/dist/cjs/ssr/createRequestHandler.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/transformer.cjs +14 -12
- package/dist/cjs/ssr/serializer/transformer.cjs.map +1 -1
- package/dist/cjs/ssr/serializer/transformer.d.cts +55 -15
- package/dist/cjs/ssr/ssr-client.cjs.map +1 -1
- package/dist/cjs/ssr/ssr-server.cjs +5 -2
- package/dist/cjs/ssr/ssr-server.cjs.map +1 -1
- package/dist/cjs/ssr/ssr-server.d.cts +4 -1
- package/dist/cjs/utils.cjs +68 -46
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/esm/Matches.d.ts +2 -2
- package/dist/esm/Matches.js +2 -1
- package/dist/esm/Matches.js.map +1 -1
- package/dist/esm/config.d.ts +17 -0
- package/dist/esm/config.js +10 -0
- package/dist/esm/config.js.map +1 -0
- package/dist/esm/fileRoute.d.ts +3 -2
- package/dist/esm/index.d.ts +10 -5
- package/dist/esm/index.js +10 -3
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/load-matches.js +5 -3
- package/dist/esm/load-matches.js.map +1 -1
- package/dist/esm/location.d.ts +38 -0
- package/dist/esm/path.d.ts +6 -7
- package/dist/esm/path.js +27 -64
- package/dist/esm/path.js.map +1 -1
- package/dist/esm/process-route-tree.d.ts +10 -0
- package/dist/esm/process-route-tree.js +144 -0
- package/dist/esm/process-route-tree.js.map +1 -0
- package/dist/esm/redirect.js +1 -1
- package/dist/esm/redirect.js.map +1 -1
- package/dist/esm/rewrite.d.ts +22 -0
- package/dist/esm/rewrite.js +63 -0
- package/dist/esm/rewrite.js.map +1 -0
- package/dist/esm/route.d.ts +62 -44
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/router.d.ts +81 -44
- package/dist/esm/router.js +104 -212
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/scroll-restoration.d.ts +9 -0
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/esm/ssr/createRequestHandler.js +4 -1
- package/dist/esm/ssr/createRequestHandler.js.map +1 -1
- package/dist/esm/ssr/serializer/transformer.d.ts +55 -15
- package/dist/esm/ssr/serializer/transformer.js +14 -12
- package/dist/esm/ssr/serializer/transformer.js.map +1 -1
- package/dist/esm/ssr/ssr-client.js.map +1 -1
- package/dist/esm/ssr/ssr-server.d.ts +4 -1
- package/dist/esm/ssr/ssr-server.js +5 -2
- package/dist/esm/ssr/ssr-server.js.map +1 -1
- package/dist/esm/utils.js +68 -46
- package/dist/esm/utils.js.map +1 -1
- package/package.json +2 -2
- package/src/Matches.ts +4 -3
- package/src/config.ts +42 -0
- package/src/fileRoute.ts +25 -3
- package/src/index.ts +23 -6
- package/src/load-matches.ts +31 -21
- package/src/location.ts +38 -0
- package/src/path.ts +44 -82
- package/src/process-route-tree.ts +233 -0
- package/src/redirect.ts +1 -1
- package/src/rewrite.ts +70 -0
- package/src/route.ts +311 -74
- package/src/router.ts +263 -389
- package/src/scroll-restoration.ts +1 -1
- package/src/ssr/createRequestHandler.ts +4 -1
- package/src/ssr/serializer/transformer.ts +168 -31
- package/src/ssr/server.ts +6 -0
- package/src/ssr/ssr-client.ts +2 -2
- package/src/ssr/ssr-server.ts +10 -7
- package/src/utils.ts +83 -61
package/dist/esm/router.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Store, batch } from "@tanstack/store";
|
|
2
|
-
import {
|
|
3
|
-
import invariant from "tiny-invariant";
|
|
2
|
+
import { createBrowserHistory, parseHref } from "@tanstack/history";
|
|
4
3
|
import { createControlledPromise, deepEqual, replaceEqualDeep, last, findLast, functionalUpdate } from "./utils.js";
|
|
5
|
-
import {
|
|
4
|
+
import { processRouteTree } from "./process-route-tree.js";
|
|
5
|
+
import { resolvePath, cleanPath, trimPathRight, trimPath, matchPathname, interpolatePath } 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";
|
|
@@ -10,6 +10,7 @@ import { rootRouteId } from "./root.js";
|
|
|
10
10
|
import { redirect, isRedirect } from "./redirect.js";
|
|
11
11
|
import { createLRUCache } from "./lru-cache.js";
|
|
12
12
|
import { loadMatches, loadRouteChunk, routeNeedsPreload } from "./load-matches.js";
|
|
13
|
+
import { rewriteBasepath, composeRewrites, executeRewriteInput, executeRewriteOutput } from "./rewrite.js";
|
|
13
14
|
function defaultSerializeError(err) {
|
|
14
15
|
if (err instanceof Error) {
|
|
15
16
|
const obj = {
|
|
@@ -54,7 +55,6 @@ class RouterCore {
|
|
|
54
55
|
"The notFoundRoute API is deprecated and will be removed in the next major version. See https://tanstack.com/router/v1/docs/framework/react/guide/not-found-errors#migrating-from-notfoundroute for more info."
|
|
55
56
|
);
|
|
56
57
|
}
|
|
57
|
-
const previousOptions = this.options;
|
|
58
58
|
this.options = {
|
|
59
59
|
...this.options,
|
|
60
60
|
...newOptions
|
|
@@ -66,24 +66,43 @@ class RouterCore {
|
|
|
66
66
|
char
|
|
67
67
|
])
|
|
68
68
|
) : void 0;
|
|
69
|
-
if (!this.
|
|
70
|
-
if (
|
|
71
|
-
this.
|
|
69
|
+
if (!this.history || this.options.history && this.options.history !== this.history) {
|
|
70
|
+
if (!this.options.history) {
|
|
71
|
+
if (!this.isServer) {
|
|
72
|
+
this.history = createBrowserHistory();
|
|
73
|
+
}
|
|
72
74
|
} else {
|
|
73
|
-
this.
|
|
75
|
+
this.history = this.options.history;
|
|
74
76
|
}
|
|
75
77
|
}
|
|
76
|
-
if (
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
})
|
|
80
|
-
|
|
78
|
+
if (this.options.basepath) {
|
|
79
|
+
const basepathRewrite = rewriteBasepath({
|
|
80
|
+
basepath: this.options.basepath
|
|
81
|
+
});
|
|
82
|
+
if (this.options.rewrite) {
|
|
83
|
+
this.rewrite = composeRewrites([basepathRewrite, this.options.rewrite]);
|
|
84
|
+
} else {
|
|
85
|
+
this.rewrite = basepathRewrite;
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
this.rewrite = this.options.rewrite;
|
|
89
|
+
}
|
|
90
|
+
this.origin = this.options.origin;
|
|
91
|
+
if (!this.origin) {
|
|
92
|
+
if (!this.isServer) {
|
|
93
|
+
this.origin = window.origin;
|
|
94
|
+
} else {
|
|
95
|
+
this.origin = "http://localhost";
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (this.history) {
|
|
99
|
+
this.updateLatestLocation();
|
|
81
100
|
}
|
|
82
101
|
if (this.options.routeTree !== this.routeTree) {
|
|
83
102
|
this.routeTree = this.options.routeTree;
|
|
84
103
|
this.buildRouteTree();
|
|
85
104
|
}
|
|
86
|
-
if (!this.__store) {
|
|
105
|
+
if (!this.__store && this.latestLocation) {
|
|
87
106
|
this.__store = new Store(getInitialRouterState(this.latestLocation), {
|
|
88
107
|
onUpdate: () => {
|
|
89
108
|
this.__store.state = {
|
|
@@ -102,6 +121,12 @@ class RouterCore {
|
|
|
102
121
|
);
|
|
103
122
|
}
|
|
104
123
|
};
|
|
124
|
+
this.updateLatestLocation = () => {
|
|
125
|
+
this.latestLocation = this.parseLocation(
|
|
126
|
+
this.history.location,
|
|
127
|
+
this.latestLocation
|
|
128
|
+
);
|
|
129
|
+
};
|
|
105
130
|
this.buildRouteTree = () => {
|
|
106
131
|
const { routesById, routesByPath, flatRoutes } = processRouteTree({
|
|
107
132
|
routeTree: this.routeTree,
|
|
@@ -139,25 +164,30 @@ class RouterCore {
|
|
|
139
164
|
}
|
|
140
165
|
});
|
|
141
166
|
};
|
|
142
|
-
this.parseLocation = (
|
|
167
|
+
this.parseLocation = (locationToParse, previousLocation) => {
|
|
143
168
|
const parse = ({
|
|
144
|
-
|
|
145
|
-
search,
|
|
146
|
-
hash,
|
|
169
|
+
href,
|
|
147
170
|
state
|
|
148
171
|
}) => {
|
|
149
|
-
const
|
|
172
|
+
const fullUrl = new URL(href, this.origin);
|
|
173
|
+
const url = executeRewriteInput(this.rewrite, fullUrl);
|
|
174
|
+
const parsedSearch = this.options.parseSearch(url.search);
|
|
150
175
|
const searchStr = this.options.stringifySearch(parsedSearch);
|
|
176
|
+
url.search = searchStr;
|
|
177
|
+
const fullPath = url.href.replace(url.origin, "");
|
|
178
|
+
const { pathname, hash } = url;
|
|
151
179
|
return {
|
|
180
|
+
href: fullPath,
|
|
181
|
+
publicHref: href,
|
|
182
|
+
url: url.href,
|
|
152
183
|
pathname,
|
|
153
184
|
searchStr,
|
|
154
185
|
search: replaceEqualDeep(previousLocation?.search, parsedSearch),
|
|
155
186
|
hash: hash.split("#").reverse()[0] ?? "",
|
|
156
|
-
href: `${pathname}${searchStr}${hash}`,
|
|
157
187
|
state: replaceEqualDeep(previousLocation?.state, state)
|
|
158
188
|
};
|
|
159
189
|
};
|
|
160
|
-
const location = parse(locationToParse
|
|
190
|
+
const location = parse(locationToParse);
|
|
161
191
|
const { __tempLocation, __tempKey } = location.state;
|
|
162
192
|
if (__tempLocation && (!__tempKey || __tempKey === this.tempLocationKey)) {
|
|
163
193
|
const parsedTempLocation = parse(__tempLocation);
|
|
@@ -173,11 +203,9 @@ class RouterCore {
|
|
|
173
203
|
};
|
|
174
204
|
this.resolvePathWithBase = (from, path) => {
|
|
175
205
|
const resolvedPath = resolvePath({
|
|
176
|
-
basepath: this.basepath,
|
|
177
206
|
base: from,
|
|
178
207
|
to: cleanPath(path),
|
|
179
208
|
trailingSlash: this.options.trailingSlash,
|
|
180
|
-
caseSensitive: this.options.caseSensitive,
|
|
181
209
|
parseCache: this.parsePathnameCache
|
|
182
210
|
});
|
|
183
211
|
return resolvedPath;
|
|
@@ -199,7 +227,6 @@ class RouterCore {
|
|
|
199
227
|
return getMatchedRoutes({
|
|
200
228
|
pathname,
|
|
201
229
|
routePathname,
|
|
202
|
-
basepath: this.basepath,
|
|
203
230
|
caseSensitive: this.options.caseSensitive,
|
|
204
231
|
routesByPath: this.routesByPath,
|
|
205
232
|
routesById: this.routesById,
|
|
@@ -226,30 +253,23 @@ class RouterCore {
|
|
|
226
253
|
_buildLocation: true
|
|
227
254
|
});
|
|
228
255
|
const lastMatch = last(allCurrentLocationMatches);
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return comparePaths(d.fullPath, fromPath);
|
|
243
|
-
});
|
|
244
|
-
const matchedCurrent = findLast(allFromMatches, (d) => {
|
|
245
|
-
return comparePaths(d.fullPath, currentLocation.pathname);
|
|
246
|
-
});
|
|
247
|
-
if (!matchedFrom && !matchedCurrent) {
|
|
248
|
-
console.warn(`Could not find match for from: ${fromPath}`);
|
|
249
|
-
}
|
|
256
|
+
if (dest.from && process.env.NODE_ENV !== "production" && dest._isNavigate) {
|
|
257
|
+
const allFromMatches = this.getMatchedRoutes(
|
|
258
|
+
dest.from,
|
|
259
|
+
void 0
|
|
260
|
+
).matchedRoutes;
|
|
261
|
+
const matchedFrom = findLast(allCurrentLocationMatches, (d) => {
|
|
262
|
+
return comparePaths(d.fullPath, dest.from);
|
|
263
|
+
});
|
|
264
|
+
const matchedCurrent = findLast(allFromMatches, (d) => {
|
|
265
|
+
return comparePaths(d.fullPath, lastMatch.fullPath);
|
|
266
|
+
});
|
|
267
|
+
if (!matchedFrom && !matchedCurrent) {
|
|
268
|
+
console.warn(`Could not find match for from: ${dest.from}`);
|
|
250
269
|
}
|
|
251
270
|
}
|
|
252
|
-
|
|
271
|
+
const defaultedFromPath = dest.unsafeRelative === "path" ? currentLocation.pathname : dest.from ?? lastMatch.fullPath;
|
|
272
|
+
const fromPath = this.resolvePathWithBase(defaultedFromPath, ".");
|
|
253
273
|
const fromSearch = lastMatch.search;
|
|
254
274
|
const fromParams = { ...lastMatch.params };
|
|
255
275
|
const nextTo = dest.to ? this.resolvePathWithBase(fromPath, `${dest.to}`) : this.resolvePathWithBase(fromPath, ".");
|
|
@@ -314,13 +334,18 @@ class RouterCore {
|
|
|
314
334
|
const hashStr = hash ? `#${hash}` : "";
|
|
315
335
|
let nextState = dest.state === true ? currentLocation.state : dest.state ? functionalUpdate(dest.state, currentLocation.state) : {};
|
|
316
336
|
nextState = replaceEqualDeep(currentLocation.state, nextState);
|
|
337
|
+
const fullPath = `${nextPathname}${searchStr}${hashStr}`;
|
|
338
|
+
const url = new URL(fullPath, this.origin);
|
|
339
|
+
const rewrittenUrl = executeRewriteOutput(this.rewrite, url);
|
|
317
340
|
return {
|
|
341
|
+
publicHref: rewrittenUrl.pathname + rewrittenUrl.search + rewrittenUrl.hash,
|
|
342
|
+
href: fullPath,
|
|
343
|
+
url: rewrittenUrl.href,
|
|
318
344
|
pathname: nextPathname,
|
|
319
345
|
search: nextSearch,
|
|
320
346
|
searchStr,
|
|
321
347
|
state: nextState,
|
|
322
348
|
hash: hash ?? "",
|
|
323
|
-
href: `${nextPathname}${searchStr}${hashStr}`,
|
|
324
349
|
unmaskOnReload: dest.unmaskOnReload
|
|
325
350
|
};
|
|
326
351
|
};
|
|
@@ -331,7 +356,6 @@ class RouterCore {
|
|
|
331
356
|
let params = {};
|
|
332
357
|
const foundMask = this.options.routeMasks?.find((d) => {
|
|
333
358
|
const match = matchPathname(
|
|
334
|
-
this.basepath,
|
|
335
359
|
next.pathname,
|
|
336
360
|
{
|
|
337
361
|
to: d.from,
|
|
@@ -357,8 +381,7 @@ class RouterCore {
|
|
|
357
381
|
}
|
|
358
382
|
}
|
|
359
383
|
if (maskedNext) {
|
|
360
|
-
|
|
361
|
-
next.maskedLocation = maskedFinal;
|
|
384
|
+
next.maskedLocation = maskedNext;
|
|
362
385
|
}
|
|
363
386
|
return next;
|
|
364
387
|
};
|
|
@@ -392,7 +415,7 @@ class RouterCore {
|
|
|
392
415
|
});
|
|
393
416
|
return isEqual;
|
|
394
417
|
};
|
|
395
|
-
const isSameUrl = this.latestLocation.href === next.href;
|
|
418
|
+
const isSameUrl = trimPathRight(this.latestLocation.href) === trimPathRight(next.href);
|
|
396
419
|
const previousCommitPromise = this.commitLocationPromise;
|
|
397
420
|
this.commitLocationPromise = createControlledPromise(() => {
|
|
398
421
|
previousCommitPromise?.resolve();
|
|
@@ -428,7 +451,7 @@ class RouterCore {
|
|
|
428
451
|
nextHistory.state.__hashScrollIntoViewOptions = hashScrollIntoView ?? this.options.defaultHashScrollIntoView ?? true;
|
|
429
452
|
this.shouldViewTransition = viewTransition;
|
|
430
453
|
this.history[next.replace ? "replace" : "push"](
|
|
431
|
-
nextHistory.
|
|
454
|
+
nextHistory.publicHref,
|
|
432
455
|
nextHistory.state,
|
|
433
456
|
{ ignoreBlocker }
|
|
434
457
|
);
|
|
@@ -481,7 +504,7 @@ class RouterCore {
|
|
|
481
504
|
if (reloadDocument) {
|
|
482
505
|
if (!href) {
|
|
483
506
|
const location = this.buildLocation({ to, ...rest });
|
|
484
|
-
href =
|
|
507
|
+
href = location.href;
|
|
485
508
|
}
|
|
486
509
|
if (rest.replace) {
|
|
487
510
|
window.location.replace(href);
|
|
@@ -499,7 +522,7 @@ class RouterCore {
|
|
|
499
522
|
};
|
|
500
523
|
this.beforeLoad = () => {
|
|
501
524
|
this.cancelMatches();
|
|
502
|
-
this.
|
|
525
|
+
this.updateLatestLocation();
|
|
503
526
|
if (this.isServer) {
|
|
504
527
|
const nextLocation = this.buildLocation({
|
|
505
528
|
to: this.latestLocation.pathname,
|
|
@@ -828,7 +851,6 @@ class RouterCore {
|
|
|
828
851
|
const pending = opts?.pending === void 0 ? !this.state.isLoading : opts.pending;
|
|
829
852
|
const baseLocation = pending ? this.latestLocation : this.state.resolvedLocation || this.state.location;
|
|
830
853
|
const match = matchPathname(
|
|
831
|
-
this.basepath,
|
|
832
854
|
baseLocation.pathname,
|
|
833
855
|
{
|
|
834
856
|
...opts,
|
|
@@ -914,25 +936,6 @@ class RouterCore {
|
|
|
914
936
|
}
|
|
915
937
|
return rootRouteId;
|
|
916
938
|
})();
|
|
917
|
-
const parseErrors = matchedRoutes.map((route) => {
|
|
918
|
-
let parsedParamsError;
|
|
919
|
-
const parseParams = route.options.params?.parse ?? route.options.parseParams;
|
|
920
|
-
if (parseParams) {
|
|
921
|
-
try {
|
|
922
|
-
const parsedParams = parseParams(routeParams);
|
|
923
|
-
Object.assign(routeParams, parsedParams);
|
|
924
|
-
} catch (err) {
|
|
925
|
-
parsedParamsError = new PathParamError(err.message, {
|
|
926
|
-
cause: err
|
|
927
|
-
});
|
|
928
|
-
if (opts?.throwOnError) {
|
|
929
|
-
throw parsedParamsError;
|
|
930
|
-
}
|
|
931
|
-
return parsedParamsError;
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
return;
|
|
935
|
-
});
|
|
936
939
|
const matches = [];
|
|
937
940
|
const getParentContext = (parentMatch) => {
|
|
938
941
|
const parentMatchId = parentMatch?.id;
|
|
@@ -971,7 +974,7 @@ class RouterCore {
|
|
|
971
974
|
search: preMatchSearch
|
|
972
975
|
}) ?? "";
|
|
973
976
|
const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : "";
|
|
974
|
-
const {
|
|
977
|
+
const { interpolatedPath, usedParams } = interpolatePath({
|
|
975
978
|
path: route.fullPath,
|
|
976
979
|
params: routeParams,
|
|
977
980
|
decodeCharMap: this.pathParamsDecodeCharMap
|
|
@@ -987,6 +990,27 @@ class RouterCore {
|
|
|
987
990
|
const previousMatch = this.state.matches.find(
|
|
988
991
|
(d) => d.routeId === route.id
|
|
989
992
|
);
|
|
993
|
+
const strictParams = existingMatch?._strictParams ?? usedParams;
|
|
994
|
+
let paramsError = void 0;
|
|
995
|
+
if (!existingMatch) {
|
|
996
|
+
const strictParseParams = route.options.params?.parse ?? route.options.parseParams;
|
|
997
|
+
if (strictParseParams) {
|
|
998
|
+
try {
|
|
999
|
+
Object.assign(
|
|
1000
|
+
strictParams,
|
|
1001
|
+
strictParseParams(strictParams)
|
|
1002
|
+
);
|
|
1003
|
+
} catch (err) {
|
|
1004
|
+
paramsError = new PathParamError(err.message, {
|
|
1005
|
+
cause: err
|
|
1006
|
+
});
|
|
1007
|
+
if (opts?.throwOnError) {
|
|
1008
|
+
throw paramsError;
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
Object.assign(routeParams, strictParams);
|
|
990
1014
|
const cause = previousMatch ? "stay" : "enter";
|
|
991
1015
|
let match;
|
|
992
1016
|
if (existingMatch) {
|
|
@@ -994,7 +1018,7 @@ class RouterCore {
|
|
|
994
1018
|
...existingMatch,
|
|
995
1019
|
cause,
|
|
996
1020
|
params: previousMatch ? replaceEqualDeep(previousMatch.params, routeParams) : routeParams,
|
|
997
|
-
_strictParams:
|
|
1021
|
+
_strictParams: strictParams,
|
|
998
1022
|
search: previousMatch ? replaceEqualDeep(previousMatch.search, preMatchSearch) : replaceEqualDeep(existingMatch.search, preMatchSearch),
|
|
999
1023
|
_strictSearch: strictMatchSearch
|
|
1000
1024
|
};
|
|
@@ -1005,8 +1029,8 @@ class RouterCore {
|
|
|
1005
1029
|
index,
|
|
1006
1030
|
routeId: route.id,
|
|
1007
1031
|
params: previousMatch ? replaceEqualDeep(previousMatch.params, routeParams) : routeParams,
|
|
1008
|
-
_strictParams:
|
|
1009
|
-
pathname:
|
|
1032
|
+
_strictParams: strictParams,
|
|
1033
|
+
pathname: interpolatedPath,
|
|
1010
1034
|
updatedAt: Date.now(),
|
|
1011
1035
|
search: previousMatch ? replaceEqualDeep(previousMatch.search, preMatchSearch) : preMatchSearch,
|
|
1012
1036
|
_strictSearch: strictMatchSearch,
|
|
@@ -1014,7 +1038,7 @@ class RouterCore {
|
|
|
1014
1038
|
status,
|
|
1015
1039
|
isFetching: false,
|
|
1016
1040
|
error: void 0,
|
|
1017
|
-
paramsError
|
|
1041
|
+
paramsError,
|
|
1018
1042
|
__routeContext: void 0,
|
|
1019
1043
|
_nonReactive: {
|
|
1020
1044
|
loadPromise: createControlledPromise()
|
|
@@ -1126,139 +1150,9 @@ function validateSearch(validateSearch2, input) {
|
|
|
1126
1150
|
}
|
|
1127
1151
|
return {};
|
|
1128
1152
|
}
|
|
1129
|
-
const REQUIRED_PARAM_BASE_SCORE = 0.5;
|
|
1130
|
-
const OPTIONAL_PARAM_BASE_SCORE = 0.4;
|
|
1131
|
-
const WILDCARD_PARAM_BASE_SCORE = 0.25;
|
|
1132
|
-
const BOTH_PRESENCE_BASE_SCORE = 0.05;
|
|
1133
|
-
const PREFIX_PRESENCE_BASE_SCORE = 0.02;
|
|
1134
|
-
const SUFFIX_PRESENCE_BASE_SCORE = 0.01;
|
|
1135
|
-
const PREFIX_LENGTH_SCORE_MULTIPLIER = 2e-4;
|
|
1136
|
-
const SUFFIX_LENGTH_SCORE_MULTIPLIER = 1e-4;
|
|
1137
|
-
function handleParam(segment, baseScore) {
|
|
1138
|
-
if (segment.prefixSegment && segment.suffixSegment) {
|
|
1139
|
-
return baseScore + BOTH_PRESENCE_BASE_SCORE + PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length + SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length;
|
|
1140
|
-
}
|
|
1141
|
-
if (segment.prefixSegment) {
|
|
1142
|
-
return baseScore + PREFIX_PRESENCE_BASE_SCORE + PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length;
|
|
1143
|
-
}
|
|
1144
|
-
if (segment.suffixSegment) {
|
|
1145
|
-
return baseScore + SUFFIX_PRESENCE_BASE_SCORE + SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length;
|
|
1146
|
-
}
|
|
1147
|
-
return baseScore;
|
|
1148
|
-
}
|
|
1149
|
-
function processRouteTree({
|
|
1150
|
-
routeTree,
|
|
1151
|
-
initRoute
|
|
1152
|
-
}) {
|
|
1153
|
-
const routesById = {};
|
|
1154
|
-
const routesByPath = {};
|
|
1155
|
-
const recurseRoutes = (childRoutes) => {
|
|
1156
|
-
childRoutes.forEach((childRoute, i) => {
|
|
1157
|
-
initRoute?.(childRoute, i);
|
|
1158
|
-
const existingRoute = routesById[childRoute.id];
|
|
1159
|
-
invariant(
|
|
1160
|
-
!existingRoute,
|
|
1161
|
-
`Duplicate routes found with id: ${String(childRoute.id)}`
|
|
1162
|
-
);
|
|
1163
|
-
routesById[childRoute.id] = childRoute;
|
|
1164
|
-
if (!childRoute.isRoot && childRoute.path) {
|
|
1165
|
-
const trimmedFullPath = trimPathRight(childRoute.fullPath);
|
|
1166
|
-
if (!routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith("/")) {
|
|
1167
|
-
routesByPath[trimmedFullPath] = childRoute;
|
|
1168
|
-
}
|
|
1169
|
-
}
|
|
1170
|
-
const children = childRoute.children;
|
|
1171
|
-
if (children?.length) {
|
|
1172
|
-
recurseRoutes(children);
|
|
1173
|
-
}
|
|
1174
|
-
});
|
|
1175
|
-
};
|
|
1176
|
-
recurseRoutes([routeTree]);
|
|
1177
|
-
const scoredRoutes = [];
|
|
1178
|
-
const routes = Object.values(routesById);
|
|
1179
|
-
routes.forEach((d, i) => {
|
|
1180
|
-
if (d.isRoot || !d.path) {
|
|
1181
|
-
return;
|
|
1182
|
-
}
|
|
1183
|
-
const trimmed = trimPathLeft(d.fullPath);
|
|
1184
|
-
let parsed = parsePathname(trimmed);
|
|
1185
|
-
let skip = 0;
|
|
1186
|
-
while (parsed.length > skip + 1 && parsed[skip]?.value === "/") {
|
|
1187
|
-
skip++;
|
|
1188
|
-
}
|
|
1189
|
-
if (skip > 0) parsed = parsed.slice(skip);
|
|
1190
|
-
let optionalParamCount = 0;
|
|
1191
|
-
let hasStaticAfter = false;
|
|
1192
|
-
const scores = parsed.map((segment, index) => {
|
|
1193
|
-
if (segment.value === "/") {
|
|
1194
|
-
return 0.75;
|
|
1195
|
-
}
|
|
1196
|
-
let baseScore = void 0;
|
|
1197
|
-
if (segment.type === SEGMENT_TYPE_PARAM) {
|
|
1198
|
-
baseScore = REQUIRED_PARAM_BASE_SCORE;
|
|
1199
|
-
} else if (segment.type === SEGMENT_TYPE_OPTIONAL_PARAM) {
|
|
1200
|
-
baseScore = OPTIONAL_PARAM_BASE_SCORE;
|
|
1201
|
-
optionalParamCount++;
|
|
1202
|
-
} else if (segment.type === SEGMENT_TYPE_WILDCARD) {
|
|
1203
|
-
baseScore = WILDCARD_PARAM_BASE_SCORE;
|
|
1204
|
-
}
|
|
1205
|
-
if (baseScore) {
|
|
1206
|
-
for (let i2 = index + 1; i2 < parsed.length; i2++) {
|
|
1207
|
-
const nextSegment = parsed[i2];
|
|
1208
|
-
if (nextSegment.type === SEGMENT_TYPE_PATHNAME && nextSegment.value !== "/") {
|
|
1209
|
-
hasStaticAfter = true;
|
|
1210
|
-
return handleParam(segment, baseScore + 0.2);
|
|
1211
|
-
}
|
|
1212
|
-
}
|
|
1213
|
-
return handleParam(segment, baseScore);
|
|
1214
|
-
}
|
|
1215
|
-
return 1;
|
|
1216
|
-
});
|
|
1217
|
-
scoredRoutes.push({
|
|
1218
|
-
child: d,
|
|
1219
|
-
trimmed,
|
|
1220
|
-
parsed,
|
|
1221
|
-
index: i,
|
|
1222
|
-
scores,
|
|
1223
|
-
optionalParamCount,
|
|
1224
|
-
hasStaticAfter
|
|
1225
|
-
});
|
|
1226
|
-
});
|
|
1227
|
-
const flatRoutes = scoredRoutes.sort((a, b) => {
|
|
1228
|
-
const minLength = Math.min(a.scores.length, b.scores.length);
|
|
1229
|
-
for (let i = 0; i < minLength; i++) {
|
|
1230
|
-
if (a.scores[i] !== b.scores[i]) {
|
|
1231
|
-
return b.scores[i] - a.scores[i];
|
|
1232
|
-
}
|
|
1233
|
-
}
|
|
1234
|
-
if (a.scores.length !== b.scores.length) {
|
|
1235
|
-
if (a.optionalParamCount !== b.optionalParamCount) {
|
|
1236
|
-
if (a.hasStaticAfter === b.hasStaticAfter) {
|
|
1237
|
-
return a.optionalParamCount - b.optionalParamCount;
|
|
1238
|
-
} else if (a.hasStaticAfter && !b.hasStaticAfter) {
|
|
1239
|
-
return -1;
|
|
1240
|
-
} else if (!a.hasStaticAfter && b.hasStaticAfter) {
|
|
1241
|
-
return 1;
|
|
1242
|
-
}
|
|
1243
|
-
}
|
|
1244
|
-
return b.scores.length - a.scores.length;
|
|
1245
|
-
}
|
|
1246
|
-
for (let i = 0; i < minLength; i++) {
|
|
1247
|
-
if (a.parsed[i].value !== b.parsed[i].value) {
|
|
1248
|
-
return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
|
|
1249
|
-
}
|
|
1250
|
-
}
|
|
1251
|
-
return a.index - b.index;
|
|
1252
|
-
}).map((d, i) => {
|
|
1253
|
-
d.child.rank = i;
|
|
1254
|
-
return d.child;
|
|
1255
|
-
});
|
|
1256
|
-
return { routesById, routesByPath, flatRoutes };
|
|
1257
|
-
}
|
|
1258
1153
|
function getMatchedRoutes({
|
|
1259
1154
|
pathname,
|
|
1260
1155
|
routePathname,
|
|
1261
|
-
basepath,
|
|
1262
1156
|
caseSensitive,
|
|
1263
1157
|
routesByPath,
|
|
1264
1158
|
routesById,
|
|
@@ -1269,7 +1163,6 @@ function getMatchedRoutes({
|
|
|
1269
1163
|
const trimmedPath = trimPathRight(pathname);
|
|
1270
1164
|
const getMatchedParams = (route) => {
|
|
1271
1165
|
const result = matchPathname(
|
|
1272
|
-
basepath,
|
|
1273
1166
|
trimmedPath,
|
|
1274
1167
|
{
|
|
1275
1168
|
to: route.fullPath,
|
|
@@ -1399,7 +1292,6 @@ export {
|
|
|
1399
1292
|
getInitialRouterState,
|
|
1400
1293
|
getLocationChangeInfo,
|
|
1401
1294
|
getMatchedRoutes,
|
|
1402
|
-
lazyFn
|
|
1403
|
-
processRouteTree
|
|
1295
|
+
lazyFn
|
|
1404
1296
|
};
|
|
1405
1297
|
//# sourceMappingURL=router.js.map
|