@tanstack/router-core 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.
@@ -839,6 +839,16 @@
839
839
  return parsedPath;
840
840
  }
841
841
 
842
+ var prefix = 'Invariant failed';
843
+ function invariant(condition, message) {
844
+ if (condition) {
845
+ return;
846
+ }
847
+ var provided = typeof message === 'function' ? message() : message;
848
+ var value = provided ? "".concat(prefix, ": ").concat(provided) : prefix;
849
+ throw new Error(value);
850
+ }
851
+
842
852
  // @ts-nocheck
843
853
  // We're inlining qss here for compression's sake, but we've included it as a hard dependency for the MIT license it requires.
844
854
  function encode(obj, pfx) {
@@ -892,7 +902,7 @@
892
902
  return out;
893
903
  }
894
904
 
895
- const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId) {
905
+ const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId, parentPath) {
896
906
  if (options === void 0) {
897
907
  options = {};
898
908
  }
@@ -903,8 +913,6 @@
903
913
 
904
914
  if (isRoot) {
905
915
  options.path = rootRouteId;
906
- } else {
907
- warning(!options.path, 'Routes must have a path property.');
908
916
  } // Strip the root from parentIds
909
917
 
910
918
 
@@ -912,13 +920,14 @@
912
920
  parentId = '';
913
921
  }
914
922
 
915
- let path = String(isRoot ? rootRouteId : options.path); // If the path is anything other than an index path, trim it up
923
+ let path = isRoot ? rootRouteId : options.path; // If the path is anything other than an index path, trim it up
916
924
 
917
- if (path !== '/') {
925
+ if (path && path !== '/') {
918
926
  path = trimPath(path);
919
927
  }
920
928
 
921
- let id = joinPaths([parentId, path]);
929
+ const routeId = path || options.id;
930
+ let id = joinPaths([parentId, routeId]);
922
931
 
923
932
  if (path === rootRouteId) {
924
933
  path = '/';
@@ -928,14 +937,15 @@
928
937
  id = joinPaths(['/', id]);
929
938
  }
930
939
 
931
- const fullPath = id === rootRouteId ? '/' : trimPathRight(id);
940
+ const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parentPath, path]));
932
941
  return {
933
942
  id: id,
943
+ routeId: routeId,
934
944
  path: path,
935
945
  fullPath: fullPath,
936
946
  options: options,
937
947
  children,
938
- addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id)), false, parentId)
948
+ addChildren: cb => createRouteConfig(options, cb(childOptions => createRouteConfig(childOptions, undefined, false, id, fullPath)), false, parentId, parentPath)
939
949
  };
940
950
  };
941
951
  const rootRouteId = '__root__';
@@ -958,6 +968,7 @@
958
968
  let router = {
959
969
  options: originalOptions,
960
970
  listeners: [],
971
+ removeActionQueue: [],
961
972
  // Resolved after construction
962
973
  basepath: '',
963
974
  routeTree: undefined,
@@ -1081,12 +1092,14 @@
1081
1092
  strictParseParams: true
1082
1093
  });
1083
1094
  const toMatches = router.matchRoutes(pathname);
1084
- const prevParams = (_last = last(fromMatches)) == null ? void 0 : _last.params;
1095
+
1096
+ const prevParams = _extends$1({}, (_last = last(fromMatches)) == null ? void 0 : _last.params);
1097
+
1085
1098
  let nextParams = ((_dest$params = dest.params) != null ? _dest$params : true) === true ? prevParams : functionalUpdate(dest.params, prevParams);
1086
1099
 
1087
1100
  if (nextParams) {
1088
1101
  toMatches.map(d => d.options.stringifyParams).filter(Boolean).forEach(fn => {
1089
- Object.assign(nextParams, fn(nextParams));
1102
+ Object.assign({}, nextParams, fn(nextParams));
1090
1103
  });
1091
1104
  }
1092
1105
 
@@ -1189,22 +1202,33 @@
1189
1202
  router.startedLoadingAt = id;
1190
1203
 
1191
1204
  if (next) {
1205
+ // If the location.href has changed
1192
1206
  // Ingest the new location
1193
1207
  router.location = next;
1194
- } // Cancel any pending matches
1208
+ } // Clear out old actions
1209
+
1195
1210
 
