@tanstack/react-router 1.86.1 → 1.87.1

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,8 @@ 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));
882
+ let executeBeforeLoad = true;
876
883
  if (
877
884
  // If we are in the middle of a load, either of these will be present
878
885
  // (not to be confused with `loadPromise`, which is always defined)
@@ -887,7 +894,9 @@ class Router {
887
894
  }, pendingMs);
888
895
  }
889
896
  await existingMatch.beforeLoadPromise;
890
- } else {
897
+ executeBeforeLoad = this.getMatch(matchId).status !== "success";
898
+ }
899
+ if (executeBeforeLoad) {
891
900
  try {
892
901
  updateMatch(matchId, (prev) => ({
893
902
  ...prev,
@@ -928,11 +937,12 @@ class Router {
928
937
  }
929
938
  }));
930
939
  const { search, params, context, cause } = this.getMatch(matchId);
940
+ const preload = resolvePreload(matchId);
931
941
  const beforeLoadFnContext = {
932
942
  search,
933
943
  abortController,
934
944
  params,
935
- preload: !!preload,
945
+ preload,
936
946
  context,
937
947
  location,
938
948
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
@@ -1000,20 +1010,22 @@ class Router {
1000
1010
  context,
1001
1011
  cause
1002
1012
  } = this.getMatch(matchId);
1013
+ const preload2 = resolvePreload(matchId);
1003
1014
  return {
1004
1015
  params,
1005
1016
  deps: loaderDeps,
1006
- preload: !!preload,
1017
+ preload: !!preload2,
1007
1018
  parentMatchPromise,
1008
1019
  abortController,
1009
1020
  context,
1010
1021
  location,
1011
1022
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
1012
- cause: preload ? "preload" : cause,
1023
+ cause: preload2 ? "preload" : cause,
1013
1024
  route
1014
1025
  };
1015
1026
  };
1016
1027
  const age = Date.now() - this.getMatch(matchId).updatedAt;
1028
+ const preload = resolvePreload(matchId);
1017
1029
  const staleAge = preload ? route.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route.options.staleTime ?? this.options.defaultStaleTime ?? 0;
1018
1030
  const shouldReloadOption = route.options.shouldReload;
1019
1031
  const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(getLoaderContext()) : shouldReloadOption;
@@ -1169,7 +1181,7 @@ class Router {
1169
1181
  await triggerOnReady();
1170
1182
  } catch (err) {
1171
1183
  if (redirects.isRedirect(err) || notFound.isNotFound(err)) {
1172
- if (notFound.isNotFound(err) && !preload) {
1184
+ if (notFound.isNotFound(err) && !allPreload) {
1173
1185
  await triggerOnReady();
1174
1186
  }
1175
1187
  throw err;
@@ -1245,16 +1257,18 @@ class Router {
1245
1257
  preload: true,
1246
1258
  dest: opts
1247
1259
  });
1248
- const loadedMatchIds = Object.fromEntries(
1249
- [
1250
- ...this.state.matches,
1251
- ...this.state.pendingMatches ?? [],
1252
- ...this.state.cachedMatches
1253
- ].map((d) => [d.id, true])
1260
+ const activeMatchIds = new Set(
1261
+ [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1262
+ (d) => d.id
1263
+ )
1254
1264
  );
1265
+ const loadedMatchIds = /* @__PURE__ */ new Set([
1266
+ ...activeMatchIds,
1267
+ ...this.state.cachedMatches.map((d) => d.id)
1268
+ ]);
1255
1269
  this.__store.batch(() => {
1256
1270
  matches.forEach((match) => {
1257
- if (!loadedMatchIds[match.id]) {
1271
+ if (!loadedMatchIds.has(match.id)) {
1258
1272
  this.__store.setState((s) => ({
1259
1273
  ...s,
1260
1274
  cachedMatches: [...s.cachedMatches, match]
@@ -1262,11 +1276,6 @@ class Router {
1262
1276
  }
1263
1277
  });
1264
1278
  });
1265
- const activeMatchIds = new Set(
1266
- [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1267
- (d) => d.id
1268
- )
1269
- );
1270
1279
  try {
1271
1280
  matches = await this.loadMatches({
1272
1281
  matches,