@tanstack/router-core 0.0.1-beta.35 → 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.
@@ -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,51 +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
- console.info(err);
939
- invariant(false, "A route's beforeLoad middleware failed! \uD83D\uDC46");
921
+ console.log(err);
922
+ invariant(false, 'Matches failed to load due to error above ☝️. Navigation cancelled!');
940
923
  }
941
- router.notify();
942
-
943
- // Load the matches
944
- await router.loadMatches(matches);
945
924
  if (router.startedLoadingAt !== id) {
946
925
  // Ignore side-effects of match loading
947
926
  return router.navigationPromise;
948
927
  }
949
- const previousMatches = router.state.matches;
928
+ const previousMatches = router.state.currentMatches;
950
929
  const exiting = [],
951
930
  staying = [];
952
931
  previousMatches.forEach(d => {
@@ -1005,10 +984,11 @@ function createRouter(userOptions) {
1005
984
  }
1006
985
  });
1007
986
  router.state = _extends({}, router.state, {
1008
- location: router.__location,
1009
- matches,
1010
- pending: undefined,
1011
- status: 'idle'
987
+ status: 'idle',
988
+ currentLocation: router.state.latestLocation,
989
+ currentMatches: matches,
990
+ pendingLocation: undefined,
991
+ pendingMatches: undefined
1012
992
  });
1013
993
  router.notify();
1014
994
  router.resolveNavigation();
@@ -1034,7 +1014,7 @@ function createRouter(userOptions) {
1034
1014
  },