1211
+ router.removeActionQueue.forEach(_ref => {
1212
+ let {
1213
+ action,
1214
+ actionState
1215
+ } = _ref;
1216
+
1217
+ if (router.state.currentAction === actionState) {
1218
+ router.state.currentAction = undefined;
1219
+ }
1220
+
1221
+ if (action.current === actionState) {
1222
+ action.current = undefined;
1223
+ }
1224
+ });
1225
+ router.removeActionQueue = []; // Cancel any pending matches
1196
1226
 
1197
1227
  router.cancelMatches(); // Match the routes
1198
1228
 
1199
1229
  const unloadedMatches = router.matchRoutes(location.pathname, {
1200
1230
  strictParseParams: true
1201
1231
  });
1202
- unloadedMatches.forEach((match, index) => {
1203
- const parent = unloadedMatches[index - 1];
1204
- const child = unloadedMatches[index + 1];
1205
- if (parent) match.__.setParentMatch(parent);
1206
- if (child) match.__.addChildMatch(child);
1207
- });
1208
1232
  router.state = _extends$1({}, router.state, {
1209
1233
  pending: {
1210
1234
  matches: unloadedMatches,
@@ -1311,59 +1335,81 @@
1311
1335
 
1312
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 : [])];
1313
1337
 
1314
- const recurse = async (routes, parentMatch) => {
1315
- var _parentMatch$params, _router$options$filte, _router$preloadCache$, _route$childRoutes2;
1338
+ const recurse = async routes => {
1339
+ var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1316
1340
 
1341
+ const parentMatch = last(matches);
1317
1342
  let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
1318
1343
  const filteredRoutes = (_router$options$filte = router.options.filterRoutes == null ? void 0 : router.options.filterRoutes(routes)) != null ? _router$options$filte : routes;
1319
- const route = filteredRoutes == null ? void 0 : filteredRoutes.find(route => {
1320
- var _route$childRoutes, _route$options$caseSe;
1321
-
1322
- const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length);
1323
- const matchParams = matchPathname(pathname, {
1324
- to: route.fullPath,
1325
- fuzzy,
1326
- caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1327
- });
1344
+ let foundRoutes = [];
1328
1345
 
1329
- if (matchParams) {
1330
- let parsedParams;
1346
+ const findMatchInRoutes = (parentRoutes, routes) => {
1347
+ routes.some(route => {
1348
+ var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
1331
1349
 
1332
- try {
1333
- var _route$options$parseP;
1350
+ if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
1351
+ return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
1352
+ }
1334
1353
 
1335
- parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1336
- } catch (err) {
1337
- if (opts != null && opts.strictParseParams) {
1338
- 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
+ }
1339
1372
  }
1373
+
1374
+ params = _extends$1({}, params, parsedParams);
1340
1375
  }
1341
1376
 
1342
- params = _extends$1({}, params, parsedParams);
1343
- }
1377
+ if (!!matchParams) {
1378
+ foundRoutes = [...parentRoutes, route];
1379
+ }
1344
1380
 
1345
- return !!matchParams;
1346
- });
1381
+ return !!foundRoutes.length;
1382
+ });
1383
+ return !!foundRoutes.length;
1384
+ };
1347
1385
 
1348
- if (!route) {
1386
+ findMatchInRoutes([], filteredRoutes);
1387
+
1388
+ if (!foundRoutes.length) {
1349
1389
  return;
1350
1390
  }
1351
1391
 
1352
- const interpolatedPath = interpolatePath(route.routePath, params);
1353
- const matchId = interpolatePath(route.routeId, params, true);
1354
- const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, route, {
1355
- matchId,
1356
- params,
1357
- 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);
1358
1403
  });
1359
- matches.push(match);
1404
+ const foundRoute = last(foundRoutes);
1360
1405
 
1361
- if ((_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length) {
1362
- recurse(route.childRoutes, match);
1406
+ if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
1407
+ recurse(foundRoute.childRoutes);
1363
1408
  }
1364
1409
  };
1365
1410
 
1366
1411
  recurse([router.routeTree]);
1412
+ cascadeLoaderData(matches);
1367
1413
  return matches;
1368
1414
  },
1369
1415
  loadMatches: async (resolvedMatches, loaderOpts) => {
@@ -1442,14 +1488,14 @@
1442
1488
  const next = router.buildNext(location);
1443
1489
  return router.commitLocation(next, location.replace);
1444
1490
  },
