diff --git a/packages/wujie-core/src/iframe.ts b/packages/wujie-core/src/iframe.ts index b688c4e8a..fe446adcf 100644 --- a/packages/wujie-core/src/iframe.ts +++ b/packages/wujie-core/src/iframe.ts @@ -618,16 +618,31 @@ function initIframeDom(iframeWindow: Window, wujie: WuJie, mainHostPath: string, * 防止运行主应用的js代码,给子应用带来很多副作用 */ // TODO 更加准确抓取停止时机 -function stopIframeLoading(iframeWindow: Window, mainHostPath: string, appHostPath: string, appRoutePath: string) { - const sandbox = iframeWindow.__WUJIE; +function stopIframeLoading( + iframeWindow: Window, + mainHostPath: string, + appHostPath: string, + appRoutePath: string, + sandbox: WuJie +) { + const oldDoc = iframeWindow.document; sandbox.iframeReady = new Promise((resolve) => { function loop() { setTimeout(() => { - // location ready - if (iframeWindow.location.href === "about:blank") { + let newDoc = null; + try { + newDoc = iframeWindow.document; + } catch (err) { + newDoc = null; + } + // wait for document ready + if (!newDoc || newDoc == oldDoc) { loop(); } else { iframeWindow.stop ? iframeWindow.stop() : iframeWindow.document.execCommand("Stop"); + if (!iframeWindow.__WUJIE) { + patchIframeVariable(iframeWindow, sandbox, appHostPath); + } initIframeDom(iframeWindow, sandbox, mainHostPath, appHostPath); /** * 如果有同步优先同步,非同步从url读取 @@ -775,6 +790,6 @@ export function iframeGenerator( const iframeWindow = iframe.contentWindow; // 变量需要提前注入,在入口函数通过变量防止死循环 patchIframeVariable(iframeWindow, sandbox, appHostPath); - stopIframeLoading(iframeWindow, mainHostPath, appHostPath, appRoutePath); + stopIframeLoading(iframeWindow, mainHostPath, appHostPath, appRoutePath, sandbox); return iframe; }