Skip to content

Commit

Permalink
fix: 修复async脚本导致脚本执行顺序错乱问题 (#173)
Browse files Browse the repository at this point in the history
close #173
  • Loading branch information
yiludege authored Sep 23, 2022
1 parent adf5a72 commit 60a3e0c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
2 changes: 2 additions & 0 deletions docs/api/preloadApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ interface ScriptObjectLoader {
src?: string;
/** 脚本是否为module模块 */
module?: boolean;
/** 脚本是否为async执行 */
async?: boolean;
/** 脚本是否设置crossorigin */
crossorigin?: boolean;
/** 脚本crossorigin的类型 */
Expand Down
2 changes: 2 additions & 0 deletions docs/api/startApp.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ interface ScriptObjectLoader {
src?: string;
/** 脚本是否为module模块 */
module?: boolean;
/** 脚本是否为async执行 */
async?: boolean;
/** 脚本是否设置crossorigin */
crossorigin?: boolean;
/** 脚本crossorigin的类型 */
Expand Down
7 changes: 4 additions & 3 deletions packages/wujie-core/src/iframe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ export function syncIframeUrlToWindow(iframeWindow: Window): void {
* @param iframeWindow
*/
export function insertScriptToIframe(scriptResult: ScriptObject | ScriptObjectLoader, iframeWindow: Window) {
const { src, module, content, crossorigin, crossoriginType, callback } = scriptResult as ScriptObjectLoader;
const { src, module, content, crossorigin, crossoriginType, async, callback } = scriptResult as ScriptObjectLoader;
const scriptElement = iframeWindow.document.createElement("script");
const nextScriptElement = iframeWindow.document.createElement("script");
const { replace, plugins, proxyLocation } = iframeWindow.__WUJIE;
Expand Down Expand Up @@ -686,14 +686,15 @@ export function insertScriptToIframe(scriptResult: ScriptObject | ScriptObjectLo

if (/^<!DOCTYPE html/i.test(code)) {
error(WUJIE_TIPS_SCRIPT_ERROR_REQUESTED, scriptResult);
return container.appendChild(nextScriptElement);
return !async && container.appendChild(nextScriptElement);
}
container.appendChild(scriptElement);

// 调用回调
callback?.(iframeWindow);

container.appendChild(nextScriptElement);
// async脚本不在执行队列,无需next操作
!async && container.appendChild(nextScriptElement);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/wujie-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export interface ScriptObjectLoader {
src?: string;
/** 脚本是否为module模块 */
module?: boolean;
/** 脚本是否为async执行 */
async?: boolean;
/** 脚本是否设置crossorigin */
crossorigin?: boolean;
/** 脚本crossorigin的类型 */
Expand Down

0 comments on commit 60a3e0c

Please sign in to comment.