-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redundant queries invalidation when combining useQuery
with fetchQuery
in v5
#7129
Comments
interesting. deduplication happens correctly when you use |
I made a snippet with more logs which shows that upon invalidation, the query
So all in all, I guess the dependent queries just don't play well with the I'm wondering - wouldn't it be a more sensible/intuitive behavior of the |
Isn't that what it is doing ? E.g.:
will, for each matching query, cancel it if it already fetches and re-start a fetch. I'm also curious why this doesn't happen when calling query/packages/query-core/src/queryClient.ts Line 294 in e34bf4d
|
I don't think so - the problem with the current implementation of My understanding of what goes on with the snippet upon running
My understanding is that does it make sense? |
yeah this makes sense, thanks for looking into it. Because all queries are invalidated upfront, both fn1 and fn2 are invalid by the time fn1 refetches. Then, it will also refetch fn2 from within its queryFn. let's take a step back. What's the reason for using |
The snippet could be probably modified to use |
my recommendation would be to switch to The problem with calling |
Thanks for the suggestion. We are aware of this implication and we are solving (to a good enough extent for us) the issue of inactive observers by using
|
inactive observers are garbage collected after
that's a good point. You can set if you don't want to change anything, I think the best move is to go from |
yes, that's kind of what we do already, just by setting Nevertheless, I believe that the current behavior of |
Describe the bug
Consider the below code that uses
fetchQuery
insideuseQuery
. Note that this is meant to be a minimalistic example, you can also find a link for playground here https://codesandbox.io/p/sandbox/pedantic-kapitsa-jvv78v?file=%2Fsrc%2FApp.js:Now the problem is that when
refetch
is called and invalidation happens the following is being logged:Though, given the example uses
staleTime: Infinity
and the react-query is meant to guarantee that duplicate requests are not fired I would expect to see this sequence instead:or
Even if react-query runs separate fetch for
fn2
, one forfetchQuery
, and one foruseQuery
, this does not explain whyfn1
is being called two times.We noticed this behavior once updating from v3. Though it seems to go away when using
cancelRefetch: false
it still seems incorrect even withcancelRefetch: true
. The motivation for usingfetchQuery
insideuseQuery
callback is that we often use it to reuse other query functions from imperative code and also cache their values at the same time, while avoiding duplicate requests.Could you please confirm whether is this behavior expected and if yes point to some docs or explain why, or confirm that its a bug?
Thanks a lot.
Your minimal, reproducible example
https://codesandbox.io/p/sandbox/pedantic-kapitsa-jvv78v?file=%2Fsrc%2FApp.js
Steps to reproduce
Expected behavior
I would expect to only see
FN_1, FN_2
logs in the console orFN_1, FN_2, FN_2
.How often does this bug happen?
Every time
Screenshots or Videos
[
Screen.Recording.2024-03-17.at.11.33.42.mov
](url)
Platform
OS - [macOS]
browser - Chrome
version - 122.0.6261.112
Tanstack Query adapter
None
TanStack Query version
5.27.3
TypeScript version
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: