Skip to content

Commit

Permalink
⚡️ fix the performance issue in sandbox (#1626)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuitos authored Aug 2, 2021
1 parent d8c808a commit b1ff6b9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/sandbox/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@

import { isBoundedFunction, isCallable, isConstructable } from '../utils';

let currentRunningSandboxProxy: WindowProxy | null = null;
declare global {
interface Window {
__currentRunningSandboxProxy__: WindowProxy | null;
}
}

// Get native global window with a sandbox disgusted way, thus we could share it between qiankun instances🤪
// eslint-disable-next-line no-new-func
const nativeGlobal: Window = new Function('return this')();
Object.defineProperty(nativeGlobal, '__currentRunningSandboxProxy__', { enumerable: false, writable: true });
export function getCurrentRunningSandboxProxy() {
return currentRunningSandboxProxy;
return nativeGlobal.__currentRunningSandboxProxy__;
}

export function setCurrentRunningSandboxProxy(proxy: WindowProxy | null) {
currentRunningSandboxProxy = proxy;
// set currentRunningSandboxProxy to global window, as its only use case is for document.createElement from now on, which hijacked by a global way
nativeGlobal.__currentRunningSandboxProxy__ = proxy;
}

const functionBoundedValueMap = new WeakMap<CallableFunction, CallableFunction>();
Expand Down
4 changes: 2 additions & 2 deletions src/sandbox/proxySandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,14 @@ export default class ProxySandbox implements SandBox {
},

get(target: FakeWindow, p: PropertyKey): any {
if (p === Symbol.unscopables) return unscopables;

setCurrentRunningSandboxProxy(proxy);
// FIXME if you have any other good ideas
// remove the mark in next tick, thus we can identify whether it in micro app or not
// this approach is just a workaround, it could not cover all complex cases, such as the micro app runs in the same task context with master in some case
nextTick(() => setCurrentRunningSandboxProxy(null));

if (p === Symbol.unscopables) return unscopables;

// avoid who using window.window or window.self to escape the sandbox environment to touch the really window
// see https://github.com/eligrey/FileSaver.js/blob/master/src/FileSaver.js#L13
if (p === 'window' || p === 'self') {
Expand Down

1 comment on commit b1ff6b9

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for qiankun ready!

✅ Preview
https://qiankun-mn3sqegbi-umijs.vercel.app

Built with commit b1ff6b9.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.