Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b5c0f1f

Browse files
fix: update examples and minimize URLs (#799)
1 parent ab31f89 commit b5c0f1f

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

‎README.md‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ npm i @googlemaps/js-api-loader
2222
Alternatively you may add the umd package directly to the html document using the unpkg link.
2323

2424
```html
25-
<script src="https://unpkg.com/@googlemaps/js-api-loader@1.0.0/dist/index.min.js"></script>
25+
<script src="https://unpkg.com/@googlemaps/js-api-loader@1.x/dist/index.min.js"></script>
2626
```
2727

2828
When adding via unpkg, the loader can be accessed at `google.maps.plugins.loader.Loader`.
@@ -61,6 +61,20 @@ const mapOptions = {
6161

6262
```
6363

64+
Using a promise for a specific library.
65+
66+
```javascript
67+
// Promise for a specific library
68+
loader
69+
.importLibrary('maps')
70+
.then(({Map}) => {
71+
new Map(document.getElementById("map"), mapOptions);
72+
})
73+
.catch((e) => {
74+
// do something
75+
});
76+
```
77+
6478
Using a promise for when the script has loaded.
6579

6680
```javascript

‎examples/index.html‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
</style>
3636
<script
3737
nonce="caffe67d7b989af3a1c7f4a1a6c79bd9fb2b4eb0"
38-
type="text/javascript"
3938
src="../dist/index.umd.js"
4039
></script>
4140

@@ -55,14 +54,23 @@
5554
apiKey: "",
5655
version: "weekly",
5756
libraries: ["places"],
58-
nonce: "caffe67d7b989af3a1c7f4a1a6c79bd9fb2b4eb0",
5957
});
6058

59+
// Promise for a specific library
60+
loader
61+
.importLibrary('maps')
62+
.then(({Map}) => {
63+
new Map(document.getElementById("map"), mapOptions);
64+
})
65+
.catch((e) => {
66+
// do something
67+
});
68+
6169
// Promise
6270
loader
6371
.load()
6472
.then((google) => {
65-
new google.maps.Map(document.getElementById("map"), mapOptions);
73+
// new google.maps.Map(document.getElementById("map"), mapOptions);
6674
})
6775
.catch((e) => {
6876
// do something

‎src/index.ts‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,23 @@ export class Loader {
492492
return;
493493
}
494494

495+
const params = {
496+
key: this.apiKey,
497+
channel: this.channel,
498+
client: this.client,
499+
libraries: this.libraries,
500+
v: this.version,
501+
mapIds: this.mapIds,
502+
language: this.language,
503+
region: this.region,
504+
authReferrerPolicy: this.authReferrerPolicy,
505+
};
506+
// keep the URL minimal:
507+
Object.keys(params).forEach(
508+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
509+
(key) => !(params as any)[key] && delete (params as any)[key]
510+
);
511+
495512
if (!window?.google?.maps?.importLibrary) {
496513
// tweaked copy of https://developers.google.com/maps/documentation/javascript/load-maps-js-api#dynamic-library-import
497514
// which also sets the url, the id, and the nonce
@@ -531,17 +548,7 @@ export class Loader {
531548
}));
532549
// @ts-ignore
533550
d[l] ? console.warn(p + " only loads once. Ignoring:", g) : (d[l] = (f, ...n) => r.add(f) && u().then(() => d[l](f, ...n)));
534-
})({
535-
key: this.apiKey,
536-
channel: this.channel,
537-
client: this.client,
538-
libraries: this.libraries,
539-
v: this.version,
540-
mapIds: this.mapIds,
541-
language: this.language,
542-
region: this.region,
543-
authReferrerPolicy: this.authReferrerPolicy,
544-
});
551+
})(params);
545552
/* eslint-enable */
546553
}
547554

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /