Skip to content

Commit

Permalink
fix(vercel#39993): avoid race condition for next/script onReady
Browse files Browse the repository at this point in the history
  • Loading branch information
SukkaW committed Aug 27, 2022
1 parent 46b8959 commit 2c6834c
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/next/client/script.tsx
Expand Up @@ -61,6 +61,9 @@ const loadScript = (props: ScriptProps): void => {

const loadPromise = new Promise<void>((resolve, reject) => {
el.addEventListener('load', function (e) {
// add cacheKey to LoadCache when load successfully
LoadCache.add(cacheKey)

resolve()
if (onLoad) {
onLoad.call(this, e)
Expand All @@ -82,19 +85,26 @@ 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'
? children
: 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)) {
Expand Down

0 comments on commit 2c6834c

Please sign in to comment.