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-12 16:43:49 +00:00
parent ea752069d0
commit fa39122d39
2 changed files with 49 additions and 17 deletions

View File

@@ -1097,18 +1097,9 @@ 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;
}
// We cancel module preloads when registering an import map.
MOZ_ASSERT_IF(request && request->IsModuleRequest(),
!mModuleLoader->HasImportMapRegistered());
if (request) {
// Use the preload request.
@@ -1399,6 +1390,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