Skip to content
Source
Sign inSign up
TanStack/query

Inactive query is not garbage collected when a useSuspenseQuery is made inactive while in fetching state #8159

Send to my agent
Closedcirex-web(GitHub profile, opens in a new tab) opened this issue 5 comments

Describe the bug

I set gcTime to 1000, so we should expect inactive queries to disappear from the cache pretty quickly. This does not happen when we inactivate the query while it is still being fetched.

useQuery doesn't have this bug, however.

Your minimal, reproducible example

https://github.com/cirex-web/tanstack-gc-bug

Steps to reproduce

See video above

Expected behavior

I expected the query cached value to be deleted within 1s of being inactive.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

OS - macOS Browser - Chrome Browser Version - 129.0.6668.71

Tanstack Query adapter

react-query

TanStack Query version

v5.59.8

TypeScript version

v5.6.3

Additional context

No response

  1. TkDodo added labelssuspense

  2. TkDodo added labelspackage: react-query

  3. Thanks for reporting this. It’s worth noticing that it works fine with useQuery, but not with useSuspenseQuery. I think we’re “missing” to fire the gcTimer somewhere 🙈

  4. here’s the same thing with useQuery and it garbage collects just fine:

    https://stackblitz.com/edit/github-eryyap?file=src%2Fapp%2Fsubpage%2Fpage.tsx

  5. It would also be great if the minimum gcTime was smaller (like 100ms). What I want to do is pretty much guarantee that the cache isn't being used after leaving and then re-opening the subpage. (I'm aware that we can also just clear the cache manually or check for the isFetchedAfterMount property on subpage queries, but if there are like 10 useQuery calls happening in the subpage, that might be a bit of a hassle...)

  6. It's also interesting that garbage collection only fails when the observer gets removed during the first fetch

  7. TkDodo closed this

  8. Thanks for resolving! That was fast

Sign in to join this conversation.