diff --git a/src/sandbox/index.ts b/src/sandbox/index.ts index 26e3eee2c..8138630ce 100644 --- a/src/sandbox/index.ts +++ b/src/sandbox/index.ts @@ -25,11 +25,6 @@ import SnapshotSandbox from './snapshotSandbox'; * @param singular */ export function createSandbox(appName: string, elementGetter: () => HTMLElement | ShadowRoot, singular: boolean) { - // mounting freers are one-off and should be re-init at every mounting time - let mountingFreers: Freer[] = []; - - let sideEffectsRebuilders: Rebuilder[] = []; - let sandbox: SandBox; if (window.Proxy) { sandbox = singular ? new LegacySandbox(appName) : new ProxySandbox(appName); @@ -39,6 +34,10 @@ export function createSandbox(appName: string, elementGetter: () => HTMLElement // some side effect could be be invoked while bootstrapping, such as dynamic stylesheet injection with style-loader, especially during the development phase const bootstrappingFreers = patchAtBootstrapping(appName, elementGetter, sandbox.proxy, singular); + // mounting freers are one-off and should be re-init at every mounting time + let mountingFreers: Freer[] = []; + + let sideEffectsRebuilders: Rebuilder[] = []; return { proxy: sandbox.proxy, diff --git a/src/sandbox/patchers/dynamicHeadAppend.ts b/src/sandbox/patchers/dynamicHeadAppend.ts index c4f15c4ba..ca231e717 100644 --- a/src/sandbox/patchers/dynamicHeadAppend.ts +++ b/src/sandbox/patchers/dynamicHeadAppend.ts @@ -271,7 +271,8 @@ function getNewInsertBefore(...args: any[]) { }; } -let patchCount = 0; +let bootstrappingPatchCount = 0; +let mountingPatchCount = 0; /** * Just hijack dynamic head append, that could avoid accidentally hijacking the insertion of elements except in head. @@ -351,17 +352,16 @@ export default function patch( ); } - if (mounting) { - patchCount++; - } + if (!mounting) bootstrappingPatchCount++; + if (mounting) mountingPatchCount++; return function free() { - if (mounting) { - patchCount--; - } + // bootstrap patch just called once but its freer will be called multiple times + if (!mounting && bootstrappingPatchCount !== 0) bootstrappingPatchCount--; + if (mounting) mountingPatchCount--; // release the overwrite prototype after all the micro apps unmounted - if (patchCount === 0) { + if (mountingPatchCount === 0 && bootstrappingPatchCount === 0) { HTMLHeadElement.prototype.appendChild = rawHeadAppendChild; HTMLHeadElement.prototype.insertBefore = rawHeadInsertBefore; HTMLHeadElement.prototype.removeChild = rawHeadRemoveChild;