Bug 1877703 - Don't add the URI into mFetchedModules if it has a parse error and is preloaded. r=jonco

Differential Revision: https://phabricator.services.mozilla.com/D200431
This commit is contained in:
Yoshi Cheng-Hao Huang
2024-02-02 17:42:32 +00:00
parent 0f47785cac
commit 820d6037f5
4 changed files with 110 additions and 1 deletions

View File

@@ -478,6 +478,23 @@ void ModuleLoaderBase::SetModuleFetchStarted(ModuleLoadRequest* aRequest) {
mFetchingModules.InsertOrUpdate(aRequest->mURI, nullptr);
}
bool ModuleLoaderBase::IsScriptPreloadedAndHasParseError(
ModuleLoadRequest* aRequest) {
RefPtr<ModuleScript> moduleScript(aRequest->mModuleScript);
bool hasParseError = false;
if (moduleScript) {
hasParseError = moduleScript->HasParseError();
}
bool isPreload = false;
if (aRequest->HasScriptLoadContext()) {
isPreload = aRequest->GetScriptLoadContext()->IsPreload();
}
return hasParseError && isPreload;
}
void ModuleLoaderBase::SetModuleFetchFinishedAndResumeWaitingRequests(
ModuleLoadRequest* aRequest, nsresult aResult) {
// Update module map with the result of fetching a single module script.
@@ -505,7 +522,14 @@ void ModuleLoaderBase::SetModuleFetchFinishedAndResumeWaitingRequests(
RefPtr<ModuleScript> moduleScript(aRequest->mModuleScript);
MOZ_ASSERT(NS_FAILED(aResult) == !moduleScript);
mFetchedModules.InsertOrUpdate(aRequest->mURI, RefPtr{moduleScript});
// In the case an import map is inserted dynamically, the preloaded module
// scripts might fail to resolve the import specifiers and hence get parse
// errors. In that case, we don't put the URI of the preloaded module script
// into mFetchedModules map, and ScriptLoader will fetch the module script
// again.
if (!IsScriptPreloadedAndHasParseError(aRequest)) {
mFetchedModules.InsertOrUpdate(aRequest->mURI, RefPtr{moduleScript});
}
if (waitingRequests) {
LOG(("ScriptLoadRequest (%p): Resuming waiting requests", aRequest));