diff --git a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts index 3fdbdfe..07a992b 100644 --- a/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts +++ b/src/sandbox/patchers/dynamicAppend/forStrictSandbox.ts @@ -3,9 +3,8 @@ * @since 2020-10-13 */ -import { noop } from 'lodash'; import type { Freer, SandBox } from '../../../interfaces'; -import { nativeGlobal } from '../../../utils'; +import { nativeDocument, nativeGlobal } from '../../../utils'; import { getCurrentRunningApp } from '../../common'; import type { ContainerConfig } from './common'; import { @@ -70,7 +69,21 @@ function patchDocument(cfg: { sandbox: SandBox; speedy: boolean }) { sandbox.patchDocument(proxyDocument); - return noop; + // patch MutationObserver.prototype.observe to avoid type error + // https://github.com/umijs/qiankun/issues/2406 + const nativeMutationObserverObserveFn = MutationObserver.prototype.observe; + MutationObserver.prototype.observe = function observe( + this: MutationObserver, + target: Node, + options: MutationObserverInit, + ) { + const realTarget = target instanceof Document ? nativeDocument : target; + return nativeMutationObserverObserveFn.call(this, realTarget, options); + }; + + return () => { + MutationObserver.prototype.observe = nativeMutationObserverObserveFn; + }; } const docCreateElementFnBeforeOverwrite = docCreatePatchedMap.get(document.createElement); diff --git a/src/utils.ts b/src/utils.ts index d58e108..d9ae5a0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -158,6 +158,8 @@ export function getWrapperId(name: string) { export const nativeGlobal = new Function('return this')(); +export const nativeDocument = new Function('return document')(); + const getGlobalAppInstanceMap = once<() => Record>(() => { if (!nativeGlobal.hasOwnProperty('__app_instance_name_map__')) { Object.defineProperty(nativeGlobal, '__app_instance_name_map__', {