Optimizing with Kinsta Analytics and KeyCDN

Kinsta just release a new analytics dashboard which contains a list of the top 30 bandwidth usage files. I thought I’d give it a look over to see if I could make any optimizations. Since all of my Kinsta websites have KeyCDN enabled anything in the top list are most likely not loading from my CDN. At first glance there was one file which stuck out called zxcvbn.min.js.

Screen Shot 2017-07-30 at 7.28.56 PM.png

That particular file is part of WordPress core and allows for password strength estimating. One of my websites has a bunch of 3rd party WooCommerce addons which is causing zxcvbn.min.js, a 393KB file, to enqueue excessively. While fixing that would reduce my Kinsta bandwidth, the real issue is this file isn’t being served from the CDN.

The following will reregister the script to use a KeyCDN’s url instead of the local install. I have the script set to run only if my KeyCDN configs are found in wp-config.php.

// Load zxcvbn.min.js (393KB file) from CDN if KeyCDN is defined via wp-config.php
add_action('wp_enqueue_scripts','zxcvbn_script_fix',1);
function zxcvbn_script_fix(){
    if( defined('KEYCDN_API_KEY') && defined('KEYCDN_ZONE_ID') ) {
      wp_deregister_script( 'zxcvbn-async' );
      wp_register_script('zxcvbn-async', 'https://websitename-XXXX.kxcdn.com/wp-includes/js/zxcvbn.min.js',array(),'1.0');
    }
}