Unverified Commit 01b1e6b5 authored by Sukka's avatar Sukka Committed by GitHub

fix(#40025): run `next/script` beforeInteractive test in both dev & prod (#40541)

Ref: #40002 #40026 #40191
Fixes #40025

This is the final step of fixing #40025. The PR migrates the rest of the
`next/script` test cases to run in both dev (strict mode) and
production, confirming that the `next/script` component is now
completely concurrent rendering resilient and is ready for the upcoming
React 18 `<OffScreen />`.

## Bug

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`
parent 385e3f03
...@@ -94,74 +94,72 @@ const runTests = (isDev = false) => { ...@@ -94,74 +94,72 @@ const runTests = (isDev = false) => {
} }
}) })
if (!isDev) { it('priority beforeInteractive', async () => {
it('priority beforeInteractive', async () => { const html = await renderViaHTTP(appPort, '/page1')
const html = await renderViaHTTP(appPort, '/page1') const $ = cheerio.load(html)
const $ = cheerio.load(html)
function test(id) { function test(id) {
const script = $(`#${id}`) const script = $(`#${id}`)
// Renders script tag // Renders script tag
expect(script.length).toBe(1) expect(script.length).toBe(1)
expect(script.attr('data-nscript')).toBeDefined() expect(script.attr('data-nscript')).toBeDefined()
// Script is inserted before NextScripts // Script is inserted before NextScripts
expect( expect(
$(`#${id} ~ script[src^="/_next/static/chunks/main"]`).length $(`#${id} ~ script[src^="/_next/static/chunks/main"]`).length
).toBeGreaterThan(0) ).toBeGreaterThan(0)
} }
test('scriptBeforeInteractive') test('scriptBeforeInteractive')
}) })
// Warning - Will be removed in the next major release // Warning - Will be removed in the next major release
it('priority beforeInteractive - older version', async () => { it('priority beforeInteractive - older version', async () => {
const html = await renderViaHTTP(appPort, '/page6') const html = await renderViaHTTP(appPort, '/page6')
const $ = cheerio.load(html) const $ = cheerio.load(html)
function test(id) { function test(id) {
const script = $(`#${id}`) const script = $(`#${id}`)
// Renders script tag // Renders script tag
expect(script.length).toBe(1) expect(script.length).toBe(1)
expect(script.attr('data-nscript')).toBeDefined() expect(script.attr('data-nscript')).toBeDefined()
// Script is inserted before NextScripts // Script is inserted before NextScripts
expect( expect(
$(`#${id} ~ script[src^="/_next/static/chunks/main"]`).length $(`#${id} ~ script[src^="/_next/static/chunks/main"]`).length
).toBeGreaterThan(0) ).toBeGreaterThan(0)
} }
test('scriptBeforePageRenderOld') test('scriptBeforePageRenderOld')
}) })
it('priority beforeInteractive on navigate', async () => { it('priority beforeInteractive on navigate', async () => {
let browser let browser
try { try {
browser = await webdriver(appPort, '/') browser = await webdriver(appPort, '/')
// beforeInteractive scripts should load once // beforeInteractive scripts should load once
let documentBIScripts = await browser.elementsByCss( let documentBIScripts = await browser.elementsByCss(
'[src$="scriptBeforeInteractive"]' '[src$="scriptBeforeInteractive"]'
) )
expect(documentBIScripts.length).toBe(2) expect(documentBIScripts.length).toBe(2)
await browser.waitForElementByCss('[href="/page1"]') await browser.waitForElementByCss('[href="/page1"]')
await browser.click('[href="/page1"]') await browser.click('[href="/page1"]')
await browser.waitForElementByCss('.container') await browser.waitForElementByCss('.container')
// Ensure beforeInteractive script isn't duplicated on navigation // Ensure beforeInteractive script isn't duplicated on navigation
documentBIScripts = await browser.elementsByCss( documentBIScripts = await browser.elementsByCss(
'[src$="scriptBeforeInteractive"]' '[src$="scriptBeforeInteractive"]'
) )
expect(documentBIScripts.length).toBe(2) expect(documentBIScripts.length).toBe(2)
} finally { } finally {
if (browser) await browser.close() if (browser) await browser.close()
} }
}) })
}
it('onload fires correctly', async () => { it('onload fires correctly', async () => {
let browser let browser
...@@ -187,37 +185,39 @@ const runTests = (isDev = false) => { ...@@ -187,37 +185,39 @@ const runTests = (isDev = false) => {
} }
}) })
if (!isDev) { it('priority beforeInteractive with inline script', async () => {
it('priority beforeInteractive with inline script', async () => { const html = await renderViaHTTP(appPort, '/page5')
const html = await renderViaHTTP(appPort, '/page5') const $ = cheerio.load(html)
const $ = cheerio.load(html)
const script = $('#inline-before') const script = $('#inline-before')
expect(script.length).toBe(1) expect(script.length).toBe(1)
// css bundle is only generated in production, so only perform inline script position check in production
if (!isDev) {
// Script is inserted before CSS // Script is inserted before CSS
expect( expect(
$(`#inline-before ~ link[href^="/_next/static/css"]`).length $(`#inline-before ~ link[href^="/_next/static/css"]`).length
).toBeGreaterThan(0) ).toBeGreaterThan(0)
}) }
})
it('priority beforeInteractive with inline script should execute', async () => { it('priority beforeInteractive with inline script should execute', async () => {
let browser let browser
try { try {
browser = await webdriver(appPort, '/page7') browser = await webdriver(appPort, '/page7')
await waitFor(1000) await waitFor(1000)
const logs = await browser.log() const logs = await browser.log()
expect( // not only should inline script run, but also should only run once
logs.some((log) => expect(
log.message.includes('beforeInteractive inline script run') logs.filter((log) =>
) log.message.includes('beforeInteractive inline script run')
).toBe(true) ).length
} finally { ).toBe(1)
if (browser) await browser.close() } finally {
} if (browser) await browser.close()
}) }
} })
it('Does not duplicate inline scripts', async () => { it('Does not duplicate inline scripts', async () => {
let browser let browser
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment