Knowledge base / Performance

Migrate Google Maps to loading=async

If your console says the Maps JavaScript API was loaded directly without loading=async, your map still works. Google is telling you the page loads the API in a way that can hurt performance. Switching is a small change, but it alters when your map code runs, so it's worth doing carefully. While you're in the loader, check the v= parameter too.

How to tell this is your problem

  • The console shows the loading=async warning below on every page with a map.
  • The loader is a plain <script src="https://maps.googleapis.com/maps/api/js?key=…"> tag, with no loading=async in the URL.
  • Lighthouse or PageSpeed Insights points at the Maps script as a render-blocking or long main-thread task.
  • The console also shows a RetiredVersion or InvalidVersion warning, which means the v= parameter needs attention.
Google Maps JavaScript API has been loaded directly without loading=async. This can result in suboptimal performance. For best-practice loading patterns please see https://goo.gle/js-api-loading
Google Maps JavaScript API warning: RetiredVersion https://developers.google.com/maps/documentation/javascript/error-messages#retired-version

Why it happens

The classic loader is a single script tag that downloads the whole API and then runs your callback, or leaves google.maps ready for code further down the page. Google now recommends two other patterns that let the API load its pieces asynchronously:

  • Dynamic library import. A short inline bootstrap script defines google.maps.importLibrary(). Your code awaits the libraries it needs, and they load on demand. Google's documentation notes this also protects the page from loading the API more than once.
  • Direct script tag with loading=async. The same script tag you have now, with loading=async added and a callback to tell you when the API is ready.

Older templates, plugins and tutorials still use the classic tag, which is why the warning is so common.

The quick fix: add loading=async

  1. 01Find the loader. Search your theme, templates or plugin settings for maps.googleapis.com/maps/api/js.
  2. 02Add loading=async to the query string and give the tag the async attribute.
  3. 03Make sure there is a callback parameter naming a global function, and that the function is defined before the script can finish loading.
  4. 04Move every piece of map set-up into that callback. Nothing outside it should touch google.maps directly.
HTML
<script>
  async function initMap() {
    const { Map } = await google.maps.importLibrary("maps");
    new Map(document.getElementById("map"), {
      center: { lat: 51.5072, lng: -0.1276 },
      zoom: 12,
    });
  }
</script>
<script async
  src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&loading=async&callback=initMap">
</script>

Loading the classes with importLibrary() inside the callback is the safest pattern. It works whether or not a library was preloaded with libraries=.

The fuller fix: dynamic library import

Google's recommended migration is to replace the script tag with the inline bootstrap loader, copied unchanged from Google's documentation, then make your init function async and import what you need:

JavaScript
(g=>{/* Google's inline bootstrap loader, copied unchanged from the docs */})({
  key: "YOUR_API_KEY",
  v: "weekly",
});

async function initMap() {
  const [{ Map }, { AdvancedMarkerElement }] = await Promise.all([
    google.maps.importLibrary("maps"),
    google.maps.importLibrary("marker"),
  ]);
  // build the map here
}
initMap();

Load several libraries with Promise.all() rather than one await after another, so they download in parallel. With this loader there's no callback and no libraries= list. Each library is fetched when you ask for it.

What breaks, and how to avoid it

  • Code that assumes google.maps exists at parse time. Inline scripts further down the page that call new google.maps.Map() straight away will now run before the API is ready. Move them into the callback or after an await.
  • Plugins that read google.maps.places on page load. Address autocomplete in form plugins often expects the classic loader. Test every form after the switch.
  • Two loaders. If a plugin still adds its own classic tag, you'll get the "multiple times" warning instead. See finding the duplicate loader.
  • Script optimisers. Plugins that defer or combine scripts can move your callback function after the loader. Exclude the Maps loader and its callback from optimisation if the map stops appearing.
  • A strict Content-Security-Policy. The inline bootstrap is an inline script, so it needs a nonce or hash. See the CSP directives Maps needs.

Versions: weekly, quarterly, beta and RetiredVersion

The v parameter chooses which release of the API you get. Google documents these options:

ValueWhat you get
weeklyThe most current release, updated every week. This is the default if you leave v out.
quarterlyUpdated once a quarter. The most predictable. No new features or regular fixes during the quarter.
betaBased on weekly, for early testing. Not covered by any SLA or deprecation policy.
alphaExperimental features. For development only, not for production.
A number such as 3.nnA specific release. New numbered versions arrive quarterly and old ones are eventually deleted.

Google releases a new version each quarter, in mid-February, mid-May, mid-August and mid-November, and keeps four versions available at any one time.

RetiredVersion means the page asks for a numbered version that has been retired. InvalidVersion means the value doesn't match any version at all, often a typo or an old value such as one copied from a tutorial. In both cases Google ignores the request and serves your default channel, so the map usually still loads, but on a different release from the one you pinned. That can change behaviour without warning, and the console warning stays until you fix the tag.

  1. 01Find v= in the loader URL, or v: in the bootstrap parameters.
  2. 02Change it to weekly, or to quarterly if you'd rather have fewer changes. Pin a current numbered version only if you have a reason to and someone will update it each quarter.
  3. 03If a plugin sets the version, update the plugin or look for a version setting in its options.

Confirm it is fixed

Reload the page with the console open. The loading=async warning and any RetiredVersion or InvalidVersion warning should be gone. Click markers, open info windows and try any address fields. Then scan the page: the scanner flags a loader missing loading=async and reports RetiredVersion and InvalidVersion warnings, so you can check every page that has a map.

Common questions

Is the loading=async warning an error?

No. It's a performance warning. The map keeps working, but Google recommends the async patterns because they let the API load without holding up the rest of the page.

Do I still need a callback with loading=async?

With the direct script tag, yes. Google's documentation says to use the callback parameter to run code when the API is available. With the dynamic library import, you await importLibrary() instead and don't need a callback.

Should I use v=weekly or v=quarterly?

Weekly is the default and gets fixes first. Quarterly changes less often, which suits sites that want predictable behaviour. Avoid beta and alpha in production.

My map still loads with a RetiredVersion warning. Do I need to act?

Yes, though it isn't urgent. Google is already serving you its default channel in place of the version you asked for. Update the v parameter so the page gets the release you expect and the warning goes away.

Check whether this is your error

A scan tells you in about twelve seconds, from a clean session.

Related articles