Appended module script runs too early
Categories
(Core :: DOM: Core & HTML, defect, P3)
Tracking
()
People
(Reporter: jakea, Unassigned)
References
Details
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36
Steps to reproduce:
https://jsbin.com/qewudur/edit?html,console
const script = document.createElement('script');
script.textContent = `console.log(3);`;
script.type = 'module';
console.log(1);
document.head.append(script);
console.log(2);
Actual results:
Logs 1, 3, 2
Expected results:
Logs 1, 2, 3
In the spec:
We hit https://html.spec.whatwg.org/multipage/scripting.html#script-processing-model:prepare-a-script-6, since the first condition becomes true, so we go through https://html.spec.whatwg.org/multipage/scripting.html#prepare-a-script.
On step 27.2.module, we go async, where classic non-module scripts do not.
On step 28 we match If the script's type is "module", and the element does not have an async attribute, and the element does not have the "non-blocking" flag set
, and the script execution is queued (this is another async step). Whereas classic scripts will fall through to "Otherwise", and perform immediate execution.
Firefox is missing one or both of those async steps. Thankfully it goes async if the module script contains an import.
Here's a weird variant with top-level-await https://jsbin.com/fisivos/edit?html,console. In this case Firefox appears to process a microtask in the middle of executing another script.
Comment 1•4 years ago
|
||
Jon, Yulia, is this something WebVM could look at?
Comment 2•4 years ago
|
||
I can probably take a look, since I am in that part of the code right now anyway.
Updated•4 years ago
|
Comment 4•4 years ago
|
||
Yulia, what is the severity of this bug? Could you please help set it? Thanks.
Updated•4 years ago
|
Updated•4 years ago
|
Comment 5•4 years ago
|
||
Changing severity to S3 because workaround exists (the page can loads modules in another way).
Updated•2 years ago
|
Description
•