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

Apollo Devtools extension download message check if does not exist #11216

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
52 changes: 34 additions & 18 deletions src/core/ApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export type ApolloClientOptions<TCacheShape> = {
// previously declared and exported from this module, and then reexported from
// @apollo/client/core. Since we need to preserve that API anyway, the easiest
// solution is to reexport mergeOptions where it was previously declared (here).
import { mergeOptions } from "../utilities/index.js";
import { mergeOptions, detectExtension } from "../utilities/index.js";
export { mergeOptions };

/**
Expand All @@ -85,6 +85,7 @@ export class ApolloClient<TCacheShape> implements DataProxy {
public defaultOptions: DefaultOptions;
public readonly typeDefs: ApolloClientOptions<TCacheShape>["typeDefs"];

private EXTENSION_ID = 'jdkknkkbebbapilgoeccciglkfbmbnfm';
private queryManager: QueryManager<TCacheShape>;
private devToolsHookCb: Function;
private resetStoreCallbacks: Array<() => Promise<any>> = [];
Expand Down Expand Up @@ -253,34 +254,49 @@ export class ApolloClient<TCacheShape> implements DataProxy {
if (
typeof window !== "undefined" &&
window.document &&
window.top === window.self &&
!(window as any).__APOLLO_DEVTOOLS_GLOBAL_HOOK__
window.top === window.self
) {
const nav = window.navigator;
const ua = nav && nav.userAgent;
const userAgentExists = typeof ua === 'string';
const isChromeAgent = userAgentExists && ua.indexOf("Chrome/") > -1;
const isFirefoxAgent = userAgentExists && ua.indexOf("Chrome/") > -1;
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 (isChromeAgent) {
url =
"https://chrome.google.com/webstore/detail/" +
`apollo-client-developer-t/${this.EXTENSION_ID}`;
} else if (isFirefoxAgent) {
url =
"https://addons.mozilla.org/en-US/firefox/addon/apollo-developer-tools/";
}

if (isFirefoxAgent && url) {
this.logExtensionDownloadMessage(url);
return;
}
if (url) {
invariant.log(
"Download the Apollo DevTools for a better development " +
"experience: %s",
url
);

if (isChromeAgent) {
detectExtension(this.EXTENSION_ID, (isInstalled) => {
if (!isInstalled && url) {
this.logExtensionDownloadMessage(url);
}
});
}
}
}, 10000);
}
}

private logExtensionDownloadMessage(url: string) {
invariant.log(
"Download the Apollo DevTools for a better development " +
"experience: %s",
url
);
}

/**
* The `DocumentTransform` used to modify GraphQL documents before a request
* is made. If a custom `DocumentTransform` is not provided, this will be the
Expand Down
12 changes: 12 additions & 0 deletions src/utilities/common/detectExtension.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export function detectExtension(extensionId: string, callback: (status: boolean) => void) {
const img = new Image();
img.src = `chrome-extension://${extensionId}/images/logo64.png`;

img.onload = function () {
callback(true);
};

img.onerror = function () {
callback(false);
};
}
1 change: 1 addition & 0 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export * from "./common/makeUniqueId.js";
export * from "./common/stringifyForDisplay.js";
export * from "./common/mergeOptions.js";
export * from "./common/incrementalResult.js";
export * from "./common/detectExtension.js";

export { omitDeep } from "./common/omitDeep.js";
export { stripTypename } from "./common/stripTypename.js";
Expand Down