angular-architects / module-federation-plugin

MIT License
688 stars 187 forks source link

Multiple load remoteEntry.js when use type = "script" #482

Open ChugunovD opened 3 months ago

ChugunovD commented 3 months ago

There are load remoteEntry.js multiple times if we expose multiple modules in one module and try to get it with "loadRemoteModule" API. For fix it behaviour need to store load status to containerMap instead of container of modules.

Possible high level solution, which guaranted only one load script per module with type="script":


const remoteEntryMap = {};

async function loadRemoteScriptEntry(
  remoteEntry: string,
): Promise<void> {
          if (remoteEntryMap[remoteEntry]) {
            return remoteEntryMap[remoteEntry];
        }
        remoteEntryMap[remoteEntry] = new Promise((resolve, reject) => {
            const script = document.createElement("script");
            script.src = remoteEntry;
            script.addEventListener("error", reject);
            script.addEventListener("load", () => {
                resolve(true);
            });
            document.body.append(script);
        });

        return remoteEntryMap[remoteEntry];
}