@tanstack/react-router 1.63.5 → 1.64.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/router.cjs +66 -45
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +10 -0
- package/dist/esm/router.d.ts +10 -0
- package/dist/esm/router.js +67 -46
- package/dist/esm/router.js.map +1 -1
- package/package.json +2 -2
- package/src/router.ts +86 -70
package/dist/cjs/router.cjs
CHANGED
|
@@ -226,6 +226,38 @@ class Router {
|
|
|
226
226
|
});
|
|
227
227
|
return resolvedPath;
|
|
228
228
|
};
|
|
229
|
+
this.getMatchedRoutes = (next, dest) => {
|
|
230
|
+
let routeParams = {};
|
|
231
|
+
const trimmedPath = path.trimPathRight(next.pathname);
|
|
232
|
+
const getMatchedParams = (route) => {
|
|
233
|
+
const result = path.matchPathname(this.basepath, trimmedPath, {
|
|
234
|
+
to: route.fullPath,
|
|
235
|
+
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
|
|
236
|
+
fuzzy: true
|
|
237
|
+
});
|
|
238
|
+
return result;
|
|
239
|
+
};
|
|
240
|
+
let foundRoute = (dest == null ? void 0 : dest.to) !== void 0 ? this.routesByPath[dest.to] : void 0;
|
|
241
|
+
if (foundRoute) {
|
|
242
|
+
routeParams = getMatchedParams(foundRoute);
|
|
243
|
+
} else {
|
|
244
|
+
foundRoute = this.flatRoutes.find((route) => {
|
|
245
|
+
const matchedParams = getMatchedParams(route);
|
|
246
|
+
if (matchedParams) {
|
|
247
|
+
routeParams = matchedParams;
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
return false;
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
let routeCursor = foundRoute || this.routesById[root.rootRouteId];
|
|
254
|
+
const matchedRoutes = [routeCursor];
|
|
255
|
+
while (routeCursor.parentRoute) {
|
|
256
|
+
routeCursor = routeCursor.parentRoute;
|
|
257
|
+
matchedRoutes.unshift(routeCursor);
|
|
258
|
+
}
|
|
259
|
+
return { matchedRoutes, routeParams, foundRoute };
|
|
260
|
+
};
|
|
229
261
|
this.cancelMatch = (id) => {
|
|
230
262
|
const match = this.getMatch(id);
|
|
231
263
|
if (!match) return;
|
|
@@ -239,7 +271,7 @@ class Router {
|
|
|
239
271
|
});
|
|
240
272
|
};
|
|
241
273
|
this.buildLocation = (opts) => {
|
|
242
|
-
const build = (dest = {},
|
|
274
|
+
const build = (dest = {}, matchedRoutesResult) => {
|
|
243
275
|
var _a, _b, _c, _d, _e;
|
|
244
276
|
const fromMatches = dest._fromLocation ? this.matchRoutes(dest._fromLocation, { _buildLocation: true }) : this.state.matches;
|
|
245
277
|
const fromMatch = dest.from != null ? fromMatches.find(
|
|
@@ -255,21 +287,32 @@ class Router {
|
|
|
255
287
|
"Could not find match for from: " + dest.from
|
|
256
288
|
);
|
|
257
289
|
const fromSearch = ((_a = this.state.pendingMatches) == null ? void 0 : _a.length) ? (_b = utils.last(this.state.pendingMatches)) == null ? void 0 : _b.search : ((_c = utils.last(fromMatches)) == null ? void 0 : _c.search) || this.latestLocation.search;
|
|
258
|
-
const stayingMatches =
|
|
259
|
-
(d) => fromMatches.find((e) => e.routeId === d.
|
|
260
|
-
);
|
|
261
|
-
const fromRouteByFromPathRouteId = this.routesById[(_d = stayingMatches == null ? void 0 : stayingMatches.find((d) => d.pathname === fromPath)) == null ? void 0 : _d.routeId];
|
|
262
|
-
let pathname = dest.to ? this.resolvePathWithBase(fromPath, `${dest.to}`) : this.resolvePathWithBase(
|
|
263
|
-
fromPath,
|
|
264
|
-
(fromRouteByFromPathRouteId == null ? void 0 : fromRouteByFromPathRouteId.to) ?? fromPath
|
|
290
|
+
const stayingMatches = matchedRoutesResult == null ? void 0 : matchedRoutesResult.matchedRoutes.filter(
|
|
291
|
+
(d) => fromMatches.find((e) => e.routeId === d.id)
|
|
265
292
|
);
|
|
293
|
+
let pathname;
|
|
294
|
+
if (dest.to) {
|
|
295
|
+
pathname = this.resolvePathWithBase(fromPath, `${dest.to}`);
|
|
296
|
+
} else {
|
|
297
|
+
const fromRouteByFromPathRouteId = this.routesById[(_d = stayingMatches == null ? void 0 : stayingMatches.find((route) => {
|
|
298
|
+
const interpolatedPath = path.interpolatePath({
|
|
299
|
+
path: route.fullPath,
|
|
300
|
+
params: (matchedRoutesResult == null ? void 0 : matchedRoutesResult.routeParams) ?? {}
|
|
301
|
+
});
|
|
302
|
+
const pathname2 = path.joinPaths([this.basepath, interpolatedPath]);
|
|
303
|
+
return pathname2 === fromPath;
|
|
304
|
+
})) == null ? void 0 : _d.id];
|
|
305
|
+
pathname = this.resolvePathWithBase(
|
|
306
|
+
fromPath,
|
|
307
|
+
(fromRouteByFromPathRouteId == null ? void 0 : fromRouteByFromPathRouteId.to) ?? fromPath
|
|
308
|
+
);
|
|
309
|
+
}
|
|
266
310
|
const prevParams = { ...(_e = utils.last(fromMatches)) == null ? void 0 : _e.params };
|
|
267
311
|
let nextParams = (dest.params ?? true) === true ? prevParams : { ...prevParams, ...utils.functionalUpdate(dest.params, prevParams) };
|
|
268
312
|
if (Object.keys(nextParams).length > 0) {
|
|
269
|
-
|
|
313
|
+
matchedRoutesResult == null ? void 0 : matchedRoutesResult.matchedRoutes.map((route) => {
|
|
270
314
|
var _a2;
|
|
271
|
-
|
|
272
|
-
return ((_a2 = route == null ? void 0 : route.options.params) == null ? void 0 : _a2.stringify) ?? route.options.stringifyParams;
|
|
315
|
+
return ((_a2 = route.options.params) == null ? void 0 : _a2.stringify) ?? route.options.stringifyParams;
|
|
273
316
|
}).filter(Boolean).forEach((fn) => {
|
|
274
317
|
nextParams = { ...nextParams, ...fn(nextParams) };
|
|
275
318
|
});
|
|
@@ -280,12 +323,8 @@ class Router {
|
|
|
280
323
|
leaveWildcards: false,
|
|
281
324
|
leaveParams: opts.leaveParams
|
|
282
325
|
});
|
|
283
|
-
const preSearchFilters = (stayingMatches == null ? void 0 : stayingMatches.map(
|
|
284
|
-
|
|
285
|
-
).flat().filter(Boolean)) ?? [];
|
|
286
|
-
const postSearchFilters = (stayingMatches == null ? void 0 : stayingMatches.map(
|
|
287
|
-
(match) => this.looseRoutesById[match.routeId].options.postSearchFilters ?? []
|
|
288
|
-
).flat().filter(Boolean)) ?? [];
|
|
326
|
+
const preSearchFilters = (stayingMatches == null ? void 0 : stayingMatches.map((route) => route.options.preSearchFilters ?? []).flat().filter(Boolean)) ?? [];
|
|
327
|
+
const postSearchFilters = (stayingMatches == null ? void 0 : stayingMatches.map((route) => route.options.postSearchFilters ?? []).flat().filter(Boolean)) ?? [];
|
|
289
328
|
const preFilteredSearch = preSearchFilters.length ? preSearchFilters.reduce((prev, next) => next(prev), fromSearch) : fromSearch;
|
|
290
329
|
const destSearch = dest.search === true ? preFilteredSearch : dest.search ? utils.functionalUpdate(dest.search, preFilteredSearch) : preSearchFilters.length ? preFilteredSearch : {};
|
|
291
330
|
const postFilteredSearch = postSearchFilters.length ? postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
|
|
@@ -333,11 +372,11 @@ class Router {
|
|
|
333
372
|
maskedNext = build(maskedDest);
|
|
334
373
|
}
|
|
335
374
|
}
|
|
336
|
-
const nextMatches = this.
|
|
337
|
-
const maskedMatches = maskedNext ? this.matchRoutes(maskedNext, { _buildLocation: true }) : void 0;
|
|
338
|
-
const maskedFinal = maskedNext ? build(maskedDest, maskedMatches) : void 0;
|
|
375
|
+
const nextMatches = this.getMatchedRoutes(next, dest);
|
|
339
376
|
const final = build(dest, nextMatches);
|
|
340
|
-
if (
|
|
377
|
+
if (maskedNext) {
|
|
378
|
+
const maskedMatches = this.getMatchedRoutes(maskedNext, maskedDest);
|
|
379
|
+
const maskedFinal = build(maskedDest, maskedMatches);
|
|
341
380
|
final.maskedLocation = maskedFinal;
|
|
342
381
|
}
|
|
343
382
|
return final;
|
|
@@ -1020,7 +1059,8 @@ class Router {
|
|
|
1020
1059
|
const next = this.buildLocation(opts);
|
|
1021
1060
|
let matches = this.matchRoutes(next, {
|
|
1022
1061
|
throwOnError: true,
|
|
1023
|
-
preload: true
|
|
1062
|
+
preload: true,
|
|
1063
|
+
dest: opts
|
|
1024
1064
|
});
|
|
1025
1065
|
const loadedMatchIds = Object.fromEntries(
|
|
1026
1066
|
[
|
|
@@ -1265,25 +1305,10 @@ class Router {
|
|
|
1265
1305
|
}
|
|
1266
1306
|
}
|
|
1267
1307
|
matchRoutesInternal(next, opts) {
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
path.trimPathRight(next.pathname),
|
|
1273
|
-
{
|
|
1274
|
-
to: route.fullPath,
|
|
1275
|
-
caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
|
|
1276
|
-
fuzzy: true
|
|
1277
|
-
}
|
|
1278
|
-
);
|
|
1279
|
-
if (matchedParams) {
|
|
1280
|
-
routeParams = matchedParams;
|
|
1281
|
-
return true;
|
|
1282
|
-
}
|
|
1283
|
-
return false;
|
|
1284
|
-
});
|
|
1285
|
-
let routeCursor = foundRoute || this.routesById[root.rootRouteId];
|
|
1286
|
-
const matchedRoutes = [routeCursor];
|
|
1308
|
+
const { foundRoute, matchedRoutes, routeParams } = this.getMatchedRoutes(
|
|
1309
|
+
next,
|
|
1310
|
+
opts == null ? void 0 : opts.dest
|
|
1311
|
+
);
|
|
1287
1312
|
let isGlobalNotFound = false;
|
|
1288
1313
|
if (
|
|
1289
1314
|
// If we found a route, and it's not an index route and we have left over path
|
|
@@ -1298,10 +1323,6 @@ class Router {
|
|
|
1298
1323
|
isGlobalNotFound = true;
|
|
1299
1324
|
}
|
|
1300
1325
|
}
|
|
1301
|
-
while (routeCursor.parentRoute) {
|
|
1302
|
-
routeCursor = routeCursor.parentRoute;
|
|
1303
|
-
matchedRoutes.unshift(routeCursor);
|
|
1304
|
-
}
|
|
1305
1326
|
const globalNotFoundRouteId = (() => {
|
|
1306
1327
|
if (!isGlobalNotFound) {
|
|
1307
1328
|
return void 0;
|