Skip to content

fix(core): 子应用 location.href 跳转后不再销毁 sandbox,修复非降级模式下容器空白、无法后退恢复 - #1124

Merged
yiludege merged 1 commit into
masterfrom
fix/href-navigation-blank
Nov 14, 2026
Merged

yiludege merged 1 commit into
masterfrom
fix/href-navigation-blank

Conversation

@yiludege

Copy link
Copy Markdown
Collaborator

问题

从 v2.0.0 开始,非降级模式下子应用执行 location.href = "..." 后,容器一片空白,浏览器后退也无法恢复原应用。

原因

location.href 跳转时,wujie 会用一个渲染目标地址的新 iframe 替换掉 <wujie-app>,同时保留 sandbox(hrefFlag),用户后退时再把 <wujie-app> 放回去。

7bfd3c9 之后,重建模式(非保活、没有做生命周期改造)下 <wujie-app> 一旦 disconnect 就会执行 sandbox.destroy():

  • destroy() 会清空容器,刚插入的新 iframe 也被一起清掉;
  • sandbox 被销毁后,后退时也就没有可以恢复的应用。

修复

handleWujieAppDisconnect 中,hrefFlag 为 true 时不执行 destroy,改为 unmount,与 v2.0.0 之前一致。unmount() 本来就会在 hrefFlag 为 true 时跳过子应用的生命周期卸载和 DOM 清理。

验证

  • 单测:handleWujieAppDisconnect.test.ts 新增「重建模式下 href 跳转只 unmount」的用例。
  • Chrome 实测:主应用、子应用、外部站点分属三个不同的源,子应用跳到外部站点后再后退:
版本 非降级:跳转后 非降级:后退后 降级:跳转后
7bfd3c9 之前 显示外部站点 原应用恢复 显示外部站点
master 容器为空 容器为空 显示外部站点
本 PR 显示外部站点 原应用恢复 显示外部站点

location.replace / location.assign 跳转会破坏沙箱的问题,在 #575 中处理。

v2.0.0 起重建模式的 wujie-app disconnect 改为 destroy,而 location.href 跳转时 wujie-app
正是被跳转后的 iframe 替换下来的:destroy 清空容器连带清掉新 iframe,sandbox 被销毁后浏览器
后退也无法恢复原应用。hrefFlag 为 true 时恢复为 unmount(unmount 本身已跳过 hrefFlag 下的
生命周期卸载与 DOM 清理)。

Co-authored-by: Cursor <cursoragent@cursor.com>
@yiludege
yiludege merged commit 46b2dfc into master Nov 14, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant