Skip to content

feat: allow setting script id #60

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

Merged
merged 1 commit into from
Sep 30, 2020
Merged
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
20 changes: 20 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
[
Expand Down Expand Up @@ -52,13 +56,29 @@ 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();
expect(script.onerror).toBeTruthy();
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" });

Expand Down
25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -164,6 +174,10 @@ export class Loader {
* See [[LoaderOptions.apiKey]]
*/
apiKey: string;
/**
* See [[LoaderOptions.id]]
*/
id: string;
/**
* See [[LoaderOptions.libraries]]
*/
Expand Down Expand Up @@ -205,6 +219,7 @@ export class Loader {
*/
constructor({
apiKey,
id = "__googleMapsScriptId",
libraries = [],
language,
region,
Expand All @@ -214,6 +229,7 @@ export class Loader {
}: LoaderOptions) {
this.version = version;
this.apiKey = apiKey;
this.id = id;
this.libraries = libraries;
this.language = language;
this.region = region;
Expand Down Expand Up @@ -293,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);
Expand Down