Bug 1877703 - Part 3: Also remove currently fetching preload modules from the module map when import map is registered r=smaug

A further problem with dynamically inserted import maps was discovered where
sometimes module scripts would never execute. This happens when the script is
still being fetched when the import map is added.

To fix this, cancel all fetching module preloads as well when an import map is
registered and remove them from the module map. In theory this shouldn't be
necessary but I wasn't able to make the tests pass without this step.

For simplicity also remove all module preloads from the scriptloader's list of
preload requests rather than detecting and ignoring them later on.

Differential Revision: https://phabricator.services.mozilla.com/D204202
This commit is contained in:
Jon Coppeard
2024-03-19 10:07:35 +00:00
parent f7a047ee4c
commit 2d8c18b5d5
2 changed files with 46 additions and 18 deletions

View File

@@ -1097,19 +1097,6 @@ bool ScriptLoader::ProcessExternalScript(nsIScriptElement* aElement,
return false;
}
if (request && request->IsModuleRequest() &&
mModuleLoader->HasImportMapRegistered() &&
request->mState > ScriptLoadRequest::State::Compiling) {
// We don't preload module scripts after seeing an import map but a script
// can dynamically insert an import map after preloading has happened.
//
// In the case of an import map is inserted after preloading has happened,
// We also check if the request has started loading imports, if not then we
// can reuse the preloaded request.
request->Cancel();
request = nullptr;
}
if (request) {
// Use the preload request.
@@ -1399,6 +1386,16 @@ bool ScriptLoader::ProcessInlineScript(nsIScriptElement* aElement,
return false;
}
// Remove any module preloads. Module specifier resolution is invalidated by
// adding an import map, and incorrect dependencies may have been loaded.
mPreloads.RemoveElementsBy([](const PreloadInfo& info) {
if (info.mRequest->IsModuleRequest()) {
info.mRequest->Cancel();
return true;
}
return false;
});
// TODO: Bug 1781758: Move RegisterImportMap into EvaluateScriptElement.
//
// https://html.spec.whatwg.org/multipage/scripting.html#execute-the-script-element