diff --git a/src/sandbox/index.ts b/src/sandbox/index.ts index 0387f5e..e5ad1c2 100644 --- a/src/sandbox/index.ts +++ b/src/sandbox/index.ts @@ -42,12 +42,14 @@ export function createSandboxContainer( ) { let sandbox: SandBox; if (window.Proxy) { - sandbox = useLooseSandbox ? new LegacySandbox(appName, globalContext) : new ProxySandbox(appName, globalContext); + sandbox = useLooseSandbox + ? new LegacySandbox(appName, globalContext) + : new ProxySandbox(appName, globalContext, { speedy: !!speedySandBox }); } else { sandbox = new SnapshotSandbox(appName); } - // some side effect could be be invoked while bootstrapping, such as dynamic stylesheet injection with style-loader, especially during the development phase + // some side effect could be invoked while bootstrapping, such as dynamic stylesheet injection with style-loader, especially during the development phase const bootstrappingFreers = patchAtBootstrapping( appName, elementGetter, diff --git a/src/sandbox/patchers/dynamicAppend/common.ts b/src/sandbox/patchers/dynamicAppend/common.ts index 333b920..144f0af 100644 --- a/src/sandbox/patchers/dynamicAppend/common.ts +++ b/src/sandbox/patchers/dynamicAppend/common.ts @@ -397,7 +397,7 @@ export function patchHTMLDynamicAppendPrototypeFunctions( isInvokedByMicroApp: (element: HTMLElement) => boolean, containerConfigGetter: (element: HTMLElement) => ContainerConfig, ) { - // Just overwrite it while it have not been overwrite + // Just overwrite it while it have not been overwritten if ( HTMLHeadElement.prototype.appendChild === rawHeadAppendChild && HTMLBodyElement.prototype.appendChild === rawBodyAppendChild && @@ -424,7 +424,7 @@ export function patchHTMLDynamicAppendPrototypeFunctions( }) as typeof rawHeadInsertBefore; } - // Just overwrite it while it have not been overwrite + // Just overwrite it while it have not been overwritten if ( HTMLHeadElement.prototype.removeChild === rawHeadRemoveChild && HTMLBodyElement.prototype.removeChild === rawBodyRemoveChild diff --git a/src/sandbox/proxySandbox.ts b/src/sandbox/proxySandbox.ts index 91667eb..b7c86d3 100644 --- a/src/sandbox/proxySandbox.ts +++ b/src/sandbox/proxySandbox.ts @@ -80,7 +80,7 @@ const useNativeWindowForBindingsProps = new Map([ ['mockDomAPIInBlackList', process.env.NODE_ENV === 'test'], ]); -function createFakeWindow(globalContext: Window) { +function createFakeWindow(globalContext: Window, speedy: boolean) { // map always has the fastest performance in has check scenario // see https://jsperf.com/array-indexof-vs-set-has/23 const propertiesWithGetter = new Map(); @@ -111,6 +111,8 @@ function createFakeWindow(globalContext: Window) { p === 'parent' || p === 'self' || p === 'window' || + // window.document is overwriting in speedy mode + (p === 'document' && speedy) || (inTest && (p === mockTop || p === mockSafariTop)) ) { descriptor.configurable = true; @@ -193,13 +195,14 @@ export default class ProxySandbox implements SandBox { {}; globalContext: typeof window; - constructor(name: string, globalContext = window) { + constructor(name: string, globalContext = window, opts?: { speedy: boolean }) { this.name = name; this.globalContext = globalContext; this.type = SandBoxType.Proxy; const { updatedValueSet } = this; + const { speedy } = opts || {}; - const { fakeWindow, propertiesWithGetter } = createFakeWindow(globalContext); + const { fakeWindow, propertiesWithGetter } = createFakeWindow(globalContext, !!speedy); const descriptorTargetMap = new Map(); const hasOwnProperty = (key: PropertyKey) => fakeWindow.hasOwnProperty(key) || globalContext.hasOwnProperty(key);