diff --git a/packages/wujie-core/src/effect.ts b/packages/wujie-core/src/effect.ts index 9f70920c4..7137b0ed8 100644 --- a/packages/wujie-core/src/effect.ts +++ b/packages/wujie-core/src/effect.ts @@ -161,6 +161,7 @@ function patchStylesheetElement( }); } +let dynamicScriptExecStack = Promise.resolve(); function rewriteAppendOrInsertChild(opts: { rawDOMAppendOrInsertBefore: (newChild: T, refChild?: Node | null) => T; wujieId: string; @@ -274,27 +275,29 @@ function rewriteAppendOrInsertChild(opts: { crossoriginType: crossOrigin || "", ignore: isMatchUrl(src, getEffectLoaders("jsIgnores", plugins)), } as ScriptObject; - getExternalScripts([scriptOptions], fetch, lifecycles.loadError, fiber).forEach((scriptResult) => - scriptResult.contentPromise.then( - (content) => { - if (sandbox.execQueue === null) return warn(WUJIE_TIPS_REPEAT_RENDER); - const execQueueLength = sandbox.execQueue?.length; - sandbox.execQueue.push(() => - fiber - ? requestIdleCallback(() => { - execScript({ ...scriptResult, content }); - }) - : execScript({ ...scriptResult, content }) - ); - // 同步脚本如果都执行完了,需要手动触发执行 - if (!execQueueLength) sandbox.execQueue.shift()(); - }, - () => { - manualInvokeElementEvent(element, "error"); - element = null; - } - ) - ); + getExternalScripts([scriptOptions], fetch, lifecycles.loadError, fiber).forEach((scriptResult) => { + dynamicScriptExecStack = dynamicScriptExecStack.then(() => + scriptResult.contentPromise.then( + (content) => { + if (sandbox.execQueue === null) return warn(WUJIE_TIPS_REPEAT_RENDER); + const execQueueLength = sandbox.execQueue?.length; + sandbox.execQueue.push(() => + fiber + ? requestIdleCallback(() => { + execScript({ ...scriptResult, content }); + }) + : execScript({ ...scriptResult, content }) + ); + // 同步脚本如果都执行完了,需要手动触发执行 + if (!execQueueLength) sandbox.execQueue.shift()(); + }, + () => { + manualInvokeElementEvent(element, "error"); + element = null; + } + ) + ); + }); } else { const execQueueLength = sandbox.execQueue?.length; sandbox.execQueue.push(() => diff --git a/packages/wujie-core/src/utils.ts b/packages/wujie-core/src/utils.ts index 62d4a2917..50bff72bf 100644 --- a/packages/wujie-core/src/utils.ts +++ b/packages/wujie-core/src/utils.ts @@ -290,9 +290,10 @@ export function isScriptElement(element: HTMLElement): boolean { return element.tagName?.toUpperCase() === "SCRIPT"; } +let count = 1; export function setTagToScript(element: HTMLScriptElement, tag?: string): void { if (isScriptElement(element)) { - const scriptTag = tag || `${new Date().valueOf()}`; + const scriptTag = tag || String(count++); element.setAttribute(WUJIE_SCRIPT_ID, scriptTag); } }