Excluding inline JavaScript from combine

Combine JavaScript files applies also to inline JavaScript and 3rd party scripts.

If this behavior is creating any issue on your site, you can exclude inline JavaScript from the combine option. 

In this Article

Exclude specific scripts

Find a unique string in the inline JavaScript you wish to exclude and place that in the Excluded Inline JavaScript field. WP Rocket will search for this string in the inline JavaScript to decide if it should be excluded or not:

If you're experiencing an issue with too many JavaScript files being created, please read this guide: Fix Large Cache Folder Size Caused by Dynamic Inline JavaScript

Exclude all inline scripts

It can be useful to quickly exclude all inline scripts while troubleshooting, to check if the issue you're facing is related to inline scripts at all. To exclude them all, add the following in the exclusion field:

;
,
=
(
{

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.

How to find inline JavaScript to exclude

The instructions below will cover cases where the optimization of Inline JavaScript is causing an error message in your browser console.

We understand it can be difficult to locate which inline JavaScript to exclude, so we'll walk through one way to do it.

In this example, we refer specifically to using the Chrome browser.

1

Open Developer Tools. You do that either by right-clicking and choosing Inspect Element, or at the top of the window go to: View→Developer→Developer Tools

2

Find the red error message indicator on the right-hand side:

3

Click it to open the Console, then click the file referenced in the error notice:

4

This takes you directly to the code in the file that is related to the error. Look for the red underline and copy part of that code:

5

Open a new browser tab and load the uncached version of the page. You do that by adding ?nowprocket to the URL—this will load an uncached, unminified version of the page. It is necessary in order to find the original URLs of the files.

An uncached URL would look something like:   https://example.com/?nowprocket or: https://example.com/some-page/?nowprocket

6

Open Developer Tools in the new browser, then click on the Console:

7

Paste the following code in the Console:

var scripts = document.getElementsByTagName("script");
for (var i=0;i<scripts.length;i++) {
   if (scripts[i].src) console.log(i,scripts[i].src)
   else if (scripts[i].dataset.rocketSrc) console.log(scripts[i].dataset.rocketSrc)
   else console.log(i,scripts[i].innerHTML)
}

The snippet found here too.

Like this:

      
8

Press enter to populate the console with all scripts, including inline scripts in red:

You can search for the inline script code reference, then choose a unique string to enter into the exclusion area in our settings. 

If the error is related to a 3rd party JavaScript file, follow the directions here to exclude it from combining.

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