Currently the module loads the Google Maps JS on every page, even if on the page there is any map.

Steps to reproduce

  1. create a new block type
  2. add a geofield field on the new block type
  3. set the styled google map visualization style
  4. create a block of the newly created type
  5. add the block only on the front page
  6. open a different page and check the source code.

To fix

Move the two external JS libraries into style_google_map.libraries.yml:

styled-google-map:
  version: 1.0
  js:
    //maps.google.com/maps/api/js: { type: external }
    //cdn.rawgit.com/googlemaps/js-info-bubble/gh-pages/src/infobubble.js: { type: external }
    js/styled-google-map.js: {}
  css:
    css/styled-google-map.css : {}
  dependencies:
    - core/jquery
    - core/drupal
    - core/drupalSettings

Then remove the styled_google_map_page_attachments() function from the .module file.

The map still works and now the JS is loaded only if necessary.

Comments

FiNeX created an issue. See original summary.

a.dmitriiev’s picture

I will take a look at this during this week.

finex’s picture

Thanks, let me know :-)

geerlingguy’s picture

Title: Don't put JS in every website page. Add JS only if needed. » Add Google Maps JS only where needed
Status: Active » Needs review
StatusFileSize
new1.5 KB

Just noticed this on my site as well; attached patch implements the suggestion in the original issue summary, and is tested and working on my site (the maps JS only loads on pages where a map/field is present and the library files are loaded).

geerlingguy’s picture

Issue summary: View changes

Cleaned up issue summary.

  • a.dmitriiev committed e82cfb7 on 8.x-1.x
    Issue #2736879 by geerlingguy: Add Google Maps JS only where needed....
a.dmitriiev’s picture

Thank you guys,

I added this to dev version. I will gather some more patches and changes and will create a stable release a bit later

a.dmitriiev’s picture

Status: Needs review » Closed (fixed)