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

make disableNetworkFetches reactive #8721

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
{
"name": "apollo-client",
"path": "./dist/apollo-core.cjs.min.js",
"maxSize": "24.7 kB"
"maxSize": "24.9 kB"
}
],
"engines": {
Expand Down
49 changes: 42 additions & 7 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export function mergeOptions<
export class ApolloClient<TCacheShape> implements DataProxy {
public link: ApolloLink;
public cache: ApolloCache<TCacheShape>;
public disableNetworkFetches: boolean;
public version: string;
public queryDeduplication: boolean;
public defaultOptions: DefaultOptions = {};
Expand All @@ -101,6 +100,14 @@ export class ApolloClient<TCacheShape> implements DataProxy {
private clearStoreCallbacks: Array<() => Promise<any>> = [];
private localState: LocalState<TCacheShape>;

// This boolean is set by ssr-related options/properties like
// options.ssrMode, options.ssrForceFetchDelay, and
// options.disableNetworkFetches, and causes network-first fetch policies to
// be overridden.
private forceCache: boolean = false;
private ssrFetchPolicyOverrides =
new Map<ObservableQuery, WatchQueryFetchPolicy>();

/**
* Constructs an instance of {@link ApolloClient}.
*
Expand Down Expand Up @@ -180,14 +187,14 @@ export class ApolloClient<TCacheShape> implements DataProxy {

this.link = link;
this.cache = cache;
this.disableNetworkFetches = ssrMode || ssrForceFetchDelay > 0;
this.forceCache = !!(ssrMode || ssrForceFetchDelay > 0);
this.queryDeduplication = queryDeduplication;
this.defaultOptions = defaultOptions || {};
this.typeDefs = typeDefs;

if (ssrForceFetchDelay) {
setTimeout(
() => (this.disableNetworkFetches = false),
() => (this.forceCache = false),
ssrForceFetchDelay,
);
}
Expand Down Expand Up @@ -268,6 +275,24 @@ export class ApolloClient<TCacheShape> implements DataProxy {
});
}

// TODO: deprecate and remove this property?
public get disableNetworkFetches(): boolean {
return this.forceCache;
}

public set disableNetworkFetches(value: boolean) {
this.forceCache = value;
this.queryManager.ssrMode = value;
if (value) {
Comment on lines +283 to +286
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should do the stuff below only if value is changing from false to true?

const overrides = Array.from(this.ssrFetchPolicyOverrides);
this.ssrFetchPolicyOverrides.clear();
overrides.forEach(
([query, fetchPolicy]) => query.setOptions({ fetchPolicy }),
);
this.getObservableQueries('all').forEach((oq) => oq['updatePolling']());
}
}

/**
* Call this method to terminate any active client processes, making it safe
* to dispose of this `ApolloClient` instance.
Expand Down Expand Up @@ -302,16 +327,26 @@ export class ApolloClient<TCacheShape> implements DataProxy {
options = mergeOptions(this.defaultOptions.watchQuery, options);
}

const { fetchPolicy } = options;
// XXX Overwriting options is probably not the best way to do this long term...
if (
this.disableNetworkFetches &&
(options.fetchPolicy === 'network-only' ||
options.fetchPolicy === 'cache-and-network')
this.forceCache && (
fetchPolicy === 'network-only' ||
fetchPolicy === 'cache-and-network'
)
) {
options = { ...options, fetchPolicy: 'cache-first' };
}

return this.queryManager.watchQuery<T, TVariables>(options);
const query = this.queryManager.watchQuery<T, TVariables>(options);
if (fetchPolicy !== options.fetchPolicy) {
this.ssrFetchPolicyOverrides.set(query, fetchPolicy!);
query.subscribe({
complete: () => this.ssrFetchPolicyOverrides.delete(query),
});
}

return query;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/QueryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class QueryManager<TStore> {
public cache: ApolloCache<TStore>;
public link: ApolloLink;
public readonly assumeImmutableResults: boolean;
public readonly ssrMode: boolean;
public ssrMode: boolean;

private queryDeduplication: boolean;
private clientAwareness: Record<string, string> = {};
Expand Down