Dev-tools error on 'Trigger Loading' clicking for slower networks. #5157
Send to my agentDescribe the bug#
Hard error gets thrown when 'Trigger Loading' is clicked excessively on slower networks.
Your minimal, reproducible example#
https://codesandbox.io/s/react-query-dev-tools-trigger-loading-bug-xltgjc
Steps to reproduce#
- Limit network speed in browser dev tools.
- Navigate to ReactQuery dev-tools choose the appropriate query key and click on 'Trigger loading' button excessively.
Expected behavior#
Not to throw a hard error for excessive clicking on 'Trigger loading' when on slower network speeds.
How often does this bug happen?#
Can replicate consistently.
Screenshots or Videos#

https://user-images.githubusercontent.com/8208674/226138014-9f772975-9641-4a3c-b82b-d1058b5bfb24.mp4
Platform#
OS: Windows Browser: Latest Firefox and Chrome
Tanstack Query adapter#
None
TanStack Query version#
v4.27.0
TypeScript version#
No response
Additional context#
No response
TkDodo added labelspackage: react-query-devtools
TkDodo added labelsbug
@TkDodo So I initially tried exactly that, basically just setting the status, but I tested it with Suspense and it wasn't triggering loading in that case. Perhaps there's another way to do it that can maintain the data but also trigger the suspense boundary. We might be able to store the previous data and set that in the cache.
ah yes, for a suspense boundary to trigger, you need to "throw the promise", and we only do that if we are really in loading state:
However, this would mean that the Promise would resolve likely pretty fast, so it wouldn't stay in that state. I think for suspense, what we would need to do is throw a new Promise that we would only resolve once we hit the
restore loadingbutton in the devtools ...@TkDodo You are right, I tried this locally and the problem happens when the API request takes a lot of time and someone manages to click the restore loading button more than once.
I tried to debug the problem and found that whenever the user clicks on the Restore loading button, it triggers the fetch with the original queryFn which is there in the
__previousQueryOptions, but when this fetch is trigerred it clears the existingfetchMetaand hence the__previousQueryOptions. So if the user manages to click the button again while the query is still fetching the error in the issue is thrown because__previousQueryOptionsdoesn't exist anymore.I was able to fix the issue by modifying trigger/restore loading function, to return early from the function if the query is fetching and
__previousQueryOptionsisundefined. LMK if this seems like a correct solution, and if I should open a PR with the fix?sounds good 👍
@TkDodo Opened a PR here: #5311
TkDodo closed this
Sign in to join this conversation.
I think the problem is that
restore loadingactually goes and fetches the data again. I was hoping that setting the query intoloadingstate would somehow store the old data, and then just re-store that one instead of really fetching it again 🤔@paul-sachs what do you think?