From 42dbcaeabe10a53c4a2a0cdccad48151ae7409f8 Mon Sep 17 00:00:00 2001 From: Kuitos Date: Wed, 22 Feb 2023 13:58:56 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20compatible=20with=20MutationObse?= =?UTF-8?q?rver=20as=20document=20had=20been=20wrapped=20as=20a=20proxy=20?= =?UTF-8?q?(#2407)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dynamicAppend/forStrictSandbox.ts | 19 ++++++++++++++++--- src/utils.ts | 2 ++ 2 files changed, 18 insertions(+), 3 deletions(-) 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__', {