Faster Google Maps load times

Google Maps is notoriously slow. The moment you add a Google Map to your page you can cut your Lighthouse performance score in half.

There are several ways you can fight with it:

  • Add async/defer to your Google script - certainly helps, but no much. This technique is very common so I won't be talking about it.
  • Loading the map only when a user scrolls to it. This works well the map is located below the fold. Code.
  • Using a static google maps image. There are cases when you might not need an interactive map and an image is all you need. The API for it is surprisingly versatile. Code.
  • Skipping fonts loading. Whenever you include Google maps script it automatically fetches a Roboto font. The map works perfectly fine without it, you can shave off a decent amount of time if you know how to block the font. Code.
  • Using a static preview of the map and then loading a full version on click. Perhaps the best technique when a map is located above the fold. Code.

By using these tricks you can achieve much better page performance (and SEO!) and save money on the costly Google Maps API.

Loading map on scroll (Code)

The idea here is to skip loading script until a user scrolls to it. This can be achieved using the IntersectionObserver API.
Below is an example implementation:

Using Maps Static API (Code)

Instead of loading the heavy script, at times, we can get away with loading only an image of the map. Google provides a way to add markets to it as well.
One of the unpleasant gotchas is that image resolution cannot go above 640px (1280px for x2 scale). Which means that an image is blurry.
Another interesting observation is that while this API is fast, you can make the image load 2x as fast by placing it behind a CDN. You can save some money this way as well.
You can read more about this API here.
Below is an example implementation:

Skipping font load (Code)

Whenever you include the Google maps script it automatically loads Roboto fonts to load. IMHO maps looks just fine without them and the extra HTTP requests are not necessary when every byte and millisecond counts.
Below is an example implementation:

Using an image placeholder and loading the map on click (Code)

If you want to provide users with a dynamic map but also care about page performance you can go with a hybrid approach and show a static image with a button to load the full version
Below is an example implementation:

These techniques can be combined any way you want to get even better results. For example, you can use observer API and use a static image as a placeholder.
You can read the full code at https://github.com/TheRusskiy/google-maps-optimizations.
Thank you for reading!

Popular posts from this blog

Next.js: restrict pages to authenticated users

HTTP server in Ruby 3 - Fibers & Ractors

Using image loader is Next.js