Fix Script beforeInteractive on navigation (#26995)
## Bug - [x] fixes #26342 - [x] Integration tests added - [x] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [x] Make sure the linting passes
This commit is contained in:
parent
7644c17f43
commit
3b388c346c
3 changed files with 28 additions and 4 deletions
|
@ -152,6 +152,8 @@ function Script(props: ScriptProps): JSX.Element | null {
|
|||
},
|
||||
])
|
||||
updateScripts(scripts)
|
||||
} else {
|
||||
loadScript(props)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import Script from 'next/script'
|
||||
import Link from 'next/link'
|
||||
|
||||
const Page = () => {
|
||||
return (
|
||||
|
@ -8,6 +9,7 @@ const Page = () => {
|
|||
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js?a=scriptAfterInteractive"
|
||||
></Script>
|
||||
<div>index</div>
|
||||
<Link href="/page1">Page1</Link>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
|
|
@ -43,13 +43,13 @@ describe('Script Loader', () => {
|
|||
async function test(id) {
|
||||
const script = await browser.elementById(id)
|
||||
const endScripts = await browser.elementsByCss(
|
||||
`#${id} ~ script[src^="/_next/static/"]`
|
||||
`#__NEXT_DATA__ ~ #${id}`
|
||||
)
|
||||
|
||||
// Renders script tag
|
||||
expect(script).toBeDefined()
|
||||
// Script is inserted at the end
|
||||
expect(endScripts.length).toBe(0)
|
||||
expect(endScripts.length).toBe(1)
|
||||
}
|
||||
|
||||
// afterInteractive script in page
|
||||
|
@ -72,13 +72,13 @@ describe('Script Loader', () => {
|
|||
async function test(id) {
|
||||
const script = await browser.elementById(id)
|
||||
const endScripts = await browser.elementsByCss(
|
||||
`#${id} ~ script[src^="/_next/static/"]`
|
||||
`#__NEXT_DATA__ ~ #${id}`
|
||||
)
|
||||
|
||||
// Renders script tag
|
||||
expect(script).toBeDefined()
|
||||
// Script is inserted at the end
|
||||
expect(endScripts.length).toBe(0)
|
||||
expect(endScripts.length).toBe(1)
|
||||
}
|
||||
|
||||
// lazyOnload script in page
|
||||
|
@ -110,6 +110,26 @@ describe('Script Loader', () => {
|
|||
test('documentBeforeInteractive')
|
||||
})
|
||||
|
||||
it('priority beforeInteractive on navigate', async () => {
|
||||
let browser
|
||||
try {
|
||||
browser = await webdriver(appPort, '/')
|
||||
|
||||
await browser.waitForElementByCss('[href="/page1"]')
|
||||
await browser.click('#page1')
|
||||
|
||||
await browser.waitForElementByCss('.container')
|
||||
await waitFor(1000)
|
||||
|
||||
const script = await browser.elementById('scriptBeforeInteractive')
|
||||
|
||||
// Renders script tag
|
||||
expect(script).toBeDefined()
|
||||
} finally {
|
||||
if (browser) await browser.close()
|
||||
}
|
||||
})
|
||||
|
||||
it('onload fires correctly', async () => {
|
||||
let browser
|
||||
try {
|
||||
|
|
Loading…
Reference in a new issue