Skip to content
Source
Sign inSign up
TanStack/query

infiniteQuery throws "getNextPageParam is not a function" when 2+ stale pages are cached #12569

Closeda-botermans(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

Hey :)

I ran into this while testing query-core.

I can call queryClient.infiniteQuery without getNextPageParam as long as I don't pass pages. TypeScript is fine with it, and it works on an empty cache.

But if the same key already has 2 or more pages cached and they're stale, the exact same call throws:

TypeError: options.getNextPageParam is not a function

Same with fetchInfiniteQuery. prefetchInfiniteQuery doesn't throw, but it leaves the query in an error state.

So the same call works or crashes depending on what's already in the cache.

Thanks!

Your minimal, reproducible example

https://stackblitz.com/github/a-botermans/tanstack-query-infinite-repro

Steps to reproduce

  1. Have 2 pages in the cache for a key. The demo uses setQueryData, but loading them with useInfiniteQuery + fetchNextPage gives the same result.
  2. Let them go stale. The default staleTime: 0 is enough.
  3. Call infiniteQuery on the same key, without pages and without getNextPageParam:
queryClient.setQueryData(['pages'], {
  pages: ['page 0', 'page 1'],
  pageParams: [0, 1],
})

await queryClient.infiniteQuery({
  queryKey: ['pages'],
  queryFn: ({ pageParam }) => `page ${pageParam}`,
  initialPageParam: 0,
})

It refetches the first page fine, then throws when it needs the param for page 2.

Expected behavior

No internal TypeError for a call the types accept. Either it works without the callback, or the types should ask for it.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

Not platform specific. Reproduced on Node 26 and in Chromium.

Tanstack Query adapter

vanilla

TanStack Query version

v5.107.1

TypeScript version

v5.6.3

Additional context

  • The same steps work on 4.44.0 and fail on 5.0.0, so it looks like it came in with v5.
  • With the default retries in the browser, the TypeError gets retried like a network error. The call hangs for about 7 seconds before it rejects.
  • Workaround: pass getNextPageParam in the call too. Then it refetches all the pages.
  1. Took a look at the reproduction. The crash happens in `infiniteQueryBehavior.ts` inside the refetch loop:
    
    ```ts
    do {
      const param =
        currentPage === 0
          ? (oldPageParams[0] ?? initialPageParam)
          : getNextPageParam(options, result)
      if (currentPage > 0 && param == null) {
        break
      }
      result = await fetchPage(result, param)
      currentPage++
    } while (hasMorePages())

    When 2+ pages are cached, hasMorePages() evaluates to true based on oldPages.length. On currentPage > 0, it unconditionally calls getNextPageParam(options, result). Because getNextPageParam is optional in InfiniteQueryExecuteOptions when pages isn't specified, calling queryClient.infiniteQuery or prefetchInfiniteQuery without it succeeds on an empty cache but throws TypeError: getNextPageParam is not a function when stale pages exist.

    Before opening a PR, which design direction do maintainers prefer here?

    1. Runtime fallback to cached params: If typeof options.getNextPageParam !== 'function', fall back to oldPageParams[currentPage] so cached pages are refetched with their existing parameters, and treat hasNextPage as false if no callback is supplied.
    2. Type requirement: Make getNextPageParam strictly required on InfiniteQueryExecuteOptions / prefetchInfiniteQuery / infiniteQuery so callers must always provide it.

    Happy to put up a PR with tests for whichever approach fits best.

  2. TkDodo closed this

  3. TkDodo linked this to #12636

Sign in to join this conversation.