@tanstack/router-core 0.0.1-beta.34 → 0.0.1-beta.36

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.
@@ -214,10 +214,10 @@ function matchPathname(basepath, currentPathname, matchLocation) {
214
214
  }
215
215
  function matchByPath(basepath, from, matchLocation) {
216
216
  var _matchLocation$to;
217
- if (basepath && !from.startsWith(basepath)) {
217
+ if (!from.startsWith(basepath)) {
218
218
  return undefined;
219
219
  }
220
- from = from.startsWith(basepath) ? from.substring(basepath.length) : from;
220
+ from = basepath != '/' ? from.substring(basepath.length) : from;
221
221
  const baseSegments = parsePathname(from);
222
222
  const to = "" + ((_matchLocation$to = matchLocation.to) != null ? _matchLocation$to : '*');
223
223
  const routeSegments = parsePathname(to);
@@ -377,7 +377,6 @@ function createRoute(routeConfig, options, parent, router) {
377
377
  actionState.status = 'success';
378
378
  return res;
379
379
  } catch (err) {
380
- console.log('tanner');
381
380
  console.error(err);
382
381
  actionState.error = err;
383
382
  actionState.status = 'error';
@@ -531,7 +530,7 @@ function createRouteMatch(router, route, opts) {
531
530
  validate: () => {
532
531
  var _routeMatch$parentMat, _routeMatch$parentMat2;
533
532
  // Validate the search params and stabilize them
534
- const parentSearch = (_routeMatch$parentMat = (_routeMatch$parentMat2 = routeMatch.parentMatch) == null ? void 0 : _routeMatch$parentMat2.search) != null ? _routeMatch$parentMat : router.__location.search;
533
+ const parentSearch = (_routeMatch$parentMat = (_routeMatch$parentMat2 = routeMatch.parentMatch) == null ? void 0 : _routeMatch$parentMat2.search) != null ? _routeMatch$parentMat : router.state.currentLocation.search;
535
534
  try {
536
535
  var _validator;
537
536
  const prevSearch = routeMatch.routeSearch;
@@ -583,7 +582,7 @@ function createRouteMatch(router, route, opts) {
583
582
  // If this is a preload, add it to the preload cache
584
583
  if (loaderOpts != null && loaderOpts.preload && minMaxAge > 0) {
585
584
  // If the match is currently active, don't preload it
586
- if (router.state.matches.find(d => d.matchId === routeMatch.matchId)) {
585
+ if (router.state.currentMatches.find(d => d.matchId === routeMatch.matchId)) {
587
586
  return;
588
587
  }
589
588
  router.matchCache[routeMatch.matchId] = {
@@ -737,8 +736,9 @@ const createDefaultHistory = () => isServer ? createMemoryHistory() : createBrow
737
736
  function getInitialRouterState() {
738
737
  return {
739
738
  status: 'idle',
740
- location: null,
741
- matches: [],
739
+ latestLocation: null,
740
+ currentLocation: null,
741
+ currentMatches: [],
742
742
  actions: {},
743
743
  loaders: {},
744
744
  lastUpdated: Date.now(),
@@ -769,7 +769,6 @@ function createRouter(userOptions) {
769
769
  basepath: '',
770
770
  routeTree: undefined,
771
771
  routesById: {},
772
- __location: undefined,
773
772
  //
774
773
  resolveNavigation: () => {},
775
774
  matchCache: {},
@@ -789,65 +788,53 @@ function createRouter(userOptions) {
789
788
  return router.routesById[id];
790
789
  },
791
790
  notify: () => {
792
- const isFetching = router.state.status === 'loading' || router.state.matches.some(d => d.isFetching);
793
- const isPreloading = Object.values(router.matchCache).some(d => d.match.isFetching && !router.state.matches.find(dd => dd.matchId === d.match.matchId));
791
+ const isFetching = router.state.status === 'loading' || router.state.currentMatches.some(d => d.isFetching);
792
+ const isPreloading = Object.values(router.matchCache).some(d => d.match.isFetching && !router.state.currentMatches.find(dd => dd.matchId === d.match.matchId));
794
793
  if (router.state.isFetching !== isFetching || router.state.isPreloading !== isPreloading) {
795
794
  router.state = _extends({}, router.state, {
796
795
  isFetching,
797
796
  isPreloading
798
797
  });
799
798
  }
800
- cascadeLoaderData(router.state.matches);
799
+ cascadeLoaderData(router.state.currentMatches);
801
800
  router.listeners.forEach(listener => listener(router));
802
801
  },
803
802
  dehydrate: () => {
804
803
  return {
805
- location: router.__location,
806
- state: _extends({}, pick(router.state, ['status', 'location', 'lastUpdated', 'location']), {
807
- matches: router.state.matches.map(match => pick(match, ['matchId', 'status', 'routeLoaderData', 'loaderData', 'isInvalid', 'invalidAt']))
804
+ state: _extends({}, pick(router.state, ['latestLocation', 'currentLocation', 'status', 'lastUpdated']), {
805
+ currentMatches: router.state.currentMatches.map(match => pick(match, ['matchId', 'status', 'routeLoaderData', 'loaderData', 'isInvalid', 'invalidAt']))
808
806
  }),
809
807
  context: router.options.context
810
808
  };
811
809
  },
812
810
  hydrate: dehydratedState => {
813
811
  // Update the location
814
- router.__location = dehydratedState.location;
812
+ router.state.latestLocation = dehydratedState.state.latestLocation;
813
+ router.state.currentLocation = dehydratedState.state.currentLocation;
815
814
 
816
815
  // Update the context
817
816
  router.options.context = dehydratedState.context;
818
817
 
819
818
  // Match the routes
820
- const matches = router.matchRoutes(router.__location.pathname, {
819
+ const currentMatches = router.matchRoutes(router.state.latestLocation.pathname, {
821
820
  strictParseParams: true
822
821
  });
823
- matches.forEach((match, index) => {
824
- const dehydratedMatch = dehydratedState.state.matches[index];
822
+ currentMatches.forEach((match, index) => {
823
+ const dehydratedMatch = dehydratedState.state.currentMatches[index];
825
824
  invariant(dehydratedMatch, 'Oh no! Dehydrated route matches did not match the active state of the router 😬');
826
825
  Object.assign(match, dehydratedMatch);
827
826
  });
828
- matches.forEach(match => match.__.validate());
827
+ currentMatches.forEach(match => match.__.validate());
829
828
  router.state = _extends({}, router.state, dehydratedState, {
830
- matches
829
+ currentMatches
831
830
  });
832
831
  },
833
832
  mount: () => {
834
- router.__.buildLocation({
835
- to: '.',
836
- search: true,
837
- hash: true
838
- });
839
-
840
- // If the current location isn't updated, trigger a navigation
841
- // to the current location. Otherwise, load the current location.
842
- // if (next.href !== router.__location.href) {
843
- // router.__.commitLocation(next, true)
844
- // }
845
-
846
- if (!router.state.matches.length) {
833
+ if (!router.state.currentMatches.length) {
847
834
  router.load();
848
835
  }
849
836
  const unsub = router.history.listen(event => {
850
- router.load(router.__.parseLocation(event.location, router.__location));
837
+ router.load(router.__.parseLocation(event.location, router.state.latestLocation));
851
838
  });
852
839
 
853
840
  // addEventListener does not exist in React Native, but window does
@@ -872,12 +859,12 @@ function createRouter(userOptions) {
872
859
  update: opts => {
873
860
  var _trimPath;
874
861
  const newHistory = (opts == null ? void 0 : opts.history) !== router.history;
875
- if (!router.__location || newHistory) {
862
+ if (!router.state.latestLocation || newHistory) {
876
863
  if (opts != null && opts.history) {
877
864
  router.history = opts.history;
878
865
  }
879
- router.__location = router.__.parseLocation(router.history.location);
880
- router.state.location = router.__location;
866
+ router.state.latestLocation = router.__.parseLocation(router.history.location);
867
+ router.state.currentLocation = router.state.latestLocation;
881
868
  }
882
869
  Object.assign(router.options, opts);
883
870
  const {
@@ -892,8 +879,8 @@ function createRouter(userOptions) {
892
879
  return router;
893
880
  },
894
881
  cancelMatches: () => {
895
- var _router$state$pending, _router$state$pending2;
896
- [...router.state.matches, ...((_router$state$pending = (_router$state$pending2 = router.state.pending) == null ? void 0 : _router$state$pending2.matches) != null ? _router$state$pending : [])].forEach(match => {
882
+ var _router$state$pending;
883
+ [...router.state.currentMatches, ...((_router$state$pending = router.state.pendingMatches) != null ? _router$state$pending : [])].forEach(match => {
897
884
  match.cancel();
898
885
  });
899
886
  },
@@ -902,53 +889,43 @@ function createRouter(userOptions) {
902
889
  router.startedLoadingAt = id;
903
890
  if (next) {
904
891
  // Ingest the new location
905
- router.__location = next;
892
+ router.state.latestLocation = next;
906
893
  }
907
894
 
908
895
  // Cancel any pending matches
909
896
  router.cancelMatches();
910
897
 
911
898
  // Match the routes
912
- const matches = router.matchRoutes(router.__location.pathname, {
899
+ const matches = router.matchRoutes(router.state.latestLocation.pathname, {
913
900
  strictParseParams: true
914
901
  });
915
902
  if (typeof document !== 'undefined') {
916
903
  router.state = _extends({}, router.state, {
917
- pending: {
918
- matches: matches,
919
- location: router.__location
920
- },
921
- status: 'loading'
904
+ status: 'loading',
905
+ pendingMatches: matches,
906
+ pendingLocation: router.state.latestLocation
922
907
  });
923
908
  } else {
924
909
  router.state = _extends({}, router.state, {
925
- matches: matches,
926
- location: router.__location,
927
- status: 'loading'
910
+ status: 'loading',
911
+ currentMatches: matches,
912
+ currentLocation: router.state.latestLocation
928
913
  });
929
914
  }
915
+ router.notify();
930
916
 
931
- // Check if each match middleware to see if the route can be accessed
917
+ // Load the matches
932
918
  try {
933
- await Promise.all(matches.map(match => match.options.beforeLoad == null ? void 0 : match.options.beforeLoad({
934
- router: router,
935
- match
936
- })));
919
+ await router.loadMatches(matches);
937
920
  } catch (err) {
938
- if (err != null && err.then) {
939
- await new Promise(() => {});
940
- }
941
- throw err;
921
+ console.log(err);
922
+ invariant(false, 'Matches failed to load due to error above ☝️. Navigation cancelled!');
942
923
  }
943
- router.notify();
944
-
945
- // Load the matches
946
- await router.loadMatches(matches);
947
924
  if (router.startedLoadingAt !== id) {
948
925
  // Ignore side-effects of match loading
949
926
  return router.navigationPromise;
950
927
  }
951
- const previousMatches = router.state.matches;
928
+ const previousMatches = router.state.currentMatches;
952
929
  const exiting = [],
953
930
  staying = [];
954
931
  previousMatches.forEach(d => {
@@ -1007,10 +984,11 @@ function createRouter(userOptions) {
1007
984
  }
1008
985
  });
1009
986
  router.state = _extends({}, router.state, {
1010
- location: router.__location,
1011
- matches,
1012
- pending: undefined,
1013
- status: 'idle'
987
+ status: 'idle',
988
+ currentLocation: router.state.latestLocation,
989
+ currentMatches: matches,
990
+ pendingLocation: undefined,
991
+ pendingMatches: undefined
1014
992
  });
1015
993
  router.notify();
1016
994
  router.resolveNavigation();
@@ -1036,7 +1014,7 @@ function createRouter(userOptions) {
1036
1014
  },
1037
1015
  loadRoute: async function loadRoute(navigateOpts) {
1038
1016
  if (navigateOpts === void 0) {
1039
- navigateOpts = router.__location;
1017
+ navigateOpts = router.state.latestLocation;
1040
1018
  }
1041
1019
  const next = router.buildNext(navigateOpts);
1042
1020
  const matches = router.matchRoutes(next.pathname, {
@@ -1048,7 +1026,7 @@ function createRouter(userOptions) {
1048
1026
  preloadRoute: async function preloadRoute(navigateOpts, loaderOpts) {
1049
1027
  var _ref3, _ref4, _loaderOpts$maxAge, _ref5, _ref6, _loaderOpts$gcMaxAge;
1050
1028
  if (navigateOpts === void 0) {
1051
- navigateOpts = router.__location;
1029
+ navigateOpts = router.state.latestLocation;
1052
1030
  }
1053
1031
  const next = router.buildNext(navigateOpts);
1054
1032
  const matches = router.matchRoutes(next.pathname, {
@@ -1062,13 +1040,13 @@ function createRouter(userOptions) {
1062
1040
  return matches;
1063
1041
  },
1064
1042
  matchRoutes: (pathname, opts) => {
1065
- var _router$state$pending3, _router$state$pending4;
1043
+ var _router$state$pending2;
1066
1044
  router.cleanMatchCache();
1067
1045
  const matches = [];
1068
1046
  if (!router.routeTree) {
1069
1047
  return matches;
1070
1048
  }
1071
- 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 : [])];
1049
+ const existingMatches = [...router.state.currentMatches, ...((_router$state$pending2 = router.state.pendingMatches) != null ? _router$state$pending2 : [])];
1072
1050
  const recurse = async routes => {
1073
1051
  var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1074
1052
  const parentMatch = last(matches);
@@ -1087,6 +1065,15 @@ function createRouter(userOptions) {
1087
1065
  fuzzy,
1088
1066
  caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1089
1067
  });
1068
+
1069
+ // console.log(
1070
+ // router.basepath,
1071
+ // route.fullPath,
1072
+ // fuzzy,
1073
+ // pathname,
1074
+ // matchParams,
1075
+ // )
1076
+
1090
1077
  if (matchParams) {
1091
1078
  let parsedParams;
1092
1079
  try {
@@ -1132,10 +1119,27 @@ function createRouter(userOptions) {
1132
1119
  return matches;
1133
1120
  },
1134
1121
  loadMatches: async (resolvedMatches, loaderOpts) => {
1135
- const matchPromises = resolvedMatches.map(async match => {
1136
- var _search$__data;
1122
+ resolvedMatches.forEach(async match => {
1137
1123
  // Validate the match (loads search params etc)
1138
1124
  match.__.validate();
1125
+ });
1126
+
1127
+ // Check each match middleware to see if the route can be accessed
1128
+ await Promise.all(resolvedMatches.map(async match => {
1129
+ try {
1130
+ await (match.options.beforeLoad == null ? void 0 : match.options.beforeLoad({
1131
+ router: router,
1132
+ match
1133
+ }));
1134
+ } catch (err) {
1135
+ if (!(loaderOpts != null && loaderOpts.preload)) {
1136
+ match.options.onLoadError == null ? void 0 : match.options.onLoadError(err);
1137
+ }
1138
+ throw err;
1139
+ }
1140
+ }));
1141
+ const matchPromises = resolvedMatches.map(async match => {
1142
+ var _search$__data;
1139
1143
  const search = match.search;
1140
1144
  if ((_search$__data = search.__data) != null && _search$__data.matchId && search.__data.matchId !== match.matchId) {
1141
1145
  return;
@@ -1190,10 +1194,10 @@ function createRouter(userOptions) {
1190
1194
  }
1191
1195
  },
1192
1196
  invalidateRoute: opts => {
1193
- var _router$state$pending5, _router$state$pending6;
1197
+ var _router$state$pending3;
1194
1198
  const next = router.buildNext(opts);
1195
1199
  const unloadedMatchIds = router.matchRoutes(next.pathname).map(d => d.matchId);
1196
- [...router.state.matches, ...((_router$state$pending5 = (_router$state$pending6 = router.state.pending) == null ? void 0 : _router$state$pending6.matches) != null ? _router$state$pending5 : [])].forEach(match => {
1200
+ [...router.state.currentMatches, ...((_router$state$pending3 = router.state.pendingMatches) != null ? _router$state$pending3 : [])].forEach(match => {
1197
1201
  if (unloadedMatchIds.includes(match.matchId)) {
1198
1202
  match.invalidate();
1199
1203
  }
@@ -1216,15 +1220,14 @@ function createRouter(userOptions) {
1216
1220
  });
1217
1221
  const next = router.buildNext(location);
1218
1222
  if (opts != null && opts.pending) {
1219
- var _router$state$pending7;
1220
- if (!((_router$state$pending7 = router.state.pending) != null && _router$state$pending7.location)) {
1223
+ if (!router.state.pendingLocation) {
1221
1224
  return false;
1222
1225
  }
1223
- return !!matchPathname(router.basepath, router.state.pending.location.pathname, _extends({}, opts, {
1226
+ return !!matchPathname(router.basepath, router.state.pendingLocation.pathname, _extends({}, opts, {
1224
1227
  to: next.pathname
1225
1228
  }));
1226
1229
  }
1227
- return !!matchPathname(router.basepath, router.state.location.pathname, _extends({}, opts, {
1230
+ return !!matchPathname(router.basepath, router.state.currentLocation.pathname, _extends({}, opts, {
1228
1231
  to: next.pathname
1229
1232
  }));
1230
1233
  },
@@ -1302,11 +1305,11 @@ function createRouter(userOptions) {
1302
1305
  const preloadDelay = (_ref9 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultPreloadDelay) != null ? _ref9 : 0;
1303
1306
 
1304
1307
  // Compare path/hash for matches
1305
- const pathIsEqual = router.state.location.pathname === next.pathname;
1306
- const currentPathSplit = router.state.location.pathname.split('/');
1308
+ const pathIsEqual = router.state.currentLocation.pathname === next.pathname;
1309
+ const currentPathSplit = router.state.currentLocation.pathname.split('/');
1307
1310
  const nextPathSplit = next.pathname.split('/');
1308
1311
  const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
1309
- const hashIsEqual = router.state.location.hash === next.hash;
1312
+ const hashIsEqual = router.state.currentLocation.hash === next.hash;
1310
1313
  // Combine the matches based on user options
1311
1314
  const pathTest = activeOptions != null && activeOptions.exact ? pathIsEqual : pathIsFuzzyEqual;
1312
1315
  const hashTest = activeOptions != null && activeOptions.includeHash ? hashIsEqual : true;
@@ -1333,6 +1336,9 @@ function createRouter(userOptions) {
1333
1336
  router.preloadRoute(nextOpts, {
1334
1337
  maxAge: userPreloadMaxAge,
1335
1338
  gcMaxAge: userPreloadGcMaxAge
1339
+ }).catch(err => {
1340
+ console.log(err);
1341
+ console.warn('Error preloading route! ☝️');
1336
1342
  });
1337
1343
  }
1338
1344
  };
@@ -1347,6 +1353,9 @@ function createRouter(userOptions) {
1347
1353
  router.preloadRoute(nextOpts, {
1348
1354
  maxAge: userPreloadMaxAge,
1349
1355
  gcMaxAge: userPreloadGcMaxAge
1356
+ }).catch(err => {
1357
+ console.log(err);
1358
+ console.warn('Error preloading route! ☝️');
1350
1359
  });
1351
1360
  }, preloadDelay);
1352
1361
  }
@@ -1429,9 +1438,9 @@ function createRouter(userOptions) {
1429
1438
  if (dest === void 0) {
1430
1439
  dest = {};
1431
1440
  }
1432
- const fromPathname = dest.fromCurrent ? router.__location.pathname : (_dest$from = dest.from) != null ? _dest$from : router.__location.pathname;
1441
+ const fromPathname = dest.fromCurrent ? router.state.latestLocation.pathname : (_dest$from = dest.from) != null ? _dest$from : router.state.latestLocation.pathname;
1433
1442
  let pathname = resolvePath((_router$basepath = router.basepath) != null ? _router$basepath : '/', fromPathname, "" + ((_dest$to = dest.to) != null ? _dest$to : '.'));
1434
- const fromMatches = router.matchRoutes(router.__location.pathname, {
1443
+ const fromMatches = router.matchRoutes(router.state.latestLocation.pathname, {
1435
1444
  strictParseParams: true
1436
1445
  });
1437
1446
  const toMatches = router.matchRoutes(pathname);
@@ -1445,7 +1454,7 @@ function createRouter(userOptions) {
1445
1454
  pathname = interpolatePath(pathname, nextParams != null ? nextParams : {});
1446
1455
 
1447
1456
  // Pre filters first
1448
- const preFilteredSearch = (_dest$__preSearchFilt = dest.__preSearchFilters) != null && _dest$__preSearchFilt.length ? dest.__preSearchFilters.reduce((prev, next) => next(prev), router.__location.search) : router.__location.search;
1457
+ const preFilteredSearch = (_dest$__preSearchFilt = dest.__preSearchFilters) != null && _dest$__preSearchFilt.length ? dest.__preSearchFilters.reduce((prev, next) => next(prev), router.state.latestLocation.search) : router.state.latestLocation.search;
1449
1458
 
1450
1459
  // Then the link/navigate function
1451
1460
  const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
@@ -1455,15 +1464,15 @@ function createRouter(userOptions) {
1455
1464
 
1456
1465
  // Then post filters
1457
1466
  const postFilteredSearch = (_dest$__postSearchFil = dest.__postSearchFilters) != null && _dest$__postSearchFil.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
1458
- const search = replaceEqualDeep(router.__location.search, postFilteredSearch);
1467
+ const search = replaceEqualDeep(router.state.latestLocation.search, postFilteredSearch);
1459
1468
  const searchStr = router.options.stringifySearch(search);
1460
- let hash = dest.hash === true ? router.__location.hash : functionalUpdate(dest.hash, router.__location.hash);
1469
+ let hash = dest.hash === true ? router.state.latestLocation.hash : functionalUpdate(dest.hash, router.state.latestLocation.hash);
1461
1470
  hash = hash ? "#" + hash : '';
1462
1471
  return {
1463
1472
  pathname,
1464
1473
  search,
1465
1474
  searchStr,
1466
- state: router.__location.state,
1475
+ state: router.state.latestLocation.state,
1467
1476
  hash,
1468
1477
  href: "" + pathname + searchStr + hash,
1469
1478
  key: dest.key