Exclude files from Load JavaScript Deferred

In some cases, the option Load JavaScript deferred  may conflict with a specific file.

If this option causes any problems, you may need to exclude a file from the process. 

Exclude files and domains

You can use the field in the plugin settings to exclude files:

 Exclusions may be done by using any of the following:

  • Full file URLs (with query strings removed)
  • Keywords from the file URL
  • Wildcards

Heads up!

Certain patterns of exclusions cannot be handled and are automatically removed.

If you add them, you should see a notice with the "WP Rocket: The following patterns are invalid and have been removed" message.

Please see more in our Invalid patterns of exclusions article.

Exclude inline scripts

You can use the rocket_defer_inline_exclusions filter to exclude specific inline scripts from being deferred, like this:

add_filter( 'rocket_defer_inline_exclusions', function( $inline_exclusions_list ) {
  if ( ! is_array( $inline_exclusions_list ) ) {
    $inline_exclusions_list = array();
  }

  // Duplicate this line if you need to exclude more
  $inline_exclusions_list[] = 'key_from_target_script';

  return $inline_exclusions_list;
} );

You'd need to replace key_from_target_script with a string of your real inline script.

You can add the above script as shown in the Add Code Snippets guide.

Please note, key_from_target_script should be a unique word that is used only in the script you want to exclude, because if the same word is used in other scripts then all the scripts in which it is used will be excluded.

For example, in this script:

<script id='kl-identify-browser-js-extra'> var klUser = {"current_user_email":"","commenter_email":""}; </script>

kl-identify-browser-js-extra would be a good candidate.

Note: if any jQuery library is excluded, WP Rocket won't defer inline JavaScript.

Examples

Exclude an externally hosted file

<script src="//maps.googleapis.com/maps/api/js?key=AIzaSyBusRuOn8uzsREWbwi-KCU4puf0me2aj3A&libraries=places" type="text/javascript"> </script>

You could simply use the domain:

maps.googleapis.com

This applies to external files that weren't minified by WP Rocket, and thus, kept their original external domain name.

Exclude a single file

To exclude this file:

https://example.org/wp-content/plugins/elementor-pro/assets/js/frontend.min.js?ver=3.0.5

Use the following:

https://example.org/wp-content/plugins/elementor-pro/assets/js/frontend.min.js

Exclude a group of files

To exclude all files from a specific plugin folder, you could use a wildcard:

https://www.example.org/wp-content/plugins/my-plugin/assets/js/um-crop.min.js?ver=2.1.12

Use the following:

/wp-content/plugins/my-plugin/assets/js/(.*).js

Exclude a minified file

To exclude a file which is also minified, the exclusion has to be based on the minified file URL. So choose a part of the URL (excluding the domain) which is unchanged by minification.

To exclude this file:
https://www.example.org/wp-content/cache/min/1/wp-content/plugins/plugin-name/assets/js/pickadate/picker.time-f9a2f2b5fb728f25892f2f41b6f8a4b9.js

Use the following:

plugin-name/assets/js/pickadate/picker.time

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