Repository: googlemaps/js-api-loader
Pull request #58 (2020-09-21 06:06:59)

build(deps-dev): bump @types/jest from 26.0.10 to 26.0.14
Bumps [@types/jest](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/HEAD/types/jest) from 26.0.10 to 26.0.14.
<details>
<summary>Commits</summary>
<ul>
<li>See full diff in <a href="https://github.com/DefinitelyTyped/DefinitelyTyped/commits/HEAD/types/jest">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility_score?dependency-name=@types/jest&package-manager=npm_and_yarn&previous-version=26.0.10&new-version=26.0.14)](https://docs.github.com/en/github/managing-security-vulnerabilities/configuring-github-dependabot-security-updates)

Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)


</details>

codecov[bot]: # [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/58?src=pr&el=h1) Report
> Merging [#58](https://codecov.io/gh/googlemaps/js-api-loader/pull/58?src=pr&el=desc) into [master](https://codecov.io/gh/googlemaps/js-api-loader/commit/caffe67d7b989af3a1c7f4a1a6c79bd9fb2b4eb0?el=desc) will **not change** coverage.
> The diff coverage is `n/a`.

[![Impacted file tree graph](https://codecov.io/gh/googlemaps/js-api-loader/pull/58/graphs/tree.svg?width=650&height=150&src=pr&token=aAmgcWFXMs)](https://codecov.io/gh/googlemaps/js-api-loader/pull/58?src=pr&el=tree)

```diff
@@            Coverage Diff            @@
##            master       #58   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           74        74           
  Branches        10        10           
=========================================
  Hits            74        74           
```



------

[Continue to review full report at Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/58?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/googlemaps/js-api-loader/pull/58?src=pr&el=footer). Last update [caffe67...85de9c9](https://codecov.io/gh/googlemaps/js-api-loader/pull/58?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index f9c31c3..678efda 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -38,3 +38,15 @@ jobs:
         env:
           GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
           NPM_TOKEN: ${{ secrets.NPM_WOMBAT_TOKEN }}
+      - run: |        
+          npm i
+          npm run docs
+          cp -r dist docs/dist
+          cp -r examples docs/examples
+      - uses: peaceiris/actions-gh-pages@v3
+        with:
+          github_token: ${{ secrets.GITHUB_TOKEN }}
+          publish_dir: ./docs
+          user_name: 'googlemaps-bot'
+          user_email: 'googlemaps-bot@users.noreply.github.com'
+          commit_message: ${{ github.event.head_commit.message }}
diff --git a/.releaserc b/.releaserc
new file mode 100644
index 0000000..8343600
--- /dev/null
+++ b/.releaserc
@@ -0,0 +1,8 @@
+plugins:
+  - "@semantic-release/commit-analyzer"
+  - "@semantic-release/release-notes-generator"
+  - "@semantic-release/github"
+  - "@semantic-release/npm"
+  - "@semantic-release/git"
+options:
+  debug: true
diff --git a/package-lock.json b/package-lock.json
index 4734901..d65c990 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,6 +1,6 @@
 {
   "name": "@googlemaps/js-api-loader",
-  "version": "1.0.0",
+  "version": "1.4.1",
   "lockfileVersion": 1,
   "requires": true,
   "dependencies": {
@@ -2760,9 +2760,9 @@
       }
     },
     "@types/jest": {
-      "version": "26.0.10",
-      "resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.10.tgz",
-      "integrity": "sha512-i2m0oyh8w/Lum7wWK/YOZJakYF8Mx08UaKA1CtbmFeDquVhAEdA7znacsVSf2hJ1OQ/OfVMGN90pw/AtzF8s/Q==",
+      "version": "26.0.14",
+      "resolved": "https://registry.npmjs.org/@types/jest/-/jest-26.0.14.tgz",
+      "integrity": "sha512-Hz5q8Vu0D288x3iWXePSn53W7hAjP0H7EQ6QvDO9c7t46mR0lNOLlfuwQ+JkVxuhygHzlzPX+0jKdA3ZgSh+Vg==",
       "dev": true,
       "requires": {
         "jest-diff": "^25.2.1",
diff --git a/package.json b/package.json
index 31eeac6..1210dcf 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@googlemaps/js-api-loader",
-  "version": "1.0.0",
+  "version": "1.4.1",
   "description": "Wrapper for the loading of Google Maps JavaScript API script in the browser",
   "keywords": [
     "google",
diff --git a/src/index.ts b/src/index.ts
index 733be02..35ed926 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -23,7 +23,13 @@ declare global {
   }
 }
 
-type Libraries = ("drawing" | "geometry" | "localContext" | "places" | "visualization")[];
+type Libraries = (
+  | "drawing"
+  | "geometry"
+  | "localContext"
+  | "places"
+  | "visualization"
+)[];
 
 /**
  * The Google Maps JavaScript API
@@ -65,6 +71,10 @@ export interface LoaderOptions {
    * receive your default channel.
    */
   version?: string;
+  /**
+   * The id of the script tag. Before adding a new script, the Loader will check for an existing one.
+   */
+  id?: string;
   /**
    * When loading the Maps JavaScript API via the URL you may optionally load
    * additional libraries through use of the libraries URL parameter. Libraries
@@ -130,6 +140,10 @@ export interface LoaderOptions {
    * your bootstrap request.
    */
   mapIds?: string[];
+  /**
+   * Use a custom url and path to load the Google Maps API script.
+   */
+  url?: string;
 }
 
 /**
@@ -160,6 +174,10 @@ export class Loader {
    * See [[LoaderOptions.apiKey]]
    */
   apiKey: string;
+  /**
+   * See [[LoaderOptions.id]]
+   */
+  id: string;
   /**
    * See [[LoaderOptions.libraries]]
    */
@@ -179,8 +197,12 @@ export class Loader {
    */
   mapIds: string[];
 
+  /**
+   * See [[LoaderOptions.url]]
+   */
+  url: string;
+
   private CALLBACK = "__googleMapsCallback";
-  private URL = "https://maps.googleapis.com/maps/api/js";
   private callbacks: ((e: Event) => void)[] = [];
   private done = false;
   private loading = false;
@@ -197,18 +219,22 @@ export class Loader {
    */
   constructor({
     apiKey,
+    id = "__googleMapsScriptId",
     libraries = [],
     language,
     region,
     version,
     mapIds,
+    url = "https://maps.googleapis.com/maps/api/js",
   }: LoaderOptions) {
     this.version = version;
     this.apiKey = apiKey;
+    this.id = id;
     this.libraries = libraries;
     this.language = language;
     this.region = region;
     this.mapIds = mapIds;
+    this.url = url;
   }
   /**
    * CreateUrl returns the Google Maps JavaScript API script url given the [[LoaderOptions]].
@@ -216,7 +242,7 @@ export class Loader {
    * @ignore
    */
   createUrl(): string {
-    let url = this.URL;
+    let url = this.url;
 
     url += `?callback=${this.CALLBACK}`;
 
@@ -283,9 +309,14 @@ export class Loader {
    * Set the script on document.
    */
   private setScript(): void {
+    if (this.id && document.getElementById(this.id)) {
+      this.callback();
+      return;      
+    }
+    
     const url = this.createUrl();
     const script = document.createElement("script");
-
+    script.id = this.id;
     script.type = "text/javascript";
     script.src = url;
     script.onerror = this.loadErrorCallback.bind(this);


Test patch:
diff --git a/src/index.test.ts b/src/index.test.ts
index de5f0d7..727cff2 100644
--- a/src/index.test.ts
+++ b/src/index.test.ts
@@ -16,6 +16,10 @@
 
 import { Loader, LoaderOptions } from ".";
 
+afterEach(() => {
+  document.getElementsByTagName('html')[0].innerHTML = ''; 
+});
+
 test.each([
   [{}, "https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback"],
   [
@@ -36,6 +40,10 @@ test.each([
     { mapIds: ["foo", "bar"] },
     "https://maps.googleapis.com/maps/api/js?callback=__googleMapsCallback&map_ids=foo,bar",
   ],
+  [
+    { url: "https://example.com/js" },
+    "https://example.com/js?callback=__googleMapsCallback",
+  ],
 ])("createUrl is correct", (options: LoaderOptions, expected: string) => {
   const loader = new Loader(options);
   expect(loader.createUrl()).toEqual(expected);
@@ -48,6 +56,7 @@ test("setScript adds a script to head with correct attributes", () => {
 
   const script = document.head.childNodes[0] as HTMLScriptElement;
 
+  expect(script.id).toEqual(loader.id);
   expect(script.src).toEqual(loader.createUrl());
   expect(script.defer).toBeTruthy();
   expect(script.async).toBeTruthy();
@@ -55,6 +64,21 @@ test("setScript adds a script to head with correct attributes", () => {
   expect(script.type).toEqual("text/javascript");
 });
 
+test("setScript does not add second script with same id", () => {
+  new Loader({ apiKey: "foo", id: "bar" })['setScript']();
+  new Loader({ apiKey: "foo", id: "bar" })['setScript']();
+
+  expect(document.head.childNodes.length).toBe(1);
+});
+
+test("setScript adds a script with id", () => {
+  const loader = new Loader({ apiKey: "foo", id: "bar" });
+  loader["setScript"]();
+
+  const script = document.head.childNodes[0] as HTMLScriptElement;
+  expect(script.id).toEqual(loader.id);
+});
+
 test("load should return a promise that resolves even if called twice", () => {
   const loader = new Loader({ apiKey: "foo" });