Skip to content
Source
Sign inSign up
TanStack/query

Cancelation not working with useSuspenseQuery #7889

Send to my agent
Closedromain-trotard(GitHub profile, opens in a new tab) opened this issue 3 comments

Describe the bug

Hello,

I have implemented cancelation in my project, but it does not seem to work when working with Suspense and useSuspenseQuery / useSuspenseQueries hooks. I have tested with useQuery and it works perfectly.

I don't see any mention in the documentation that the cancelation does not work with suspense, and don't see at first sight any reason it would be impossible to cancel queries when working with Suspense.

Thanks for the library which is really cool :)

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-5dtu8c?file=src%2FApp.tsx

Steps to reproduce

  1. Click on the increment button that will simulate multiple call to get the person information for the id: count
  2. See in the console that there is no log concerning the cancelation of the request

ps: it's also possible to simulate the unmounting of the component while a request is running (it does not cancelled the request too)

Expected behavior

I expect that running queries are cancelled

How often does this bug happen?

Every time

Screenshots or Videos

Cancelation working with useQuery:

https://github.com/user-attachments/assets/c8c19efe-343b-4104-8240-087639701147

Cancelation not working with useSuspenseQuery:

https://github.com/user-attachments/assets/d6747dbc-a709-44db-957c-14f0dee5c973

Platform

  • OS: Mac OS 14.6.1
  • Browser: Chrome 127.0.6533.100

Tanstack Query adapter

react-query

TanStack Query version

5.51.23

TypeScript version

5.5.4

Additional context

No response

  1. Hm, I think I know why that's the case: We only trigger a cancelation as soon as the last observer unsubscribes. But with suspense, the observer never subscribes in the first place, because we throw the promise to react before that happens in useSyncExternalStore.

    I need to think about if that's even possible or if it's a known limitation that it doesn't work with suspense.

  2. TkDodo added labelssuspense

  3. after discussing this on twitter, I don't think there is a good way to solve this:

    https://x.com/phry/status/1825828334502113295

    We can mark it as a known limitation in the docs. Would you like to contribute that?

  4. TkDodo added labelsknown limitation

  5. Hey Dominik Thank you for the exploration and the explanation of the problem. I can definitely contribute by marking it as a known limitation in the documentation :)

  6. TkDodo closed this

Sign in to join this conversation.