1445
- navigate: async _ref => {
1491
+ navigate: async _ref2 => {
1446
1492
  let {
1447
1493
  from,
1448
1494
  to = '.',
1449
1495
  search,
1450
1496
  hash,
1451
1497
  replace
1452
- } = _ref;
1498
+ } = _ref2;
1453
1499
  // If this link simply reloads the current route,
1454
1500
  // make sure it has a new key so it will trigger a data refresh
1455
1501
  // If this `to` is a valid external URL, return
@@ -1463,12 +1509,7 @@
1463
1509
  isExternal = true;
1464
1510
  } catch (e) {}
1465
1511
 
1466
- if (isExternal) {
1467
- {
1468
- throw new Error('Attempting to navigate to external url with router.navigate!');
1469
- }
1470
- }
1471
-
1512
+ invariant(!isExternal, 'Attempting to navigate to external url with router.navigate!');
1472
1513
  return router._navigate({
1473
1514
  from: fromString,
1474
1515
  to: toString,
@@ -1476,8 +1517,8 @@
1476
1517
  hash
1477
1518
  });
1478
1519
  },
1479
- buildLink: _ref2 => {
1480
- var _preload, _ref3, _ref4;
1520
+ buildLink: _ref3 => {
1521
+ var _preload, _ref4, _ref5;
1481
1522
 
1482
1523
  let {
1483
1524
  from,
@@ -1492,7 +1533,7 @@
1492
1533
  preloadMaxAge: userPreloadMaxAge,
1493
1534
  preloadDelay: userPreloadDelay,
1494
1535
  disabled
1495
- } = _ref2;
1536
+ } = _ref3;
1496
1537
 
1497
1538
  // If this link simply reloads the current route,
1498
1539
  // make sure it has a new key so it will trigger a data refresh
@@ -1516,8 +1557,8 @@
1516
1557
  };
1517
1558
  const next = router.buildNext(nextOpts);
1518
1559
  preload = (_preload = preload) != null ? _preload : router.options.defaultLinkPreload;
