Inline JS Not Being Delayed When Included in the "JS Delayed Includes"Setting
I created a support thread on WordPress.org but the person responding there wasn't very helpful and doesn't seem like they understand my issue and/or how the plugin works. But perhaps I am wrong. So I'm coming here in hopes that I can get some clarification/confirmation on things. Here are the steps to reproduce:
- I have set the "Load JS Deferred" option to "Deferred".
- I am including a linked script in the "JS Delayed Includes" setting. It gets properly delayed.
- I am including an inline script in the "JS Delayed Includes" setting. It does not get delayed and remains deferred.
After looking at the plugin, there appears to be a missing conditional check for inline JS. When I add that check, the inline JS is properly delayed just like the linked JS files. Here is a video showing the issue:
https://share.cleanshot.com/qcCP9zfq
The support person on WP.org says you can't use deferred and delayed together. But that seems to be false as I am already able to use deferred and then pick and choose linked scripts which are to be delayed. That is already working. The issue simply seems to be that it doesn't work with inline JS.
Any update on this? Didn't see a fix for it in 7.1.
@thekendog I work as developer for LSC. I saw the WP related.
We talked internally about what this change implies:
- could be a good improvement
- but also is changing how JS is being handled and run on sites
For now we will not make any changes but we are are looking into how we can implement this
Well that is disappointing and I don't really understand it TBH. The help/description text for the "JS Delayed Includes" setting says this....
Listed JS files or inline JS code will be delayed. Both full URLs and partial strings can be used.
It specifically mentions inline JS so it seems like this was supposed to be working. If that isn't going to be fixed, then maybe edit that text to remove the mention of inline JS.
"but also is changing how JS is being handled and run on sites"....I don't really get that. You have to explicitly include the script in the setting if "Load JS Deferred" is set to "Deferred". That's exactly how the plugin already handles external scripts, so I don't see how doing the same for inline JS would be any different. If anything, this behavior is confusing because inline JS is treated the same as external scripts when "Load JS Deferred" is set to "Delayed", but different when set to "Deferred".
@thekendog I'm a bit late to the party, but I just found this issue while also having problems ( #876 ) with related settings. I saw that you also put a lot of effort and created a high quality report, so I thought I want to help you in some way. I also realized that you are an experienced developer as you exactly found and fixed the issue in one of the most complicated parts of the code - great job!
Well that is disappointing and I don't really understand it TBH.
The "problem" is that LSC became one of the biggest plugins in the whole WP ecosystem with over 7M active installs and unfortunately I can understand that changing something like that - which can definitely break stuff - in a plugin, with so many active websites, might have really bad consequences. And this can even mean it's "safer" to leave an actual bug as it is.
What I can suggest to you as a final effort is to use the litespeed_buffer_finalize filter, which is one of the highest level filters in the core. I haven't tested anything, but I guess you could hook it with 19 (or 21?) priority (before or after optimize.cls.php features are applied) and do what you need in your own code. Come up with a data attribute, or define a custom array for yourself, you get it.
Also, to read your "JS Delayed Includes" config value from any plugin/child-theme:
$lsc_core = \LiteSpeed\Core::get_instance();
$value = $lsc_core->conf( 'optm-js_delay_inc' )
// just make sure to properly check if `\LiteSpeed\Core` is available or you crash your site if you disable LSC
Edit:
Alternatively, as these guys were really nice with me, I guess if you ask nicely enough, @timotei-litespeed could might add a litespeed_optm_js_delay_inc_force or similar filter to _js_inline_defer() which would force the inlining of the specified cfg_js_delay_inc scripts. :)
@frzsombor hehe :) this is not about how nice he ask, or how many downloads we have. We(I talked to the team about) agree with the issue described, but at this moment there are no plans to change this functionality. Changes will come, but we want to find/apply the best solution.
Why we are not changing this now: it affects current setups and will change how current sites are working.
As you said: litespeed_buffer_finalize will allow to change how JS is defered/delayed.
@timotei-litespeed I can totally understand that and respect your decision. However, as I just wrote in an other issue, I highly recommend to update the docs and the input descriptions and highlight these edge cases to the end users, because the vast majority of them has no ability to read and understand the code like we here and they will simply feel that the library is not working and/or having bugs.
@frzsombor I ended up having to buy Perfmatters as it works properly. A little annoying since this is an easy fix. And like you said, tt's also frustrating that the instructions literally say inline JS can be targeted but that is false.
https://share.cleanshot.com/cH1h7J6H