From ecf77f6f5b5ccf64cfba51e838e96549fb6c92fe Mon Sep 17 00:00:00 2001 From: Jerel Miller Date: Tue, 23 Jul 2024 08:59:44 -0600 Subject: [PATCH] Don't attempt to connect to devtools in non-browser environments (#11971) --- .changeset/famous-berries-remember.md | 5 ++ src/core/ApolloClient.ts | 86 ++++++++++++++------------- 2 files changed, 49 insertions(+), 42 deletions(-) create mode 100644 .changeset/famous-berries-remember.md diff --git a/.changeset/famous-berries-remember.md b/.changeset/famous-berries-remember.md new file mode 100644 index 0000000000..0204827b57 --- /dev/null +++ b/.changeset/famous-berries-remember.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": patch +--- + +Prevent the `setTimeout` for suggesting devtools from running in non-browser environments. diff --git a/src/core/ApolloClient.ts b/src/core/ApolloClient.ts index 6070591ffd..7dce981b88 100644 --- a/src/core/ApolloClient.ts +++ b/src/core/ApolloClient.ts @@ -319,57 +319,59 @@ export class ApolloClient implements DataProxy { } private connectToDevTools() { - if (typeof window === "object") { - type DevToolsConnector = { - push(client: ApolloClient): void; - }; - const windowWithDevTools = window as Window & { - [devtoolsSymbol]?: DevToolsConnector; - __APOLLO_CLIENT__?: ApolloClient; - }; - const devtoolsSymbol = Symbol.for("apollo.devtools"); - (windowWithDevTools[devtoolsSymbol] = - windowWithDevTools[devtoolsSymbol] || ([] as DevToolsConnector)).push( - this - ); - windowWithDevTools.__APOLLO_CLIENT__ = this; + if (typeof window === "undefined") { + return; } + type DevToolsConnector = { + push(client: ApolloClient): void; + }; + const windowWithDevTools = window as Window & { + [devtoolsSymbol]?: DevToolsConnector; + __APOLLO_CLIENT__?: ApolloClient; + }; + const devtoolsSymbol = Symbol.for("apollo.devtools"); + (windowWithDevTools[devtoolsSymbol] = + windowWithDevTools[devtoolsSymbol] || ([] as DevToolsConnector)).push( + this + ); + windowWithDevTools.__APOLLO_CLIENT__ = this; + /** * Suggest installing the devtools for developers who don't have them */ if (!hasSuggestedDevtools && __DEV__) { hasSuggestedDevtools = true; - setTimeout(() => { - if ( - typeof window !== "undefined" && - window.document && - window.top === window.self && - !(window as any).__APOLLO_DEVTOOLS_GLOBAL_HOOK__ && - /^(https?|file):$/.test(window.location.protocol) - ) { - const nav = window.navigator; - const ua = nav && nav.userAgent; - let url: string | undefined; - if (typeof ua === "string") { - if (ua.indexOf("Chrome/") > -1) { - url = - "https://chrome.google.com/webstore/detail/" + - "apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm"; - } else if (ua.indexOf("Firefox/") > -1) { - url = - "https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/"; + if ( + window.document && + window.top === window.self && + /^(https?|file):$/.test(window.location.protocol) + ) { + setTimeout(() => { + if (!(window as any).__APOLLO_DEVTOOLS_GLOBAL_HOOK__) { + const nav = window.navigator; + const ua = nav && nav.userAgent; + let url: string | undefined; + if (typeof ua === "string") { + if (ua.indexOf("Chrome/") > -1) { + url = + "https://chrome.google.com/webstore/detail/" + + "apollo-client-developer-t/jdkknkkbebbapilgoeccciglkfbmbnfm"; + } else if (ua.indexOf("Firefox/") > -1) { + url = + "https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/"; + } + } + if (url) { + invariant.log( + "Download the Apollo DevTools for a better development " + + "experience: %s", + url + ); } } - if (url) { - invariant.log( - "Download the Apollo DevTools for a better development " + - "experience: %s", - url - ); - } - } - }, 10000); + }, 10000); + } } }