@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.
@@ -835,6 +835,20 @@ function parsePath(path) {
835
835
  return parsedPath;
836
836
  }
837
837
 
838
+ var isProduction = process.env.NODE_ENV === 'production';
839
+ var prefix = 'Invariant failed';
840
+ function invariant(condition, message) {
841
+ if (condition) {
842
+ return;
843
+ }
844
+ if (isProduction) {
845
+ throw new Error(prefix);
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
+
838
852
  // @ts-nocheck
839
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.
840
854
  function encode(obj, pfx) {
@@ -888,7 +902,7 @@ function decode(str) {
888
902
  return out;
889
903
  }
890
904
 
891
- const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId) {
905
+ const createRouteConfig = function createRouteConfig(options, children, isRoot, parentId, parentPath) {
892
906
  if (options === void 0) {
893
907
  options = {};
894
908
  }
@@ -899,8 +913,6 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
899
913
 
900
914
  if (isRoot) {
901
915
  options.path = rootRouteId;
902
- } else {
903
- warning(!options.path, 'Routes must have a path property.');
904
916
  } // Strip the root from parentIds
905
917
 
906
918
 
@@ -908,13 +920,14 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
908
920
  parentId = '';
909
921
  }
910
922
 
911
- 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
912
924
 
913
- if (path !== '/') {
925
+ if (path && path !== '/') {
914
926
  path = trimPath(path);
915
927
  }
916
928
 
917
- let id = joinPaths([parentId, path]);
929
+ const routeId = path || options.id;
930
+ let id = joinPaths([parentId, routeId]);
918
931
 
919
932
  if (path === rootRouteId) {
920
933
  path = '/';
@@ -924,14 +937,15 @@ const createRouteConfig = function createRouteConfig(options, children, isRoot,
924
937
  id = joinPaths(['/', id]);
925
938
  }
926
939
 
927
- const fullPath = id === rootRouteId ? '/' : trimPathRight(id);
940
+ const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parentPath, path]));
928
941
  return {
929
942
  id: id,
943
+ routeId: routeId,
930
944
  path: path,
931
945
  fullPath: fullPath,
932
946
  options: options,
933
947
  children,
934
- 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)
935
949
  };
936
950
  };
937
951
  const rootRouteId = '__root__';
