DEV Community

Lakhveer Bawa
Lakhveer Bawa

Posted on • Updated on

Troubleshooting and Adding Google Maps to individual Nuxt js pages

Update[New]: If you need simple Location Autocomplete, I have created new Gist: check this out and staff if find useful :)
Enter fullscreen mode Exit fullscreen mode

I had a very hard time figuring out the best solution to add Google maps API into Nuxt js Project. It was because of several issues and the unique nature of Nuxt js.
My Goal was Simple

  • Add google maps API only on those pages where it is being used.

I tried varies techniques, and most of them were initiating some issues.

Some of the issues I was facing were.

  • Script was being appended to the page as many times as the component was loaded.
  • It was being hard to know when maps API was loaded.
  • I simply didn't want to add maps API to all pages, which could be easily achieved by adding a script in nuxt.config.js

What were the techniques I tried?

  1. I tried to use head() element of nuxt js page, which appends the scripts to head section of the page


  • Every time component was loaded, it would append the script again to head section of the page, so the script was being appended multiple times in the page.
  • It was also not working as expected when I would reload the page
  1. Using packages and, they were simply not working

  2. Vue2-google-maps - It was appending the maps API code to the whole codebase so increasing the overall page size.

What was it that worked.

I used mounted() hook of page component to the first check if google variable already exists (if yes, that means API is already loaded).

mounted() {
// if (!process.server) {
    if (typeof google === 'undefined') {
      const script = document.createElement('script')
      script.onload = this.onScriptLoaded
      script.type = 'text/javascript'
      script.src = ` 
    } else {

    // }

Enter fullscreen mode Exit fullscreen mode

It would call onScriptLoaded() method.

methods: {

onScriptLoaded(event = null) {

      // if (event) {
      //  console.log('Was added')
      // } else {
      //  console.log('Already existed')
      // }
Enter fullscreen mode Exit fullscreen mode

I hope it will save somebody's time. Thanks for reading and Have a good day.

Discussion (5)

israelortuno profile image
Israel Ortuño • Edited on

Consider using this library:

    const loader = new Loader({
      apiKey: 'XXX',
      version: 'weekly',
      libraries: ['places']

    await loader.load()

    this.placesAutocomplete = new$
Enter fullscreen mode Exit fullscreen mode
tomqsm profile image

Hi, thanks for sharing this, you saved me some headache and time :) best wishes.

bawa_geek profile image
Lakhveer Bawa Author

Glad, I could be of help

drevantonder profile image
Andre van Tonder

Have a look at . I had the same issue where head() would add it multiple times. Using skip you can prevent it from being added twice.

jessecogollo_2 profile image
Jesse cogollo

thank you, it's very useful to me 💪