@tanstack/react-router 1.49.1 → 1.49.7

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.
@@ -211,200 +211,6 @@ class Router {
211
211
  });
212
212
  return resolvedPath;
213
213
  };
214
- this.matchRoutes = (next, opts) => {
215
- let routeParams = {};
216
- const foundRoute = this.flatRoutes.find((route) => {
217
- const matchedParams = path.matchPathname(
218
- this.basepath,
219
- path.trimPathRight(next.pathname),
220
- {
221
- to: route.fullPath,
222
- caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
223
- fuzzy: true
224
- }
225
- );
226
- if (matchedParams) {
227
- routeParams = matchedParams;
228
- return true;
229
- }
230
- return false;
231
- });
232
- let routeCursor = foundRoute || this.routesById[root.rootRouteId];
233
- const matchedRoutes = [routeCursor];
234
- let isGlobalNotFound = false;
235
- if (
236
- // If we found a route, and it's not an index route and we have left over path
237
- foundRoute ? foundRoute.path !== "/" && routeParams["**"] : (
238
- // Or if we didn't find a route and we have left over path
239
- path.trimPathRight(next.pathname)
240
- )
241
- ) {
242
- if (this.options.notFoundRoute) {
243
- matchedRoutes.push(this.options.notFoundRoute);
244
- } else {
245
- isGlobalNotFound = true;
246
- }
247
- }
248
- while (routeCursor.parentRoute) {
249
- routeCursor = routeCursor.parentRoute;
250
- matchedRoutes.unshift(routeCursor);
251
- }
252
- const globalNotFoundRouteId = (() => {
253
- if (!isGlobalNotFound) {
254
- return void 0;
255
- }
256
- if (this.options.notFoundMode !== "root") {
257
- for (let i = matchedRoutes.length - 1; i >= 0; i--) {
258
- const route = matchedRoutes[i];
259
- if (route.children) {
260
- return route.id;
261
- }
262
- }
263
- }
264
- return root.rootRouteId;
265
- })();
266
- const parseErrors = matchedRoutes.map((route) => {
267
- var _a;
268
- let parsedParamsError;
269
- const parseParams = ((_a = route.options.params) == null ? void 0 : _a.parse) ?? route.options.parseParams;
270
- if (parseParams) {
271
- try {
272
- const parsedParams = parseParams(routeParams);
273
- Object.assign(routeParams, parsedParams);
274
- } catch (err) {
275
- parsedParamsError = new PathParamError(err.message, {
276
- cause: err
277
- });
278
- if (opts == null ? void 0 : opts.throwOnError) {
279
- throw parsedParamsError;
280
- }
281
- return parsedParamsError;
282
- }
283
- }
284
- return;
285
- });
286
- const matches = [];
287
- matchedRoutes.forEach((route, index) => {
288
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
289
- const parentMatch = matches[index - 1];
290
- const [preMatchSearch, searchError] = (() => {
291
- const parentSearch = (parentMatch == null ? void 0 : parentMatch.search) ?? next.search;
292
- try {
293
- const validator = typeof route.options.validateSearch === "object" ? route.options.validateSearch.parse : route.options.validateSearch;
294
- const search = (validator == null ? void 0 : validator(parentSearch)) ?? {};
295
- return [
296
- {
297
- ...parentSearch,
298
- ...search
299
- },
300
- void 0
301
- ];
302
- } catch (err) {
303
- const searchParamError = new SearchParamError(err.message, {
304
- cause: err
305
- });
306
- if (opts == null ? void 0 : opts.throwOnError) {
307
- throw searchParamError;
308
- }
309
- return [parentSearch, searchParamError];
310
- }
311
- })();
312
- const loaderDeps = ((_b = (_a = route.options).loaderDeps) == null ? void 0 : _b.call(_a, {
313
- search: preMatchSearch
314
- })) ?? "";
315
- const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : "";
316
- const interpolatedPath = path.interpolatePath({
317
- path: route.fullPath,
318
- params: routeParams
319
- });
320
- const matchId = path.interpolatePath({
321
- path: route.id,
322
- params: routeParams,
323
- leaveWildcards: true
324
- }) + loaderDepsHash;
325
- const existingMatch = this.getMatch(matchId);
326
- const cause = this.state.matches.find((d) => d.id === matchId) ? "stay" : "enter";
327
- let match;
328
- if (existingMatch) {
329
- match = {
330
- ...existingMatch,
331
- cause,
332
- params: routeParams
333
- };
334
- } else {
335
- const status = route.options.loader || route.options.beforeLoad || route.lazyFn ? "pending" : "success";
336
- match = {
337
- id: matchId,
338
- index,
339
- routeId: route.id,
340
- params: routeParams,
341
- pathname: path.joinPaths([this.basepath, interpolatedPath]),
342
- updatedAt: Date.now(),
343
- search: {},
344
- searchError: void 0,
345
- status,
346
- isFetching: false,
347
- error: void 0,
348
- paramsError: parseErrors[index],
349
- __routeContext: {},
350
- __beforeLoadContext: {},
351
- context: {},
352
- abortController: new AbortController(),
353
- fetchCount: 0,
354
- cause,
355
- loaderDeps,
356
- invalid: false,
357
- preload: false,
358
- links: (_d = (_c = route.options).links) == null ? void 0 : _d.call(_c),
359
- scripts: (_f = (_e = route.options).scripts) == null ? void 0 : _f.call(_e),
360
- staticData: route.options.staticData || {},
361
- loadPromise: utils.createControlledPromise()
362
- };
363
- }
364
- if (match.status === "success") {
365
- match.meta = (_h = (_g = route.options).meta) == null ? void 0 : _h.call(_g, {
366
- matches,
367
- match,
368
- params: match.params,
369
- loaderData: match.loaderData
370
- });
371
- match.headers = (_j = (_i = route.options).headers) == null ? void 0 : _j.call(_i, {
372
- loaderData: match.loaderData
373
- });
374
- }
375
- if (!(opts == null ? void 0 : opts.preload)) {
376
- match.globalNotFound = globalNotFoundRouteId === route.id;
377
- }
378
- match.search = utils.replaceEqualDeep(match.search, preMatchSearch);
379
- match.searchError = searchError;
380
- const parentMatchId = parentMatch == null ? void 0 : parentMatch.id;
381
- const parentContext = !parentMatchId ? this.options.context ?? {} : parentMatch.context ?? this.options.context ?? {};
382
- match.context = {
383
- ...parentContext,
384
- ...match.__routeContext,
385
- ...match.__beforeLoadContext
386
- };
387
- const contextFnContext = {
388
- search: match.search,
389
- params: match.params,
390
- context: match.context,
391
- location: next,
392
- navigate: (opts2) => this.navigate({ ...opts2, _fromLocation: next }),
393
- buildLocation: this.buildLocation,
394
- cause: match.cause,
395
- abortController: match.abortController,
396
- preload: !!match.preload
397
- };
398
- match.__routeContext = ((_l = (_k = route.options).context) == null ? void 0 : _l.call(_k, contextFnContext)) ?? {};
399
- match.context = {
400
- ...parentContext,
401
- ...match.__routeContext,
402
- ...match.__beforeLoadContext
403
- };
404
- matches.push(match);
405
- });
406
- return matches;
407
- };
408
214
  this.cancelMatch = (id) => {
409
215
  const match = this.getMatch(id);
410
216
  if (!match) return;
@@ -1416,6 +1222,213 @@ class Router {
1416
1222
  get looseRoutesById() {
1417
1223
  return this.routesById;
1418
1224
  }
1225
+ matchRoutes(pathnameOrNext, locationSearchOrOpts, opts) {
1226
+ if (typeof pathnameOrNext === "string") {
1227
+ return this.matchRoutesInternal(
1228
+ {
1229
+ pathname: pathnameOrNext,
1230
+ search: locationSearchOrOpts
1231
+ },
1232
+ opts
1233
+ );
1234
+ } else {
1235
+ return this.matchRoutesInternal(pathnameOrNext, locationSearchOrOpts);
1236
+ }
1237
+ }
1238
+ matchRoutesInternal(next, opts) {
1239
+ let routeParams = {};
1240
+ const foundRoute = this.flatRoutes.find((route) => {
1241
+ const matchedParams = path.matchPathname(
1242
+ this.basepath,
1243
+ path.trimPathRight(next.pathname),
1244
+ {
1245
+ to: route.fullPath,
1246
+ caseSensitive: route.options.caseSensitive ?? this.options.caseSensitive,
1247
+ fuzzy: true
1248
+ }
1249
+ );
1250
+ if (matchedParams) {
1251
+ routeParams = matchedParams;
1252
+ return true;
1253
+ }
1254
+ return false;
1255
+ });
1256
+ let routeCursor = foundRoute || this.routesById[root.rootRouteId];
1257
+ const matchedRoutes = [routeCursor];
1258
+ let isGlobalNotFound = false;
1259
+ if (
1260
+ // If we found a route, and it's not an index route and we have left over path
1261
+ foundRoute ? foundRoute.path !== "/" && routeParams["**"] : (
1262
+ // Or if we didn't find a route and we have left over path
1263
+ path.trimPathRight(next.pathname)
1264
+ )
1265
+ ) {
1266
+ if (this.options.notFoundRoute) {
1267
+ matchedRoutes.push(this.options.notFoundRoute);
1268
+ } else {
1269
+ isGlobalNotFound = true;
1270
+ }
1271
+ }
1272
+ while (routeCursor.parentRoute) {
1273
+ routeCursor = routeCursor.parentRoute;
1274
+ matchedRoutes.unshift(routeCursor);
1275
+ }
1276
+ const globalNotFoundRouteId = (() => {
1277
+ if (!isGlobalNotFound) {
1278
+ return void 0;
1279
+ }
1280
+ if (this.options.notFoundMode !== "root") {
1281
+ for (let i = matchedRoutes.length - 1; i >= 0; i--) {
1282
+ const route = matchedRoutes[i];
1283
+ if (route.children) {
1284
+ return route.id;
1285
+ }
1286
+ }
1287
+ }
1288
+ return root.rootRouteId;
1289
+ })();
1290
+ const parseErrors = matchedRoutes.map((route) => {
1291
+ var _a;
1292
+ let parsedParamsError;
1293
+ const parseParams = ((_a = route.options.params) == null ? void 0 : _a.parse) ?? route.options.parseParams;
1294
+ if (parseParams) {
1295
+ try {
1296
+ const parsedParams = parseParams(routeParams);
1297
+ Object.assign(routeParams, parsedParams);
1298
+ } catch (err) {
1299
+ parsedParamsError = new PathParamError(err.message, {
1300
+ cause: err
1301
+ });
1302
+ if (opts == null ? void 0 : opts.throwOnError) {
1303
+ throw parsedParamsError;
1304
+ }
1305
+ return parsedParamsError;
1306
+ }
1307
+ }
1308
+ return;
1309
+ });
1310
+ const matches = [];
1311
+ matchedRoutes.forEach((route, index) => {
1312
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
1313
+ const parentMatch = matches[index - 1];
1314
+ const [preMatchSearch, searchError] = (() => {
1315
+ const parentSearch = (parentMatch == null ? void 0 : parentMatch.search) ?? next.search;
1316
+ try {
1317
+ const validator = typeof route.options.validateSearch === "object" ? route.options.validateSearch.parse : route.options.validateSearch;
1318
+ const search = (validator == null ? void 0 : validator(parentSearch)) ?? {};
1319
+ return [
1320
+ {
1321
+ ...parentSearch,
1322
+ ...search
1323
+ },
1324
+ void 0
1325
+ ];
1326
+ } catch (err) {
1327
+ const searchParamError = new SearchParamError(err.message, {
1328
+ cause: err
1329
+ });
1330
+ if (opts == null ? void 0 : opts.throwOnError) {
1331
+ throw searchParamError;
1332
+ }
1333
+ return [parentSearch, searchParamError];
1334
+ }
1335
+ })();
1336
+ const loaderDeps = ((_b = (_a = route.options).loaderDeps) == null ? void 0 : _b.call(_a, {
1337
+ search: preMatchSearch
1338
+ })) ?? "";
1339
+ const loaderDepsHash = loaderDeps ? JSON.stringify(loaderDeps) : "";
1340
+ const interpolatedPath = path.interpolatePath({
1341
+ path: route.fullPath,
1342
+ params: routeParams
1343
+ });
1344
+ const matchId = path.interpolatePath({
1345
+ path: route.id,
1346
+ params: routeParams,
1347
+ leaveWildcards: true
1348
+ }) + loaderDepsHash;
1349
+ const existingMatch = this.getMatch(matchId);
1350
+ const cause = this.state.matches.find((d) => d.id === matchId) ? "stay" : "enter";
1351
+ let match;
1352
+ if (existingMatch) {
1353
+ match = {
1354
+ ...existingMatch,
1355
+ cause,
1356
+ params: routeParams
1357
+ };
1358
+ } else {
1359
+ const status = route.options.loader || route.options.beforeLoad || route.lazyFn ? "pending" : "success";
1360
+ match = {
1361
+ id: matchId,
1362
+ index,
1363
+ routeId: route.id,
1364
+ params: routeParams,
1365
+ pathname: path.joinPaths([this.basepath, interpolatedPath]),
1366
+ updatedAt: Date.now(),
1367
+ search: {},
1368
+ searchError: void 0,
1369
+ status,
1370
+ isFetching: false,
1371
+ error: void 0,
1372
+ paramsError: parseErrors[index],
1373
+ __routeContext: {},
1374
+ __beforeLoadContext: {},
1375
+ context: {},
1376
+ abortController: new AbortController(),
1377
+ fetchCount: 0,
1378
+ cause,
1379
+ loaderDeps,
1380
+ invalid: false,
1381
+ preload: false,
1382
+ links: (_d = (_c = route.options).links) == null ? void 0 : _d.call(_c),
1383
+ scripts: (_f = (_e = route.options).scripts) == null ? void 0 : _f.call(_e),
1384
+ staticData: route.options.staticData || {},
1385
+ loadPromise: utils.createControlledPromise()
1386
+ };
1387
+ }
1388
+ if (match.status === "success") {
1389
+ match.meta = (_h = (_g = route.options).meta) == null ? void 0 : _h.call(_g, {
1390
+ matches,
1391
+ match,
1392
+ params: match.params,
1393
+ loaderData: match.loaderData
1394
+ });
1395
+ match.headers = (_j = (_i = route.options).headers) == null ? void 0 : _j.call(_i, {
1396
+ loaderData: match.loaderData
1397
+ });
1398
+ }
1399
+ if (!(opts == null ? void 0 : opts.preload)) {
1400
+ match.globalNotFound = globalNotFoundRouteId === route.id;
1401
+ }
1402
+ match.search = utils.replaceEqualDeep(match.search, preMatchSearch);
1403
+ match.searchError = searchError;
1404
+ const parentMatchId = parentMatch == null ? void 0 : parentMatch.id;
1405
+ const parentContext = !parentMatchId ? this.options.context ?? {} : parentMatch.context ?? this.options.context ?? {};
1406
+ match.context = {
1407
+ ...parentContext,
1408
+ ...match.__routeContext,
1409
+ ...match.__beforeLoadContext
1410
+ };
1411
+ const contextFnContext = {
1412
+ search: match.search,
1413
+ params: match.params,
1414
+ context: match.context,
1415
+ location: next,
1416
+ navigate: (opts2) => this.navigate({ ...opts2, _fromLocation: next }),
1417
+ buildLocation: this.buildLocation,
1418
+ cause: match.cause,
1419
+ abortController: match.abortController,
1420
+ preload: !!match.preload
1421
+ };
1422
+ match.__routeContext = ((_l = (_k = route.options).context) == null ? void 0 : _l.call(_k, contextFnContext)) ?? {};
1423
+ match.context = {
1424
+ ...parentContext,
1425
+ ...match.__routeContext,
1426
+ ...match.__beforeLoadContext
1427
+ };
1428
+ matches.push(match);
1429
+ });
1430
+ return matches;
1431
+ }
1419
1432
  }
1420
1433
  function lazyFn(fn, key) {
1421
1434
  return async (...args) => {