1519
- const preloadMaxAge = (_ref3 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref3 : 2000;
1520
- const preloadDelay = (_ref4 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref4 : 50; // Compare path/hash for matches
1560
+ const preloadMaxAge = (_ref4 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref4 : 2000;
1561
+ const preloadDelay = (_ref5 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultLinkPreloadDelay) != null ? _ref5 : 50; // Compare path/hash for matches
1521
1562
 
1522
1563
  const pathIsEqual = router.state.location.pathname === next.pathname;
1523
1564
  const currentPathSplit = router.state.location.pathname.split('/');
@@ -1591,11 +1632,11 @@
1591
1632
  },
1592
1633
  __experimental__createSnapshot: () => {
1593
1634
  return _extends$1({}, router.state, {
1594
- matches: router.state.matches.map(_ref5 => {
1635
+ matches: router.state.matches.map(_ref6 => {
1595
1636
  let {
1596
1637
  routeLoaderData: loaderData,
1597
1638
  matchId
1598
- } = _ref5;
1639
+ } = _ref6;
1599
1640
  return {
1600
1641
  matchId,
1601
1642
  loaderData
@@ -1623,13 +1664,14 @@
1623
1664
  // ]).replace(new RegExp(`^${rootRouteId}`), '')
1624
1665
  // ) as TRouteInfo['id']
1625
1666
  const {
1626
- id: routeId,
1667
+ id,
1668
+ routeId,
1627
1669
  path: routePath,
1628
1670
  fullPath
1629
1671
  } = routeConfig;
1630
1672
 
1631
- const action = router.state.actions[routeId] || (() => {
1632
- router.state.actions[routeId] = {
1673
+ const action = router.state.actions[id] || (() => {
1674
+ router.state.actions[id] = {
1633
1675
  pending: [],
1634
1676
  submit: async (submission, actionOpts) => {
1635
1677
  var _actionOpts$invalidat;
@@ -1644,10 +1686,12 @@
1644
1686
  status: 'pending',
1645
1687
  submission
1646
1688
  };
1689
+ action.current = actionState;
1647
1690
  action.latest = actionState;
1648
1691
  action.pending.push(actionState);
1649
1692
  router.state = _extends$1({}, router.state, {
1650
- action: actionState
1693
+ currentAction: actionState,
1694
+ latestAction: actionState
1651
1695
  });
1652
1696
  router.notify();
1653
1697
 
@@ -1671,20 +1715,20 @@
1671
1715
  actionState.status = 'error';
1672
1716
  } finally {
1673
1717
  action.pending = action.pending.filter(d => d !== actionState);
1674
-
1675
- if (actionState === router.state.action) {
1676
- router.state.action = undefined;
1677
- }
1678
-
1718
+ router.removeActionQueue.push({
1719
+ action,
1720
+ actionState
1721
+ });
1679
1722
  router.notify();
1680
1723
  }
1681
1724
  }
1682
1725
  };
1683
- return router.state.actions[routeId];
1726
+ return router.state.actions[id];
1684
1727
  })();
1685
1728
 
1686
1729
  let route = {
1687
- routeId,
1730
+ routeId: id,
1731
+ routeRouteId: routeId,
1688
1732
  routePath,
1689
1733
  fullPath,
1690
1734
  options,
@@ -1761,16 +1805,17 @@
1761
1805
  clearTimeout(routeMatch.__.pendingMinTimeout);
1762
1806
  delete routeMatch.__.pendingMinPromise;
1763
1807
  },
1764
- setParentMatch: parentMatch => {
1765
- routeMatch.parentMatch = parentMatch;
1766
- },
1767
- addChildMatch: childMatch => {
1768
- if (routeMatch.childMatches.find(d => d.matchId === childMatch.matchId)) {
1769
- return;
1770
- }
1771
-
1772
- routeMatch.childMatches.push(childMatch);
1773
- },
1808
+ // setParentMatch: (parentMatch?: RouteMatch) => {
1809
+ // routeMatch.parentMatch = parentMatch
1810
+ // },
1811
+ // addChildMatch: (childMatch: RouteMatch) => {
1812
+ // if (
1813
+ // routeMatch.childMatches.find((d) => d.matchId === childMatch.matchId)
1814
+ // ) {
1815
+ // return
1816
+ // }
1817
+ // routeMatch.childMatches.push(childMatch)
1818
+ // },
1774
1819
  validate: () => {
1775
1820
  var _routeMatch$parentMat, _routeMatch$parentMat2;
1776
1821
 
@@ -1874,7 +1919,6 @@
1874
1919
  }
1875
1920
 
1876
1921
  routeMatch.routeLoaderData = replaceEqualDeep(routeMatch.routeLoaderData, data);
1877
- cascadeLoaderData(routeMatch);
1878
1922
  routeMatch.error = undefined;
1879
1923
  routeMatch.status = 'success';
1880
1924
  routeMatch.updatedAt = Date.now();
@@ -1934,16 +1978,14 @@
1934
1978
  return routeMatch;
1935
1979
  }
1936
1980
 
1937
- function cascadeLoaderData(routeMatch) {
1938
- if (routeMatch.parentMatch) {
1939
- routeMatch.loaderData = replaceEqualDeep(routeMatch.loaderData, _extends$1({}, routeMatch.parentMatch.loaderData, routeMatch.routeLoaderData));
1940
- }
1981
+ function cascadeLoaderData(matches) {
1982
+ matches.forEach((match, index) => {
1983
+ const parent = matches[index - 1];
1941
1984
 
1942
- if (routeMatch.childMatches.length) {
1943
- routeMatch.childMatches.forEach(childMatch => {
1944
- cascadeLoaderData(childMatch);
1945
- });
1946
- }
1985
+ if (parent) {
1986
+ match.loaderData = replaceEqualDeep(match.loaderData, _extends$1({}, parent.loaderData, match.routeLoaderData));
1987
+ }
1988
+ });
1947
1989
  }
1948
1990
 
1949
1991
  function matchPathname(currentPathname, matchLocation) {
@@ -2305,6 +2347,7 @@
2305
2347
  exports.defaultParseSearch = defaultParseSearch;
2306
2348
  exports.defaultStringifySearch = defaultStringifySearch;
2307
2349
  exports.functionalUpdate = functionalUpdate;
2350
+ exports.invariant = invariant;
2308
2351
  exports.last = last;
2309
2352
  exports.matchByPath = matchByPath;
2310
2353
  exports.matchPathname = matchPathname;