Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refetch should not happen if no active subscribers #1974

Merged
merged 4 commits into from
Feb 1, 2022

Conversation

AlexanderArvidsson
Copy link
Contributor

@AlexanderArvidsson AlexanderArvidsson commented Jan 29, 2022

Fixes #1530

All logic that checks if there are active subscriptions in the code does a falsey check as well as a keys length check on the subscription sub state, except for the code that refetches valid queries.
Here is an example from the handleUnsubscribe function.
Here is another from invalidateTags.
This PR includes the same check to resolve this issue. See #1530 for more information.
Attempting a refetch for data that has no subscribers should remove it from the cache, similar to invalidateTags, because the data would become stale relative to other cache data which would be refetched on focus / reconnect.

@phryneas
Copy link
Member

Ha, timing ^^

What I said in the other thread pretty much applies here - "not fetching" in that case is not really an option, as that would leave stale data in the cache. In this case, the data would need to be removed from the cache, like it is in the case of cache invalidation.
Could you please adjust the PR accordingly?

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 29, 2022

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 f80108e:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
reduxjs/redux-toolkit Configuration
@examples-action-listener/counter Configuration

@AlexanderArvidsson
Copy link
Contributor Author

I will adjust the PR tomorrow with the fixes, as per our discussion in the issue.

@AlexanderArvidsson
Copy link
Contributor Author

AlexanderArvidsson commented Jan 30, 2022

@phryneas I changed the logic now and wrote a test for it. I considered placing the logic in cleanup.test.tsx but since refetchValidQueries is only run when refetching on focus or reconnect, I added it in refetchBehaviors.test.tsx.

Is it necessary to also include the same test for reconnect? They run the same function, so if the other tests for reconnect passes, and my new test passes, then it should be guaranteed to pass on reconnect too.

@AlexanderArvidsson
Copy link
Contributor Author

@phryneas That should be the last changes. If there is anything else you'd like me to include, I'll be glad to.

When we can we expect this to be published? The current logic (where the data is refetched) results in quite a high performance impact on both backend and frontend in our current project. I am sure however we could work around it for now by disabling refetching on our specific endpoints.

@phryneas
Copy link
Member

phryneas commented Feb 1, 2022

I'l try tol take a look at it this evening or tomorrow.
Afaik, @markerikson wanted to do a patch release soon so we should be able to get this in.

@phryneas
Copy link
Member

phryneas commented Feb 1, 2022

Looks good to me. Let's get this in.

@phryneas phryneas merged commit 67d41a6 into reduxjs:master Feb 1, 2022
@phryneas
Copy link
Member

phryneas commented Feb 3, 2022

Released in https://github.com/reduxjs/redux-toolkit/releases/tag/v1.7.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

RTK Query - refetchOnFocus refetches unmounted queries
2 participants