Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复async脚本导致脚本执行顺序错乱问题 #173

Merged
merged 1 commit into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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