Reduce unused JavaScript
This recommendation can be resolved by activating the WP Rocket option Delay JavaScript Execution. Although it does not technically remove the unused parts of the code from your site, the delayed code is no longer detected by PageSpeed and the recommendation will not be triggered. Files excluded from this option may still trigger the recommendation.
If you expand the suggestion, this is what you’ll read:
“Consider reducing, or switching, the number of WordPress plugins loading unused JavaScript in your page.”
Plugins and themes have to load CSS and JavaScript on your pages to do their jobs. A slider at the top of your homepage, a form on the contact page, a shop page… All are loading scripts. The problem is when these same plugins, and your theme, still load all the same scripts and styles on all other pages, even when there’s absolutely no need for a slider, for a form, or for a single product on these pages… These are simply unnecessary. That’s what Google will consider as Unused JavaScript and CSS here.
In the example above, the JavaScript files are combined into one single JavaScript file by WP Rocket, so you can’t see which file(s) are concerned in detail.
To identify plugins that are adding extraneous JavaScript, analyze your non-cached version bypassing WP Rocket adding the ?nowprocket
query to your URL (i.e: https://www.example.com/?nowprocket
) then you can identify the theme/plugin responsible for the script's URL:
frontend.min.js from Elementor builder plugin could save more than half of its weight in this case.
What can I do then?
Our feature, Delay JavaScript Execution can help. By delaying the files, PageSpeed will no longer warn you about them.
In the example above, maybe the execution of the external /main.js loaded by widget.trustpilot.com could be delayed and not appear anymore in this diagnostic.
However, not every file can be safely delayed. You should only delay files which are not needed for anything “above the fold”. Please see our documentation for more details.
Alternatively, the following plugins can help you to minimize unused JavaScript from your pages. Please use them with care and consult your developer if you need help.
- https://wordpress.org/plugins/plugin-organizer/
- https://wordpress.org/plugins/wp-asset-clean-up/
- https://tomasz-dobrzynski.com/wordpress-gonzales
But the only way to truly satisfy any recommendation about removing unused JavaScript is to fully custom-code a theme/plugin, with page-level JavaScript. For custom development, we have partnered with Codeable, you can find more details about it here: https://wp-rocket.me/resources/codeable/