Skip to content

Commit 81715b2

Browse files
committed
Document refetchCachedPages
1 parent aa64f8e commit 81715b2

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

docs/rtk-query/api/createApi.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ export type InfiniteQueryDefinition<
329329
* direction will be dropped from the cache.
330330
*/
331331
maxPages?: number
332+
/**
333+
* Defaults to `true`. When this is `true` and an infinite query endpoint is refetched
334+
* (due to tag invalidation, polling, arg change configuration, or manual refetching),
335+
* RTK Query will try to sequentially refetch all pages currently in the cache.
336+
* When `false` only the first page will be refetched.
337+
*/
338+
refetchCachedPages?: boolean
332339
}
333340
}
334341
```

docs/rtk-query/api/created-api/hooks.mdx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ type UseInfiniteQueryOptions = {
463463
refetchOnMountOrArgChange?: boolean | number
464464
selectFromResult?: (result: UseQueryStateDefaultResult) => any
465465
initialPageParam?: PageParam
466+
refetchCachedPages?: boolean
466467
}
467468

468469
type UseInfiniteQueryResult<Data, PageParam> = {
@@ -514,7 +515,9 @@ type UseInfiniteQueryResult<Data, PageParam> = {
514515
isFetchPreviousPageError: boolean
515516

516517
// A function to force refetch the query - returns a Promise with additional methods
517-
refetch: () => InfiniteQueryActionCreatorResult
518+
refetch: (options?: {
519+
refetchCachedPages?: boolean
520+
}) => InfiniteQueryActionCreatorResult
518521

519522
// Triggers a fetch for the next page, based on the current cache
520523
fetchNextPage: () => InfiniteQueryActionCreatorResult

docs/rtk-query/usage/infinite-queries.mdx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,18 @@ The promise returned from `fetchNextPage()` does have [a `promise.abort()` metho
277277

278278
### Refetching
279279

280-
When an infinite query endpoint is refetched (due to tag invalidation, polling, arg change configuration, or manual refetching), RTK Query will try to sequentially refetch all pages currently in the cache. This ensures that the client is always working with the latest data, and avoids stale cursors or duplicate records.
280+
When an infinite query endpoint is refetched (due to tag invalidation, polling, arg change configuration, or manual refetching), RTK Query's default behavior is **sequentially refetching _all_ pages currently in the cache**. This ensures that the client is always working with the latest data, and avoids stale cursors or duplicate records.
281281

282282
If the cache entry is ever removed and then re-added, it will start with only fetching the initial page.
283283

284+
There may be cases when you want a refetch to _only_ refetch the first page, and not any of the other pages in cache (ie, the refetch shrinks the cache from N pages to 1 page). This can be done via the `refetchCachedPages` option, which can be passed in several different places:
285+
286+
- Defined on the endpoint as part of `infiniteQueryOptions`: applies to all attempted refetches
287+
- Passed as an option to a `useInfiniteQuery` hook: applies to all attempted refetches
288+
- Passed as an option to `endpoint.initiate()`, or the `refetch` method available on the `initiate` or hook result objects: applies only to the manually triggered refetch
289+
290+
Overall, the refetch logic defaults to refetching all pages, but will override that with the option provided to the endpoint or a manual refetch.
291+
284292
### Limiting Cache Entry Size
285293

286294
All fetched pages for a given query arg are stored in the `pages` array in that cache entry. By default, there is no limit to the number of stored pages - if you call `fetchNextPage()` 1000 times, `data.pages` will have 1000 pages stored.

0 commit comments

Comments
 (0)