Implementation of load handlers to ensure that the execution order of inline scripts is retained, where applicable #20
Labels
30
Estimate of 30 points
Script Loading Strategy
Issues relating to the Script Loading Strategy (WPP)
type:enhancement
New feature or request.
Milestone
Description
Given that inline scripts need to retain their execution order even when their parent/main scripts contain a loading strategy such as
defer
orasync
, these scripts will need to be executed as specific times as dictated by the execution order of said parent/main scripts.For example:
after
inline script, it could wrapped with aload event listener
on its parent/main script (when it isdeferred
), so that the code is only executed once its parent/main script is loaded, whenever that may be.before
inline script(s), the underlying dependency chain becomes complicated to solve for, withonload
handlers needing to be applied to the last dependency prior to the inline script in question, or even after if said last dependency has anafter
script associated with it. Due to the inherent complexity surrounding this,before
scripts should be forced to have ablocking
strategy. This also means that any dependencies that aredeferred
should too becomeblocking
unless thebefore
script is opted in as standalone (see issue 15).For any
after
inline script, using a load wrapper approach could have a sequence of events that look something like the following:deferred
loading.$standalone
parameter value oftrue
orfalse
. Iftrue
, no further action is required and the script is treated as a standalone blocking inline script. Iffalse
, however, said inline script is output in the footer, but instead of being output with the usual<script type="text/javascript">
tag, it is output in a form that will not immediately be executed as JavaScript by the browser, i.e<script type="text/template">
. This script element may be given a sensible data attribute associating it with a script handle and identifying it for later conversion to JavaScript and execution thereof. It should be noted that simply converting the scripttype
will not ensure automatic execution by the browser, and execution thereof will need to be triggered programatically.<script type="text/template">
elements containingdata-wp-executes-after
attributes (or similar, this is merely an example), and references them and the handles they rely on.load
events of saiddeferred
parent/main script(s). The callback itself will parse the applicable<script type="text/template">
elements, changing them to<script type="text/javascript">
and applying necessary logic that causes them to be executed and parsed as JavaScript by the browser.Acceptance Criteria
data-wp-executes-after
data attribute to applicable inline scripts who have been identified as having deferred main/parent scrips, requiring a specific execution order, specifically those rendering as<script type="text/template">
.load
event of said parent/main script. This should be in the form of a string, i.edata-wp-executes-after="script-handle-1"
defer
and has an associated inline script(s) that are not opted in as standalone (see issue 15).<script type="text/template">
elements.data-wp-executes-after
attribute applied to said element(s).<script type="text/template">
DOM elements to<script type="text/javascript">
and manually execute them as JavaScript in the browser.Unit Tests
<script type="text/template">
as opposed to<script type="text/javascript">
data-wp-executes-after
attribute.defer
and has an associated inline script(s) that are not opted in as standalone.<script type="text/template">
.<script type="text/template">
shall be converted to<script type="text/javascript">
<script type="text/javascript">
Designs
No response
Describe alternatives you've considered
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: