@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.
@@ -541,7 +541,7 @@ export declare class Router<in out TRouteTree extends AnyRoute, in out TTrailing
541
541
  startViewTransition: (fn: () => Promise<void>) => void;
542
542
  updateMatch: (id: string, updater: (match: AnyRouteMatch) => AnyRouteMatch) => AnyRouteMatch;
543
543
  getMatch: (matchId: string) => import('./Matches.cjs').RouteMatch<any, any, any, any, any, any, any> | undefined;
544
- loadMatches: ({ location, matches, preload, onReady, updateMatch, }: {
544
+ loadMatches: ({ location, matches, preload: allPreload, onReady, updateMatch, }: {
545
545
  location: ParsedLocation;
546
546
  matches: Array<AnyRouteMatch>;
547
547
  preload?: boolean;
@@ -541,7 +541,7 @@ export declare class Router<in out TRouteTree extends AnyRoute, in out TTrailing
541
541
  startViewTransition: (fn: () => Promise<void>) => void;
542
542
  updateMatch: (id: string, updater: (match: AnyRouteMatch) => AnyRouteMatch) => AnyRouteMatch;
543
543
  getMatch: (matchId: string) => import('./Matches.js').RouteMatch<any, any, any, any, any, any, any> | undefined;
544
- loadMatches: ({ location, matches, preload, onReady, updateMatch, }: {
544
+ loadMatches: ({ location, matches, preload: allPreload, onReady, updateMatch, }: {
545
545
  location: ParsedLocation;
546
546
  matches: Array<AnyRouteMatch>;
547
547
  preload?: boolean;
@@ -761,16 +761,19 @@ class Router {
761
761
  let updated;
762
762
  const isPending = (_a = this.state.pendingMatches) == null ? void 0 : _a.find((d) => d.id === id);
763
763
  const isMatched = this.state.matches.find((d) => d.id === id);
764
- const matchesKey = isPending ? "pendingMatches" : isMatched ? "matches" : "cachedMatches";
765
- this.__store.setState((s) => {
766
- var _a2;
767
- return {
768
- ...s,
769
- [matchesKey]: (_a2 = s[matchesKey]) == null ? void 0 : _a2.map(
770
- (d) => d.id === id ? updated = updater(d) : d
771
- )
772
- };
773
- });
764
+ const isCached = this.state.cachedMatches.find((d) => d.id === id);
765
+ const matchesKey = isPending ? "pendingMatches" : isMatched ? "matches" : isCached ? "cachedMatches" : "";
766
+ if (matchesKey) {
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
+ });
776
+ }
774
777
  return updated;
775
778
  };
776
779
  this.getMatch = (matchId) => {
@@ -783,7 +786,7 @@ class Router {
783
786
  this.loadMatches = async ({
784
787
  location,
785
788
  matches,
786
- preload,
789
+ preload: allPreload,
787
790
  onReady,
788
791
  updateMatch = this.updateMatch
789
792
  }) => {
@@ -795,6 +798,9 @@ class Router {
795
798
  await (onReady == null ? void 0 : onReady());
796
799
  }
797
800
  };
801
+ const resolvePreload = (matchId) => {
802
+ return !!(allPreload && !this.state.matches.find((d) => d.id === matchId));
803
+ };
798
804
  if (!this.isServer && !this.state.matches.length) {
799
805
  triggerOnReady();
800
806
  }
@@ -870,7 +876,8 @@ class Router {
870
876
  const parentMatchId = (_a = matches[index - 1]) == null ? void 0 : _a.id;
871
877
  const route = this.looseRoutesById[routeId];
872
878
  const pendingMs = route.options.pendingMs ?? this.options.defaultPendingMs;
873
- const shouldPending = !!(onReady && !this.isServer && !preload && (route.options.loader || route.options.beforeLoad) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? this.options.defaultPendingComponent));
879
+ const shouldPending = !!(onReady && !this.isServer && !resolvePreload(matchId) && (route.options.loader || route.options.beforeLoad) && typeof pendingMs === "number" && pendingMs !== Infinity && (route.options.pendingComponent ?? this.options.defaultPendingComponent));
880
+ let executeBeforeLoad = true;
874
881
  if (
875
882
  // If we are in the middle of a load, either of these will be present
876
883
  // (not to be confused with `loadPromise`, which is always defined)
@@ -885,7 +892,9 @@ class Router {
885
892
  }, pendingMs);
886
893
  }
887
894
  await existingMatch.beforeLoadPromise;
888
- } else {
895
+ executeBeforeLoad = this.getMatch(matchId).status !== "success";
896
+ }
897
+ if (executeBeforeLoad) {
889
898
  try {
890
899
  updateMatch(matchId, (prev) => ({
891
900
  ...prev,
@@ -926,11 +935,12 @@ class Router {
926
935
  }
927
936
  }));
928
937
  const { search, params, context, cause } = this.getMatch(matchId);
938
+ const preload = resolvePreload(matchId);
929
939
  const beforeLoadFnContext = {
930
940
  search,
931
941
  abortController,
932
942
  params,
933
- preload: !!preload,
943
+ preload,
934
944
  context,
935
945
  location,
936
946
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
@@ -998,20 +1008,22 @@ class Router {
998
1008
  context,
999
1009
  cause
1000
1010
  } = this.getMatch(matchId);
1011
+ const preload2 = resolvePreload(matchId);
1001
1012
  return {
1002
1013
  params,
1003
1014
  deps: loaderDeps,
1004
- preload: !!preload,
1015
+ preload: !!preload2,
1005
1016
  parentMatchPromise,
1006
1017
  abortController,
1007
1018
  context,
1008
1019
  location,
1009
1020
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
1010
- cause: preload ? "preload" : cause,
1021
+ cause: preload2 ? "preload" : cause,
1011
1022
  route
1012
1023
  };
1013
1024
  };
1014
1025
  const age = Date.now() - this.getMatch(matchId).updatedAt;
1026
+ const preload = resolvePreload(matchId);
1015
1027
  const staleAge = preload ? route.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route.options.staleTime ?? this.options.defaultStaleTime ?? 0;
1016
1028
  const shouldReloadOption = route.options.shouldReload;
1017
1029
  const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(getLoaderContext()) : shouldReloadOption;
@@ -1167,7 +1179,7 @@ class Router {
1167
1179
  await triggerOnReady();
1168
1180
  } catch (err) {
1169
1181
  if (isRedirect(err) || isNotFound(err)) {
1170
- if (isNotFound(err) && !preload) {
1182
+ if (isNotFound(err) && !allPreload) {
1171
1183
  await triggerOnReady();
1172
1184
  }
1173
1185
  throw err;
@@ -1243,16 +1255,18 @@ class Router {
1243
1255
  preload: true,
1244
1256
  dest: opts
1245
1257
  });
1246
- const loadedMatchIds = Object.fromEntries(
1247
- [
1248
- ...this.state.matches,
1249
- ...this.state.pendingMatches ?? [],
1250
- ...this.state.cachedMatches
1251
- ].map((d) => [d.id, true])
1258
+ const activeMatchIds = new Set(
1259
+ [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1260
+ (d) => d.id
1261
+ )
1252
1262
  );
1263
+ const loadedMatchIds = /* @__PURE__ */ new Set([
1264
+ ...activeMatchIds,
1265
+ ...this.state.cachedMatches.map((d) => d.id)
1266
+ ]);
1253
1267
  this.__store.batch(() => {
1254
1268
  matches.forEach((match) => {
1255
- if (!loadedMatchIds[match.id]) {
1269
+ if (!loadedMatchIds.has(match.id)) {
1256
1270
  this.__store.setState((s) => ({
1257
1271
  ...s,
1258
1272
  cachedMatches: [...s.cachedMatches, match]
@@ -1260,11 +1274,6 @@ class Router {
1260
1274
  }
1261
1275
  });
1262
1276
  });
1263
- const activeMatchIds = new Set(
1264
- [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1265
- (d) => d.id
1266
- )
1267
- );
1268
1277
  try {
1269
1278
  matches = await this.loadMatches({
1270
1279
  matches,