@tanstack/react-router 0.0.1-alpha.2 → 0.0.1-alpha.4

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.
@@ -849,6 +849,20 @@ function parsePath(path) {
849
849
  return parsedPath;
850
850
  }
851
851
 
852
+ var isProduction = process.env.NODE_ENV === 'production';
853
+ var prefix = 'Invariant failed';
854
+ function invariant(condition, message) {
855
+ if (condition) {
856
+ return;
857
+ }
858
+ if (isProduction) {
859
+ throw new Error(prefix);
860
+ }
861
+ var provided = typeof message === 'function' ? message() : message;
862
+ var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix;
863
+ throw new Error(value);
864
+ }
865
+
852
866
  // @ts-nocheck
853
867
  // We're inlining qss here for compression's sake, but we've included it as a hard dependency for the MIT license it requires.
854
868
  function encode(obj, pfx) {
@@ -902,7 +916,7 @@ function decode(str) {
902
916
  return out;
903
917
  }
904
918
 
905
- const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId) {
919
+ const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId, parentPath) {
906
920
  if (options === void 0) {
907
921
  options = {};
908
922
  }
@@ -913,8 +927,6 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
913
927
 
914
928
  if (isRoot) {
915
929
  options.path = rootRouteId;
916
- } else {
917
- warning(!options.path, 'Routes must have a path property.');
918
930
  } // Strip the root from parentIds
919
931
 
920
932
 
@@ -922,13 +934,14 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
922
934
  parentId = '';
923
935
  }
924
936
 
925
- let path = String(isRoot ? rootRouteId : options.path); // If the path is anything other than an index path, trim it up
937
+ let path = isRoot ? rootRouteId : options.path; // If the path is anything other than an index path, trim it up
926
938
 
927
- if (path !== '/') {
939
+ if (path && path !== '/') {
928
940
  path = trimPath(path);
929
941
  }
930
942
 
931
- let id = joinPaths([parentId, path]);
943
+ const routeId = path || options.id;
944
+ let id = joinPaths([parentId, routeId]);
932
945
 
933
946
  if (path === rootRouteId) {
934
947
  path = '/';
@@ -938,14 +951,15 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
938
951
  id = joinPaths(['/', id]);
939
952
  }
940
953
 
941
- const fullPath = id === rootRouteId ? '/' : trimPathRight(id);
954
+ const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parentPath, path]));
942
955
  return {
943
956
  id: id,
957
+ routeId: routeId,
944
958
  path: path,
945
959
  fullPath: fullPath,
946
960
  options: options,
947
961
  children,
948
- addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id)), false, parentId)
962
+ addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id, fullPath)), false, parentId, parentPath)
949
963
  };
950
964
  };
951
965
  const rootRouteId = '__root__';