1035
1015
  loadRoute: async function loadRoute(navigateOpts) {
1036
1016
  if (navigateOpts === void 0) {
1037
- navigateOpts = router.__location;
1017
+ navigateOpts = router.state.latestLocation;
1038
1018
  }
1039
1019
  const next = router.buildNext(navigateOpts);
1040
1020
  const matches = router.matchRoutes(next.pathname, {
@@ -1046,7 +1026,7 @@ function createRouter(userOptions) {
1046
1026
  preloadRoute: async function preloadRoute(navigateOpts, loaderOpts) {
1047
1027
  var _ref3, _ref4, _loaderOpts$maxAge, _ref5, _ref6, _loaderOpts$gcMaxAge;
1048
1028
  if (navigateOpts === void 0) {
1049
- navigateOpts = router.__location;
1029
+ navigateOpts = router.state.latestLocation;
1050
1030
  }
1051
1031
  const next = router.buildNext(navigateOpts);
1052
1032
  const matches = router.matchRoutes(next.pathname, {
@@ -1060,13 +1040,13 @@ function createRouter(userOptions) {
1060
1040
  return matches;
1061
1041
  },
1062
1042
  matchRoutes: (pathname, opts) => {
1063
- var _router$state$pending3, _router$state$pending4;
1043
+ var _router$state$pending2;
1064
1044
  router.cleanMatchCache();
1065
1045
  const matches = [];
1066
1046
  if (!router.routeTree) {
1067
1047
  return matches;
1068
1048
  }
1069
- 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 : [])];
1070
1050
  const recurse = async routes => {
1071
1051
  var _parentMatch$params, _router$options$filte, _foundRoute$childRout;
1072
1052
  const parentMatch = last(matches);
@@ -1085,7 +1065,15 @@ function createRouter(userOptions) {
1085
1065
  fuzzy,
1086
1066
  caseSensitive: (_route$options$caseSe = route.options.caseSensitive) != null ? _route$options$caseSe : router.options.caseSensitive
1087
1067
  });
1088
- console.log(router.basepath, route.fullPath, fuzzy, pathname, matchParams);
1068
+
1069
+ // console.log(
1070
+ // router.basepath,
1071
+ // route.fullPath,
1072
+ // fuzzy,
1073
+ // pathname,
1074
+ // matchParams,
1075
+ // )
1076
+
1089
1077
  if (matchParams) {
1090
1078
  let parsedParams;
1091
1079
  try {
@@ -1131,10 +1119,27 @@ function createRouter(userOptions) {
1131
1119
  return matches;
1132
1120
  },
1133
1121
  loadMatches: async (resolvedMatches, loaderOpts) => {
1134
- const matchPromises = resolvedMatches.map(async match => {
1135
- var _search$__data;
1122
+ resolvedMatches.forEach(async match => {
1136
1123
  // Validate the match (loads search params etc)
1137
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;
1138
1143
  const search = match.search;
1139
1144
  if ((_search$__data = search.__data) != null && _search$__data.matchId && search.__data.matchId !== match.matchId) {
1140
1145
  return;
@@ -1189,10 +1194,10 @@ function createRouter(userOptions) {
1189
1194
  }
1190
1195
  },
1191
1196
  invalidateRoute: opts => {
1192
- var _router$state$pending5, _router$state$pending6;
1197
+ var _router$state$pending3;
1193
1198
  const next = router.buildNext(opts);
1194
1199
  const unloadedMatchIds = router.matchRoutes(next.pathname).map(d => d.matchId);
1195
- [...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 => {
1196
1201
  if (unloadedMatchIds.includes(match.matchId)) {
1197
1202
  match.invalidate();
1198
1203
  }
@@ -1215,15 +1220,14 @@ function createRouter(userOptions) {
1215
1220
  });
1216
1221
  const next = router.buildNext(location);
1217
1222
  if (opts != null && opts.pending) {
1218
- var _router$state$pending7;
1219
- if (!((_router$state$pending7 = router.state.pending) != null && _router$state$pending7.location)) {
1223
+ if (!router.state.pendingLocation) {
1220
1224
  return false;
1221
1225
  }
1222
- return !!matchPathname(router.basepath, router.state.pending.location.pathname, _extends({}, opts, {
1226
+ return !!matchPathname(router.basepath, router.state.pendingLocation.pathname, _extends({}, opts, {
1223
1227
  to: next.pathname
1224
1228
  }));
1225
1229
  }
1226
- return !!matchPathname(router.basepath, router.state.location.pathname, _extends({}, opts, {
1230
+ return !!matchPathname(router.basepath, router.state.currentLocation.pathname, _extends({}, opts, {
1227
1231
  to: next.pathname
1228
1232
  }));
1229
1233
  },
@@ -1301,11 +1305,11 @@ function createRouter(userOptions) {
1301
1305
  const preloadDelay = (_ref9 = userPreloadDelay != null ? userPreloadDelay : router.options.defaultPreloadDelay) != null ? _ref9 : 0;
1302
1306
 
1303
1307
  // Compare path/hash for matches
1304
- const pathIsEqual = router.state.location.pathname === next.pathname;
1305
- const currentPathSplit = router.state.location.pathname.split('/');
1308
+ const pathIsEqual = router.state.currentLocation.pathname === next.pathname;
1309
+ const currentPathSplit = router.state.currentLocation.pathname.split('/');
1306
1310
  const nextPathSplit = next.pathname.split('/');
1307
1311
  const pathIsFuzzyEqual = nextPathSplit.every((d, i) => d === currentPathSplit[i]);
1308
- const hashIsEqual = router.state.location.hash === next.hash;
1312
+ const hashIsEqual = router.state.currentLocation.hash === next.hash;
1309
1313
  // Combine the matches based on user options
1310
1314
  const pathTest = activeOptions != null && activeOptions.exact ? pathIsEqual : pathIsFuzzyEqual;
1311
1315
  const hashTest = activeOptions != null && activeOptions.includeHash ? hashIsEqual : true;
@@ -1332,6 +1336,9 @@ function createRouter(userOptions) {
1332
1336
  router.preloadRoute(nextOpts, {
1333
1337
  maxAge: userPreloadMaxAge,
1334
1338
  gcMaxAge: userPreloadGcMaxAge
1339
+ }).catch(err => {
1340
+ console.log(err);
1341
+ console.warn('Error preloading route! ☝️');
1335
1342
  });
1336
1343
  }
1337
1344
  };
@@ -1346,6 +1353,9 @@ function createRouter(userOptions) {
1346
1353
  router.preloadRoute(nextOpts, {
1347
1354
  maxAge: userPreloadMaxAge,
1348
1355
  gcMaxAge: userPreloadGcMaxAge
1356
+ }).catch(err => {
1357
+ console.log(err);
1358
+ console.warn('Error preloading route! ☝️');
1349
1359
  });
1350
1360
  }, preloadDelay);
1351
1361
  }
@@ -1428,9 +1438,9 @@ function createRouter(userOptions) {
1428
1438
  if (dest === void 0) {
1429
1439
  dest = {};
1430
1440
  }
1431
- 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;
1432
1442
  let pathname = resolvePath((_router$basepath = router.basepath) != null ? _router$basepath : '/', fromPathname, "" + ((_dest$to = dest.to) != null ? _dest$to : '.'));
1433
- const fromMatches = router.matchRoutes(router.__location.pathname, {
1443
+ const fromMatches = router.matchRoutes(router.state.latestLocation.pathname, {
1434
1444
  strictParseParams: true
1435
1445
  });
1436
1446
  const toMatches = router.matchRoutes(pathname);
@@ -1444,7 +1454,7 @@ function createRouter(userOptions) {
1444
1454
  pathname = interpolatePath(pathname, nextParams != null ? nextParams : {});
1445
1455
 
1446
1456
  // Pre filters first
1447
- 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;
1448
1458
 
1449
1459
  // Then the link/navigate function
1450
1460
  const destSearch = dest.search === true ? preFilteredSearch // Preserve resolvedFrom true
@@ -1454,15 +1464,15 @@ function createRouter(userOptions) {
1454
1464
 
1455
1465
  // Then post filters
1456
1466
  const postFilteredSearch = (_dest$__postSearchFil = dest.__postSearchFilters) != null && _dest$__postSearchFil.length ? dest.__postSearchFilters.reduce((prev, next) => next(prev), destSearch) : destSearch;
1457
- const search = replaceEqualDeep(router.__location.search, postFilteredSearch);
1467
+ const search = replaceEqualDeep(router.state.latestLocation.search, postFilteredSearch);
1458
1468
  const searchStr = router.options.stringifySearch(search);
1459
- 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);
1460
1470
  hash = hash ? "#" + hash : '';
1461
1471
  return {
1462
1472
  pathname,
1463
1473
  search,
1464
1474
  searchStr,
1465
- state: router.__location.state,
1475
+ state: router.state.latestLocation.state,
1466
1476
  hash,
1467
1477
  href: "" + pathname + searchStr + hash,
1468
1478
  key: dest.key