search ]

Exclude Scripts from WP Rocket’s Load JS Deferred

WP Rocket is a great plugin to speed up your WordPress site. One of its features, named “Load JavaScript Deferred”, helps speed up your site by delaying the loading of JavaScript files until after the main content is displayed.

However, this can sometimes interfere with scripts that need to load right away.

If something on your site breaks after enabling “Load JavaScript Deferred”, you might need to exclude specific files or scripts from this optimization.

When Should You Use “Load JavaScript Deferred”?

This feature is most effective for speeding up the initial page render. It works best on content-heavy pages without interactive elements.

WP Rocket also offers a separate feature called “Delay JavaScript Execution” that delays script execution until the user’s first interaction (scroll, click, or mouse move).

The key difference: Defer loads the script in the background and runs it after the HTML is ready, while Delay does not load the script at all until the user interacts. Defer is safer and suits most sites; Delay is more aggressive and can break components that need to run immediately.

Avoid using Defer, or prepare to exclude scripts, if your site depends on:

  • Live chat widgets
  • Interactive sliders
  • Maps (like Google Maps)
  • Form validation scripts
  • Analytics or tracking tools

In these cases, you’ll likely need to manually exclude scripts that break functionality, here’s how to do that:

Exclude Files and Domains

To exclude a script file, go to WP Rocket → File Optimization → JavaScript Files, and look for the field called “Excluded JavaScript Files”.

Excluded JavaScript Files

You can exclude scripts using:

  • The full URL of the script (remove any ?ver= or extra parameters)
  • A keyword from the URL
  • A wildcard to match a group of files

The exclusion field uses Regular Expression (regex) patterns. Special characters like (, ), [, +, ?, and * have special meaning in regex. To match a literal character, escape it with a backslash (e.g. gtag(). Invalid patterns are removed by WP Rocket with a warning.

If you enter an invalid pattern, WP Rocket will remove it and show a warning message: “WP Rocket: The following patterns are invalid and have been removed”. More details are in the invalid patterns article.

How to Identify Scripts to Exclude

If something breaks after enabling the feature, open your browser’s developer tools (F12 or Right Click → Inspect) and check the Console tab for errors. Look for:

  • Script 404 errors
  • “Is not defined” JavaScript errors
  • jQuery-related issues

How to Find the URL or Handle of the Script

Once you spot an error in the console, find the script’s URL. Go to the Sources tab in developer tools, or search the page source (Ctrl+U / Cmd+U) for <script> tags.

Scripts loaded via wp_enqueue_script get an id in the format handle-js. For example, if the handle is jquery-core, you’ll find:

<script src="...jquery.min.js" id="jquery-core-js"></script>

The keyword you enter in the exclusion field is part of the script’s URL or id.

For more on defer and script loading, see our guide on how to defer JavaScript and improve loading time.

Exclude Inline Scripts

If the problem comes from inline JavaScript (scripts inside your HTML, not loaded from a file), you can use this WordPress filter in your theme’s functions.php file or via a code snippets plugin:

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

  // Add your keyword here
  $inline_exclusions_list[] = 'custom-chat-widget-inline-js';

  return $inline_exclusions_list;
} );

In this example, we’re excluding a script with this ID:

<script id="custom-chat-widget-inline-js">
var chatOptions = { theme: "dark", autoOpen: false };
</script>

The keyword custom-chat-widget-inline-js will match that specific inline script.

Important: If any jQuery file is excluded, WP Rocket will stop deferring all inline scripts for safety.

Examples

1. Exclude a live chat widget script

<script src="https://cdn.livechat.com/widget.js?account=12345"></script>

Use this in the exclusion field:

cdn.livechat.com

2. Exclude a specific theme file

To exclude this file:

https://yoursite.com/wp-content/themes/your-theme/js/animations.js?ver=1.2.0

Use:

/wp-content/themes/your-theme/js/animations.js

3. Exclude all JavaScript files from a custom plugin

Use this format to target all files in a specific folder:

/wp-content/plugins/custom-gallery/js/(.*).js

4. Exclude a minified script loaded from cache

To exclude this file:

https://yourdomain.com/wp-content/cache/min/1/wp-content/plugins/social-share/js/share.min-ab123c456.js

Use:

social-share/js/share.min

Always test your site after excluding any script. If something still doesn’t work, try a broader or more specific pattern based on the file name or location.

Best Practices for Managing Script Exclusions

To get the most out of “Load JavaScript Deferred” while keeping your site functional, follow these tips:

  • Exclude as few scripts as possible, each one reduces the benefit of defer.
  • Prefer keywords over full URLs to keep exclusions flexible across environments.
  • Document your exclusions in a comment or internal doc for future updates.
  • Combine this feature with other optimizations like Critical CSS and lazy loading scripts.
  • If you remove scripts that plugins load on pages that don’t need them, you’ll need fewer exclusions in the first place.

FAQs

Common questions about excluding scripts from Defer:

What's the difference between "Load JavaScript Deferred" and "Delay JavaScript Execution" in WP Rocket?
Defer loads the script in the background and runs it after the HTML is ready. Delay does not load the script at all until the user interacts with the page (scroll, click, mouse move). Defer is safer and works for most sites; Delay is more aggressive and can break components that need to run immediately.
Can I exclude a script on a specific page only?
The exclusion field in WP Rocket applies to all pages. For page-specific exclusions, use the helper plugin available on WP Rocket's GitHub, or add custom code with an is_page() check inside the rocket_defer_inline_exclusions filter.
What happens if I exclude jQuery from Defer?
If jQuery is excluded, WP Rocket stops deferring all inline scripts site-wide. Many inline scripts depend on jQuery, so deferring them while jQuery loads normally would cause errors. WP Rocket disables inline defer for compatibility.
How do I know if the problem is from Defer and not another plugin?
Turn off "Load JavaScript Deferred" in WP Rocket and check if the issue disappears. If it does, the problem is Defer-related. Re-enable it and exclude scripts one by one until you find the culprit.
Does excluding scripts hurt performance?
Every excluded script loads normally and blocks rendering. The more you exclude, the less benefit you get from the feature. Exclude only scripts that actually cause problems, not "just in case."
Should I use a full URL or a keyword in the exclusion field?
Keywords are usually better. Full URLs can change between staging and production or across plugin versions. A keyword like the service domain or plugin name stays relevant after updates.

Summary

WP Rocket’s “Load JavaScript Deferred” feature helps improve site speed by delaying JavaScript loading. But sometimes it may cause issues with specific scripts. In those cases, you can exclude certain files or inline scripts:

  • Use the Excluded JavaScript Files field in WP Rocket → File Optimization.
  • Exclude scripts by full URL (without ?ver=), keyword, or wildcard pattern.
  • To exclude inline scripts, use the rocket_defer_inline_exclusions filter in your theme’s functions.php file.
  • Examples in this guide include excluding live chat scripts, custom theme files, plugin folders, and cached minified scripts.

Always test your site after excluding scripts to make sure everything works as expected.

Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo