Disabled queries (enabled: false) get affected by invalidateQueries (refetchType: "all") when they are inactive (not being rendered anymore) #8118
Describe the bug#
This is similar to an old bug: https://github.com/TanStack/query/issues/3642
But now this only happens, if the disabled query isn't actively rendered anymore and TanStack Query still has it in the cache.
Then the query will be executed even though it was never enabled.
Your minimal, reproducible example#
https://codesandbox.io/p/sandbox/brave-khayyam-62yx75
Steps to reproduce#
In the CodeSandbox you have a button to invaldate the Query and another button to toggle if the query is being actively rendered.
If it's not actively being rendered, you can see that the queries get fetched on invalidation even though it was always disabled.
Expected behavior#
Disabled queries shouldn't be fetched on invalidateQueries when they never were enabled in the first place.
How often does this bug happen?#
Every time
Screenshots or Videos#
https://share.cleanshot.com/Msh4Bk4q
Platform#
- OS: macOS
- Browser: Arc (Chrome)
- Version: 1.61.2 (54148) (Chromium Engine Version 129.0.6668.71)
Tanstack Query adapter#
react-query
TanStack Query version#
v5.56.2
TypeScript version#
No response
Additional context#
No response
ah I see! .. my initial thought was: why put a query into the cache when it wasn't enabled in the first place?
there are probably arguments against this, which I'm not thinking about atm, but wouldn't it be okay to not even keep disabled queries in the cache (because they weren't even executed) .. which would then also fix this issue?
While most cases will have a query either enabled or disabled, you can temporary enable / disable a query. For example, you can have a list query that you want to disable while a dialog is open so that it doesn’t refetch until the dialog is closed. So it’s not that black & white.
However, I think the answer is simply
skipToken. Whenenabledis passed, we lose that information once the observer unmounts. ButskipTokenis passed in place of thequeryFn.So I think we can consider a query do bedisabledif it has aqueryFnthat is equal toskipToken. This check would need to go here:would you like to maybe contribute that + a test case?
Would this get closer to a deprecation of
enabledin favor ofskipToken?Would this get closer to a deprecation of
enabledin favor ofskipToken?yeah maybe. I’ll think about it
TkDodo added labelspackage: query-core
TkDodo closed this
wow, thank you very much for the super fast fix @TkDodo ❤️
Sign in to join this conversation.
oh wow. problem is that
enabledis an observer level property. As soon as there is no observer anymore, a query cannot be “enabled” or “disabled”. This isn’t a problem if you use the standard invalidation, but if you bypass it to refetch everything, this will hit too.wild that this was never reported before. need to think about what we can do here (if anything) because a query cannot (per definition) be enabled/disabled - only an observer can.