diff --git a/src/index.test.ts b/src/index.test.ts index 40bdaa83..0b416024 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -329,6 +329,16 @@ test("loader should resolve immediately when google.maps defined", async () => { expect(console.warn).toHaveBeenCalledTimes(1); }); +test("loader should not warn if done and google.maps is defined", async () => { + const loader = new Loader({ apiKey: "foo" }); + loader["done"] = true; + window.google = { maps: { version: "3.*.*" } as any }; + console.warn = jest.fn(); + await expect(loader.loadPromise()).resolves.toBeUndefined(); + delete window.google; + expect(console.warn).toHaveBeenCalledTimes(0); +}); + test("deleteScript removes script tag from head", () => { const loader = new Loader({ apiKey: "foo" }); loader["setScript"](); diff --git a/src/index.ts b/src/index.ts index acf1ce04..e9d229f5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -482,18 +482,21 @@ export class Loader { } private execute(): void { - if (window.google && window.google.maps && window.google.maps.version) { - console.warn( - "Google Maps already loaded outside @googlemaps/js-api-loader." + - "This may result in undesirable behavior as options and script parameters may not match." - ); - this.callback(); - } - this.resetIfRetryingFailed(); + if (this.done) { this.callback(); } else { + // short circuit and warn if google.maps is already loaded + if (window.google && window.google.maps && window.google.maps.version) { + console.warn( + "Google Maps already loaded outside @googlemaps/js-api-loader." + + "This may result in undesirable behavior as options and script parameters may not match." + ); + this.callback(); + return; + } + if (this.loading) { // do nothing but wait } else {