Skip to content

Infinite query page param resets back to the first page during refetch if a page request is retried #8046

Closed

Description

Describe the bug

I am using useInfiniteQuery to additively load more data into a table from a nextToken based list API (e.g. example AWS SDK list API). Requesting next pages and refetching works great for the most part, but I'm having some trouble figuring out a way to preserve the next page param for a retried request triggered by a manual or automated refetch.

When fetching next pages with fetchNextPage and the queryFn rejects with an error response, the request is retried as expected using the same page params. However, I noticed retried requests from a manual or automated refetch will start over back to the first page after the first error response is encountered. I understand the need to fetch sequentially from the first page for the refetch, however, I would expect the failed request to retry using the same page params and continue onto the next page request once a retry succeeds.

The backend service I'm working with has a reasonable rate limit set (50 TPM in a 1 minute sliding window), but I'm trying to ensure a manual or automatic refetch will be able to recover from worst-case rate limiting scenarios where the user can continuously rate-limit themselves by refetching a table view loaded with 50+ pages.

Original discussion: #8044

Your minimal, reproducible example

https://codesandbox.io/p/sandbox/mqvdz4

Steps to reproduce

  1. Open code sandbox and open browser console to view logs
  2. Click the "Get next page" button 3 times to paginate through all the mock data. The second requested page is hard coded to simulate a rate limiting error and will be retried 3 times before succeeding.
  3. After loading all the data, click the "Refetch" button to invoke query refetch.
  4. You should be able to observe the page param being reset back to 0 during the refetch when the rate limiting error is encountered with page 2. This will continue to loop until the number of retry attempts are exhausted.

Example console logs:

// fetch next page

getting page 0...
got page 0
getting page 1...
got page 1
getting page 2... // simulating rate limit for page index 2
rate limited!
getting page 2...
rate limited!
getting page 2...
rate limited!
getting page 2...
got page 2 // rate limit quota restored
getting page 3...
got page 3

// refetch

getting page 0...
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
getting page 0... // page param is reset to 0, expected page 2 to be retried here
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
getting page 0... // repeatedly getting rate limited
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
getting page 0...
got page 0
getting page 1...
got page 1
getting page 2...
rate limited!
...

Expected behavior

During a manual and automated infinite query refetch, I expect a failed page request to retry using the same page params and continue onto the next page request once the retry succeeds. Currently, infinite query refetch will start over back to the first page for retry attempts after the first error response is encountered.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS Sonoma 14.6.1
  • Browser: Chrome
  • Version: 128.0.6613.120 (Official Build) (x86_64)

Tanstack Query adapter

react-query

TanStack Query version

v5.51.23

TypeScript version

v5.2.2

Additional context

No response

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

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions