From 7d60f6d9b3c59828ea5323458e74b104e3b50bcf Mon Sep 17 00:00:00 2001 From: bailicangdu <1264889788@qq.com> Date: Fri, 9 Sep 2022 14:56:19 +0800 Subject: [PATCH] fix: fix bug of scopeproperties & keep-alive app for memory-router --- dev/children/react16/src/index.js | 14 +++++++++++++ dev/main-react16/src/global.jsx | 5 ++++- dev/main-react16/src/pages/react16/react16.js | 2 +- docs/1.x/zh-cn/changelog.md | 2 ++ package.json | 2 +- src/create_app.ts | 12 +++++++---- src/sandbox/index.ts | 20 +++++++++++++++++-- 7 files changed, 48 insertions(+), 9 deletions(-) diff --git a/dev/children/react16/src/index.js b/dev/children/react16/src/index.js index fda0a9eab..7068da864 100644 --- a/dev/children/react16/src/index.js +++ b/dev/children/react16/src/index.js @@ -1,3 +1,4 @@ +/* eslint-disable */ // import './public-path'; // import 'babel-polyfill' // import '@babel/polyfill' @@ -285,6 +286,19 @@ window.escapeKey5 = 'escapeKey5' // should be undefined in rawWindow window.escapeKey6 = 'escapeKey6' // should be undefined in rawWindow +console.log('scopeProperties scopeKeySpe: ', scopeKeySpe) +console.log('scopeProperties window.scopeKeySpe: ', window.scopeKeySpe) + +console.log('scopeProperties Vue: ', Vue) +console.log('scopeProperties window.Vue: ', window.Vue) + +window.Vue = Vue ? Vue : 'child Vue' + +console.log('scopeProperties Vue: ', Vue) +console.log('scopeProperties window.Vue: ', window.Vue) + + + /* ---------------------- pureCreateElement & removeDomScope --------------------- */ if (window.__MICRO_APP_ENVIRONMENT__) { const unBoundDom1 = window.microApp.pureCreateElement('div') diff --git a/dev/main-react16/src/global.jsx b/dev/main-react16/src/global.jsx index bbefdbfa0..206af381e 100644 --- a/dev/main-react16/src/global.jsx +++ b/dev/main-react16/src/global.jsx @@ -49,6 +49,9 @@ const prefetchConfig = [ // microApp.preFetch(prefetchConfig) +window['scopeKeySpe'] = 'value from base app' +window.Vue = 'Vue from base' + microApp.start({ // shadowDOM: true, // inline: true, @@ -95,7 +98,7 @@ microApp.start({ plugins: { global: [ { - scopeProperties: ['scopeKey1', 'scopeKey2'], + scopeProperties: ['scopeKey1', 'scopeKey2', 'scopeKeySpe'], escapeProperties: ['escapeKey1', 'escapeKey2'], options: {a: 1,}, loader(code, url, options) { diff --git a/dev/main-react16/src/pages/react16/react16.js b/dev/main-react16/src/pages/react16/react16.js index 5af934bd7..9048229ca 100644 --- a/dev/main-react16/src/pages/react16/react16.js +++ b/dev/main-react16/src/pages/react16/react16.js @@ -407,7 +407,7 @@ export default class App extends React.Component { onAfterhidden={this.handleAfterhidden} onDataChange={this.handleDataChange} baseroute='/micro-app/demo/react16' - keep-alive + // keep-alive // destroy // inline // disableSandbox diff --git a/docs/1.x/zh-cn/changelog.md b/docs/1.x/zh-cn/changelog.md index e0d20fb2e..8f0c24c0d 100644 --- a/docs/1.x/zh-cn/changelog.md +++ b/docs/1.x/zh-cn/changelog.md @@ -16,6 +16,8 @@ - 🐞 修复了angular框架下,micro-app设置动态url导致应用多次渲染的问题。 - 🐞 修复了子应用title、meta元素丢失的问题。 + - 🐞 修复了部分场景下`scopeProperties`可以逃逸的问题。 + - 🐞 修复了关闭虚拟路由系统时keep-alive应用依然可以触发虚拟路由系统的问题。 - **Update** diff --git a/package.json b/package.json index ac042543f..f3e53bbb8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@micro-zoe/micro-app", - "version": "1.0.0-alpha.8", + "version": "1.0.0-alpha.9", "description": "A lightweight, efficient and powerful micro front-end framework", "private": false, "main": "lib/index.min.js", diff --git a/src/create_app.ts b/src/create_app.ts index 5c058ab10..77bdf52db 100644 --- a/src/create_app.ts +++ b/src/create_app.ts @@ -553,8 +553,10 @@ export default class CreateApp implements AppInterface { lifeCycles.AFTERHIDDEN, ) - // called after lifeCyclesEvent - this.sandBox?.removeRouteInfoForKeepAliveApp() + if (this.useMemoryRouter) { + // called after lifeCyclesEvent + this.sandBox?.removeRouteInfoForKeepAliveApp() + } this.recordAndReleaseEffect() @@ -587,8 +589,10 @@ export default class CreateApp implements AppInterface { this.keepAliveState = keepAliveStates.KEEP_ALIVE_SHOW - // called before lifeCyclesEvent - this.sandBox?.setRouteInfoForKeepAliveApp() + if (this.useMemoryRouter) { + // called before lifeCyclesEvent + this.sandBox?.setRouteInfoForKeepAliveApp() + } // dispatch afterShow event to micro-app dispatchCustomEventToMicroApp('appstate-change', this.name, { diff --git a/src/sandbox/index.ts b/src/sandbox/index.ts index 6b6c5a802..df3ab35d5 100644 --- a/src/sandbox/index.ts +++ b/src/sandbox/index.ts @@ -156,8 +156,9 @@ export default class SandBox implements SandBoxInterface { } /** - * 1. prevent the key deleted during sandBox.stop after rewrite - * 2. umd mode will not delete any keys during sandBox.stop + * 1. Prevent the key deleted during sandBox.stop after rewrite + * 2. Umd mode will not delete any keys during sandBox.stop + * 3. It must not be umd mode when call sandbox.start at the first time */ if (!umdMode) { this.initGlobalKeysWhenStart( @@ -443,6 +444,7 @@ export default class SandBox implements SandBoxInterface { pureCreateElement, router, }) + this.setProxyDocument(microAppWindow, appName) this.setMappingPropertiesWithRawDescriptor(microAppWindow) } @@ -529,6 +531,7 @@ export default class SandBox implements SandBoxInterface { microAppWindow.hasOwnProperty = (key: PropertyKey) => rawHasOwnProperty.call(microAppWindow, key) || rawHasOwnProperty.call(globalEnv.rawWindow, key) this.setHijackProperty(microAppWindow, appName) if (!disablePatchRequest) this.patchRequestApi(microAppWindow, appName, url) + this.setScopeProperties(microAppWindow) } // set hijack Properties to microAppWindow @@ -600,6 +603,19 @@ export default class SandBox implements SandBoxInterface { }) } + /** + * Init scope keys to microAppWindow, prevent fall to rawWindow from with(microAppWindow) + * like: if (!xxx) {} + * NOTE: + * 1. Symbol.unscopables cannot affect undefined keys + * 2. Doesn't use for window.xxx because it fall to proxyWindow + */ + setScopeProperties (microAppWindow: microAppWindowType): void { + this.scopeProperties.forEach((key: PropertyKey) => { + Reflect.set(microAppWindow, key, microAppWindow[key]) + }) + } + // set location & history for memory router private setMicroAppRouter (microAppWindow: microAppWindowType, appName: string, url: string): void { const { microLocation, microHistory } = createMicroRouter(appName, url)