@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.
@@ -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,7 @@ 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));
874
880
  if (
875
881
  // If we are in the middle of a load, either of these will be present
876
882
  // (not to be confused with `loadPromise`, which is always defined)
@@ -926,11 +932,12 @@ class Router {
926
932
  }
927
933
  }));
928
934
  const { search, params, context, cause } = this.getMatch(matchId);
935
+ const preload = resolvePreload(matchId);
929
936
  const beforeLoadFnContext = {
930
937
  search,
931
938
  abortController,
932
939
  params,
933
- preload: !!preload,
940
+ preload,
934
941
  context,
935
942
  location,
936
943
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
@@ -998,20 +1005,22 @@ class Router {
998
1005
  context,
999
1006
  cause
1000
1007
  } = this.getMatch(matchId);
1008
+ const preload2 = resolvePreload(matchId);
1001
1009
  return {
1002
1010
  params,
1003
1011
  deps: loaderDeps,
1004
- preload: !!preload,
1012
+ preload: !!preload2,
1005
1013
  parentMatchPromise,
1006
1014
  abortController,
1007
1015
  context,
1008
1016
  location,
1009
1017
  navigate: (opts) => this.navigate({ ...opts, _fromLocation: location }),
1010
- cause: preload ? "preload" : cause,
1018
+ cause: preload2 ? "preload" : cause,
1011
1019
  route
1012
1020
  };
1013
1021
  };
1014
1022
  const age = Date.now() - this.getMatch(matchId).updatedAt;
1023
+ const preload = resolvePreload(matchId);
1015
1024
  const staleAge = preload ? route.options.preloadStaleTime ?? this.options.defaultPreloadStaleTime ?? 3e4 : route.options.staleTime ?? this.options.defaultStaleTime ?? 0;
1016
1025
  const shouldReloadOption = route.options.shouldReload;
1017
1026
  const shouldReload = typeof shouldReloadOption === "function" ? shouldReloadOption(getLoaderContext()) : shouldReloadOption;
@@ -1167,7 +1176,7 @@ class Router {
1167
1176
  await triggerOnReady();
1168
1177
  } catch (err) {
1169
1178
  if (isRedirect(err) || isNotFound(err)) {
1170
- if (isNotFound(err) && !preload) {
1179
+ if (isNotFound(err) && !allPreload) {
1171
1180
  await triggerOnReady();
1172
1181
  }
1173
1182
  throw err;
@@ -1243,16 +1252,18 @@ class Router {
1243
1252
  preload: true,
1244
1253
  dest: opts
1245
1254
  });
1246
- const loadedMatchIds = Object.fromEntries(
1247
- [
1248
- ...this.state.matches,
1249
- ...this.state.pendingMatches ?? [],
1250
- ...this.state.cachedMatches
1251
- ].map((d) => [d.id, true])
1255
+ const activeMatchIds = new Set(
1256
+ [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1257
+ (d) => d.id
1258
+ )
1252
1259
  );
1260
+ const loadedMatchIds = /* @__PURE__ */ new Set([
1261
+ ...activeMatchIds,
1262
+ ...this.state.cachedMatches.map((d) => d.id)
1263
+ ]);
1253
1264
  this.__store.batch(() => {
1254
1265
  matches.forEach((match) => {
1255
- if (!loadedMatchIds[match.id]) {
1266
+ if (!loadedMatchIds.has(match.id)) {
1256
1267
  this.__store.setState((s) => ({
1257
1268
  ...s,
1258
1269
  cachedMatches: [...s.cachedMatches, match]
@@ -1260,11 +1271,6 @@ class Router {
1260
1271
  }
1261
1272
  });
1262
1273
  });
1263
- const activeMatchIds = new Set(
1264
- [...this.state.matches, ...this.state.pendingMatches ?? []].map(
1265
- (d) => d.id
1266
- )
1267
- );
1268
1274
  try {
1269
1275
  matches = await this.loadMatches({
1270
1276
  matches,