Skip to content

Add refetchCachedPages option for infinite query #5016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/toolkit/src/query/core/apiState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export type InfiniteQueryConfigOptions<DataType, PageParam, QueryArg> = {
* direction will be dropped from the cache.
*/
maxPages?: number
/**
* Defaults to `true`. When this is `true` and 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.
* When `false` only the first page will be refetched.
*/
refetchCachedPages?: boolean
}

export type InfiniteData<DataType, PageParam> = {
Expand Down
29 changes: 16 additions & 13 deletions packages/toolkit/src/query/core/buildThunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,8 @@ export function buildThunks<
const { infiniteQueryOptions } = endpointDefinition

// Runtime checks should guarantee this is a positive number if provided
const { maxPages = Infinity } = infiniteQueryOptions
const { maxPages = Infinity, refetchCachedPages = true } =
infiniteQueryOptions

let result: QueryReturnValue

Expand Down Expand Up @@ -730,18 +731,20 @@ export function buildThunks<
} as QueryReturnValue
}

// Fetch remaining pages
for (let i = 1; i < totalPages; i++) {
const param = getNextPageParam(
infiniteQueryOptions,
result.data as InfiniteData<unknown, unknown>,
arg.originalArgs,
)
result = await fetchPage(
result.data as InfiniteData<unknown, unknown>,
param,
maxPages,
)
if (refetchCachedPages) {
// Fetch remaining pages
for (let i = 1; i < totalPages; i++) {
const param = getNextPageParam(
infiniteQueryOptions,
result.data as InfiniteData<unknown, unknown>,
arg.originalArgs,
)
result = await fetchPage(
result.data as InfiniteData<unknown, unknown>,
param,
maxPages,
)
}
}
}

Expand Down