@tanstack/react-router 0.0.1-alpha.3 → 0.0.1-alpha.5

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__';
@@ -968,6 +982,7 @@ function createRouter(userOptions) {
968
982
  let router = {
969
983
  options: originalOptions,
970
984
  listeners: [],
985
+ removeActionQueue: [],
971
986
  // Resolved after construction
972
987
  basepath: '',
973
988
  routeTree: undefined,
@@ -1091,12 +1106,14 @@ function createRouter(userOptions) {
1091
1106
  strictParseParams: true
1092
1107
  });
1093
1108
  const toMatches = router.matchRoutes(pathname);
1094
- const prevParams = (_last = last(fromMatches)) == null ? void 0 : _last.params;
1109
+
1110
+ const prevParams = _extends$1({}, (_last = last(fromMatches)) == null ? void 0 : _last.params);
1111
+
1095
1112
  let nextParams = ((_dest$params = dest.params) != null ? _dest$params : true) === true ? prevParams : functionalUpdate(dest.params, prevParams);
1096
1113
 
1097
1114
  if (nextParams) {
1098
1115
  toMatches.map(d => d.options.stringifyParams).filter(Boolean).forEach(fn => {
1099
- Object.assign(nextParams, fn(nextParams));
1116
+ Object.assign({}, nextParams, fn(nextParams));
1100
1117
  });
1101
1118
  }
1102
1119
 
@@ -1199,22 +1216,33 @@ function createRouter(userOptions) {
1199
1216
  router.startedLoadingAt = id;
1200
1217
 
1201
1218
  if (next) {
1219
+ // If the location.href has changed
1202
1220
  // Ingest the new location
1203
1221
  router.location = next;
1204
- } // Cancel any pending matches
1222
+ } // Clear out old actions
1205
1223
 
1206
1224
 
1225
+ router.removeActionQueue.forEach(_ref => {
1226
+ let {
1227
+ action,
1228
+ actionState
1229
+ } = _ref;
1230
+
1231
+ if (router.state.currentAction === actionState) {
1232
+ router.state.currentAction = undefined;
1233
+ }
1234
+
1235
+ if (action.current === actionState) {
1236
+ action.current = undefined;
1237
+ }
1238
+ });
1239
+ router.removeActionQueue = []; // Cancel any pending matches
1240
+
1207
1241
  router.cancelMatches(); // Match the routes
1208
1242
 
1209
1243
  const unloadedMatches = router.matchRoutes(location.pathname, {
1210
1244
  strictParseParams: true
1211
1245
  });
1212
- unloadedMatches.forEach((match, index) => {
1213
- const parent = unloadedMatches[index - 1];
1214
- const child = unloadedMatches[index + 1];
1215
- if (parent) match.__.setParentMatch(parent);
1216
- if (child) match.__.addChildMatch(child);
1217
- });
1218
1246
  router.state = _extends$1({}, router.state, {
1219
1247
  pending: {
1220
1248
  matches: unloadedMatches,
@@ -1321,59 +1349,81 @@ function createRouter(userOptions) {
1321
1349
 
1322
1350
  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
1351
 
1324
- const recurse = async (routes, parentMatch) => {
1325
- var _parentMatch$params, _router$options$filte, _router$preloadCache$, _route$childRoutes2;
1352
+ const recurse = async routes => {
1353
+ var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1326
1354
 
1355
+ const parentMatch = last(matches);
1327
1356
  let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
1328
1357
  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
- });
1358
+ let foundRoutes = [];
1338
1359
 
1339
- if (matchParams) {
1340
- let parsedParams;
1360
+ const findMatchInRoutes = (parentRoutes, routes) => {
1361
+ routes.some(route => {
1362
+ var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
1341
1363
 
1342
- try {
1343
- var _route$options$parseP;
1364
+ if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
1365
+ return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
1366
+ }
1344
1367
 
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;
1368
+ const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length);
1369
+ const matchParams = matchPathname(pathname, {
1370
+ to: route.fullPath,
1371
+ fuzzy,
1372
+ caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1373
+ });
1374
+
1375
+ if (matchParams) {
1376
+ let parsedParams;
1377
+
1378
+ try {
1379
+ var _route$options$parseP;
1380
+
1381
+ parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1382
+ } catch (err) {
1383
+ if (opts != null && opts.strictParseParams) {
1384
+ throw err;
1385
+ }
1349
1386
  }
1387
+
1388
+ params = _extends$1({}, params, parsedParams);
1350
1389
  }
1351
1390
 
1352
- params = _extends$1({}, params, parsedParams);
1353
- }
1391
+ if (!!matchParams) {
1392
+ foundRoutes = [...parentRoutes, route];
1393
+ }
1354
1394
 
1355
- return !!matchParams;
1356
- });
1395
+ return !!foundRoutes.length;
1396
+ });
1397
+ return !!foundRoutes.length;
1398
+ };
1357
1399
 
1358
- if (!route) {
1400
+ findMatchInRoutes([], filteredRoutes);
1401
+
1402
+ if (!foundRoutes.length) {
1359
1403
  return;
1360
1404
  }
1361
1405
 
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])
1406
+ foundRoutes.forEach(foundRoute => {
1407
+ var _router$preloadCache$;
1408
+
1409
+ const interpolatedPath = interpolatePath(foundRoute.routePath, params);
1410
+ const matchId = interpolatePath(foundRoute.routeId, params, true);
1411
+ const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, foundRoute, {
1412
+ matchId,
1413
+ params,
1414
+ pathname: joinPaths([pathname, interpolatedPath])
1415
+ });
1416
+ matches.push(match);
1368
1417
  });
