@tanstack/react-router 1.45.3 → 1.45.5

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/src/Matches.tsx CHANGED
@@ -68,6 +68,7 @@ export interface RouteMatch<
68
68
  globalNotFound?: boolean
69
69
  staticData: StaticDataRouteOption
70
70
  minPendingPromise?: ControlledPromise<void>
71
+ pendingTimeout?: ReturnType<typeof setTimeout>
71
72
  }
72
73
 
73
74
  export type MakeRouteMatch<
@@ -185,6 +186,10 @@ export type UseMatchRouteOptions<
185
186
  export function useMatchRoute<TRouter extends AnyRouter = RegisteredRouter>() {
186
187
  const router = useRouter()
187
188
 
189
+ useRouterState({
190
+ select: (s) => [s.location.href, s.resolvedLocation.href, s.status],
191
+ })
192
+
188
193
  return React.useCallback(
189
194
  <
190
195
  TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
package/src/router.ts CHANGED
@@ -1126,7 +1126,12 @@ export class Router<
1126
1126
  }
1127
1127
 
1128
1128
  cancelMatch = (id: string) => {
1129
- this.getMatch(id)?.abortController.abort()
1129
+ const match = this.getMatch(id)
1130
+
1131
+ if (!match) return
1132
+
1133
+ match.abortController.abort()
1134
+ clearTimeout(match.pendingTimeout)
1130
1135
  }
1131
1136
 
1132
1137
  cancelMatches = () => {
@@ -1142,22 +1147,13 @@ export class Router<
1142
1147
  } = {},
1143
1148
  matches?: Array<MakeRouteMatch<TRouteTree>>,
1144
1149
  ): ParsedLocation => {
1145
- // if the router is loading the previous location is what
1146
- // we should use because the old matches are still around
1147
- // and the latest location has already been updated.
1148
- // If the router is not loading we should always use the
1149
- // latest location because resolvedLocation can lag behind
1150
- // and the new matches could already render
1151
- const currentLocation = this.state.isLoading
1152
- ? this.state.resolvedLocation
1153
- : this.latestLocation
1154
-
1155
- const location = dest._fromLocation ?? currentLocation
1156
-
1157
- let fromPath = location.pathname
1158
- let fromSearch = dest.fromSearch || location.search
1159
-
1160
- const fromMatches = this.matchRoutes(location.pathname, fromSearch)
1150
+ const fromMatches =
1151
+ dest._fromLocation != null
1152
+ ? this.matchRoutes(
1153
+ dest._fromLocation.pathname,
1154
+ dest.fromSearch || dest._fromLocation.search,
1155
+ )
1156
+ : this.state.matches
1161
1157
 
1162
1158
  const fromMatch =
1163
1159
  dest.from != null
@@ -1170,14 +1166,14 @@ export class Router<
1170
1166
  )
1171
1167
  : undefined
1172
1168
 
1173
- fromPath = fromMatch?.pathname || fromPath
1169
+ const fromPath = fromMatch?.pathname || this.latestLocation.pathname
1174
1170
 
1175
1171
  invariant(
1176
1172
  dest.from == null || fromMatch != null,
1177
1173
  'Could not find match for from: ' + dest.from,
1178
1174
  )
1179
1175
 
1180
- fromSearch = last(fromMatches)?.search || this.latestLocation.search
1176
+ const fromSearch = last(fromMatches)?.search || this.latestLocation.search
1181
1177
 
1182
1178
  const stayingMatches = matches?.filter((d) =>
1183
1179
  fromMatches.find((e) => e.routeId === d.routeId),
@@ -1865,10 +1861,12 @@ export class Router<
1865
1861
  this.options.defaultPendingComponent)
1866
1862
  )
1867
1863
 
1864
+ let pendingTimeout: ReturnType<typeof setTimeout>
1865
+
1868
1866
  if (shouldPending) {
1869
1867
  // If we might show a pending component, we need to wait for the
1870
1868
  // pending promise to resolve before we start showing that state
1871
- setTimeout(() => {
1869
+ pendingTimeout = setTimeout(() => {
1872
1870
  try {
1873
1871
  // Update the match and prematurely resolve the loadMatches promise so that
1874
1872
  // the pending component can start rendering
@@ -1899,6 +1897,7 @@ export class Router<
1899
1897
  ),
1900
1898
  context: replaceEqualDeep(prev.context, parentContext),
1901
1899
  abortController,
1900
+ pendingTimeout,
1902
1901
  }))
1903
1902
 
1904
1903
  const { search, params, routeContext, cause } =