WordPress jQuery is Render-Blocking in PageSpeed Insights

PageSpeed Insights Score

After running my site through Google PageSpeed insights, I noticed that all of my JavaScript was being deferred except for the jQuery that comes with WordPress.

This was dinging by PageSpeed insights score, as it was telling me to eliminate the render-blocking resource (jQuery 1.12.4). My caching plugin wasn’t able to defer it along with the rest of my site’s JavaScript code, as it’s a core element in WordPress.

To resolve this, I had to deregister jQuery, and replace it with a new version from jQuery.com:

/**
* Remove built in jQuery and replace with hosted version to improve speed, prevent render blocking, and defer
*/

function my_enqueued_assets() {
wp_deregister_script( ‘jquery’ );
wp_enqueue_script( ‘script-name’, ‘//code.jquery.com/jquery-1.12.4.min.js’, array(), ‘1.12.4’ );
}
add_action( ‘wp_enqueue_scripts’, ‘my_enqueued_assets’ );

I’m pretty sure all of the latest versions of WordPress load 1.12.4, but check the source of your WordPress website to ensure that yours is the same.

This helped save nearly 1 full second from my page load time, and my score went up 6 points. Not a drastic difference, but still a welcomed improvement.

Oddly enough, when I tried using the Google Hosted Library with the above code, it wasn’t able to defer it either:

https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js

So, I stuck with the jQuery.com hosted version linked above instead.

I’m using WP Rocket for caching, but this should also use with other caching plugins, or plugins that help to defer javascript.

Please let me know if this helped you in the comments below!

UPDATE:

There’s a second method that does the job as well; installing the plugin “Async Javascript”.
After installing, in the settings, there’s an area under “Quick Settings” that says “Apply Async”.

It’s important to check your console for JavaScript errors and make sure that it doesn’t break jQuery. jQuery can sometimes break when loaded with async or defer, but on my particular instance, it worked perfectly.

Comments:

No comments yet. Be the first!

Leave a Reply

Copyright 2020, All rights reserved. Yadda yadda.