-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Conversation
680b796
to
bf08d7f
Compare
There was a problem hiding this 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 case
s 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)!
7d3f4f0
to
f5e0995
Compare
f5e0995
to
8152b31
Compare
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; | ||
} |
There was a problem hiding this comment.
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!
There was a problem hiding this 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!
1d79890
to
0ba32b3
Compare
ac1348a
to
b4cd8fd
Compare
…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.
Shouldn't this be controlled by the |
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 |
Fixes #7611 & #6998. While we want to force delivery for an incomplete cache result with
loading: true
, we should preserve theno-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 infetchQueryByPolicy
'sswitch
statement. This means fetch policies are now preserved even withnotifyOnNetworkStatusChange
set.Checklist: