Skip to content

Avoid multiple api loads #955

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
Oct 3, 2020
Merged
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
30 changes: 16 additions & 14 deletions src/loaders/google_map_loader.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Loader } from '@googlemaps/js-api-loader';

let loader_ = null;

let loadPromise_;
let resolveCustomPromise_;

const _customPromise = new Promise((resolve) => {
Expand All @@ -16,6 +15,11 @@ export default (bootstrapURLKeys, heatmapLibrary) => {
return _customPromise;
}

// avoid api to be loaded multiple times
if (loadPromise_) {
Copy link
Member Author

@itsmichaeldiego itsmichaeldiego Oct 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpoehnelt Instead of checking for !loader_ we return the loadPromise, avoiding the loader to add another script, not sure why you deleted it in the first time, if you encountered any problems? I couldn't find any.

return loadPromise_;
}

if (!bootstrapURLKeys.libraries) {
bootstrapURLKeys.libraries = [];
}
Expand Down Expand Up @@ -50,23 +54,21 @@ export default (bootstrapURLKeys, heatmapLibrary) => {
throw new Error('google map cannot be loaded outside browser env');
}

if (!loader_) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpoehnelt I feel there's no need to check for this, that doesn't change much, because loader will load another script anyways.

const { key, ...restKeys } = bootstrapURLKeys;
const { key, ...restKeys } = bootstrapURLKeys;

loader_ = new Loader({
// need to keep key for backwards compatibility
apiKey: key || '',
...restKeys,
libraries,
});
}
const loader_ = new Loader({
// need to keep key for backwards compatibility
apiKey: key || '',
...restKeys,
libraries,
});

const loadPromise = loader_.load().then(() => {
loadPromise_ = loader_.load().then(() => {
resolveCustomPromise_(window.google.maps);
return window.google.maps;
});

resolveCustomPromise_(loadPromise);
resolveCustomPromise_(loadPromise_);

return loadPromise;
return loadPromise_;
};