🐛 compatible with old browser like chrome55 (#2427)

This commit is contained in:
Kuitos 2023-03-10 16:12:28 +08:00 committed by GitHub
parent f4425d1fba
commit cf7e6b4eb8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 7 deletions

View File

@ -42,12 +42,14 @@ export function createSandboxContainer(
) { ) {
let sandbox: SandBox; let sandbox: SandBox;
if (window.Proxy) { 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 { } else {
sandbox = new SnapshotSandbox(appName); 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( const bootstrappingFreers = patchAtBootstrapping(
appName, appName,
elementGetter, elementGetter,

View File

@ -397,7 +397,7 @@ export function patchHTMLDynamicAppendPrototypeFunctions(
isInvokedByMicroApp: (element: HTMLElement) => boolean, isInvokedByMicroApp: (element: HTMLElement) => boolean,
containerConfigGetter: (element: HTMLElement) => ContainerConfig, containerConfigGetter: (element: HTMLElement) => ContainerConfig,
) { ) {
// Just overwrite it while it have not been overwrite // Just overwrite it while it have not been overwritten
if ( if (
HTMLHeadElement.prototype.appendChild === rawHeadAppendChild && HTMLHeadElement.prototype.appendChild === rawHeadAppendChild &&
HTMLBodyElement.prototype.appendChild === rawBodyAppendChild && HTMLBodyElement.prototype.appendChild === rawBodyAppendChild &&
@ -424,7 +424,7 @@ export function patchHTMLDynamicAppendPrototypeFunctions(
}) as typeof rawHeadInsertBefore; }) as typeof rawHeadInsertBefore;
} }
// Just overwrite it while it have not been overwrite // Just overwrite it while it have not been overwritten
if ( if (
HTMLHeadElement.prototype.removeChild === rawHeadRemoveChild && HTMLHeadElement.prototype.removeChild === rawHeadRemoveChild &&
HTMLBodyElement.prototype.removeChild === rawBodyRemoveChild HTMLBodyElement.prototype.removeChild === rawBodyRemoveChild

View File

@ -80,7 +80,7 @@ const useNativeWindowForBindingsProps = new Map<PropertyKey, boolean>([
['mockDomAPIInBlackList', process.env.NODE_ENV === 'test'], ['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 // map always has the fastest performance in has check scenario
// see https://jsperf.com/array-indexof-vs-set-has/23 // see https://jsperf.com/array-indexof-vs-set-has/23
const propertiesWithGetter = new Map<PropertyKey, boolean>(); const propertiesWithGetter = new Map<PropertyKey, boolean>();
@ -111,6 +111,8 @@ function createFakeWindow(globalContext: Window) {
p === 'parent' || p === 'parent' ||
p === 'self' || p === 'self' ||
p === 'window' || p === 'window' ||
// window.document is overwriting in speedy mode
(p === 'document' && speedy) ||
(inTest && (p === mockTop || p === mockSafariTop)) (inTest && (p === mockTop || p === mockSafariTop))
) { ) {
descriptor.configurable = true; descriptor.configurable = true;
@ -193,13 +195,14 @@ export default class ProxySandbox implements SandBox {
{}; {};
globalContext: typeof window; globalContext: typeof window;
constructor(name: string, globalContext = window) { constructor(name: string, globalContext = window, opts?: { speedy: boolean }) {
this.name = name; this.name = name;
this.globalContext = globalContext; this.globalContext = globalContext;
this.type = SandBoxType.Proxy; this.type = SandBoxType.Proxy;
const { updatedValueSet } = this; const { updatedValueSet } = this;
const { speedy } = opts || {};
const { fakeWindow, propertiesWithGetter } = createFakeWindow(globalContext); const { fakeWindow, propertiesWithGetter } = createFakeWindow(globalContext, !!speedy);
const descriptorTargetMap = new Map<PropertyKey, SymbolTarget>(); const descriptorTargetMap = new Map<PropertyKey, SymbolTarget>();
const hasOwnProperty = (key: PropertyKey) => fakeWindow.hasOwnProperty(key) || globalContext.hasOwnProperty(key); const hasOwnProperty = (key: PropertyKey) => fakeWindow.hasOwnProperty(key) || globalContext.hasOwnProperty(key);