<script id="wp-i18n-js-after"> wp.i18n.setLocaleData({ 'text direction\u0004ltr': ['ltr'] }); </script>
Any other suggestion than excluding the 2 scripts (wp-i18n-js & wp-hooks-js) form defer?
]]>I want to defer all Javascript which is executed from plugins folder but one plugin! I did not find a way to exclude just this plugin-folder from defering …
Is there any way to sa: please defer all Javascript files from Pluginsfolder but not a specific plugin?
Best
]]>// mark recaptcha js deferred
function defer_js( $url ) {
if ( is_user_logged_in() ) return $url;
if ( strpos( $url, 'recaptcha/api.js' ) ) {
return str_replace( ' src', ' defer src', $url );
}
return $url;
}
add_filter( 'script_loader_tag', 'defer_js', 11 );
]]>I am writing to you because I have encountered issues with the “script loading strategies” recently introduced in the WordPress core (https://make.www.ads-software.com/core/2023/10/17/script-loading-changes-in-wordpress-6-4/) and the “Load JS Deferred” feature of Litespeed set with the delay option.
I am opening this thread to share the solutions found and perhaps provide Litespeed with insights for possible improvements to their fantastic plugin.
In summary, to optimize script loading (if specified during script registration), WordPress now adds the async, defer, and data-wp-strategy=”async” or data-wp-strategy=”defer” attributes to HTML tags.
—– Issue 1 – Optimization with async:
If the theme has not declared compatibility with the HTML5 SCRIPT tag, WordPress outputs the script tag specifying the values for the async and defer attributes (e.g., async=”async” or defer=”defer”).
<script type="text/javascript" src=".../wp-includes/assets/js/comment-reply.min.js" id="comment-reply-js" async="async" data-wp-strategy="async"></script>
Litespeed, configured to delay JavaScript scripts, removes the async attribute in its optimizations but ignores the possibility that there might be a value for that attribute (file plugins/litespeed-cache/src/optimize.cls.php, function _js_defer(), line 1230 -> $ori = str_replace(‘ async’, ”, $ori);). This results in an HTML tag that is not formally valid (containing the string =”async”), causing JavaScript errors on the frontend.
<!-- MALFORMED TAG - DO NOT USE! -->
<script data-optimized="1" type="litespeed/javascript" data-src=".../wp-content/litespeed/js/516fdadcf934cd94ab0b3ba4b5bb56df.js?ver=b56df" id="comment-reply-js"="async" data-wp-strategy="async"></script>
Solution 1:
Considering that the mentioned _js_defer function runs multiple times for different purposes (optimizing and deferring), to bypass the issue and restore the functioning delay mode, include the script in both “JS Excludes” and “JS Deferred/Delayed Excludes.” This way, Litespeed won’t touch that tag, and it will utilize the optimization originally intended by the script registrar.
Solution 2:
Declare theme compatibility with HTML5 SCRIPT tags so that WordPress creates the script tag with only the async attribute (without a value), allowing Litespeed to transform the code correctly.
/**
* NMod - Add compatibility with HTML5 script tags
*/
\add_action( 'after_setup_theme', function() : void {
\add_theme_support( 'html5', [ 'script' ] );
}, 10 );
—– Issue 2 – Optimization with defer:
Consider that:
– WordPress optimizes the loading of external resources, but additional “extra” code portions (e.g., added via wp_localize_script()) do not undergo optimizations;
– Litespeed optimizes the loading of all resources, both external and additional code portions.
When the loading of some scripts is natively optimized by WordPress/WooCommerce with the defer strategy (e.g., wc-single-product.js), Litespeed ignores such scripts (does not apply async, as their loading has already been optimized) but applies async to any extra scripts, altering the resource download order and disrupting script dependencies.
Solution:
IN MY CASE, having optimized the site to work with the delay applied by Litespeed, I removed the attributes for optimized loading added by WordPress from all script tags.
/*
* NMod - Remove WordPress JS optimized loading strategy
*/
\add_filter( 'script_loader_tag', function( $tag, $handle, $src ) : string {
return \str_replace(
[
' async="async"',
' async',
' data-wp-strategy="async"',
' defer="defer"',
' defer',
' data-wp-strategy="defer"',
],
'',
$tag
);
}, 10, 3 );
Hope that this thread can help someone!
]]>Thank you.
]]>Here’s the problem I want to solve : I recently started using the plugin WP-rocket to improve my website’s performance. It includes the option of deferring non-essential javascript files.
As a result, it messes up with Jetpack’s counting of the page visits, and the Jetpack stats are anormally low.
If I can identify the javascript file associated with jetpack stats, I can exclude it from the plug in and solve the problem.
Can anyone tell me the name of this file ?
Thanks
]]>we are currently facing a translation problem in relation to our WooCommerce platform.
To enhance the overall checkout experience for our customers, we have implemented a delay in sending emails. However, we have observed that this adjustment is causing a disruption in the translation process of our communication to customers through the Weglot integration and our customers receive emails in a default language.
Is it somehow possible to rectify this problem, or what measures can we take to prevent its recurrence? We wish to retain the delayed email feature while ensuring that the emails are translated.
Thanks
add_filter( 'woocommerce_defer_transactional_emails' , '__return_true' )
]]>