How to speed up Divi by avoiding unnecessary loading of Google Maps scripts

divi header

You may not have noticed, but the Divi theme sometimes loads Google Maps, even when there are no maps on the page. This results in about 5 unnecessary requests on those pages. Avoiding these requests will speed up the loading of these pages.

Why and where does Divi load unnecessary Google Maps scripts?

divi map

This happens when you are viewing a page that displays an excerpt of a publication that, when viewed in its entirety, contains a map module (or full-width map module).

Although the map module is not displayed as part of the generated excerpt, the act of generating the excerpt fully processes the publication and causes the map module to load the scripts it would need, namely Google Maps scripts.

But since the extracts don’t show a map, they don’t need to load the scripts, so it’s an unnecessary script load, which slows down the loading speed of these listing pages.

Here’s how to avoid this behavior and save about 5 requests on archive pages.

Prevent Google Maps scripts from loading in Divi when they are not needed

To prevent Divi from loading Google Maps scripts on pages where you are not actually showing a Google map, but excerpts from posts that – later – will show it, just run the following code:

/* Prevent load of Google Maps scripts */
add_action('loop_start', 'dbc_loop_start');
add_action('loop_end', 'dbc_loop_end', 100);
// Prevent the loaf of Google maps scripts by map modules
function dbc_loop_start($query) {
if (dbc_map_modules_in_excerpts($query)) { 
add_filter('et_pb_enqueue_google_maps_script', 'dbc_return_false');
}
}
// Reactivate the script when the loop is done
function dbc_loop_end($query) {
if (dbc_map_modules_in_excerpts($query)) { 
remove_filter('et_pb_enqueue_google_maps_script', 'dbc_return_false');
}
}
function dbc_map_modules_in_excerpts($query) {
// Can't affect admin
if (is_admin()) { return false; }
// Can't affect visual designer
if (!function_exists('et_core_is_fb_enabled') || et_core_is_fb_enabled()) { return false; }
// Can't affect singular posts
if (is_singular()) { return false; }
// Can't affect secondary queries
if (!$query->is_main_query()) { return false; }
// Can't affect Divi 
if (!function_exists('et_get_option') || et_get_option('divi_blog_style', 'false') === 'on') { return false; }
return true;
}
function dbc_return_false() { return false; }Code language: PHP (php)

How do I add this code?

You can run this code on your website mainly in 2 ways:

  1. By installing and activating the Divi Booster plugin and then activating the Site SpeedStop map module excerpts from unnecessarily loading maps scripts option.
  2. By manually adding the code as I explain in detail in this guide.

How useful was this post?

Click on a smiley to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top
Skip to content