Exclude files from Defer JS

In rare cases, the option Load JavaScript deferred (in File Optimization → JavaScript Files) may conflict with a specific file.

If this option causes any problems, you may need to exclude a file from the process. You can do that by downloading, installing, and editing the following small helper plugin.

  📥 Download (.zip): WP Rocket | Exclude Files from Defer JS
Developers: You can find the code for this plugin on GitHub.

Heads up! You must edit the helper plugin before use!
Remove the sample value of /wp-includes/js/jquery/jquery.masonry.min.js and insert the script path you wish to exclude. In order to exclude multiple files, copy the entire line, paste it after the first one, and edit the file path accordingly.

Tips to make this work

Files hosted on your own domain: Remove query string

Remove any query string following the .js extension. For example, to exclude:

https://example.com/wp-content/themes/neutro/js/responsive-menu.js?ver=46e6ce

use:

$excluded_files[] = '/wp-content/themes/neutro/js/responsive-menu.js';

Files hosted on external domains

From WP Rocket 3.1.3, you may exclude external JS files (i.e. those not hosted on your domain) using: 

  • Only the domain
  • Any part of the URL
  • Wildcards

 For example, to exclude this URL:

https://ws.sharethis.com/button/st_insights.js?publisher=123-abc-456&product=simpleshare

any of the following will work: 

$excluded_files[] = '/button/st_insights.js';
$excluded_files[] = 'ws.sharethis.com';
$excluded_files[] = 'https://ws.sharethis.com/button/(.*).js';

Alternate Exclusion Method

You can also exclude files from being deferred by modifying the script tag where they are included. 

Adding: async="false" will cause WP Rocket to skip the file, and setting it to "false" means async won't be applied. For example:

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.