Skip to content

Commit ecf77f6

Browse files
authored
Don't attempt to connect to devtools in non-browser environments (#11971)
1 parent 061cab6 commit ecf77f6

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

Diff for: .changeset/famous-berries-remember.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@apollo/client": patch
3+
---
4+
5+
Prevent the `setTimeout` for suggesting devtools from running in non-browser environments.

Diff for: src/core/ApolloClient.ts

+44-42
Original file line numberDiff line numberDiff line change
@@ -319,57 +319,59 @@ export class ApolloClient<TCacheShape> implements DataProxy {
319319
}
320320

321321
private connectToDevTools() {
322-
if (typeof window === "object") {
323-
type DevToolsConnector = {
324-
push(client: ApolloClient<any>): void;
325-
};
326-
const windowWithDevTools = window as Window & {
327-
[devtoolsSymbol]?: DevToolsConnector;
328-
__APOLLO_CLIENT__?: ApolloClient<any>;
329-
};
330-
const devtoolsSymbol = Symbol.for("apollo.devtools");
331-
(windowWithDevTools[devtoolsSymbol] =
332-
windowWithDevTools[devtoolsSymbol] || ([] as DevToolsConnector)).push(
333-
this
334-
);
335-
windowWithDevTools.__APOLLO_CLIENT__ = this;
322+
if (typeof window === "undefined") {
323+
return;
336324
}
337325

326+
type DevToolsConnector = {
327+
push(client: ApolloClient<any>): void;
328+
};
329+
const windowWithDevTools = window as Window & {
330+
[devtoolsSymbol]?: DevToolsConnector;
331+
__APOLLO_CLIENT__?: ApolloClient<any>;
332+
};
333+
const devtoolsSymbol = Symbol.for("apollo.devtools");
334+
(windowWithDevTools[devtoolsSymbol] =
335+
windowWithDevTools[devtoolsSymbol] || ([] as DevToolsConnector)).push(
336+
this
337+
);
338+
windowWithDevTools.__APOLLO_CLIENT__ = this;
339+
338340
/**
339341
* Suggest installing the devtools for developers who don't have them
340342
*/
341343
if (!hasSuggestedDevtools && __DEV__) {
342344
hasSuggestedDevtools = true;
343-
setTimeout(() => {
344-
if (
345-
typeof window !== "undefined" &&
346-
window.document &&
347-
window.top === window.self &&
348-
!(window as any).__APOLLO_DEVTOOLS_GLOBAL_HOOK__ &&
349-
/^(https?|file):$/.test(window.location.protocol)
350-
) {
351-
const nav = window.navigator;
352-
const ua = nav && nav.userAgent;
353-
let url: string | undefined;
354-
if (typeof ua === "string") {
355-
if (ua.indexOf("Chrome/") > -1) {
356-
url =
357-
"https://chrome.google.com/webstore/detail/" +
358-
"apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm";
359-
} else if (ua.indexOf("Firefox/") > -1) {
360-
url =
361-
"https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/";
345+
if (
346+
window.document &&
347+
window.top === window.self &&
348+
/^(https?|file):$/.test(window.location.protocol)
349+
) {
350+
setTimeout(() => {
351+
if (!(window as any).__APOLLO_DEVTOOLS_GLOBAL_HOOK__) {
352+
const nav = window.navigator;
353+
const ua = nav && nav.userAgent;
354+
let url: string | undefined;
355+
if (typeof ua === "string") {
356+
if (ua.indexOf("Chrome/") > -1) {
357+
url =
358+
"https://chrome.google.com/webstore/detail/" +
359+
"apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm";
360+
} else if (ua.indexOf("Firefox/") > -1) {
361+
url =
362+
"https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/";
363+
}
364+
}
365+
if (url) {
366+
invariant.log(
367+
"Download the Apollo DevTools for a better development " +
368+
"experience: %s",
369+
url
370+
);
362371
}
363372
}
364-
if (url) {
365-
invariant.log(
366-
"Download the Apollo DevTools for a better development " +
367-
"experience: %s",
368-
url
369-
);
370-
}
371-
}
372-
}, 10000);
373+
}, 10000);
374+
}
373375
}
374376
}
375377

0 commit comments

Comments
 (0)