Open Bug 1695192 Opened 4 years ago Updated 2 years ago

Appended module script runs too early

Categories

(Core :: DOM: Core & HTML, defect, P3)

defect

Tracking

()

UNCONFIRMED

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.

Jon, Yulia, is this something WebVM could look at?

Component: General → DOM: Core & HTML
Flags: needinfo?(ystartsev)
Flags: needinfo?(jcoppeard)

I can probably take a look, since I am in that part of the code right now anyway.

Flags: needinfo?(ystartsev)
Assignee: nobody → ystartsev

Yulia, what is the severity of this bug? Could you please help set it? Thanks.

Flags: needinfo?(ystartsev)
Flags: needinfo?(ystartsev)
Priority: -- → P3
Flags: needinfo?(jcoppeard)

Changing severity to S3 because workaround exists (the page can loads modules in another way).

Severity: -- → S3
Assignee: ystartsev → nobody
You need to log in before you can comment on or make changes to this bug.