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:
Janicklas Ralph 2021-07-15 15:51:01 -07:00 committed by GitHub
parent 7644c17f43
commit 3b388c346c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 4 deletions

View file

@ -152,6 +152,8 @@ function Script(props: ScriptProps): JSX.Element | null {
},
])
updateScripts(scripts)
} else {
loadScript(props)
}
}

View file

@ -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>
)
}

View file

@ -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 {