Knowledge base / Network & CSP

Fix NotLoadingAPIFromGoogleMapsError on Google Maps

The Maps JavaScript API has to be loaded straight from Google's servers at https://maps.googleapis.com. Your page loads it from somewhere else: your own domain, a CDN, a proxy or a bundled copy. Google rejects that. The key and billing can be perfectly fine.

How to tell this is your problem

  • The console shows NotLoadingAPIFromGoogleMapsError. Note the spelling: Google's code says Maps, plural.
  • In the browser's Network tab, the request for the Maps script (filter for maps/api/js or js?key=) goes to your own domain, a CDN host or a .js file with a hashed name, not maps.googleapis.com.
  • The map broke straight after you installed or reconfigured a performance, caching or optimisation plugin, or switched on a CDN feature.
  • The same key works fine on a plain test page that loads the script from Google.

This is what the console shows:

Google Maps JavaScript API error: NotLoadingAPIFromGoogleMapsError
https://developers.google.com/maps/documentation/javascript/error-messages#not-loading-api-from-google-maps-error

Why it happens

Google's error reference says the script element that loads the Maps JavaScript API isn't included correctly, and that the API must be loaded directly from https://maps.googleapis.com. The loader is a small bootstrap that fetches the rest of the API on demand. A copy served from anywhere else won't work. The common causes, most likely first:

  • An optimisation plugin rewrote the script. Options that combine scripts, minify third-party files or “host external scripts locally” can copy the Maps loader to your server or merge it into one big file.
  • A CDN or proxy rewrote the URL. Some edge services rewrite third-party script URLs to route them through your own domain.
  • Someone saved the file. A developer downloaded maps/api/js to “speed it up” or to work offline, and the page now loads /js/google-maps.js.
  • A bundler pulled it in. The loader's contents were pasted or imported into an application bundle, instead of being requested from Google at runtime.
  • A static copy of the site. Site export tools and archive copies rewrite external URLs to local files.

Google's own loading methods are all fine: the inline dynamic import bootstrap, the direct <script> tag, and the @googlemaps/js-api-loader npm package. All three request the API from maps.googleapis.com at runtime.

The fix, step by step

  1. 01Open the page, then the browser's developer tools. In the Network tab, reload and find the request that delivers the Maps loader. Note the full URL it actually came from.
  2. 02View the page source (not the inspector) and search for maps.googleapis.com and maps/api/js. If you find the Google URL in the source but the browser loaded a different one, something rewrites it at runtime or at the edge.
  3. 03If the URL points to your own domain or a cache folder, check your optimisation or caching plugin. Exclude maps.googleapis.com from combining, minifying, deferring into a merged file and local hosting.
  4. 04If a CDN rewrites it, turn off third-party script rewriting for that URL or for the page.
  5. 05If the script is a saved file in your theme or app, delete it and restore the official tag shown below.
  6. 06Purge every cache layer: the plugin's cache, any server cache and the CDN. Otherwise visitors keep getting the rewritten HTML.
HTML: the direct loading tag
<script async
  src="https://maps.googleapis.com/maps/api/js?key=AIzaSy...your-key&loading=async&callback=initMap"></script>

Include this tag once per page. If you build with npm, use Google's loader package and let it request the API at runtime:

JavaScript (js-api-loader v2)
import { setOptions, importLibrary } from "@googlemaps/js-api-loader";

setOptions({ key: "AIzaSy...your-key" });
const { Map } = await importLibrary("maps");

Older code using new Loader(…) comes from version 1 of the package. Either version requests the API from Google, so both avoid this error.

WordPress and other CMS sites

Most cases on WordPress come from performance plugins. Look in their settings for sections named along the lines of file optimisation, JavaScript, or third-party scripts, and add maps.googleapis.com (and maps.gstatic.com, which serves map assets) to the exclusion list. Test after each change rather than switching several settings at once.

Also check for more than one Maps loader on the page. A theme and a plugin each adding their own can leave one rewritten copy and one genuine copy, which produces confusing, intermittent results. See fixing duplicate Google Maps loaders.

Not the same as a blocked script

If the request to maps.googleapis.com is blocked outright, you get a different picture: a refused request in the console and often no Maps error code at all. That usually comes from a Content Security Policy or an ad blocker. See Google Maps and Content Security Policy. This error only appears when some version of the script did run, just from the wrong place.

Confirm it is fixed

Reload in a private window with the Network tab open. The Maps loader request should go to maps.googleapis.com, followed by further requests to Google hosts as the map draws. The console should be clear of this error.

Then run a scan. It records every Maps request the page makes from a clean browser, so you can confirm the loader comes from Google even after caches and CDN rules are applied.

Common questions

Can I self-host the Google Maps JavaScript API for speed?

No. Google requires the API to be loaded directly from maps.googleapis.com. Use async loading or load the map only when it scrolls into view if you want to save page weight.

My optimisation plugin broke the map. Do I have to turn it off?

Usually not. Exclude maps.googleapis.com from its script combining, minifying and local hosting options, then purge the cache.

Is NotLoadingAPIFromGoogleMapsError caused by my API key?

No. It's about where the script came from. Once the loader comes from Google, any key problem will show up as its own error, such as InvalidKeyMapError.

Check whether this is your error

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

Related articles