Skip to content

Commit 8276c5f

Browse files
feat: add forcePending to router.invalidate() (#4545)
1 parent 8cbb3c6 commit 8276c5f

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

docs/router/framework/react/api/router/RouterType.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,12 @@ Navigates to a new location.
139139
140140
Invalidates route matches by forcing their `beforeLoad` and `load` functions to be called again.
141141
142-
- Type: `(opts?: {filter?: (d: MakeRouteMatchUnion<TRouter>) => boolean, sync?: boolean}) => Promise<void>`
142+
- Type: `(opts?: {filter?: (d: MakeRouteMatchUnion<TRouter>) => boolean, sync?: boolean, forcePending?: boolean }) => Promise<void>`
143143
- This is useful any time your loader data might be out of date or stale. For example, if you have a route that displays a list of posts, and you have a loader function that fetches the list of posts from an API, you might want to invalidate the route matches for that route any time a new post is created so that the list of posts is always up-to-date.
144144
- if `filter` is not supplied, all matches will be invalidated
145145
- if `filter` is supplied, only matches for which `filter` returns `true` will be invalidated.
146146
- if `sync` is true, the promise returned by this function will only resolve once all loaders have finished.
147+
- if `forcePending` is true, the invalidated matches will be put into `'pending'` state regardless whether they are in `'error'` state or not.
147148
- You might also want to invalidate the Router if you imperatively `reset` the router's `CatchBoundary` to trigger loaders again.
148149
149150
### `.clearCache` method

packages/router-core/src/router.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ export type UpdateFn<
588588
export type InvalidateFn<TRouter extends AnyRouter> = (opts?: {
589589
filter?: (d: MakeRouteMatchUnion<TRouter>) => boolean
590590
sync?: boolean
591+
forcePending?: boolean
591592
}) => Promise<void>
592593

593594
export type ParseLocationFn<TRouteTree extends AnyRoute> = (
@@ -2631,7 +2632,7 @@ export class RouterCore<
26312632
return {
26322633
...d,
26332634
invalid: true,
2634-
...(d.status === 'error'
2635+
...(opts?.forcePending || d.status === 'error'
26352636
? ({ status: 'pending', error: undefined } as const)
26362637
: {}),
26372638
}

0 commit comments

Comments
 (0)