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

Preserve no-cache fetchPolicy with notifyOnNetworkStatusChange #7761

Merged
merged 4 commits into from
Mar 12, 2021

Conversation

jcreighton
Copy link
Contributor

@jcreighton jcreighton commented Feb 26, 2021

Fixes #7611 & #6998. While we want to force delivery for an incomplete cache result with loading: true, we should preserve the no-cache fetchPolicy. An additional check has been added to avoid resetting the fetch policy to "cache-and-network" when it's set to "no-cache". Thanks to @benjamn's feedback below, the "notifyOnNetworkStatusChange changes the fetch policy" trickery has been removed. The logic behind that is still valid but it's now centralized in fetchQueryByPolicy's switch statement. This means fetch policies are now preserved even with notifyOnNetworkStatusChange set.

Checklist:

  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

@jcreighton jcreighton force-pushed the 7611-preserve-no-cache-fetchpolicy branch from 680b796 to bf08d7f Compare March 1, 2021 17:49
Copy link
Member

@benjamn benjamn left a comment

Choose a reason for hiding this comment

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

This is on the right track, but I think this will make notifyOnNetworkStatusChange: true have no effect when fetchPolicy: "no-cache", which is probably safe/fine in many cases, but potentially not what people expect when they combine those options.

There may be an opportunity here to get rid of the cache-and-network trick altogether, since we have access to options.notifyOnNetworkStatusChange, options.fetchPolicy, and networkStatus inside the switch statement in fetchQueryByPolicy. I believe the notifyOnNetworkStatusChange trickery was only applicable for network-only and no-cache before (since all the other policies deliver an immediate result), so those might be the only two cases that need modification. Specifically, the network-only case would need to continue behaving like cache-and-network when notifyOnNetworkStatusChange is enabled, but the no-cache case would need to deliver an extra loading: true result without reading from the cache.

If this works out, the logic in these important methods will be a lot easier to reason about, which is exciting (to me, at least)!

@hwillson hwillson removed their request for review March 4, 2021 18:41
@jcreighton jcreighton force-pushed the 7611-preserve-no-cache-fetchpolicy branch 2 times, most recently from 7d3f4f0 to f5e0995 Compare March 9, 2021 18:35
@jcreighton jcreighton force-pushed the 7611-preserve-no-cache-fetchpolicy branch from f5e0995 to 8152b31 Compare March 9, 2021 18:46
Comment on lines -927 to -945
const mightUseNetwork =
fetchPolicy === "cache-first" ||
fetchPolicy === "cache-and-network" ||
fetchPolicy === "network-only" ||
fetchPolicy === "no-cache";

if (mightUseNetwork &&
notifyOnNetworkStatusChange &&
typeof oldNetworkStatus === "number" &&
oldNetworkStatus !== networkStatus &&
isNetworkRequestInFlight(networkStatus)) {
// In order to force delivery of an incomplete cache result with
// loading:true, we tweak the fetchPolicy to include the cache, and
// pretend that returnPartialData was enabled.
if (fetchPolicy !== "cache-first") {
fetchPolicy = "cache-and-network";
}
returnPartialData = true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Love to see this gone!

Copy link
Member

@benjamn benjamn left a comment

Choose a reason for hiding this comment

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

It would be good to add some tests of no-cache plus notifyOnNetworkStatusChange (in case we don't have enough already), but otherwise I'm happy with this. Thanks for digging into the simplification I suggested!

@jcreighton jcreighton force-pushed the 7611-preserve-no-cache-fetchpolicy branch from 1d79890 to 0ba32b3 Compare March 12, 2021 21:09
@jcreighton jcreighton force-pushed the 7611-preserve-no-cache-fetchpolicy branch from ac1348a to b4cd8fd Compare March 12, 2021 21:27
@jcreighton jcreighton merged commit 319993e into main Mar 12, 2021
@jcreighton jcreighton deleted the 7611-preserve-no-cache-fetchpolicy branch March 12, 2021 21:31
benjamn added a commit that referenced this pull request Mar 15, 2021
…cache-fetchpolicy"

This reverts commit 319993e, reversing
changes made to 6120401.

Because #7611 could be a visible change for some applications, we've
decided to relocate these changes onto the release-3.4 branch, rather than
main, so we revert the PR on main, merge main into release-3.4, then
un-revert the PR on the release-3.4 branch.
benjamn added a commit that referenced this pull request Mar 15, 2021
@kliakos
Copy link

kliakos commented Sep 28, 2021

While we want to force delivery for an incomplete cache result with loading: true

Shouldn't this be controlled by the returnPartialData: false option? I mean, what if I don't want to receive a partial result while loading: true, what do I have to do to achieve this, besides fetchPolicy: 'no-cache'?

@vknez
Copy link

vknez commented Dec 3, 2021

While we want to force delivery for an incomplete cache result with loading: true

Shouldn't this be controlled by the returnPartialData: false option? I mean, what if I don't want to receive a partial result while loading: true, what do I have to do to achieve this, besides fetchPolicy: 'no-cache'?

I agree with @kliakos. This created an issue in our large codebase because the types are not correct anymore. TypeScript expects a non-nullable field, but receives undefined, because a partial result was retrieved from cache, even though I never enabled returnPartialData. Is this behavior documented somewhere?

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.

data is cached when fetchPolicy is no-cache and notifyOnNetworkStatusChange:true
4 participants