1369
- matches.push(match);
1418
+ const foundRoute = last(foundRoutes);
1370
1419
 
1371
- if ((_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length) {
1372
- recurse(route.childRoutes, match);
1420
+ if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
1421
+ recurse(foundRoute.childRoutes);
1373
1422
  }
1374
1423
  };
1375
1424
 
1376
1425
  recurse([router.routeTree]);
1426
+ cascadeLoaderData(matches);
1377
1427
  return matches;
1378
1428
  },
1379
1429
  loadMatches: async (resolvedMatches, loaderOpts) => {
@@ -1452,14 +1502,14 @@ function createRouter(userOptions) {
1452
1502
  const next = router.buildNext(location);
1453
1503
  return router.commitLocation(next, location.replace);
1454
1504
  },
1455
- navigate: async _ref => {
1505
+ navigate: async _ref2 => {
1456
1506
  let {
1457
1507
  from,
1458
1508
  to = '.',
1459
1509
  search,
1460
1510
  hash,
1461
1511
  replace
1462
- } = _ref;
1512
+ } = _ref2;
1463
1513
  // If this link simply reloads the current route,
1464
1514
  // make sure it has a new key so it will trigger a data refresh
1465
1515
  // If this `to` is a valid external URL, return
@@ -1473,14 +1523,7 @@ function createRouter(userOptions) {
1473
1523
  isExternal = true;
1474
1524
  } catch (e) {}
1475
1525
 
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
-
1526
+ invariant(!isExternal, 'Attempting to navigate to external url with router.navigate!');
1484
1527
  return router._navigate({
1485
1528
  from: fromString,
1486
1529
  to: toString,
@@ -1488,8 +1531,8 @@ function createRouter(userOptions) {
1488
1531
  hash
1489
1532
  });
1490
1533
  },
1491
- buildLink: _ref2 => {
1492
- var _preload, _ref3, _ref4;
1534
+ buildLink: _ref3 => {
1535
+ var _preload, _ref4, _ref5;
1493
1536
 
1494
1537
  let {
1495
1538
  from,
@@ -1504,7 +1547,7 @@ function createRouter(userOptions) {
1504
1547
  preloadMaxAge: userPreloadMaxAge,
1505
1548
  preloadDelay: userPreloadDelay,
1506
1549
  disabled
1507
- } = _ref2;
1550
+ } = _ref3;
1508
1551
 
1509
1552
  // If this link simply reloads the current route,
1510
1553
  // make sure it has a new key so it will trigger a data refresh
@@ -1528,8 +1571,8 @@ function createRouter(userOptions) {
1528
1571
  };
1529
1572
  const next = router.buildNext(nextOpts);
1530
1573
  preload = (_preload = preload) != null ? _preload : router.options.defaultLinkPreload;
1531
- const preloadMaxAge = (_ref3 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref3 : 2000;
1532
- const preloadDelay = (_ref4 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref4 : 50; // Compare path/hash for matches
1574
+ const preloadMaxAge = (_ref4 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref4 : 2000;
1575
+ const preloadDelay = (_ref5 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref5 : 50; // Compare path/hash for matches
1533
1576
 
1534
1577
  const pathIsEqual = router.state.location.pathname === next.pathname;
1535
1578
  const currentPathSplit = router.state.location.pathname.split('/');
@@ -1603,11 +1646,11 @@ function createRouter(userOptions) {
1603
1646
  },
1604
1647
  __experimental__createSnapshot: () => {
1605
1648
  return _extends$1({}, router.state, {
1606
- matches: router.state.matches.map(_ref5 => {
1649
+ matches: router.state.matches.map(_ref6 => {
1607
1650
  let {
1608
1651
  routeLoaderData: loaderData,
1609
1652
  matchId
1610
- } = _ref5;
1653
+ } = _ref6;
1611
1654
  return {
1612
1655
  matchId,
1613
1656
  loaderData
@@ -1635,13 +1678,14 @@ function createRoute(routeConfig, options, parent, router) {
1635
1678
  // ]).replace(new RegExp(`^${rootRouteId}`), '')
1636
1679
  // ) as TRouteInfo['id']
1637
1680
  const {
1638
- id: routeId,
1681
+ id,
1682
+ routeId,
1639
1683
  path: routePath,
1640
1684
  fullPath
1641
1685
  } = routeConfig;
1642
1686
 
1643
- const action = router.state.actions[routeId] || (() => {
1644
- router.state.actions[routeId] = {
1687
+ const action = router.state.actions[id] || (() => {
1688
+ router.state.actions[id] = {
1645
1689
  pending: [],
1646
1690
  submit: async (submission, actionOpts) => {
1647
1691
  var _actionOpts$invalidat;
@@ -1656,10 +1700,12 @@ function createRoute(routeConfig, options, parent, router) {
1656
1700
  status: 'pending',
1657
1701
  submission
1658
1702
  };
1703
+ action.current = actionState;
1659
1704
  action.latest = actionState;
1660
1705
  action.pending.push(actionState);
1661
1706
  router.state = _extends$1({}, router.state, {
1662
- action: actionState
1707
+ currentAction: actionState,
1708
+ latestAction: actionState
1663
1709
  });
1664
1710
  router.notify();
1665
1711
 
@@ -1683,20 +1729,20 @@ function createRoute(routeConfig, options, parent, router) {
1683
1729
  actionState.status = 'error';
1684
1730
  } finally {
1685
1731
  action.pending = action.pending.filter(d => d !== actionState);
1686
-
1687
- if (actionState === router.state.action) {
1688
- router.state.action = undefined;
1689
- }
1690
-
1732
+ router.removeActionQueue.push({
1733
+ action,
1734
+ actionState
1735
+ });
1691
1736
  router.notify();
1692
1737
  }
1693
1738
  }
1694
1739
  };
1695
- return router.state.actions[routeId];
1740
+ return router.state.actions[id];
1696
1741
  })();
1697
1742
 
1698
1743
  let route = {
1699
- routeId,
1744
+ routeId: id,
1745
+ routeRouteId: routeId,
1700
1746
  routePath,
1701
1747
  fullPath,
1702
1748
  options,
@@ -1773,16 +1819,17 @@ function createRouteMatch(router, route, opts) {
1773
1819
  clearTimeout(routeMatch.__.pendingMinTimeout);
1774
1820
  delete routeMatch.__.pendingMinPromise;
1775
1821
  },
1776
- setParentMatch: parentMatch => {
1777
- routeMatch.parentMatch = parentMatch;
1778
- },
1779
- addChildMatch: childMatch => {
1780
- if (routeMatch.childMatches.find(d => d.matchId === childMatch.matchId)) {
1781
- return;
1782
- }
1783
-
1784
- routeMatch.childMatches.push(childMatch);
1785
- },
1822
+ // setParentMatch: (parentMatch?: RouteMatch) => {
1823
+ // routeMatch.parentMatch = parentMatch
1824
+ // },
1825
+ // addChildMatch: (childMatch: RouteMatch) => {
1826
+ // if (
1827
+ // routeMatch.childMatches.find((d) => d.matchId === childMatch.matchId)
1828
+ // ) {
1829
+ // return
1830
+ // }
1831
+ // routeMatch.childMatches.push(childMatch)
1832
+ // },
1786
1833
  validate: () => {
1787
1834
  var _routeMatch$parentMat, _routeMatch$parentMat2;
1788
1835
 
@@ -1886,7 +1933,6 @@ function createRouteMatch(router, route, opts) {
1886
1933
  }
1887
1934
 
1888
1935
  routeMatch.routeLoaderData = replaceEqualDeep(routeMatch.routeLoaderData, data);
1889
- cascadeLoaderData(routeMatch);
1890
1936
  routeMatch.error = undefined;
1891
1937
  routeMatch.status = 'success';
1892
1938
  routeMatch.updatedAt = Date.now();
@@ -1946,16 +1992,14 @@ function createRouteMatch(router, route, opts) {
1946
1992
  return routeMatch;
1947
1993
  }
1948
1994
 
1949
- function cascadeLoaderData(routeMatch) {
1950
- if (routeMatch.parentMatch) {
1951
- routeMatch.loaderData = replaceEqualDeep(routeMatch.loaderData, _extends$1({}, routeMatch.parentMatch.loaderData, routeMatch.routeLoaderData));
1952
- }
1995
+ function cascadeLoaderData(matches) {
1996
+ matches.forEach((match, index) => {
1997
+ const parent = matches[index - 1];
1953
1998
 
1954
- if (routeMatch.childMatches.length) {
1955
- routeMatch.childMatches.forEach(childMatch => {
1956
- cascadeLoaderData(childMatch);
1957
- });
1958
- }
1999
+ if (parent) {
2000
+ match.loaderData = replaceEqualDeep(match.loaderData, _extends$1({}, parent.loaderData, match.routeLoaderData));
2001
+ }
2002
+ });
1959
2003
  }
1960
2004
 
1961
2005
  function matchPathname(currentPathname, matchLocation) {
@@ -2317,6 +2361,7 @@ exports.createRouter = createRouter;
2317
2361
  exports.defaultParseSearch = defaultParseSearch;
2318
2362
  exports.defaultStringifySearch = defaultStringifySearch;
2319
2363
  exports.functionalUpdate = functionalUpdate;
2364
+ exports.invariant = invariant;
2320
2365
  exports.last = last;
2321
2366
  exports.matchByPath = matchByPath;
2322
2367
  exports.matchPathname = matchPathname;