Currently, âLoad JavaScript deferredâ option in the File Optimization tab of WP Rocket includes a sub-option, âSafe Mode for jQuery (recommended)â. With this update, we propose to remove this sub-option.Â
This update will be part of making WP Rocket simpler and easier to configure and use.Â
As per Mixpanel data, almost all users (98%) with âLoad JavaScript deferredâ option have enabled the âSafe Mode for jQueryâ sub-option. The UI isnât adding value to many users and can be confusing for novice users. Removing it will make WP Rocket easier to configure.Â
The reason why we need this option is because jQuery references in in-line JavaScript will need the jQuery file to be not deferred. With this enhancement, we will be delaying inline JS and this feature will no longer be needed and the jQuery file can be deferred.Â
Since inline JS is delayed with this enhancement, more JavaScript execution will be delayed and this should mean that the website is interactive faster (i.e. time to interactive is lower). This will reflect in better user experience and better ranks on PageSpeed insights.Â
Test website: http://mega.wp-rocket.me/
Test method: Tests were done first with âSafe Mode for jQueryâ enabled and then with it disabled. The website was repeated on PageSpeed thrice with each setting. Combine JS was enabled for all these tests.Â
Average PageSpeed score. With Safe Mode for jQuery enabled = 68, disabled = 78
This test doesnât account for delayed inline JS. That should only improve the scores.Â
New Test with 3.8-alpha1
Test page: http://mega.wp-rocket.me/no-defer-js
Left: WP Rocket config: 3.7.6, Load JS defer and safe mode for jQuery enabled. JS minification and combination disabled
Right: WP Rocket config: 3.8-alpah1. Load JS deferred enabled. jQuery not excluded from defer.

PageSpeed scores are better^

jQuery file is no longer recommended^
This update will be completely transparent to users. The UI will simply disappear on update and everything will be handled automatically in the background.Â
During the QA process, some changes were made to how the change would affect existing users and new users. Details are available here: https://github.com/wp-media/wp-rocket/pull/3366#issuecomment-735850052
By wrapping all jQuery inline scripts into a DOMContentLoaded event, we will be able to get rid of Safe Mode for jQuery options.
Some code example here with jQuery deferred: https://playcode.io/679488/
To know which inline scripts contain a jQuery inline script, we will detect them by checking these patterns:
jQuery
jQuery.
$.(
$(
Notes:Â
There are two methods used in the above example. One where the inline JS is converted to base64 code and then deferred. Second where inline JS is wrapped into âwindow.addEventListener('DOMContentLoaded', function()â.Â
The second approach essentially delays inline JS and can give better performance. This is the one we hope to use.Â
For comparison, âLightSpeed Cacheâ has the option to use the second approach. Check Page Optimization > JS Settings tab > Load inline JS
Other specifications:Â
If inline JS contains keywords to be delayed using our âDelay JavaScript executionâ feature, these scripts can be excluded.Â
If inline JS contains the pattern âDOMContentLoadedâ or âdocument.writeâ, these scripts will be skipped.
If jQuery is excluded from JavaScript defer, then we can skip the entire process. This will be similar to how âSafe Mode for jQueryâ worked. This can act as a backup plan if our update doesnât suit some websites.
With the UI that will be added for file exclusions, excluding the jQuery file from JS defer will be easy for our users. If they see a âjQuery not definedâ error in spite of our mechanisms, they can simply exclude the jQuery file (based on our documentation) and all can go back to normal.Â
About 60% of our users will be impacted by this update, so we need to roll this out with a major version release to take advantage of our staggered release.Â
The UI for âSafe Mode for jQueryâ is removed after the update.Â
jQuery is added to the combined JS file.Â
Inline JS is delayed and there are no console errors.Â
If inline JS contains keywords to be delayed using our âDelay JavaScript executionâ feature, these scripts are to be excluded from the delay.Â
If inline JS contains the pattern âDOMContentLoadedâ or âdocument.writeâ, these scripts will be excluded from the process.
If jQuery is excluded from JavaScript defer, the inline scripts are not delayed.Â
The following documentation needs to be updated:
Link to feature: https://wp-media.productboard.com/feature-board/planning/features/5722864
Created by: @arunbasillal
1. Update class WP_Rocket\Engine\Admin\Settings\Page and remove defer_all_js_safe
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Admin/Settings/Page.php#L804-L816
2. Remove defer_all_js_safe from upgrader.php
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/admin/upgrader.php#L187
3. Modify Settings.php and remove defer_all_js_safe
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Admin/Settings/Settings.php#L225-L232
4. Revisit get_jquery_urls() function and the use of it. I think this function and the use of it needs to be totally removed and deprecated.
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Optimization/Minify/JS/AbstractJSOptimization.php#L52-L56
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Optimization/Minify/JS/AbstractJSOptimization.php#L161
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Optimization/Minify/JS/Combine.php#L65
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Optimization/Minify/JS/Combine.php#L32
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Optimization/Minify/JS/Combine.php#L179-L184
5. Remove defer_all_js_safe from Data Support
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/Engine/Support/Data.php#L27
6. Remove jQuery from get_rocket_exclude_defer_js()
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/functions/options.php#L452-L464
7. Revisit all fixtures and tests which contain: defer_all_js_safe
defer_all_js_safedefer_all_js_safedefer_all_js_safe1. Move /inc/front/deferred-js.php into the new structure /inc/Engine/Optimization/DeferJs
https://github.com/wp-media/wp-rocket/blob/3b8b2d7debc83641045f9af82baa08b14e41daed/inc/front/deferred-js.php#L14
Create the files and functionality for:
/inc/Engine/Optimization/DeferJs/Subscriber.php/inc/Engine/Optimization/ServiceProvider.phprocket_defer_js() function and split it in 2. Handle first the scripts with src and the second part will handle the inline scripts.$( , $.( , jQuery., jQuery - and other variations đ ) - if it does not contain will be skipped. window.addEventListener('DOMContentLoaded', function() {
// Add inline JS
});
rocket_defer_js() Write tests (unit & integration )
Make sure that the new code pass the acceptance criteria:
Acceptance Criteria
The UI for âSafe Mode for jQueryâ is removed after the update.
jQuery is added to the combined JS file.
Inline JS is delayed and there are no console errors.
If inline JS contains keywords to be delayed using our âDelay JavaScript executionâ feature, these scripts are to be excluded from the delay.
If inline JS contains the pattern âDOMContentLoadedâ or âdocument.writeâ, these scripts will be excluded from the process.
If jQuery is excluded from JavaScript defer, the inline scripts are not delayed.
@wp-media/php I think this one needs to be done after this https://github.com/wp-media/wp-rocket/issues/3233 is finalized and merged. What do you think?
Yes indeed, we need to complete it first before doing this one, as there is a lot of overlap.
If we had this setting disabled before the update, will it remain disabled after the update for existing installs?
If we had this setting disabled before the update, will it remain disabled after the update for existing installs?
It's not going to be just a removal from the UI, it's also going to change how WP Rocket handles inline JS especially ones that use jQuery in it.
Disabling this option the sliders of the "revslider" plugin are not shown, 3 jquery errors appear
Disabling this option the sliders of the "revslider" plugin are not shown, 3 jquery errors appear
@Camilo517 The feature will do more than simply disabling the option. Don't worry yet. đ
Most helpful comment
@Camilo517 The feature will do more than simply disabling the option. Don't worry yet. đ