@@ -954,6 +968,7 @@ function createRouter(userOptions) {
954
968
  let router = {
955
969
  options: originalOptions,
956
970
  listeners: [],
971
+ removeActionQueue: [],
957
972
  // Resolved after construction
958
973
  basepath: '',
959
974
  routeTree: undefined,
@@ -1077,12 +1092,14 @@ function createRouter(userOptions) {
1077
1092
  strictParseParams: true
1078
1093
  });
1079
1094
  const toMatches = router.matchRoutes(pathname);
1080
- 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
+
1081
1098
  let nextParams = ((_dest$params = dest.params) != null ? _dest$params : true) === true ? prevParams : functionalUpdate(dest.params, prevParams);
1082
1099
 
1083
1100
  if (nextParams) {
1084
1101
  toMatches.map(d => d.options.stringifyParams).filter(Boolean).forEach(fn => {
1085
- Object.assign(nextParams, fn(nextParams));
1102
+ Object.assign({}, nextParams, fn(nextParams));
1086
1103
  });
1087
1104
  }
1088
1105
 
@@ -1185,22 +1202,33 @@ function createRouter(userOptions) {
1185
1202
  router.startedLoadingAt = id;
1186
1203
 
1187
1204
  if (next) {
1205
+ // If the location.href has changed
1188
1206
  // Ingest the new location
1189
1207
  router.location = next;
1190
- } // Cancel any pending matches
1208
+ } // Clear out old actions
1191
1209
 
1192
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
1226
+
1193
1227
  router.cancelMatches(); // Match the routes
1194
1228
 
1195
1229
  const unloadedMatches = router.matchRoutes(location.pathname, {
1196
1230
  strictParseParams: true
1197
1231
  });
1198
- unloadedMatches.forEach((match, index) => {
1199
- const parent = unloadedMatches[index - 1];
1200
- const child = unloadedMatches[index + 1];
1201
- if (parent) match.__.setParentMatch(parent);
1202
- if (child) match.__.addChildMatch(child);
1203
- });
1204
1232
  router.state = _extends$1({}, router.state, {
1205
1233
  pending: {
1206
1234
  matches: unloadedMatches,
@@ -1307,59 +1335,81 @@ function createRouter(userOptions) {
1307
1335
 
1308
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 : [])];
1309
1337
 
1310
- const recurse = async (routes, parentMatch) => {
1311
- var _parentMatch$params, _router$options$filte, _router$preloadCache$, _route$childRoutes2;
1338
+ const recurse = async routes => {
1339
+ var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1312
1340
 
1341
+ const parentMatch = last(matches);
1313
1342
  let params = (_parentMatch$params = parentMatch == null ? void 0 : parentMatch.params) != null ? _parentMatch$params : {};
1314
1343
  const filteredRoutes = (_router$options$filte = router.options.filterRoutes == null ? void 0 : router.options.filterRoutes(routes)) != null ? _router$options$filte : routes;
1315
- const route = filteredRoutes == null ? void 0 : filteredRoutes.find(route => {
1316
- var _route$childRoutes, _route$options$caseSe;
1317
-
1318
- const fuzzy = !!(route.routePath !== '/' || (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length);
1319
- const matchParams = matchPathname(pathname, {
1320
- to: route.fullPath,
1321
- fuzzy,
1322
- caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1323
- });
1344
+ let foundRoutes = [];
1324
1345
 
1325
- if (matchParams) {
1326
- let parsedParams;
1346
+ const findMatchInRoutes = (parentRoutes, routes) => {
1347
+ routes.some(route => {
1348
+ var _route$childRoutes, _route$childRoutes2, _route$options$caseSe;
1327
1349
 
1328
- try {
1329
- var _route$options$parseP;
1350
+ if (!route.routePath && (_route$childRoutes = route.childRoutes) != null && _route$childRoutes.length) {
1351
+ return findMatchInRoutes([...foundRoutes, route], route.childRoutes);
1352
+ }
1330
1353
 
1331
- parsedParams = (_route$options$parseP = route.options.parseParams == null ? void 0 : route.options.parseParams(matchParams)) != null ? _route$options$parseP : matchParams;
1332
- } catch (err) {
1333
- if (opts != null && opts.strictParseParams) {
1334
- 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
+ }
1335
1372
  }
1373
+
1374
+ params = _extends$1({}, params, parsedParams);
1336
1375
  }
1337
1376
 
1338
- params = _extends$1({}, params, parsedParams);
1339
- }
1377
+ if (!!matchParams) {
1378
+ foundRoutes = [...parentRoutes, route];
1379
+ }
1340
1380
 
1341
- return !!matchParams;
1342
- });
1381
+ return !!foundRoutes.length;
1382
+ });
1383
+ return !!foundRoutes.length;
1384
+ };
1343
1385
 
1344
- if (!route) {
1386
+ findMatchInRoutes([], filteredRoutes);
1387
+
1388
+ if (!foundRoutes.length) {
1345
1389
  return;
1346
1390
  }
1347
1391
 
1348
- const interpolatedPath = interpolatePath(route.routePath, params);
1349
- const matchId = interpolatePath(route.routeId, params, true);
1350
- const match = existingMatches.find(d => d.matchId === matchId) || ((_router$preloadCache$ = router.preloadCache[matchId]) == null ? void 0 : _router$preloadCache$.match) || createRouteMatch(router, route, {
1351
- matchId,
1352
- params,
1353
- 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);
1354
1403
  });
1355
- matches.push(match);
1404
+ const foundRoute = last(foundRoutes);
1356
1405
 
1357
- if ((_route$childRoutes2 = route.childRoutes) != null && _route$childRoutes2.length) {
1358
- recurse(route.childRoutes, match);
1406
+ if ((_foundRoute$childRout = foundRoute.childRoutes) != null && _foundRoute$childRout.length) {
1407
+ recurse(foundRoute.childRoutes);
1359
1408
  }
1360
1409
  };
1361
1410
 
1362
1411
  recurse([router.routeTree]);
1412
+ cascadeLoaderData(matches);
1363
1413
  return matches;
1364
1414
  },
1365
1415
  loadMatches: async (resolvedMatches, loaderOpts) => {
@@ -1438,14 +1488,14 @@ function createRouter(userOptions) {
1438
1488
  const next = router.buildNext(location);
1439
1489
  return router.commitLocation(next, location.replace);
1440
1490
  },
1441
- navigate: async _ref => {
1491
+ navigate: async _ref2 => {
1442
1492
  let {
1443
1493
  from,
1444
1494
  to = '.',
1445
1495
  search,
1446
1496
  hash,
1447
1497
  replace
1448
- } = _ref;
1498
+ } = _ref2;
1449
1499
  // If this link simply reloads the current route,
1450
1500
  // make sure it has a new key so it will trigger a data refresh
1451
1501
  // If this `to` is a valid external URL, return
@@ -1459,14 +1509,7 @@ function createRouter(userOptions) {
1459
1509
  isExternal = true;
1460
1510
  } catch (e) {}
1461
1511
 
1462
- if (isExternal) {
1463
- if (process.env.NODE_ENV !== 'production') {
1464
- throw new Error('Attempting to navigate to external url with router.navigate!');
1465
- }
1466
-
1467
- return;
1468
- }
1469
-
1512
+ invariant(!isExternal, 'Attempting to navigate to external url with router.navigate!');
1470
1513
  return router._navigate({
1471
1514
  from: fromString,
1472
1515
  to: toString,
@@ -1474,8 +1517,8 @@ function createRouter(userOptions) {
1474
1517
  hash
1475
1518
  });
1476
1519
  },
1477
- buildLink: _ref2 => {
1478
- var _preload, _ref3, _ref4;
1520
+ buildLink: _ref3 => {
1521
+ var _preload, _ref4, _ref5;
1479
1522
 
1480
1523
  let {
1481
1524
  from,
@@ -1490,7 +1533,7 @@ function createRouter(userOptions) {
1490
1533
  preloadMaxAge: userPreloadMaxAge,
1491
1534
  preloadDelay: userPreloadDelay,
1492
1535
  disabled
1493
- } = _ref2;
1536
+ } = _ref3;
1494
1537
 
1495
1538
  // If this link simply reloads the current route,
1496
1539
  // make sure it has a new key so it will trigger a data refresh
@@ -1514,8 +1557,8 @@ function createRouter(userOptions) {
1514
1557
  };
1515
1558
  const next = router.buildNext(nextOpts);
1516
1559
  preload = (_preload = preload) != null ? _preload : router.options.defaultLinkPreload;
1517
- const preloadMaxAge = (_ref3 = userPreloadMaxAge != null ? userPreloadMaxAge : router.options.defaultLinkPreloadMaxAge) != null ? _ref3 : 2000;
1518
- 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
1519
1562
 
1520
1563
  const pathIsEqual = router.state.location.pathname === next.pathname;
1521
1564
  const currentPathSplit = router.state.location.pathname.split('/');
@@ -1589,11 +1632,11 @@ function createRouter(userOptions) {
1589
1632
  },
1590
1633
  __experimental__createSnapshot: () => {
1591
1634
  return _extends$1({}, router.state, {
1592
- matches: router.state.matches.map(_ref5 => {
1635
+ matches: router.state.matches.map(_ref6 => {
1593
1636
  let {
1594
1637
  routeLoaderData: loaderData,
1595
1638
  matchId
1596
- } = _ref5;
1639
+ } = _ref6;
1597
1640
  return {
1598
1641
  matchId,
1599
1642
  loaderData
@@ -1621,13 +1664,14 @@ function createRoute(routeConfig, options, parent, router) {
1621
1664
  // ]).replace(new RegExp(`^${rootRouteId}`), '')
1622
1665
  // ) as TRouteInfo['id']
1623
1666
  const {
1624
- id: routeId,
1667
+ id,
1668
+ routeId,
1625
1669
  path: routePath,
1626
1670
  fullPath
1627
1671
  } = routeConfig;
1628
1672
 
1629
- const action = router.state.actions[routeId] || (() => {
1630
- router.state.actions[routeId] = {
1673
+ const action = router.state.actions[id] || (() => {
1674
+ router.state.actions[id] = {
1631
1675
  pending: [],
1632
1676
  submit: async (submission, actionOpts) => {
1633
1677
  var _actionOpts$invalidat;
@@ -1642,10 +1686,12 @@ function createRoute(routeConfig, options, parent, router) {
1642
1686
  status: 'pending',
1643
1687
  submission
1644
1688
  };
1689
+ action.current = actionState;
1645
1690
  action.latest = actionState;
1646
1691
  action.pending.push(actionState);
1647
1692
  router.state = _extends$1({}, router.state, {
1648
- action: actionState
1693
+ currentAction: actionState,
1694
+ latestAction: actionState
1649
1695
  });
1650
1696
  router.notify();
1651
1697
 
@@ -1669,20 +1715,20 @@ function createRoute(routeConfig, options, parent, router) {
1669
1715
  actionState.status = 'error';
1670
1716
  } finally {
1671
1717
  action.pending = action.pending.filter(d => d !== actionState);
1672
-
1673
- if (actionState === router.state.action) {
1674
- router.state.action = undefined;
1675
- }
1676
-
1718
+ router.removeActionQueue.push({
1719
+ action,
1720
+ actionState
1721
+ });
1677
1722
  router.notify();
1678
1723
  }
1679
1724
  }
1680
1725
  };
1681
- return router.state.actions[routeId];
1726
+ return router.state.actions[id];
1682
1727
  })();
1683
1728
 
1684
1729
  let route = {
1685
- routeId,
1730
+ routeId: id,
1731
+ routeRouteId: routeId,
1686
1732
  routePath,
1687
1733
  fullPath,
1688
1734
  options,
@@ -1759,16 +1805,17 @@ function createRouteMatch(router, route, opts) {
1759
1805
  clearTimeout(routeMatch.__.pendingMinTimeout);
1760
1806
  delete routeMatch.__.pendingMinPromise;
1761
1807
  },
1762
- setParentMatch: parentMatch => {
1763
- routeMatch.parentMatch = parentMatch;
1764
- },
1765
- addChildMatch: childMatch => {
1766
- if (routeMatch.childMatches.find(d => d.matchId === childMatch.matchId)) {
1767
- return;
1768
- }
1769
-
1770
- routeMatch.childMatches.push(childMatch);
1771
- },
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
+ // },
1772
1819
  validate: () => {
1773
1820
  var _routeMatch$parentMat, _routeMatch$parentMat2;
1774
1821
 
@@ -1872,7 +1919,6 @@ function createRouteMatch(router, route, opts) {
1872
1919
  }
1873
1920
 
1874
1921
  routeMatch.routeLoaderData = replaceEqualDeep(routeMatch.routeLoaderData, data);
1875
- cascadeLoaderData(routeMatch);
1876
1922
  routeMatch.error = undefined;
1877
1923
  routeMatch.status = 'success';
1878
1924
  routeMatch.updatedAt = Date.now();
@@ -1932,16 +1978,14 @@ function createRouteMatch(router, route, opts) {
1932
1978
  return routeMatch;
1933
1979
  }
1934
1980
 
1935
- function cascadeLoaderData(routeMatch) {
1936
- if (routeMatch.parentMatch) {
1937
- routeMatch.loaderData = replaceEqualDeep(routeMatch.loaderData, _extends$1({}, routeMatch.parentMatch.loaderData, routeMatch.routeLoaderData));
1938
- }
1981
+ function cascadeLoaderData(matches) {
1982
+ matches.forEach((match, index) => {
1983
+ const parent = matches[index - 1];
1939
1984
 
1940
- if (routeMatch.childMatches.length) {
1941
- routeMatch.childMatches.forEach(childMatch => {
1942
- cascadeLoaderData(childMatch);
1943
- });
1944
- }
1985
+ if (parent) {
1986
+ match.loaderData = replaceEqualDeep(match.loaderData, _extends$1({}, parent.loaderData, match.routeLoaderData));
1987
+ }
1988
+ });
1945
1989
  }
1946
1990
 
1947
1991
  function matchPathname(currentPathname, matchLocation) {
@@ -2293,5 +2337,5 @@ function last(arr) {
2293
2337
  return arr[arr.length - 1];
2294
2338
  }
2295
2339
 
2296
- export { createBrowserHistory, createHashHistory, createMemoryHistory, createRoute, createRouteConfig, createRouteMatch, createRouter, defaultParseSearch, defaultStringifySearch, functionalUpdate, last, matchByPath, matchPathname, parsePathname, parseSearchWith, replaceEqualDeep, _resolvePath as resolvePath, rootRouteId, stringifySearchWith, warning };
2340
+ export { createBrowserHistory, createHashHistory, createMemoryHistory, createRoute, createRouteConfig, createRouteMatch, createRouter, defaultParseSearch, defaultStringifySearch, functionalUpdate, invariant, last, matchByPath, matchPathname, parsePathname, parseSearchWith, replaceEqualDeep, _resolvePath as resolvePath, rootRouteId, stringifySearchWith, warning };
2297
2341
  //# sourceMappingURL=index.js.map