Skip to content
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

feat: load the google maps api dynamically #193

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

colenso
Copy link
Contributor

@colenso colenso commented Apr 17, 2024

Loads the maps api dynamically as is the recommended way.

@colenso
Copy link
Contributor Author

colenso commented Apr 17, 2024

@sandydoo Please review this one too 🙏

@colenso
Copy link
Contributor Author

colenso commented Apr 22, 2024

@sandydoo Let me know any feedback you have on this one 🙏

@sandydoo
Copy link
Owner

sandydoo commented Apr 23, 2024

I need some time to read up on this loading method to understand the implications of this switch. I do not yet know if we should support both methods or release a breaking change.
AFAIKT the current PR will quietly break setups modifying buildGoogleMapsUrl.

The new method allows us to async load the other libraries. Maybe we can take advantage of this instead of loading everything at once in a loop.

Note

To anyone reading this, the Google Maps "warning" in the console is not a critical issue. It's just a new, preferred loading method. The current method is very much supported.

Copy link
Owner

@sandydoo sandydoo left a comment

Choose a reason for hiding this comment

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

Thanks for looking into this! The two major issues I see with this are:

  1. The sequential library loading.
  2. The breaking changes to buildGoogleMapsUrl.

2 is the bigger issue for me. It's a bit more work on our end, but I would prefer to release a minor version first, then drop the old loader in a major bump.

Comment on lines +74 to +76
for (let library of libraries) {
await google.maps.importLibrary(library);
}
Copy link
Owner

Choose a reason for hiding this comment

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

This should at least use Promise.all instead of loading everything sequentially.

This is good enough for a first pass. Once the old loader is gone we can migrate everything to take advantage of the dynamic loading capabilities.

Copy link
Owner

Choose a reason for hiding this comment

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

Also, we need to mention in the docs that libraries must contain map.

* allows you to use external data when building the URL. For example, you
* could fetch the database record for the current user for localisation
* purposes.
*/
buildGoogleMapsUrl(config) {
Copy link
Owner

Choose a reason for hiding this comment

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

Removing a public function like this is a breaking change. It would be great if this continued to work as before. We can detect if this was overridden and provide a dev-only deprecation notice.

Copy link
Contributor

Choose a reason for hiding this comment

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

@sandydoo if it's useful, the one time I needed to adjust things around config (6 years ago in an app that is no longer online) I did it here. Can't remember very clearly what was needed - I think it was swap in custom info in a whitelabel scenario), but thought I'd speak up.

In that particular case, if I remember right, the ability to return a promise was invaluable

Comment on lines +89 to +109
s.textContent =
`(g=>{var h,a,k,p="The Google Maps JavaScript API",c="google",l="importLibrary",q="__ib__",m=document,b=window;b=b[c]||(b[c]={});var d=b.maps||(b.maps={}),r=new Set,e=new URLSearchParams,u=()=>h||(h=new Promise(async(f,n)=>{await (a=m.createElement("script"));e.set("libraries",[...r]+"");for(k in g)e.set(k.replace(/[A-Z]/g,t=>"_"+t[0].toLowerCase()),g[k]);e.set("callback",c+".maps."+q);a.src=\`https://maps.\${c}apis.com/maps/api/js?\`+e;d[q]=f;a.onerror=()=>h=n(Error(p+" could not load."));a.nonce=m.querySelector("script[nonce]")?.nonce||"";m.head.append(a)}));d[l]?console.warn(p+" only loads once. Ignoring:",g):d[l]=(f,...n)=>r.add(f)&&u().then(()=>d[l](f,...n))})({
key: "${key}",` +
versionString +
`
` +
channelString +
`
` +
regionString +
`
` +
languageString +
`
` +
mapIdsString +
`
// Add other bootstrap parameters as needed, using camel case.
});
initGoogleMap();
`;
Copy link
Owner

Choose a reason for hiding this comment

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

We can skip going through a script tag entirely and run this snippet as is.

@@ -312,82 +311,6 @@ module.exports = {
});
},

buildGoogleMapsUrl(config = {}) {
Copy link
Owner

Choose a reason for hiding this comment

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

Same as before, this should continue working.

@sandydoo
Copy link
Owner

sandydoo commented Jun 1, 2024

Alternatively, we could release a breaking change and check whether buildGoogleMapsUrl is defined, in which case we display an error. I just don't know if people are misusing it to just fetch the url.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants