@tanstack/router-core 1.131.37 → 1.131.39
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/index.cjs +2 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +4 -2
- package/dist/cjs/process-route-tree.cjs +139 -0
- package/dist/cjs/process-route-tree.cjs.map +1 -0
- package/dist/cjs/process-route-tree.d.cts +10 -0
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +12 -0
- package/dist/cjs/router.cjs +19 -166
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +1 -22
- package/dist/esm/index.d.ts +4 -2
- package/dist/esm/index.js +2 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/process-route-tree.d.ts +10 -0
- package/dist/esm/process-route-tree.js +139 -0
- package/dist/esm/process-route-tree.js.map +1 -0
- package/dist/esm/route.d.ts +12 -0
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/router.d.ts +1 -22
- package/dist/esm/router.js +20 -167
- package/dist/esm/router.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -3
- package/src/process-route-tree.ts +228 -0
- package/src/route.ts +13 -0
- package/src/router.ts +31 -282
package/dist/esm/router.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Store, batch } from "@tanstack/store";
|
|
2
2
|
import { createMemoryHistory, createBrowserHistory, parseHref } from "@tanstack/history";
|
|
3
|
-
import invariant from "tiny-invariant";
|
|
4
3
|
import { createControlledPromise, deepEqual, replaceEqualDeep, last, findLast, functionalUpdate } from "./utils.js";
|
|
5
|
-
import {
|
|
4
|
+
import { processRouteTree } from "./process-route-tree.js";
|
|
5
|
+
import { trimPath, resolvePath, cleanPath, matchPathname, trimPathRight, interpolatePath, joinPaths } 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";
|
|
@@ -928,26 +928,6 @@ class RouterCore {
|
|
|
928
928
|
}
|
|
929
929
|
return rootRouteId;
|
|
930
930
|
})();
|
|
931
|
-
const parseErrors = matchedRoutes.map((route) => {
|
|
932
|
-
var _a2;
|
|
933
|
-
let parsedParamsError;
|
|
934
|
-
const parseParams = ((_a2 = route.options.params) == null ? void 0 : _a2.parse) ?? route.options.parseParams;
|
|
935
|
-
if (parseParams) {
|
|
936
|
-
try {
|
|
937
|
-
const parsedParams = parseParams(routeParams);
|
|
938
|
-
Object.assign(routeParams, parsedParams);
|
|
939
|
-
} catch (err) {
|
|
940
|
-
parsedParamsError = new PathParamError(err.message, {
|
|
941
|
-
cause: err
|
|
942
|
-
});
|
|
943
|
-
if (opts == null ? void 0 : opts.throwOnError) {
|
|
944
|
-
throw parsedParamsError;
|
|
945
|
-
}
|
|
946
|
-
return parsedParamsError;
|
|
947
|
-
}
|
|
948
|
-
}
|
|
949
|
-
return;
|
|
950
|
-
});
|
|
951
931
|
const matches = [];
|
|
952
932
|
const getParentContext = (parentMatch) => {
|
|
953
933
|
const parentMatchId = parentMatch == null ? void 0 : parentMatch.id;
|
|
@@ -999,14 +979,22 @@ class RouterCore {
|
|
|
999
979
|
decodeCharMap: this.pathParamsDecodeCharMap,
|
|
1000
980
|
parseCache: this.parsePathnameCache
|
|
1001
981
|
});
|
|
1002
|
-
const
|
|
1003
|
-
|
|
1004
|
-
const
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
982
|
+
const matchId = interpolatePathResult.interpolatedPath + loaderDepsHash;
|
|
983
|
+
const existingMatch = this.getMatch(matchId);
|
|
984
|
+
const previousMatch = this.state.matches.find(
|
|
985
|
+
(d) => d.routeId === route.id
|
|
986
|
+
);
|
|
987
|
+
const strictParams = (existingMatch == null ? void 0 : existingMatch._strictParams) ?? interpolatePathResult.usedParams;
|
|
988
|
+
let paramsError = void 0;
|
|
989
|
+
if (!existingMatch) {
|
|
990
|
+
const strictParseParams = ((_c = route.options.params) == null ? void 0 : _c.parse) ?? route.options.parseParams;
|
|
991
|
+
if (strictParseParams) {
|
|
992
|
+
try {
|
|
993
|
+
Object.assign(
|
|
994
|
+
strictParams,
|
|
995
|
+
strictParseParams(strictParams)
|
|
996
|
+
);
|
|
997
|
+
} catch (err) {
|
|
1010
998
|
paramsError = new PathParamError(err.message, {
|
|
1011
999
|
cause: err
|
|
1012
1000
|
});
|
|
@@ -1016,11 +1004,7 @@ class RouterCore {
|
|
|
1016
1004
|
}
|
|
1017
1005
|
}
|
|
1018
1006
|
}
|
|
1019
|
-
|
|
1020
|
-
const existingMatch = this.getMatch(matchId);
|
|
1021
|
-
const previousMatch = this.state.matches.find(
|
|
1022
|
-
(d) => d.routeId === route.id
|
|
1023
|
-
);
|
|
1007
|
+
Object.assign(routeParams, strictParams);
|
|
1024
1008
|
const cause = previousMatch ? "stay" : "enter";
|
|
1025
1009
|
let match;
|
|
1026
1010
|
if (existingMatch) {
|
|
@@ -1160,136 +1144,6 @@ function validateSearch(validateSearch2, input) {
|
|
|
1160
1144
|
}
|
|
1161
1145
|
return {};
|
|
1162
1146
|
}
|
|
1163
|
-
const REQUIRED_PARAM_BASE_SCORE = 0.5;
|
|
1164
|
-
const OPTIONAL_PARAM_BASE_SCORE = 0.4;
|
|
1165
|
-
const WILDCARD_PARAM_BASE_SCORE = 0.25;
|
|
1166
|
-
const BOTH_PRESENCE_BASE_SCORE = 0.05;
|
|
1167
|
-
const PREFIX_PRESENCE_BASE_SCORE = 0.02;
|
|
1168
|
-
const SUFFIX_PRESENCE_BASE_SCORE = 0.01;
|
|
1169
|
-
const PREFIX_LENGTH_SCORE_MULTIPLIER = 2e-4;
|
|
1170
|
-
const SUFFIX_LENGTH_SCORE_MULTIPLIER = 1e-4;
|
|
1171
|
-
function handleParam(segment, baseScore) {
|
|
1172
|
-
if (segment.prefixSegment && segment.suffixSegment) {
|
|
1173
|
-
return baseScore + BOTH_PRESENCE_BASE_SCORE + PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length + SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length;
|
|
1174
|
-
}
|
|
1175
|
-
if (segment.prefixSegment) {
|
|
1176
|
-
return baseScore + PREFIX_PRESENCE_BASE_SCORE + PREFIX_LENGTH_SCORE_MULTIPLIER * segment.prefixSegment.length;
|
|
1177
|
-
}
|
|
1178
|
-
if (segment.suffixSegment) {
|
|
1179
|
-
return baseScore + SUFFIX_PRESENCE_BASE_SCORE + SUFFIX_LENGTH_SCORE_MULTIPLIER * segment.suffixSegment.length;
|
|
1180
|
-
}
|
|
1181
|
-
return baseScore;
|
|
1182
|
-
}
|
|
1183
|
-
function processRouteTree({
|
|
1184
|
-
routeTree,
|
|
1185
|
-
initRoute
|
|
1186
|
-
}) {
|
|
1187
|
-
const routesById = {};
|
|
1188
|
-
const routesByPath = {};
|
|
1189
|
-
const recurseRoutes = (childRoutes) => {
|
|
1190
|
-
childRoutes.forEach((childRoute, i) => {
|
|
1191
|
-
initRoute == null ? void 0 : initRoute(childRoute, i);
|
|
1192
|
-
const existingRoute = routesById[childRoute.id];
|
|
1193
|
-
invariant(
|
|
1194
|
-
!existingRoute,
|
|
1195
|
-
`Duplicate routes found with id: ${String(childRoute.id)}`
|
|
1196
|
-
);
|
|
1197
|
-
routesById[childRoute.id] = childRoute;
|
|
1198
|
-
if (!childRoute.isRoot && childRoute.path) {
|
|
1199
|
-
const trimmedFullPath = trimPathRight(childRoute.fullPath);
|
|
1200
|
-
if (!routesByPath[trimmedFullPath] || childRoute.fullPath.endsWith("/")) {
|
|
1201
|
-
routesByPath[trimmedFullPath] = childRoute;
|
|
1202
|
-
}
|
|
1203
|
-
}
|
|
1204
|
-
const children = childRoute.children;
|
|
1205
|
-
if (children == null ? void 0 : children.length) {
|
|
1206
|
-
recurseRoutes(children);
|
|
1207
|
-
}
|
|
1208
|
-
});
|
|
1209
|
-
};
|
|
1210
|
-
recurseRoutes([routeTree]);
|
|
1211
|
-
const scoredRoutes = [];
|
|
1212
|
-
const routes = Object.values(routesById);
|
|
1213
|
-
routes.forEach((d, i) => {
|
|
1214
|
-
var _a;
|
|
1215
|
-
if (d.isRoot || !d.path) {
|
|
1216
|
-
return;
|
|
1217
|
-
}
|
|
1218
|
-
const trimmed = trimPathLeft(d.fullPath);
|
|
1219
|
-
let parsed = parsePathname(trimmed);
|
|
1220
|
-
let skip = 0;
|
|
1221
|
-
while (parsed.length > skip + 1 && ((_a = parsed[skip]) == null ? void 0 : _a.value) === "/") {
|
|
1222
|
-
skip++;
|
|
1223
|
-
}
|
|
1224
|
-
if (skip > 0) parsed = parsed.slice(skip);
|
|
1225
|
-
let optionalParamCount = 0;
|
|
1226
|
-
let hasStaticAfter = false;
|
|
1227
|
-
const scores = parsed.map((segment, index) => {
|
|
1228
|
-
if (segment.value === "/") {
|
|
1229
|
-
return 0.75;
|
|
1230
|
-
}
|
|
1231
|
-
let baseScore = void 0;
|
|
1232
|
-
if (segment.type === SEGMENT_TYPE_PARAM) {
|
|
1233
|
-
baseScore = REQUIRED_PARAM_BASE_SCORE;
|
|
1234
|
-
} else if (segment.type === SEGMENT_TYPE_OPTIONAL_PARAM) {
|
|
1235
|
-
baseScore = OPTIONAL_PARAM_BASE_SCORE;
|
|
1236
|
-
optionalParamCount++;
|
|
1237
|
-
} else if (segment.type === SEGMENT_TYPE_WILDCARD) {
|
|
1238
|
-
baseScore = WILDCARD_PARAM_BASE_SCORE;
|
|
1239
|
-
}
|
|
1240
|
-
if (baseScore) {
|
|
1241
|
-
for (let i2 = index + 1; i2 < parsed.length; i2++) {
|
|
1242
|
-
const nextSegment = parsed[i2];
|
|
1243
|
-
if (nextSegment.type === SEGMENT_TYPE_PATHNAME && nextSegment.value !== "/") {
|
|
1244
|
-
hasStaticAfter = true;
|
|
1245
|
-
return handleParam(segment, baseScore + 0.2);
|
|
1246
|
-
}
|
|
1247
|
-
}
|
|
1248
|
-
return handleParam(segment, baseScore);
|
|
1249
|
-
}
|
|
1250
|
-
return 1;
|
|
1251
|
-
});
|
|
1252
|
-
scoredRoutes.push({
|
|
1253
|
-
child: d,
|
|
1254
|
-
trimmed,
|
|
1255
|
-
parsed,
|
|
1256
|
-
index: i,
|
|
1257
|
-
scores,
|
|
1258
|
-
optionalParamCount,
|
|
1259
|
-
hasStaticAfter
|
|
1260
|
-
});
|
|
1261
|
-
});
|
|
1262
|
-
const flatRoutes = scoredRoutes.sort((a, b) => {
|
|
1263
|
-
const minLength = Math.min(a.scores.length, b.scores.length);
|
|
1264
|
-
for (let i = 0; i < minLength; i++) {
|
|
1265
|
-
if (a.scores[i] !== b.scores[i]) {
|
|
1266
|
-
return b.scores[i] - a.scores[i];
|
|
1267
|
-
}
|
|
1268
|
-
}
|
|
1269
|
-
if (a.scores.length !== b.scores.length) {
|
|
1270
|
-
if (a.optionalParamCount !== b.optionalParamCount) {
|
|
1271
|
-
if (a.hasStaticAfter === b.hasStaticAfter) {
|
|
1272
|
-
return a.optionalParamCount - b.optionalParamCount;
|
|
1273
|
-
} else if (a.hasStaticAfter && !b.hasStaticAfter) {
|
|
1274
|
-
return -1;
|
|
1275
|
-
} else if (!a.hasStaticAfter && b.hasStaticAfter) {
|
|
1276
|
-
return 1;
|
|
1277
|
-
}
|
|
1278
|
-
}
|
|
1279
|
-
return b.scores.length - a.scores.length;
|
|
1280
|
-
}
|
|
1281
|
-
for (let i = 0; i < minLength; i++) {
|
|
1282
|
-
if (a.parsed[i].value !== b.parsed[i].value) {
|
|
1283
|
-
return a.parsed[i].value > b.parsed[i].value ? 1 : -1;
|
|
1284
|
-
}
|
|
1285
|
-
}
|
|
1286
|
-
return a.index - b.index;
|
|
1287
|
-
}).map((d, i) => {
|
|
1288
|
-
d.child.rank = i;
|
|
1289
|
-
return d.child;
|
|
1290
|
-
});
|
|
1291
|
-
return { routesById, routesByPath, flatRoutes };
|
|
1292
|
-
}
|
|
1293
1147
|
function getMatchedRoutes({
|
|
1294
1148
|
pathname,
|
|
1295
1149
|
routePathname,
|
|
@@ -1436,7 +1290,6 @@ export {
|
|
|
1436
1290
|
getInitialRouterState,
|
|
1437
1291
|
getLocationChangeInfo,
|
|
1438
1292
|
getMatchedRoutes,
|
|
1439
|
-
lazyFn
|
|
1440
|
-
processRouteTree
|
|
1293
|
+
lazyFn
|
|
1441
1294
|
};
|
|
1442
1295
|
//# sourceMappingURL=router.js.map
|