Unverified Commit fd1c56e6 authored by Janicklas Ralph's avatar Janicklas Ralph Committed by GitHub

next/script fix duplicate scripts (#28428)

* Fix #27747

* Fix lint error

* Add data attribute to script component

* Fix #28036

* Fix tests

* Fix tests
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent ee462442
...@@ -156,6 +156,7 @@ window.__NEXT_P = [] ...@@ -156,6 +156,7 @@ window.__NEXT_P = []
const headManager: { const headManager: {
mountedInstances: Set<unknown> mountedInstances: Set<unknown>
updateHead: (head: JSX.Element[]) => void updateHead: (head: JSX.Element[]) => void
getIsSsr?: () => boolean
} = initHeadManager() } = initHeadManager()
const appElement: HTMLElement | null = document.getElementById('__next') const appElement: HTMLElement | null = document.getElementById('__next')
...@@ -163,6 +164,9 @@ let lastRenderReject: (() => void) | null ...@@ -163,6 +164,9 @@ let lastRenderReject: (() => void) | null
let webpackHMR: any let webpackHMR: any
export let router: Router export let router: Router
let CachedApp: AppComponent, onPerfEntry: (metric: any) => void let CachedApp: AppComponent, onPerfEntry: (metric: any) => void
headManager.getIsSsr = () => {
return router.isSsr
}
class Container extends React.Component<{ class Container extends React.Component<{
fn: (err: Error, info?: any) => void fn: (err: Error, info?: any) => void
......
...@@ -140,7 +140,7 @@ function Script(props: ScriptProps): JSX.Element | null { ...@@ -140,7 +140,7 @@ function Script(props: ScriptProps): JSX.Element | null {
} = props } = props
// Context is available only during SSR // Context is available only during SSR
const { updateScripts, scripts } = useContext(HeadManagerContext) const { updateScripts, scripts, getIsSsr } = useContext(HeadManagerContext)
useEffect(() => { useEffect(() => {
if (strategy === 'afterInteractive') { if (strategy === 'afterInteractive') {
...@@ -161,7 +161,10 @@ function Script(props: ScriptProps): JSX.Element | null { ...@@ -161,7 +161,10 @@ function Script(props: ScriptProps): JSX.Element | null {
}, },
]) ])
updateScripts(scripts) updateScripts(scripts)
} else { } else if (getIsSsr && getIsSsr()) {
// Script has already loaded during SSR
LoadCache.add(restProps.id || src)
} else if (getIsSsr && !getIsSsr()) {
loadScript(props) loadScript(props)
} }
} }
......
...@@ -5,6 +5,7 @@ export const HeadManagerContext: React.Context<{ ...@@ -5,6 +5,7 @@ export const HeadManagerContext: React.Context<{
mountedInstances?: any mountedInstances?: any
updateScripts?: (state: any) => void updateScripts?: (state: any) => void
scripts?: any scripts?: any
getIsSsr?: () => boolean
}> = React.createContext({}) }> = React.createContext({})
if (process.env.NODE_ENV !== 'production') { if (process.env.NODE_ENV !== 'production') {
......
...@@ -4,11 +4,10 @@ const Page = () => { ...@@ -4,11 +4,10 @@ const Page = () => {
return ( return (
<div class="container"> <div class="container">
<Script id="inline-script"> <Script id="inline-script">
{`(window.onload = function () { {`const newDiv = document.createElement('div')
const newDiv = document.createElement('div')
newDiv.id = 'onload-div' newDiv.id = 'onload-div'
document.querySelector('body').appendChild(newDiv) document.querySelector('body').appendChild(newDiv)
})`} `}
</Script> </Script>
<Script <Script
id="scriptLazyOnload" id="scriptLazyOnload"
......
...@@ -128,14 +128,25 @@ describe('Script Loader', () => { ...@@ -128,14 +128,25 @@ describe('Script Loader', () => {
try { try {
browser = await webdriver(appPort, '/') browser = await webdriver(appPort, '/')
// beforeInteractive scripts should load once
let documentBIScripts = await browser.elementsByCss(
'[src$="documentBeforeInteractive"]'
)
expect(documentBIScripts.length).toBe(1)
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')
await waitFor(1000)
const script = await browser.elementById('scriptBeforeInteractive') const script = await browser.elementById('scriptBeforeInteractive')
// Ensure beforeInteractive script isn't duplicated on navigation
documentBIScripts = await browser.elementsByCss(
'[src$="documentBeforeInteractive"]'
)
expect(documentBIScripts.length).toBe(1)
// Renders script tag // Renders script tag
expect(script).toBeDefined() expect(script).toBeDefined()
} finally { } finally {
......
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