Load JavaScript deferred

In this Article

What does Load JavaScript Deferred do?

This option addresses the   Eliminate Render Blocking Resources PageSpeed recommendation.

It is preferable for performance that JavaScript files are either loaded in the footer of your site, or deferred so that they don’t block the downloading of other assets on your site, thereby slowing it down. 

By enabling Load JavaScript deferred, all the JavaScript files on your page, including those minified by WP Rocket, will be loaded with the defer attribute:

This includes scripts loaded with a src even if they don’t have a .js extension.

We also look for all inline scripts related to jQuery, and we defer them as well. This provides better compatibility so that jquery.js can be deferred in more cases.

Resolving Problems

Depending on your site, you may also need to exclude some more files from being deferred:  
Exclude files from Defer JS

Note that Google does not take into consideration that sometimes it’s not possible to defer all JavaScript—at least not without breaking your site. Any files you exclude from deferring will cause PageSpeed to complain, but in some cases it may be necessary to preserve functionality.

Technical Notes

To make it possible to defer jquery.js, WP Rocket looks for inline scripts that require it, and they are deferred.

We wrap them with the following code:

window.addEventListener('DOMContentLoaded', function() { wrapped_script });

Technically the inline scripts are not “deferred” - you won’t see the defer tag. But this executes the scripts later, on DOMContentLoaded.

We ignore, i.e. don’t defer inline scripts that:

  • contain DOMContentLoaded or document\\.write
  • don't contain jQuery or $.( or $(,

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