From 2c6834cf27ba2e5f81e6315a263ec9d5fb53907b Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 27 Aug 2022 16:33:54 +0800 Subject: [PATCH] fix(#39993): avoid race condition for next/script onReady --- packages/next/client/script.tsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/next/client/script.tsx b/packages/next/client/script.tsx index 33223afe0f3..27b2bb9f6f4 100644 --- a/packages/next/client/script.tsx +++ b/packages/next/client/script.tsx @@ -61,6 +61,9 @@ const loadScript = (props: ScriptProps): void => { const loadPromise = new Promise((resolve, reject) => { el.addEventListener('load', function (e) { + // add cacheKey to LoadCache when load successfully + LoadCache.add(cacheKey) + resolve() if (onLoad) { onLoad.call(this, e) @@ -82,10 +85,12 @@ const loadScript = (props: ScriptProps): void => { if (src) { ScriptCache.set(src, loadPromise) } - LoadCache.add(cacheKey) if (dangerouslySetInnerHTML) { el.innerHTML = dangerouslySetInnerHTML.__html || '' + + // add cacheKey to LoadCache for inline script + LoadCache.add(cacheKey) } else if (children) { el.textContent = typeof children === 'string' @@ -93,8 +98,13 @@ const loadScript = (props: ScriptProps): void => { : Array.isArray(children) ? children.join('') : '' + + // add cacheKey to LoadCache for inline script + LoadCache.add(cacheKey) } else if (src) { el.src = src + // do not add cacheKey into LoadCache for remote script here + // cacheKey will be added to LoadCache when it is actually loaded (see loadPromise above) } for (const [k, value] of Object.entries(props)) {