@@ -1321,55 +1335,76 @@ function createRouter(userOptions) {
1321
1335
 
1322
1336
  const existingMatches = [...router.state.matches, ...((_router$state$pending3 = (_router$state$pending4 = router.state.pending) == null ? void 0 : _router$state$pending4.matches) != null ? _router$state$pending3 : [])];
1323
1337
 
1324
- const recurse = async (routes, parentMatch) => {
1325
- var _parentMatch$params, _router$options$filte, _router$preloadCache$, _route$childRoutes2;
1338
+ const recurse = async routes => {
1339
+ var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1326
1340
 
1341
+ const parentMatch = last(matches);
1327
1342
  let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
1328
1343
  const filteredRoutes = (_router$options$filte = router.options.filterRoutes == null ? void 0 : router.options.filterRoutes(routes)) != null ? _router$options$filte : routes;
1329
- const route = filteredRoutes == null ? void 0 : filteredRoutes.find(route => {
1330
- var _route$childRoutes, _route$options$caseSe;
1331
-
1332
- const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length);
1333
- const matchParams = matchPathname(pathname, {
1334
- to: route.fullPath,
1335
- fuzzy,
1336
- caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1337
- });
1344
+ let foundRoutes = [];
1338
1345
 
1339
- if (matchParams) {
1340
- let parsedParams;
1346
+ const findMatchInRoutes = (parentRoutes, routes) => {
1347
+ routes.some(route => {
1348
+ var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
1341
1349
 
1342
- try {
1343
- var _route$options$parseP;
1350
+ if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
1351
+ return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
1352
+ }
1344
1353
 
1345
- parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1346
- } catch (err) {
1347
- if (opts != null && opts.strictParseParams) {
1348
- throw err;
1354
+ const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length);
1355
+ const matchParams = matchPathname(pathname, {
1356
+ to: route.fullPath,
1357
+ fuzzy,
1358
+ caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1359
+ });
1360
+
1361
+ if (matchParams) {
1362
+ let parsedParams;
1363
+
1364
+ try {
1365
+ var _route$options$parseP;
1366
+
1367
+ parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1368
+ } catch (err) {
1369
+ if (opts != null && opts.strictParseParams) {
1370
+ throw err;
1371
+ }
1349
1372
  }
1373
+
1374
+ params = _extends$1({}, params, parsedParams);
1350
1375
  }
1351
1376
 
1352
- params = _extends$1({}, params, parsedParams);
1353
- }
1377
+ if (!!matchParams) {
1378
+ foundRoutes = [...parentRoutes, route];
1379
+ }
1354
1380
 
1355
- return !!matchParams;
1356
- });
1381
+ return !!foundRoutes.length;
1382
+ });
1383
+ return !!foundRoutes.length;
1384
+ };
1357
1385
 
1358
- if (!route) {
1386
+ findMatchInRoutes([], filteredRoutes);
1387
+
1388
+ if (!foundRoutes.length) {
1359
1389
  return;
1360
1390
  }
1361
1391
 
1362
- const interpolatedPath = interpolatePath(route.routePath, params);
1363
- const matchId = interpolatePath(route.routeId, params, true);
1364
- const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, route, {
1365
- matchId,
1366
- params,
1367
- pathname: joinPaths([pathname, interpolatedPath])
1392
+ foundRoutes.forEach(foundRoute => {
1393
+ var _router$preloadCache$;
1394
+
1395
+ const interpolatedPath = interpolatePath(foundRoute.routePath, params);
1396
+ const matchId = interpolatePath(foundRoute.routeId, params, true);
1397
+ const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, foundRoute, {
1398
+ matchId,
1399
+ params,
1400
+ pathname: joinPaths([pathname, interpolatedPath])
1401
+ });
1402
+ matches.push(match);
1368
1403
  });
1369
- matches.push(match);
1404
+ const foundRoute = last(foundRoutes);
1370
1405
 
1371
- if ((_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length) {
1372
- recurse(route.childRoutes, match);
1406
+ if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
1407
+ recurse(foundRoute.childRoutes);
1373
1408
  }
1374
1409
  };
1375
1410
 
@@ -1473,14 +1508,7 @@ function createRouter(userOptions) {
1473
1508
  isExternal = true;
1474
1509
  } catch (e) {}
1475
1510
 
1476
- if (isExternal) {
1477
- if (process.env.NODE_ENV !== 'production') {
1478
- throw new Error('Attempting to navigate to external url with router.navigate!');
1479
- }
1480
-
1481
- return;
1482
- }
1483
-
1511
+ invariant(!isExternal, 'Attempting to navigate to external url with router.navigate!');
1484
1512
  return router._navigate({
1485
1513
  from: fromString,
1486
1514
  to: toString,
@@ -1635,13 +1663,14 @@ function createRoute(routeConfig, options, parent, router) {
1635
1663
  // ]).replace(new RegExp(`^${rootRouteId}`), '')
1636
1664
  // ) as TRouteInfo['id']
1637
1665
  const {
1638
- id: routeId,
1666
+ id,
1667
+ routeId,
1639
1668
  path: routePath,
1640
1669
  fullPath
1641
1670
  } = routeConfig;
1642
1671
 
1643
- const action = router.state.actions[routeId] || (() => {
1644
- router.state.actions[routeId] = {
1672
+ const action = router.state.actions[id] || (() => {
1673
+ router.state.actions[id] = {
1645
1674
  pending: [],
1646
1675
  submit: async (submission, actionOpts) => {
1647
1676
  var _actionOpts$invalidat;
@@ -1692,11 +1721,12 @@ function createRoute(routeConfig, options, parent, router) {
1692
1721
  }
1693
1722
  }
1694
1723
  };
1695
- return router.state.actions[routeId];
1724
+ return router.state.actions[id];
1696
1725
  })();
1697
1726
 
1698
1727
  let route = {
1699
- routeId,
1728
+ routeId: id,
1729
+ routeRouteId: routeId,
1700
1730
  routePath,
1701
1731
  fullPath,
1702
1732
  options,
@@ -2317,6 +2347,7 @@ exports.createRouter = createRouter;
2317
2347
  exports.defaultParseSearch = defaultParseSearch;
2318
2348
  exports.defaultStringifySearch = defaultStringifySearch;
2319
2349
  exports.functionalUpdate = functionalUpdate;
2350
+ exports.invariant = invariant;
2320
2351
  exports.last = last;
2321
2352
  exports.matchByPath = matchByPath;
2322
2353
  exports.matchPathname = matchPathname;