Repository navigation
Description
Area of Improvement
I had a computationally expensive (for the server) useQuery whose input was tied to what the user typed. I wanted my fetch requests to be cancelled as soon as possible if the input was changed, to keep the server from doing unnecessary work and also avoid caching a bunch of responses that weren't going to be used. From reading the React Query docs (note the Default Behavior section, which I've quoted below), I knew I wanted tRPC to consume the signal from the query function; consuming the signal would cause react-query to cancel and throw away any in-flight requests for unused queries immediately. It took me a while, but I eventually found out about tRPC's abortOnUnmount, and correctly suspected that setting that option would cause tRPC to consume the signal as I wanted.
So the issue is that abortOnUnmount doesn't just cause the query to abort on unmount. It causes tRPC to consume the signal from react-query, which has the additional effect of causing react-query to cancel the unfinished request for your previous query key if you change your useQuery to have a new query key mid-request (and this can happen whenever you change your tRPC input). The tRPC documentation on the website and in the JSDoc doesn't explicitly state that setting the flag will cause tRPC to consume the AbortSignal provided by react-query.
From the react-query docs, emphasis mine:
By default, queries that unmount or become unused before their promises are resolved are not cancelled. This means that after the promise has resolved, the resulting data will be available in the cache. This is helpful if you've started receiving a query, but then unmount the component before it finishes. If you mount the component again and the query has not been garbage collected yet, data will be available.
However, if you consume the AbortSignal, the Promise will be cancelled (e.g. aborting the fetch) and therefore, also the Query must be cancelled. Cancelling the query will result in its state being reverted to its previous state.
Link to related docs
Additional information
No response
👨👧👦 Contributing
- 🙋♂️ Yes, I'd be down to file a PR implementing the suggested changes!
Activity
TkDodo commentedon Nov 6, 2023
I think this more than just a documentation change -
abortOnUnmountis wrongly named and we should consider renaming it for v11. It not only aborts on unmount or on QueryKey change - it will also abort whenqueryClient.cancelQueries()(orutils.someRoute.cancel()in trpc) is invoked.Two examples where this was discussed / misunderstood:
renaming the option to
abort: booleanand updating the documentation accordingly should be enough to clear up the confusion.keanemind commentedon Nov 6, 2023
I concur. Ideally the field name is also changed, in addition to documenting the behavior.
ArjunCodess commentedon Sep 20, 2026
hi, is this still wanted? the issue is from 2023 and on current main it still is not there: the docs still say abortOnUnmount only aborts on unmount, and the name is unchanged on v11.19.x. tkdodo said it also aborts on cancelQueries and should be renamed in v11. should i send a docs pr, a rename, or both?
KATT commentedon Nov 2, 2026
🤖 In v12 there's no flag to misname: queries always pass TanStack's
signal, which is v11'sabortOnUnmount: true, so a query nothing observes any more (the component unmounted, or the input changed), or one hit byqueryClient.cancelQueries(), aborts the call on both ends and reverts to its previous state instead of caching a late result. The docs say so: https://v12.trpc.io/v12/docs/integrations/tanstack-query#cancellation. For v11, @ArjunCodess, a docs-only PR saying the same aboutabortOnUnmountis welcome, without a rename. v12 isn't released yet and there's no ETA.