Skip to content

fix(rtk-query): keep the hook's initialPageParam on infinite query refetch and fetchNextPage - #5627

Open
kkatalz wants to merge 1 commit into
reduxjs:masterfrom
kkatalz:fix/keep-initial-page-param-after-failed-page
Open

kkatalz wants to merge 1 commit into
reduxjs:masterfrom
kkatalz:fix/keep-initial-page-param-after-failed-page

Conversation

@kkatalz

@kkatalz kkatalz commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

If an infinite query is started with a call-site initialPageParam (the useInfiniteQuery option, or initiate(arg, { initialPageParam })) and its first page fails, the next refetch() or fetchNextPage() asks for the endpoint's initialPageParam instead. Polling and tag invalidation do the same.

The call-site value only rides on the first request's arg. refetch re-dispatches without it (buildInitiate.ts#L630-L636), and the hook's fetchNextPage trigger passes only direction. With no cached pages, the thunk falls back to the endpoint default (buildThunks.ts#L798-L805):

const { initialPageParam = infiniteQueryOptions.initialPageParam } = arg as InfiniteQueryThunkArg<any>
// ...
const firstPageParam = cachedPageParams[0] ?? initialPageParam

Once a page has loaded you never see it, because cachedPageParams[0] wins. So it only bites after a failed first page.

The pending reducer now keeps a defined initialPageParam on the infinite query cache entry, next to direction (writePendingCacheEntry in buildSlice.ts). An undefined value doesn't overwrite it, the same rule originalArgs uses, since every refetch passes initialPageParam: undefined. The thunk reads it off the entry (it already reads the entry for cachedData), and InfiniteQuerySubState gets initialPageParam?: PageParamFrom<D>. It's one place, so refetch, fetchNextPage, polling, focus/reconnect and invalidation all pick it up.

A query that never passed a value still uses the endpoint default. A later explicit value replaces the stored one. The stored value belongs to the cache entry, so a second subscriber on the same key with no value of its own also starts from it after a failed first page (which is what already happens once a page is cached, via pageParams[0]).

Tests: a test.each(['refetch', 'forward fetch']) in infiniteQueries.test.ts, on the existing makeInfiniteApi({ failOnHit: 1 }) fixture. Endpoint default 0, initiate('x', { initialPageParam: 5 }), first page fails, then refetch() or initiate('x', { direction: 'forward' }) (what fetchNextPage dispatches). Both fail on master:

AssertionError: expected [ 5, +0 ] to deeply equal [ 5, 5 ]

I also tried the hook path with a throwaway useInfiniteQuery('x', { initialPageParam: 5 }) spec: refetch() and fetchNextPage() each request [5, 5] with the fix. The whole packages/toolkit suite passes with --typecheck (114 files, 1826 passed, 2 todo), and oxfmt and oxlint are clean.

There's no issue for this one.

…refetch and `fetchNextPage`

A call-site `initialPageParam` was only on the first request's arg. When
that first page failed there were no cached `pageParams` to start from, so
`refetch()`, `fetchNextPage()`, polling and invalidation fell back to the
endpoint's `initialPageParam`. The pending reducer now keeps it on the cache
entry, like `direction`, and the thunk reads it from there.
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Infinite-query state now carries the initial page parameter through pending cache updates and thunk processing. A regression test covers refetch and forward-page fetch after an initial request failure. Both follow-up operations retry page parameter 5, leaving cached page parameters as [5].

Changes: Infinite query retry behavior

Layer / File(s) Summary
Preserve the initial page parameter across requests
packages/toolkit/src/query/core/apiState.ts, packages/toolkit/src/query/core/buildSlice.ts, packages/toolkit/src/query/core/buildThunks.ts, packages/toolkit/src/query/tests/infiniteQueries.test.ts
The infinite-query substate adds an optional initialPageParam. Pending updates store it when defined, and thunk processing reads from the cached entry. The test checks that refetch and forward-page fetch retry page parameter 5 after the initial request fails.
Merge Risk: Minimal · up to ef201

This change keeps a call-site initial page parameter when an infinite query's first page fails and is then retried. The change is small and has a regression test. No merge-blocking risk was found.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 45eb037 and ef20116.

Files selected for processing: 4

  • packages/toolkit/src/query/core/apiState.ts
  • packages/toolkit/src/query/core/buildSlice.ts
  • packages/toolkit/src/query/core/buildThunks.ts
  • packages/toolkit/src/query/tests/infiniteQueries.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@pkg-pr-new

pkg-pr-new Bot commented Nov 19, 2026

Copy link
Copy Markdown

Open in StackBlitz

@reduxjs/rtk-codemods

npm i https://pkg.pr.new/@reduxjs/rtk-codemods@ef20116 -D
yarn add https://pkg.pr.new/@reduxjs/rtk-codemods@ef20116.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/rtk-codemods@ef20116.tgz -D
bun add https://pkg.pr.new/@reduxjs/rtk-codemods@ef20116.tgz -D

@rtk-query/codegen-openapi

npm i https://pkg.pr.new/@rtk-query/codegen-openapi@ef20116 -D
yarn add https://pkg.pr.new/@rtk-query/codegen-openapi@ef20116.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/codegen-openapi@ef20116.tgz -D
bun add https://pkg.pr.new/@rtk-query/codegen-openapi@ef20116.tgz -D

@rtk-query/graphql-request-base-query

npm i https://pkg.pr.new/@rtk-query/graphql-request-base-query@ef20116 -D
yarn add https://pkg.pr.new/@rtk-query/graphql-request-base-query@ef20116.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/graphql-request-base-query@ef20116.tgz -D
bun add https://pkg.pr.new/@rtk-query/graphql-request-base-query@ef20116.tgz -D

@reduxjs/toolkit

npm i https://pkg.pr.new/@reduxjs/toolkit@ef20116 -D
yarn add https://pkg.pr.new/@reduxjs/toolkit@ef20116.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/toolkit@ef20116.tgz -D
bun add https://pkg.pr.new/@reduxjs/toolkit@ef20116.tgz -D

commit: ef20116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant