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

fix(Apollo): Set initialFetchPolicy to 'cache-first' and implement a basic nextFetchPolicy #870

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/core/helpers/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,42 @@ const createApolloClient = (headers: IncomingHttpHeaders | null = null) => {
cache,
defaultOptions: {
watchQuery: {
fetchPolicy: "cache-and-network",
initialFetchPolicy: "cache-first",
nextFetchPolicy(
currentFetchPolicy,
{
// Either "after-fetch" or "variables-changed", indicating why the
// nextFetchPolicy function was invoked.
reason,
// The rest of the options (currentFetchPolicy === options.fetchPolicy).
options,
// The original value of options.fetchPolicy, before nextFetchPolicy was
// applied for the first time.
initialFetchPolicy,
// The ObservableQuery associated with this client.watchQuery call.
observable,
},
) {
// When variables change, the default behavior is to reset
// options.fetchPolicy to context.initialFetchPolicy. If you omit this logic,
// your nextFetchPolicy function can override this default behavior to
// prevent options.fetchPolicy from changing in this case.
if (reason === "variables-changed") {
return initialFetchPolicy;
}

if (
currentFetchPolicy === "network-only" ||
currentFetchPolicy === "cache-and-network"
) {
// Demote the network policies (except "no-cache") to "cache-first"
// after the first request.
return "cache-first";
}

// Leave all other fetch policies unchanged.
return currentFetchPolicy;
},
},
},
});
Expand Down
Loading