You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So, if I have a fetchQuery and my own query (which uses useQuery) for the one entity (newsItem in the example), and my own query has specific options (fetchQuery uses default options, own query overrides cacheTime and staleTime). My own query executes on the NewsItem component mount. But, if a data for newsItem is fetched via fetchQuery, my own query is not working for that case. So, in a queryCache that query has options from default, not from my own query.
I understand, that my own query won't start its fetcher, if the data is in the cache. But it was quite unexpected, that options from my own query are not used. Check out the example from codesandbox.
Click to the button with text: "fetch newsitem with id ..." In the ReactQuery devtools you will see, that there is a query with cacheTime and staleTime from default settings.
Click to the button with text "Toggle newsItemComp". The NewsItem will be shown.
Options from the query "useNewsItem", which is used there won't be applied.
Expected behavior
As a user, I expected that useQuery will rewrite options of the query in the reactQuery cache but i am seeing, that options from fetchQuery is used (default options).
hmmm, staleTime for example is definitely applied, because if you click the toggle button, you can see that the query shows up as fresh in the devtools, not stale.
cacheTime is an interesting case. It seems to be set upon creation, and then only updated once a fetch occurs. If you add a refetch button:
you can see that the cacheTime gets applied upon refetching.
It seems that we only set observer level options (like staleTime) upon mounting of a new observer, but not cache level properties, like cacheTime:
hmmm, staleTime for example is definitely applied, because if you click the toggle button, you can see that the query shows up as fresh in the devtools, not stale.
Yes, it works in case of my own query usage. But, if I click to the "fetch newsitem with id ..." button, and after that show the NewsItem component — staleTime won't be 99999, it will be 0, cause of default options from fetcher.
I think this is a bug, but not sure what problems we would introduce if we also updated cache level options in that effect 🤔
Yeah, I have same thoughts) But, I described a real case: fetcher uses the default options, query uses its own options. It is not expected, that data is refetched, when component is mounted, case in its query there are options like:
{cacheTime: 99999,staleTime: 99999,}
These numbers are just for an example)
I think, that the latest execution (fetcher or useQuery) should update query options in a queryCache. And it's ok for another story, when useQuery is used before fetcher. What do you think?
Yes, it works in case of my own query usage. But, if I click to the "fetch newsitem with id ..." button, and after that show the NewsItem component — staleTime won't be 99999, it will be 0, cause of default options from fetcher.
no, that's not the case for me. Am I doing something wrong?
click fetch item with id
wait till the query is resolved
click the toggle button
--> query shows up as fresh, which means staleTime is picked up correctly
It also works if you're toggling while you are still fetching.
Again, I think that's because staleTime is a per-observer option, and mounting a component that has useQuery will create a new observer, so staleTime should always be respected.
@TkDodo yeah, you're right, my bad) staleTime works like a charm. So, yeah, the main problem here is a cacheTime. Moreover, I do not know, if there are problems with other options.
* Use this property to pass information that can be used in other places.
*/
meta?: QueryMeta
Also, the bug only manifests if you're having a staleTime set, because if staleTime is zero, the mount would trigger a background refetch, which would in turn cause the options to update:
Also, the bug only manifests if you're having a staleTime set, because if staleTime is zero, the mount would trigger a background refetch, which would in turn cause the options to update:
There is an exception from this rule — refetchOnMount: false in a defaultOptions. As I understand, even staleTime is zero, a query won't be refetched on mount.
So what we could try is to have the function setOptions on the QueryObserver just call this.currentQuery.setOptions(this.options) so that whenever we set observer level options, the one on the query are updated as well. setOptions is currently private so we'd need to make it public. What do you think? @artem-malko ?
This would ensure that every time when the observer options are updated (where we've identified that this works well, e.g. with `staleTime' ) that query options are also picked up correctly, even if no fetch is happening.
I think this is affecting us right now. We use fetchQuery to prefetch a query from within getServerSideProps in Next.js. We then use useQuery with a matching queryKey and any time we run invalidateQueries (either via the dev tools or programmatically), a default query function is used instead of the queryFn that is being passed into the useQuery.
For what it is worth, the issue sometimes fixes itself when I save changes locally and fast refresh kicks in. The invalidation query then picks up the right queryFn.
Expected: The Query refreshes using the custom query function
Current: The Query refreshes using the default query function which doesn't work
Note: Please note that any source changes that trigger the hot reload somehow fix this issue and clicking the Trigger Bug button then works as expected. For us it also works when we click the button a few times in a row after it failed for the first time and used all the retry attempts available. Please let me know if I should open a new ticket.
This can happen when the query is hydrated or created with setQueryData.
The problem is that in your case, we have a function set, because you have defined a default query function.
one potential workaround I've found is to explicitly pass queryFn: undefined as hydrate options to avoid that the default queryFn is being picked up for that query:
@TkDodo Thanks! Looks like you've shared my sandbox, did you mean to share a link to your fork?
EDIT: Nevermind, I just tried this in our project and it seems to have resolved the issue, thanks again! As an aside, is this fixable so the workaround is not needed? While we do intend to get rid of the default function (that's how we started before realizing custom query fns were the way to go), it'll take us some time to migrate the old code.
It seems that, as analyzed, options on the Query itself are not updated, while options on the observer are. That means options like retryDelay are not reactive - if you set them depending on state, they will not change.
I think this is quite confusing indeed, and I think it should be fixed. In the meantime, mutations were also changed to have all options updated, so it would make sense to streamline.
Activity
TkDodo commentedon Jun 13, 2022
hmmm,
staleTimefor example is definitely applied, because if you click the toggle button, you can see that the query shows up asfreshin the devtools, notstale.cacheTimeis an interesting case. It seems to be set upon creation, and then only updated once a fetch occurs. If you add a refetch button:<button onClick={() => queryResult.refetch()}>refetch</button>you can see that the cacheTime gets applied upon refetching.
It seems that we only set observer level options (like staleTime) upon mounting of a new observer, but not cache level properties, like
cacheTime:query/src/reactjs/useBaseQuery.ts
Lines 97 to 101 in e01a7bb
I think this is a bug, but not sure what problems we would introduce if we also updated cache level options in that effect 🤔
artem-malko commentedon Jun 13, 2022
Yes, it works in case of my own query usage. But, if I click to the "fetch newsitem with id ..." button, and after that show the NewsItem component — staleTime won't be 99999, it will be 0, cause of default options from fetcher.
Yeah, I have same thoughts) But, I described a real case: fetcher uses the default options, query uses its own options. It is not expected, that data is refetched, when component is mounted, case in its query there are options like:
These numbers are just for an example)
I think, that the latest execution (fetcher or useQuery) should update query options in a queryCache. And it's ok for another story, when useQuery is used before fetcher. What do you think?
TkDodo commentedon Jun 13, 2022
no, that's not the case for me. Am I doing something wrong?
fetch item with id--> query shows up as
fresh, which meansstaleTimeis picked up correctlyIt also works if you're toggling while you are still fetching.
Again, I think that's because
staleTimeis a per-observer option, and mounting a component that hasuseQuerywill create a new observer, sostaleTimeshould always be respected.artem-malko commentedon Jun 13, 2022
@TkDodo yeah, you're right, my bad)
staleTimeworks like a charm. So, yeah, the main problem here is acacheTime. Moreover, I do not know, if there are problems with other options.By the way, how do you do such videos?)
TkDodo commentedon Jun 13, 2022
I think all observer options should be fine:
query/src/core/types.ts
Line 100 in f77f6f4
and all options on the query cache itself should be affected:
query/src/core/types.ts
Lines 65 to 97 in f77f6f4
Also, the bug only manifests if you're having a
staleTimeset, because ifstaleTimeiszero, the mount would trigger a background refetch, which would in turn cause the options to update:query/src/core/query.ts
Lines 339 to 341 in 270efe6
A tool called monosnap: https://monosnap.com/
artem-malko commentedon Jun 13, 2022
There is an exception from this rule —
refetchOnMount: falsein a defaultOptions. As I understand, even staleTime is zero, a query won't be refetched on mount.TkDodo commentedon Sep 18, 2022
So what we could try is to have the function
setOptionson theQueryObserverjust callthis.currentQuery.setOptions(this.options)so that whenever we set observer level options, the one on the query are updated as well.setOptionsis currently private so we'd need to make it public. What do you think? @artem-malko ?artem-malko commentedon Sep 19, 2022
@TkDodo do you mean something like that:
TkDodo commentedon Sep 19, 2022
no what I mean was, when
setOptionson the observer is called:query/packages/query-core/src/queryObserver.ts
Line 143 in 7e51776
we could also do:
probably somewhere here:
query/packages/query-core/src/queryObserver.ts
Lines 178 to 179 in 7e51776
This would ensure that every time when the observer options are updated (where we've identified that this works well, e.g. with `staleTime' ) that query options are also picked up correctly, even if no fetch is happening.
artem-malko commentedon Sep 19, 2022
Aaa, now I understand) Looks really nice)
TkDodo commentedon Sep 19, 2022
do you want to contribute it @artem-malko ?
artem-malko commentedon Sep 19, 2022
I can do it somewhere in the end of October, in 20th. As I understand, it is not a critical right now, so, it can wait till I'll be free?
TkDodo commentedon Sep 19, 2022
Sure, thanks 🙏
TkDodo commentedon Dec 9, 2022
@artem-malko any updates on this issue? If you're not working on it, please unassign yourself, thanks.
artem-malko commentedon Dec 11, 2022
Sorry for the delay) I've started it a couple days ago
mareksuscak commentedon Aug 8, 2023
I think this is affecting us right now. We use
fetchQueryto prefetch a query from withingetServerSidePropsin Next.js. We then useuseQuerywith a matchingqueryKeyand any time we runinvalidateQueries(either via the dev tools or programmatically), a default query function is used instead of thequeryFnthat is being passed into theuseQuery.For what it is worth, the issue sometimes fixes itself when I save changes locally and fast refresh kicks in. The invalidation query then picks up the right
queryFn.TkDodo commentedon Aug 19, 2023
@mareksuscak not sure if this is really related. Can you show a separate codesandbox reproduction for this please?
mareksuscak commentedon Sep 5, 2023
@TkDodo
Here you go:
https://codesandbox.io/p/sandbox/bold-banzai-7hynrj
Steps to reproduce:
Expected: The Query refreshes using the custom query function
Current: The Query refreshes using the default query function which doesn't work
Note: Please note that any source changes that trigger the hot reload somehow fix this issue and clicking the Trigger Bug button then works as expected. For us it also works when we click the button a few times in a row after it failed for the first time and used all the retry attempts available. Please let me know if I should open a new ticket.
CleanShot.2023-09-05.at.15.21.51.mp4
TkDodo commentedon Sep 5, 2023
@mareksuscak yeah, it's a different issue :)
we actually have a "protection" to use the options (including queryFn) from the first observer if the query itself doesn't a QueryFunction:
query/packages/query-core/src/query.ts
Lines 347 to 354 in 13b17ee
The comment even mentions:
The problem is that in your case, we have a function set, because you have defined a default query function.
one potential workaround I've found is to explicitly pass
queryFn: undefinedas hydrate options to avoid that the default queryFn is being picked up for that query:Here's a working fork of your sandbox: https://codesandbox.io/p/sandbox/bold-banzai-7hynrj?file=/pages/_app.tsx:48,1-54,10
mareksuscak commentedon Sep 5, 2023
@TkDodo Thanks! Looks like you've shared my sandbox, did you mean to share a link to your fork?
EDIT: Nevermind, I just tried this in our project and it seems to have resolved the issue, thanks again! As an aside, is this fixable so the workaround is not needed? While we do intend to get rid of the default function (that's how we started before realizing custom query fns were the way to go), it'll take us some time to migrate the old code.
TkDodo commentedon Sep 6, 2023
sorry, here's the working version:
https://codesandbox.io/p/sandbox/practical-wood-9ykp2p?file=/pages/_app.tsx:50,1-55,13
artem-malko commentedon Oct 22, 2023
Looks like we can close that issue) I can live with current behaviour and looks like it was just my problem)
TkDodo commentedon Mar 11, 2024
reopening this issue because I think it is really an issue, and it was reported again here:
It seems that, as analyzed, options on the Query itself are not updated, while options on the observer are. That means options like
retryDelayare not reactive - if you set them depending on state, they will not change.I think this is quite confusing indeed, and I think it should be fixed. In the meantime, mutations were also changed to have all options updated, so it would make sense to streamline.