@tanstack/react-router 1.92.11 → 1.93.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
- "version": "1.92.11",
3
+ "version": "1.93.0",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/router.ts CHANGED
@@ -1937,7 +1937,7 @@ export class Router<
1937
1937
 
1938
1938
  latestLoadPromise: undefined | Promise<void>
1939
1939
 
1940
- load = async (): Promise<void> => {
1940
+ load = async (opts?: { sync?: boolean }): Promise<void> => {
1941
1941
  this.latestLocation = this.parseLocation(this.latestLocation)
1942
1942
 
1943
1943
  let redirect: ResolvedRedirect | undefined
@@ -2000,6 +2000,7 @@ export class Router<
2000
2000
  })
2001
2001
 
2002
2002
  await this.loadMatches({
2003
+ sync: opts?.sync,
2003
2004
  matches: pendingMatches,
2004
2005
  location: next,
2005
2006
  // eslint-disable-next-line @typescript-eslint/require-await
@@ -2188,6 +2189,7 @@ export class Router<
2188
2189
  preload: allPreload,
2189
2190
  onReady,
2190
2191
  updateMatch = this.updateMatch,
2192
+ sync,
2191
2193
  }: {
2192
2194
  location: ParsedLocation
2193
2195
  matches: Array<AnyRouteMatch>
@@ -2198,6 +2200,7 @@ export class Router<
2198
2200
  updater: (match: AnyRouteMatch) => AnyRouteMatch,
2199
2201
  ) => void
2200
2202
  getMatch?: (matchId: string) => AnyRouteMatch | undefined
2203
+ sync?: boolean
2201
2204
  }): Promise<Array<MakeRouteMatch>> => {
2202
2205
  let firstBadMatchIndex: number | undefined
2203
2206
  let rendered = false
@@ -2397,7 +2400,6 @@ export class Router<
2397
2400
  context: {
2398
2401
  ...getParentMatchContext(),
2399
2402
  ...prev.__routeContext,
2400
- ...prev.__beforeLoadContext,
2401
2403
  },
2402
2404
  }))
2403
2405
 
@@ -2485,7 +2487,8 @@ export class Router<
2485
2487
  const { loaderPromise: prevLoaderPromise } =
2486
2488
  this.getMatch(matchId)!
2487
2489
 
2488
- let loaderRunningAsync = false
2490
+ let loaderShouldRunAsync = false
2491
+ let loaderIsRunningAsync = false
2489
2492
 
2490
2493
  if (prevLoaderPromise) {
2491
2494
  await prevLoaderPromise
@@ -2666,36 +2669,50 @@ export class Router<
2666
2669
 
2667
2670
  // If the route is successful and still fresh, just resolve
2668
2671
  const { status, invalid } = this.getMatch(matchId)!
2669
- loaderRunningAsync =
2672
+ loaderShouldRunAsync =
2670
2673
  status === 'success' &&
2671
2674
  (invalid || (shouldReload ?? age > staleAge))
2672
2675
  if (preload && route.options.preload === false) {
2673
2676
  // Do nothing
2674
- } else if (loaderRunningAsync) {
2677
+ } else if (loaderShouldRunAsync && !sync) {
2678
+ loaderIsRunningAsync = true
2675
2679
  ;(async () => {
2676
2680
  try {
2677
2681
  await runLoader()
2682
+ const { loaderPromise, loadPromise } =
2683
+ this.getMatch(matchId)!
2684
+ loaderPromise?.resolve()
2685
+ loadPromise?.resolve()
2686
+ updateMatch(matchId, (prev) => ({
2687
+ ...prev,
2688
+ loaderPromise: undefined,
2689
+ }))
2678
2690
  } catch (err) {
2679
2691
  if (isResolvedRedirect(err)) {
2680
2692
  await this.navigate(err)
2681
2693
  }
2682
2694
  }
2683
2695
  })()
2684
- } else if (status !== 'success') {
2696
+ } else if (
2697
+ status !== 'success' ||
2698
+ (loaderShouldRunAsync && sync)
2699
+ ) {
2685
2700
  await runLoader()
2686
2701
  }
2687
-
2702
+ }
2703
+ if (!loaderIsRunningAsync) {
2688
2704
  const { loaderPromise, loadPromise } =
2689
2705
  this.getMatch(matchId)!
2690
-
2691
2706
  loaderPromise?.resolve()
2692
2707
  loadPromise?.resolve()
2693
2708
  }
2694
2709
 
2695
2710
  updateMatch(matchId, (prev) => ({
2696
2711
  ...prev,
2697
- isFetching: loaderRunningAsync ? prev.isFetching : false,
2698
- loaderPromise: undefined,
2712
+ isFetching: loaderIsRunningAsync ? prev.isFetching : false,
2713
+ loaderPromise: loaderIsRunningAsync
2714
+ ? prev.loaderPromise
2715
+ : undefined,
2699
2716
  invalid: false,
2700
2717
  }))
2701
2718
  return this.getMatch(matchId)!
@@ -2726,6 +2743,7 @@ export class Router<
2726
2743
 
2727
2744
  invalidate = <TRouter extends AnyRouter = typeof this>(opts?: {
2728
2745
  filter?: (d: MakeRouteMatchUnion<TRouter>) => boolean
2746
+ sync?: boolean
2729
2747
  }) => {
2730
2748
  const invalidate = (d: MakeRouteMatch<TRouteTree>) => {
2731
2749
  if (opts?.filter?.(d as MakeRouteMatchUnion<TRouter>) ?? true) {
@@ -2747,7 +2765,7 @@ export class Router<
2747
2765
  pendingMatches: s.pendingMatches?.map(invalidate),
2748
2766
  }))
2749
2767
 
2750
- return this.load()
2768
+ return this.load({ sync: opts?.sync })
2751
2769
  }
2752
2770
 
2753
2771
  resolveRedirect = (err: AnyRedirect): ResolvedRedirect => {