How to Defer Parsing of JavaScript in Breeze


#1

How do I do it in Breeze?
Thanks for any help!


#2

I would also like to know this as GTMetrix gives an F grade of 0 for “Defer parsing of JavaScript” For me, specifically, it’s showing these URLs. I tried adding them to the last setting under the advanced options, but it didn’t seem to help.

https://www.youtube.com/yts/jsbin/player_ias-vflxACNZ2/en_US/base.js (955.8KiB)
https://www.youtube.com/yts/jsbin/player_ias-vflxACNZ2/en_US/base.js (955.8KiB)
https://www.youtube.com/yts/jsbin/www-embed-player-vflvZogvA/www-embed-player.js (90.3KiB)
https://www.youtube.com/yts/jsbin/www-embed-player-vflvZogvA/www-embed-player.js (90.3KiB)
https://www.youtube.com/embed/66A8Y0dAo5g?feature=oembed&start&end&wmode=opaque&loop=0&controls=0&mute=0&rel=0&modestbranding=0 (3.3KiB of inline JavaScript)
https://www.youtube.com/embed/tCKiulQDTEE?feature=oembed&start&end&wmode=opaque&loop=0&controls=0&mute=0&rel=0&modestbranding=0 (3.3KiB of inline JavaScript)

#3

Cloudways’ Breeze plugin for Wordpress does not have a Defer Parsing feature.

So, you will either need add custom code to your functions.php file like this: (Ex: defer everything except jQuery):

// Defer javascripts (except jquery.js)
if (!is_admin()) {
  function defer_parsing_of_js ( $url ) {
    if ( FALSE === strpos( $url, '.js' ) ) return $url;
    if ( strpos( $url, 'jquery.js' ) ) return $url;
    return "$url' defer onload='";
  }
  add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 );
}

Or use a plugin like:

  • Autoptimize
  • Async JavaScript
  • WP Rocket

If you do choose to use a Plugin, you may have to blacklist some plugins/javascript files in order for certain features on your website to load properly. And if you really need a specific plugin to defer loading a javascript file, but doesn’t work if you use the above methods, the plugin’s code will likely need to change so that it will wait to try to use the functions in the javascript file until it has loaded. Meaning, you either need to contact the plugin’s developer or hire someone like me! :slight_smile: to handle this for you.