-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update the wp_add_inline_script
function to ensure the inline scripts specify a type="text/template"
attribute for inline scripts that are associated with deferred, when applicable
#18
Comments
wp_add_inline_script
function to ensure the inline scripts specify a type="module"
attribute for inline scripts that are associated with deferred, when applicablewp_add_inline_script
function to ensure the inline scripts specify a type="text/template"
attribute for inline scripts that are associated with deferred, when applicable
I'm not sure if this has already been considered, but another option we could consider here is to load the inline scripts as a callback function on custom browser events that are fired as part of the |
@joemcgill I'm not sure I understand the suggestion here. Can you provide a reference to or an example of such a custom browser event? If you're referring to a custom JavaScript event still, how can we avoid any form of DOM manipulation, even if we are to introduce a new script element as opposed to modifying an existing text/template one? |
This may be a totally incoherent suggestion, but instead of printing the inline script into a script with const wpScriptLoaded = new CustomEvent("wp-script-loaded", { detail: {handle: elem.dataset.handle });
// Main script.
<script src="{path}" defer data-handle="main-script" onLoad="() => document.dispatch(wpScriptLoaded)"
// Inline script, wrapped in a listener callback.
<script>
document.addEventListner(
"wp-script-loaded",
(e) => {
if (e.detail.handle === 'main-script' {
// Inline script printed here:
...do stuff.
}
}
);
</script> Hopefully that explanation is more clear. |
Thanks @joemcgill and this is actually exactly what I was looking at last night, so we are indeed on the same page. I have no objection to this approach, do you @felixarntz ? Browser support is probably good enough too: https://caniuse.com/?search=CustomEvent |
@joemcgill @10upsimon We had discussed wrapping the inline script in logic at some point a few weeks/months back, but that is not feasible because it means the code is wrapped. We need to ensure the scope of the inline JS execution remains the same (e.g. global JS variables remain global). That's why we should go with the |
That reasoning makes sense to me. Thanks for confirming @felixarntz. |
Thanks @felixarntz , leaving this as is then and ready to proceed @kt-12 |
Description
In order for applicable inline scripts to execute only once their deferred/async main/parent scripts have loaded (see issue 17), scripts should not execute automatically, and only on command. For this reason,
type="text/template"
should be added to said inline<script>
tags. This prevents them from being executed by the browser.onload
handlers will be applied to the parent/main script in a future issue, ensuring that the inline module scripts be converted to JavaScript scripts, and then executed.Acceptance Criteria
type="text/template"
as opposed totype="text/javascript"
when they meet the business criteria as outlined in issue 17.Unit Tests
wp_add_inline_script
method currently reside in WordPress core source, starting at this location.type="module"
.Designs
No response
Describe alternatives you've considered
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: