Skip to content
Source
Sign inSign up
TanStack/query

A Prefetched Infinity State Time Query Gets Fetched Again on Mount #8482

Send to my agent
Closedkivvvvv(GitHub profile, opens in a new tab) opened this issue 6 comments

Describe the bug

A prefetched query with staleTime of Infinity always triggers an unexpected fetch again when the component with suspenseful query(staleTime: 0) is mounted.

Steps to reproduce

I would like to achieve these following requirements(a very contrived one):

  1. Display a post list 1 to 3.
  2. Initially display the details of the first post.
  3. When the user clicks a list, always fetches the latest details.

I have provided an example here(StackBlitz): A Prefetched Infinity State Time Query Gets Fetched Again on Mount

Here is how I achieved those requirements:

  1. For "Display a post list 1 to 3", I render(hardcoded) a list of 3 posts.
  2. For "Initially display the details of the first post" I have read Prefetching & Router Integration. I know that the first post details will be needed ahead of time, so I prefetch early in the Suspense tree with the queryClient.defaultOptions.queries.staleTime of Infinity to avoid any refetching of a query down the Suspense tree.
  3. For "When the user clicks a list, always fetches the latest details" I use a state of a selected post id as a query key of a suspenseful post query with staleTime of 0.

Expected behavior

The behavior seems to be as expected, but when I observed the network request, there are 2 unexpected requests for the first post(instead of only one request). If I try to make the post query to have staleTime of non-zero for example Infinity there would be only 1 request but I lose the ability to always fetch for the latest post.

How often does this bug happen?

Every time

Platform

  • OS: macOS
  • Browser: Chrome
  • Version: 131.0.6778.205 (Official Build) (arm64)

TanStack Query version

v5.62.11

  1. After debugging locally, the behavior that I found is the prefetched data is not resolved yet then the query is fetched resulting in 2 fetches for the same thing(but different in stale time).

  2. Here is what I found in the source:

    • When the query is executed for the first time, the QueryObserver is initialized with the query.
    • To construct the QueryObserver, its constructor is called, in which, eventually, it overrides the current query options (in this case, the prefetch query with staleTime: Infinity) with query client options (in this case, the post query with staleTime: 0). The exact line of execution is this then this.
    • From this point on, it loses track with an earlier prefetched query, and proceeds to fetch the post query.

    At this point, I feel like, maybe, it is an expected behavior. For what I want to achieve, there might be some other way.

  3. staleTime is not a Query setting, but an Observer level setting. The value describes how this observer views the data - is it outdated from their perspective or not.

    staleTime on imperative methods like fetchQuery, prefetchQuery, ensureQueryData or usePrefetchQuery does something slightly different. As those methods don’t create an observer, we just take them to see if cached data can be returned from this call because it’s still fresh.

    So when you call usePrefetchQuery(postQueryOptions(1)), it gets executed with staleTime:Infinity, which means the prefetch will only run if there is no data at all in the cache. It will run only once.

    But then, your observer has staleTime:0, which means it will always want a background refetch.

    There is also a “special” situation with suspense: Because suspense unmounts your component, the combination of suspense + staleTime:0 will always result in a background refetch. We guard ourselves against that here:

    https://github.com/TanStack/query/blob/8f72f59463c49246601c965d80e0564ba3ce87b0/packages/react-query/src/suspense.ts#L25-L29

    But since you set the staleTime to 0 manually, you overwrite that.


    The possible solutions here are:

    • don’t use suspense - a normal useQuery with staleTime:0 on the observer will work as expected.
    • set a small staleTime on the observer - something like 500ms. This is enough for react to render your component after data has fetched so that the query mounting can be successfully de-duplicated. You will still get a refetch when you switch between posts unless you switch within half a second, which seems like a good enough trade-off.
  4. TkDodo closed this

  5. TkDodo added labelsworks as designed

  6. TkDodo added labelshas workaround

  7. Thank you @TkDodo, for not only providing the workaround but also explaining the existing behavior 🙏.

    Since you mentioned that:

    There is also a “special” situation with suspense..

    I think it is loosely related with this PR, #8398.

    Would you mind sharing me some insight of why it has to be staleTime: 1000, rather than any other arbitrary number? Without any configurations, by default, if the rendering takes longer than 1000ms, it would then refetch? I believe it was first introduced since V3.

  8. 1 second is indeed quite arbitrary. it doesn’t matter how long the fetch takes, but yes, we’d expect the component to finish rendering after that time in order to avoid an immediate “refetchOnMount”.

  9. I see, thank you for your time for sharing this 🙏.

Sign in to join this conversation.