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

Ensure remounted components properly adhere to fetchPolicy and nextFetchPolicy #10239

Closed
wants to merge 7 commits into from

Conversation

jerelmiller
Copy link
Member

Fixes #10222

This fix ensures the fetchPolicy and nextFetchPolicy is properly adhered to when a component unmounts, then remounts. In v3.6.8 and below, a fetchPolicy of network-only and nextFetchPolicy of cache-first would properly return the loading state when remounted. In v3.6.9 and above, a remounted component would return cached data too early due to a change in #9823 that synchronously updated the fetchPolicy as soon as the initial query was kicked off.

Internally, we were using observableQuery.getCurrentResult() in our onNext function passed to the subscription to get the result of the query, rather than relying on the argument passed to onNext. This meant in some cases where onNext was triggered, we were calling observableQuery.getCurrentResult() with a fetch policy that had already been changed to the nextFetchPolicy, therefore we were returning cached data too early (in other words, the network-only result hadn't yet finished loading).

I opted to patch useQuery with some of the behavior in observableQuery.getCurrentResult() to try and limit the blast radius of the changes. The patch really should be a part of QueryManager, but I didn't know how how many people rely on the current behavior in how results are returned (which differ from observableQuery.getCurrentResult().

@@ -270,7 +270,6 @@ export class ObservableQuery<
// terminates after a complete cache read, we can assume the next result
// we receive will have NetworkStatus.ready and !loading.
if (
diff.complete &&
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is already checked on line 264. To reduce noise, I went ahead and removed this since we already know its true.

@jerelmiller
Copy link
Member Author

The more I work with this change, the less confident I feel in the solution. While I believe that using the argument passed to onNext is the right value we should be using (since it should technically be correct), there have been a lot of edge cases popping up with differences in data between the argument and obserableQuery.getCurrentResult(). It seems there is more done under the hood for getCurrentResult than meets the eye.

Given how much has been affected by this seemingly small change, it might be a signal that releasing an update such as this could have wide-ranging negative impacts. We can hope our tests catch 99% of them, but I'm not so sure. Something I've noticed while working on this fix is that many of our tests do not check for networkStatus. I've seen a case or two where the network status is completely different between the argument and the getCurrentResult() call, but the test doesn't seem to be catching it. I've only noticed the difference because a test might be failing for other reasons.

This change is what caused the unmount/remount bug, but I think @benjamn's reasoning for this change is sound and will help prevent other types of bugs. It feels a bit like a game of whack-a-mole at this point where fixing one thing seems to cause something else.

At this point, I feel I might need to consider a different approach. Ultimately I think we should highly consider fixing the root issue where we are unable to get the correct data through the onNext argument, but this fix feels a bit too costly right now.

@jerelmiller jerelmiller marked this pull request as draft November 2, 2022 21:05
@dairyisscary
Copy link

dairyisscary commented Dec 1, 2022

glad to see this is getting some attention, thanks @jerelmiller.

if you get a fix out, even if its in an alpha build or something, i'll happily test it for you in a substantially sized application and report any problems I find (our multi hundred thousand line app basically exclusively uses this fetch policy combo + "no-cache" in a few instances).

@jerelmiller
Copy link
Member Author

@dairyisscary sounds great! I've stepped away from this a bit as I've been focused on adding suspense support to the client, but I'm hopeful I can come back to this sometime soon to come up with a better solution than whats in this PR.

@jerelmiller
Copy link
Member Author

It appears this was fixed by #10631 and is no longer needed. I'm going to go ahead and close this out as this issue has already been solved.

@jerelmiller jerelmiller closed this Feb 5, 2024
@jerelmiller jerelmiller deleted the issue-10222-next-fetch-policy branch February 5, 2024 21:49
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected Behavior with fetchPolicy "network-only" and nextFetchPolicy "cache-first" In 3.7.x
2 participants