diff --git a/docs/zh-cn/changelog.md b/docs/zh-cn/changelog.md index 2a403d1b..6a5bae9f 100644 --- a/docs/zh-cn/changelog.md +++ b/docs/zh-cn/changelog.md @@ -8,6 +8,14 @@ --- +### 1.0.0-rc.8 + +`2024-8-20` + +- **Bug Fix** + - 🐞 修复 with沙箱下子应用重写`history.pushState`、`history.replaceState`失败的问题,[issue 1210](https://github.com/micro-zoe/micro-app/issues/1210)。 + + ### 1.0.0-rc.7 `2024-8-20` diff --git a/package.json b/package.json index a8889a2f..96498c3d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@micro-zoe/micro-app", - "version": "1.0.0-rc.7", + "version": "1.0.0-rc.8", "description": "A lightweight, efficient and powerful micro front-end framework", "private": false, "main": "lib/index.min.js", diff --git a/src/micro_app_element.ts b/src/micro_app_element.ts index 0edcaeff..43c41a46 100644 --- a/src/micro_app_element.ts +++ b/src/micro_app_element.ts @@ -147,7 +147,7 @@ export function defineElement (tagName: string): void { if ( attr === ObservedAttrName.URL && ( !this.appUrl || - !this.connectStateMap.get(this.connectedCount) // TODO: 这里的逻辑可否再优化一下 + !this.connectStateMap.get(this.connectedCount) ) ) { newVal = formatAppURL(newVal, this.appName) @@ -159,7 +159,7 @@ export function defineElement (tagName: string): void { } else if ( attr === ObservedAttrName.NAME && ( !this.appName || - !this.connectStateMap.get(this.connectedCount) // TODO: 这里的逻辑可否再优化一下 + !this.connectStateMap.get(this.connectedCount) ) ) { const formatNewName = formatAppName(newVal) @@ -168,7 +168,6 @@ export function defineElement (tagName: string): void { return logError(`Invalid attribute name ${newVal}`, this.appName) } - // TODO: 当micro-app还未插入文档中就修改name,逻辑可否再优化一下 if (this.cacheData) { microApp.setData(formatNewName, this.cacheData) this.cacheData = null diff --git a/src/sandbox/router/history.ts b/src/sandbox/router/history.ts index c1cec0af..d2087261 100644 --- a/src/sandbox/router/history.ts +++ b/src/sandbox/router/history.ts @@ -75,32 +75,34 @@ export function createMicroHistory (appName: string, microLocation: MicroLocatio } } - const pushState = getMicroHistoryMethod('pushState') - const replaceState = getMicroHistoryMethod('replaceState') + const originalHistory = { + pushState: getMicroHistoryMethod('pushState'), + replaceState: getMicroHistoryMethod('replaceState'), + } if (isIframeSandbox(appName)) { - return { - pushState, - replaceState, + return assign({ go (delta?: number) { return rawHistory.go(delta) } - } as MicroHistory + }, originalHistory) as MicroHistory } return new Proxy(rawHistory, { get (target: History, key: PropertyKey): HistoryProxyValue { - if (key === 'state') { + if (key === 'pushState' || key === 'replaceState') { + return originalHistory[key] + } else if (key === 'state') { return getMicroState(appName) - } else if (key === 'pushState') { - return pushState - } else if (key === 'replaceState') { - return replaceState } return bindFunctionToRawTarget(Reflect.get(target, key), target, 'HISTORY') }, set (target: History, key: PropertyKey, value: unknown): boolean { - Reflect.set(target, key, value) + if (key === 'pushState' || key === 'replaceState') { + originalHistory[key] = value as CallableFunction + } else { + Reflect.set(target, key, value) + } /** * If the set() method returns false, and the assignment happened in strict-mode code, a TypeError will be thrown. * e.g. history.state = {} @@ -219,7 +221,6 @@ function reWriteHistoryMethod (method: History['pushState' | 'replaceState']): C excludeHiddenApp: true, excludePreRender: true, }).forEach(appName => { - // TODO: 大部分情况下,history.pushState 都是先执行,micro-app后卸载,所以会产生一种情况:跳转到新地址后,search模式会在url上添加参数,卸载后再将参数删除,所以会导致浏览器地址闪烁,是否需要去掉这个功能 if ((isRouterModeSearch(appName) || isRouterModeState(appName)) && !getMicroPathFromURL(appName)) { const app = appInstanceMap.get(appName)! attachRouteToBrowserURL(