Skip to content

Conversation

@TkDodo
Copy link
Collaborator

@TkDodo TkDodo commented Oct 15, 2021

for refetchQueries, as well as for invalidateQueries

for refetchQueries, as well as for invalidateQueries
@vercel
Copy link

vercel bot commented Oct 15, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/tannerlinsley/react-query/6uJzVJgj49NYGHoLMfGzFjPeVvmX
✅ Preview: https://react-query-git-fork-tkdodo-feature-cancel-284254-tannerlinsley.vercel.app

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 15, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 65bcf42:

Sandbox Source
tannerlinsley/react-query: basic Configuration
tannerlinsley/react-query: basic-typescript Configuration

test for making sure that refetchQueries respects the cancelRefetch flag
@TkDodo TkDodo merged commit 255f7c6 into TanStack:master Oct 15, 2021
@TkDodo TkDodo deleted the feature/cancel-refetch branch October 15, 2021 10:44
@tannerlinsley
Copy link
Member

🎉 This PR is included in version 3.27.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@dimagimburg
Copy link

dimagimburg commented Oct 17, 2021

@TkDodo Hey, until now we used whats written in this page for cancellation, the requests were cancelled without providing cancelRefetch flags.

Will this behavior change in some way?

(i.e similar to the test case you provided, basically works in our project without providing any cancelRefetch flag when using invalidateQueries 🤷 )

@TkDodo
Copy link
Collaborator Author

TkDodo commented Oct 17, 2021

Query cancellation, as described in the docs page, is just a mechanism to actually cancel the fetch on transport level. The cancelRefetch flag is used to actually let react-query "cancel" currently ongoing fetches. It's equivalent to calling queryClient.cancelQueries before doing the invalidation. That cancellation might lead to a cancellation on network level (if a .cancel method was added to the promise). otherwise, react-query will just ignore the result.

This PR has been sparked by this twitter discussion, where I outline some cases where invalidation alone might not be enough: https://twitter.com/TkDodo/status/1448677532983832580

The bottom line is: If a fetch is already in progress and you instruct react-query to refetch (e.g. via invalidateQueries), it will not do that per default, because a fetch is already going on. With cancelRefetch, the ongoing fetch will be cancelled and a new fetch will be started.

I think we might make this the default behaviour in the next major. If react-query is explicitly instructed to refetch, I think it should do so :)

@dimagimburg
Copy link

dimagimburg commented Oct 17, 2021

Thanks for this explanation @TkDodo.

Just mentioning it seems to be a different behavior than what was before cancelRefetch. I would suggest also to delegate the default decision to the client

new QueryClient({
        defaultOptions: {
            queries: {
                cancelRefetch: true
            }
        }
    }

(as I don't see it in QueryObserverOptions).

For example we pretty much rely on cancellation by default (well, not rely, but it helps us with high-bandwidth requests), and would want to avoid adding cancelRefetch to all invalidateQueries because the default is false.

@TkDodo
Copy link
Collaborator Author

TkDodo commented Oct 17, 2021

Just to be on the same page: This PR didn't change anything regarding the default behaviour. cancelRefetch was previously accepted as an option, but it was never passed to the actual fetch function - so it was always false per default.

Now, you have the option of setting it to true if you want. If things worked for your use-cases before, they should certainly work now, too :)

@VanTanev
Copy link
Contributor

I'm commenting here before opening an issue.

Should the docs at https://react-query.tanstack.com/guides/optimistic-updates be updated, so that

   onSettled: () => {
     queryClient.invalidateQueries('todos')
   },

uses { cancelRefetch: true } ?

@TkDodo
Copy link
Collaborator Author

TkDodo commented Oct 27, 2021

@VanTanev i don’t think so. We’re already canceling in onMutate, which should be enough for most cases.

@VanTanev
Copy link
Contributor

@TkDodo I wasn't sure, thanks for clarifying!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants