From 9e414d964859636008e65044542ab419c19d921f Mon Sep 17 00:00:00 2001 From: fireairforce <1344492820@qq.com> Date: Tue, 31 Mar 2026 17:23:28 +0800 Subject: [PATCH] TURBOPACK: add runtime js chunk load fallback --- .../runtime/dom/runtime-backend-dom.ts | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts b/turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts index 2fa8a05bbd81..6b2fe2db380a 100644 --- a/turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts +++ b/turbopack/crates/turbopack-ecmascript-runtime/js/src/browser/runtime/dom/runtime-backend-dom.ts @@ -141,12 +141,33 @@ const chunkResolvers: Map = new Map() return resolver.promise } - if (sourceType === SourceType.Runtime && isCss(chunkUrl)) { + if (sourceType === SourceType.Runtime) { // CSS chunks do not register themselves, and as such must be marked as // loaded instantly. resolver.loadingStarted = true - resolver.resolve() - return resolver.promise + + if (isCss(chunkUrl)) { + resolver.resolve() + return resolver.promise + } + + // Runtime JS chunks are expected to be present in the DOM already. + // Load it first + if (typeof importScripts !== 'function') { + const decodedChunkUrl = decodeURI(chunkUrl) + const previousScripts = document.querySelectorAll( + `script[src="${chunkUrl}"],script[src^="${chunkUrl}?"],script[src="${decodedChunkUrl}"],script[src^="${decodedChunkUrl}?"]` + ) + if (previousScripts.length > 0) { + for (const script of Array.from(previousScripts)) { + script.addEventListener('error', () => { + resolver.reject() + }) + } + return resolver.promise + } + } + // If it wasn't present in the DOM, fallback to loading logic. } if (typeof importScripts === 'function') {