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(#39993): avoid race condition for next/script onReady #40002

Merged
merged 7 commits into from Aug 28, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions 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 @@ -79,22 +82,27 @@ 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)

ScriptCache.set(src, loadPromise)
}

for (const [k, value] of Object.entries(props)) {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/script-loader/base/next.config.js
@@ -0,0 +1,3 @@
module.exports = {
reactStrictMode: true,
}
31 changes: 31 additions & 0 deletions test/integration/script-loader/base/pages/page10.js
@@ -0,0 +1,31 @@
import Script from 'next/script'
import Link from 'next/link'

if (typeof window !== 'undefined') {
window.remoteScriptsOnReadyCalls ??= 0
window.inlineScriptsOnReadyCalls ??= 0
}

const Page = () => {
return (
<div className="container">
<Link href="/page9">Page 9</Link>
<div id="text"></div>
<Script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"
onReady={() => {
window.remoteScriptsOnReadyCalls++
}}
/>
<Script
id="i-am-an-inline-script-that-has-on-ready"
dangerouslySetInnerHTML={{ __html: 'console.log("inline script!")' }}
onReady={() => {
window.inlineScriptsOnReadyCalls++
}}
/>
</div>
)
}

export default Page
25 changes: 25 additions & 0 deletions test/integration/script-loader/test/index.test.js
Expand Up @@ -8,6 +8,9 @@ import {
stopApp,
nextBuild,
waitFor,
findPort,
launchApp,
killApp,
} from 'next-test-utils'
import webdriver from 'next-webdriver'
import cheerio from 'cheerio'
Expand Down Expand Up @@ -214,6 +217,28 @@ describe('Next.js Script - Primary Strategies', () => {
}
})

// https://github.com/vercel/next.js/issues/39993
it('onReady should only fires once after load event in dev mode (issue #39993)', async () => {
let browser
// we will start a dedicated dev server for this test case only (scoped)
let devAppPort
let devApp

try {
devAppPort = await findPort()
devApp = await launchApp(appDir, devAppPort)
browser = await webdriver(devAppPort, '/page10')

// wait for jQuery to be loaded
await waitFor(1000)
expect(await browser.eval(`window.remoteScriptsOnReadyCalls`)).toBe(1)
expect(await browser.eval(`window.inlineScriptsOnReadyCalls`)).toBe(1)
} finally {
if (browser) await browser.close()
if (devApp) await killApp(devApp)
}
})

it('priority beforeInteractive with inline script', async () => {
const html = await renderViaHTTP(appPort, '/page5')
const $ = cheerio.load(html)
Expand Down