How to reduce unused Javascript in Wordpress

pattern 25

Boost your website speed & performance.

*No spams

Pagespeed flags a page with “Reduce unused javascript” if it has a Javascript file with more than 20kb of unused code. It’s a recommendation to make pages render faster and also to avoid serving wasteful code for mobile users who don’t have unlimited data plans.

Load the Javascript file deferred and with low priority, or remove the JS being flagged from loading to address it. In most cases, you should defer and load with low priority the JS from marketing plugins and all other non-essential plugins until user interaction.

What defines the plugin you will use is if you want to remove it from loading completely or just load with low priority.

Another factor is that speed caching plugins offer a different number of optimization features. Choose one that addresses Core Web Vitals, not just Pagespeed.

Reduce unused Javascript with Perfmatters

Perfmatters offers defer and fetchpriority features. Load the Javascript with defer and fetchpriority=”low” attributes to address Pagespeed flagging.

You can also remove unused Javascript from loading. Check in the frontend admin page of the plugins to detect all pages using it, and then remove it from loading on all pages not using it with Perfmatters.

Perfmatters offers many features alongside removing unused JS. To use it, Install Perfmatters, go to “Assets”, enable the Script Manager, and click on the “Perfmatters” button at the top of each page to remove unused JS or CSS.

Perfmatters 20% OFF Coupon: WPALPHA

image 2
Perfmatters Remove Unused JS interface

Reduce unused javascript with Wp Rocket

Wp Rocket offers both delay and defer. Use defer with plugins that can’t be delayed(breaks functionality). Use delay for non-essential plugins.

The Delay JS feature delays all javascript, including inline scripts. It has an automatic JS exclusion list and an optional list of exclusions that can be included if you’re having errors. It also lists the active installed scripts retrieved on your website, which you can exclude from delaying:

image

There is also a textbox for placing the JavaScript you want to be excluded from. Use it to place essential javascript that cannot be delayed.

Reduce unused Javascript with Asset Cleanup

To reduce unused Javascript, you can remove it from loading. Check in the frontend admin page of the plugins to detect all pages using it, and then remove it from loading on all pages not using it with Asset Cleanup.

After installing Asset Cleanup, enable the option to load Asset Cleanup’s dashboard. Go to Wordpress pages or posts and you will notice a new button “Manage CSS & JS” in each one. Click on it and a new page opens with the enqueued files from that specific page.

Remove the unused JS enabling the “unload on this page” button. Only remove 100% unused JS files, after clicking and interacting with all elements on the page.

image 2

To better assist you with removing unused CSS or JS, change the Assets List Layout to “grouped by size”, which will list assets by size. Test any combination of removed assets using “Test Mode”.

Reduce unused Javascript with code (Advanced)

If you want to dequeue a javascript asset without using a plugin, do the following on the functions.php file:

/**
 * We will Dequeue the jQuery UI script as example.
 */
function wp_remove_scripts() {
// check if user is admin, therefore only logged-out users wont load the script
	if (current_user_can( 'update_core' )) {
            return;
        } 
	else {
    // Check for the page you want to target
    if ( is_page( 'homepage' ) ) {
        // Remove Scripts
		wp_dequeue_script( 'jquery-ui-core' );
        wp_deregister_script( 'jquery-ui-core' );
	    }
	}
}
add_action( 'wp_enqueue_scripts', 'wp_remove_scripts', 99 );

How to reduce unused Javascript in Wordpress with Autoptimize

Use any of the guides above, as all plugins/solutions above are compatible with Autoptimize.

Get your Wordpress unused Javascript optimized and your pages faster!

guest

0 Comments
Inline Feedbacks
View all comments