Skip to content
Source
Sign inSign up
TanStack/query

Dev-tools error on 'Trigger Loading' clicking for slower networks. #5157

Send to my agent
ClosedSTIAANWOL(GitHub profile, opens in a new tab) opened this issue 6 comments

Describe 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

  1. Limit network speed in browser dev tools.
  2. 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

image image

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

  1. TkDodo added labelspackage: react-query-devtools

  2. TkDodo added labelsbug

  3. I think the problem is that restore loading actually goes and fetches the data again. I was hoping that setting the query into loading state 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?

  4. @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.

  5. 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:

    https://github.com/TanStack/query/blob/63943c66cfeece65708534b05c40002cd7936db1/packages/react-query/src/useBaseQuery.ts#L95-L98

    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 loading button in the devtools ...

  6. @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 existing fetchMeta and 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 __previousQueryOptions doesn'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 __previousQueryOptions is undefined. LMK if this seems like a correct solution, and if I should open a PR with the fix?

  7. sounds good 👍

  8. TkDodo closed this

Sign in to join this conversation.