@tanstack/react-router 1.86.1 → 1.87.0

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.
@@ -763,16 +763,19 @@ class Router {
763
763
  let updated;
764
764
  const isPending = (_a = this.state.pendingMatches) == null ? void 0 : _a.find((d) => d.id === id);
765
765
  const isMatched = this.state.matches.find((d) => d.id === id);
766
- const matchesKey = isPending ? "pendingMatches" : isMatched ? "matches" : "cachedMatches";
767
- this.__store.setState((s) => {
768
- var _a2;
769
- return {
770
- ...s,
771
- [matchesKey]: (_a2 = s[matchesKey]) == null ? void 0 : _a2.map(
772
- (d) => d.id === id ? updated = updater(d) : d
773
- )
774
- };
775
- });
766
+ const isCached = this.state.cachedMatches.find((d) => d.id === id);
767
+ const matchesKey = isPending ? "pendingMatches" : isMatched ? "matches" : isCached ? "cachedMatches" : "";
768
+ if (matchesKey) {
769
+ this.__store.setState((s) => {
770
+ var _a2;
771
+ return {
772
+ ...s,
773
+ [matchesKey]: (_a2 = s[matchesKey]) == null ? void 0 : _a2.map(
774
+ (d) => d.id === id ? updated = updater(d) : d
775
+ )
776
+ };
777
+ });
778
+ }
776
779
  return updated;
777
780
  };
778
781
  this.getMatch = (matchId) => {
@@ -785,7 +788,7 @@ class Router {
785
788
  this.loadMatches = async ({
786
789
  location,
787
790
  matches,
788
- preload,
791
+ preload: allPreload,
789
792
  onReady,
790
793
  updateMatch = this.updateMatch
791
794
  }) => {
@@ -797,6 +800,9 @@ class Router {
797
800
  await (onReady == null ? void 0 : onReady());
798
801
  }
799
802
  };
803
+ const resolvePreload = (matchId) => {
804
+ return !!(allPreload && !this.state.matches.find((d) => d.id === matchId));
805
+ };
800
806
  if (!this.isServer && !this.state.matches.length) {
801
807
  triggerOnReady();
802
808
  }
@@ -872,7 +878,7 @@ class Router {
872
878
  const parentMatchId = (_a = matches[index - 1]) == null ? void 0 : _a.id;
873
879
  const route = this.looseRoutesById[routeId];
874
880
  const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
875
- const shouldPending = !!(onReady && !this.isServer && !preload && (route.options.loader || route.options.beforeLoad) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? this.options.defaultPendingComponent));
881
+ const shouldPending = !!(onReady && !this.isServer && !resolvePreload(matchId) && (route.options.loader || route.options.beforeLoad) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? this.options.defaultPendingComponent));
876
882
  if (
877
883
  // If we are in the middle of a load, either of these will be present
878
884
  // (not to be confused with `loadPromise`, which is always defined)
@@ -928,11 +934,12 @@ class Router {
928
934
  }
929
935
  }));
930
936
  const { search, params, context, cause } = this.getMatch(matchId);
937
+ const preload = resolvePreload(matchId);
931
938
  const beforeLoadFnContext = {
932
939
  search,
933
940
  abortController,
934
941
  params,
935
- preload: !!preload,
942
+ preload,
936
943
  context,
937
944
  location,
938
945
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
@@ -1000,20 +1007,22 @@ class Router {
1000
1007
  context,
1001
1008
  cause
1002
1009
  } = this.getMatch(matchId);
1010
+ const preload2 = resolvePreload(matchId);
1003
1011
  return {
1004
1012
  params,
1005
1013
  deps: loaderDeps,
1006
- preload: !!preload,
1014
+ preload: !!preload2,
1007
1015
  parentMatchPromise,
1008
1016
  abortController,
1009
1017
  context,
1010
1018
  location,
1011
1019
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
1012
- cause: preload ? "preload" : cause,
1020
+ cause: preload2 ? "preload" : cause,
1013
1021
  route
1014
1022
  };
1015
1023
  };
1016
1024
  const age = Date.now() - this.getMatch(matchId).updatedAt;
1025
+ const preload = resolvePreload(matchId);
1017
1026
  const staleAge = preload ? route.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route.options.staleTime ?? this.options.defaultStaleTime ?? 0;
1018
1027
  const shouldReloadOption = route.options.shouldReload;
1019
1028
  const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(getLoaderContext()) : shouldReloadOption;
@@ -1169,7 +1178,7 @@ class Router {
1169
1178
  await triggerOnReady();
1170
1179
  } catch (err) {
1171
1180
  if (redirects.isRedirect(err) || notFound.isNotFound(err)) {
1172
- if (notFound.isNotFound(err) && !preload) {
1181
+ if (notFound.isNotFound(err) && !allPreload) {
1173
1182
  await triggerOnReady();
1174
1183
  }
1175
1184
  throw err;
@@ -1245,16 +1254,18 @@ class Router {
1245
1254
  preload: true,
1246
1255
  dest: opts
1247
1256
  });
1248
- const loadedMatchIds = Object.fromEntries(
1249
- [
1250
- ...this.state.matches,
1251
- ...this.state.pendingMatches ?? [],
1252
- ...this.state.cachedMatches
1253
- ].map((d) => [d.id, true])
1257
+ const activeMatchIds = new Set(
1258
+ [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1259
+ (d) => d.id
1260
+ )
1254
1261
  );
1262
+ const loadedMatchIds = /* @__PURE__ */ new Set([
1263
+ ...activeMatchIds,
1264
+ ...this.state.cachedMatches.map((d) => d.id)
1265
+ ]);
1255
1266
  this.__store.batch(() => {
1256
1267
  matches.forEach((match) => {
1257
- if (!loadedMatchIds[match.id]) {
1268
+ if (!loadedMatchIds.has(match.id)) {
1258
1269
  this.__store.setState((s) => ({
1259
1270
  ...s,
1260
1271
  cachedMatches: [...s.cachedMatches, match]
@@ -1262,11 +1273,6 @@ class Router {
1262
1273
  }
1263
1274
  });
1264
1275
  });
1265
- const activeMatchIds = new Set(
1266
- [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1267
- (d) => d.id
1268
- )
1269
- );
1270
1276
  try {
1271
1277
  matches = await this.loadMatches({
1272
1278
  matches,