Bug 1865410 - Part 1: Don't use preload requests after an import map is registered r=smaug

We stop speculative preloading of module scripts after seeing an import map.
However it can still happen that we see a preload request for script element
after an import map is registered because a script can dynamically insert an
import map after preloading has happened.

Don't use these preloads as they may have incorrectly resolved module
specifiers.

We can remove the previous way of dealing with this issue where we removed
preloaded modules from the module map if we later loaded an import map. This is
not safe as modules could be present in the module map for other valid reasons,
e.g. if they have been imported by another script.

Differential Revision: https://phabricator.services.mozilla.com/D194766
This commit is contained in:
Jon Coppeard
2023-12-02 08:49:59 +00:00
parent 0a1095ff59
commit 5254ac3aac

View File

@@ -1131,17 +1131,11 @@ bool ScriptLoader::ProcessExternalScript(nsIScriptElement* aElement,
return false;
}
// If there are a preloaded request and an import map, we won't use the
// preloaded request and will try to create a new one for this, because the
// import map isn't preloaded, and the preloaded request may have used the
// wrong module specifiers.
//
// We use IsModuleFetched() to check if the module has been fetched, if it
// hasn't been fetched we can simply just reuse it.
if (request && mModuleLoader->IsModuleFetched(request->mURI) &&
if (request && request->IsModuleRequest() &&
mModuleLoader->HasImportMapRegistered()) {
DebugOnly<bool> removed = mModuleLoader->RemoveFetchedModule(request->mURI);
MOZ_ASSERT(removed);
// We don't preload module scripts after seeing an import map but a script
// can dynamically insert an import map after preloading has happened.
request->Cancel();
request = nullptr;
}
@@ -3591,10 +3585,8 @@ void ScriptLoader::HandleLoadError(ScriptLoadRequest* aRequest,
modReq->CancelDynamicImport(aResult);
}
} else {
MOZ_ASSERT(!modReq->IsTopLevel());
MOZ_ASSERT(!modReq->isInList());
modReq->Cancel();
// The error is handled for the top level module.
}
} else if (mParserBlockingRequest == aRequest) {
MOZ_ASSERT(!aRequest->isInList());