From 5dd8d0880c0ec3229df13758131f1e7b088fdb0f Mon Sep 17 00:00:00 2001 From: "Chris J. Shull" Date: Fri, 2 Jun 2023 17:26:45 -0700 Subject: [PATCH 1/2] update examples and minimize URLs --- README.md | 16 +++++++++++++++- examples/index.html | 14 +++++++++++--- src/index.ts | 29 ++++++++++++++++++----------- 3 files changed, 44 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 2e239c80..6d1b27ff 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ npm i @googlemaps/js-api-loader Alternatively you may add the umd package directly to the html document using the unpkg link. ```html - + ``` When adding via unpkg, the loader can be accessed at `google.maps.plugins.loader.Loader`. @@ -61,6 +61,20 @@ const mapOptions = { ``` +Using a promise for a specific library. + +```javascript +// Promise for a specific library +loader + .importLibrary('maps') + .then(({Map}) => { + new Map(document.getElementById("map"), mapOptions); + }) + .catch((e) => { + // do something + }); +``` + Using a promise for when the script has loaded. ```javascript diff --git a/examples/index.html b/examples/index.html index a5468f98..9d24ee39 100644 --- a/examples/index.html +++ b/examples/index.html @@ -35,7 +35,6 @@ @@ -55,14 +54,23 @@ apiKey: "", version: "weekly", libraries: ["places"], - nonce: "caffe67d7b989af3a1c7f4a1a6c79bd9fb2b4eb0", }); + // Promise for a specific library + loader + .importLibrary('maps') + .then(({Map}) => { + new Map(document.getElementById("map"), mapOptions); + }) + .catch((e) => { + // do something + }); + // Promise loader .load() .then((google) => { - new google.maps.Map(document.getElementById("map"), mapOptions); + // new google.maps.Map(document.getElementById("map"), mapOptions); }) .catch((e) => { // do something diff --git a/src/index.ts b/src/index.ts index 42fe9093..b0d486ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -492,6 +492,23 @@ export class Loader { return; } + const params = { + key: this.apiKey, + channel: this.channel, + client: this.client, + libraries: this.libraries, + v: this.version, + mapIds: this.mapIds, + language: this.language, + region: this.region, + authReferrerPolicy: this.authReferrerPolicy, + }; + // keep the URL minimal: + Object.keys(params).forEach( + // eslint-disable-next-line @typescript-eslint/no-explicit-any + (key) => !(params as any)[key] && delete (params as any)[key] + ); + if (!window?.google?.maps?.importLibrary) { // tweaked copy of https://developers.google.com/maps/documentation/javascript/load-maps-js-api#dynamic-library-import // which also sets the url, the id, and the nonce @@ -531,17 +548,7 @@ export class Loader { })); // @ts-ignore d[l] ? console.warn(p + " only loads once. Ignoring:", g) : (d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n))); - })({ - key: this.apiKey, - channel: this.channel, - client: this.client, - libraries: this.libraries, - v: this.version, - mapIds: this.mapIds, - language: this.language, - region: this.region, - authReferrerPolicy: this.authReferrerPolicy, - }); + })(params); /* eslint-enable */ } From cbca1ec7d7fe9564e4efc901f5da04c9aa4e5a58 Mon Sep 17 00:00:00 2001 From: "Chris J. Shull" Date: Tue, 6 Jun 2023 09:05:52 -0700 Subject: [PATCH 2/2] fix: add unit test for valid URL generation --- src/index.test.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/index.test.ts b/src/index.test.ts index 75f03a49..496d615e 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -98,6 +98,19 @@ test("setScript does not add second script with same id", async () => { expect(document.head.childNodes.length).toBe(1); }); +test("setScript adds a script to head with valid src", async () => { + const loader = new Loader({ apiKey: "foo" }); + + loader["setScript"](); + await 0; + + const script = document.head.childNodes[0] as HTMLScriptElement; + + expect(script.src).toEqual( + "https://maps.googleapis.com/maps/api/js?libraries=&key=foo&callback=google.maps.__ib__" + ); +}); + test("load should return a promise that resolves even if called twice", () => { const loader = new Loader({ apiKey: "foo" }); loader.importLibrary = (() => Promise.resolve()) as any;