🐛 compatible with MutationObserver as document had been wrapped as a proxy (#2407)

This commit is contained in:
Kuitos 2023-02-22 13:58:56 +08:00 committed by GitHub
parent 370238d2bb
commit 42dbcaeabe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 3 deletions

View File

@ -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);

View File

@ -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<string, number>>(() => {
if (!nativeGlobal.hasOwnProperty('__app_instance_name_map__')) {
Object.defineProperty(nativeGlobal, '__app_instance_name_map__', {