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