Skip to content

Commit 337666b

Browse files
hwangstar156ijjk
andauthored
Add script to script loader when strategy prop is undefined (#65585)
### Fixing a bug fixes #65580 ### What? #65580 ### Why? Currently, afterInteractive is given as the default strategy prop, but afterInteractive is not set for the child retrieved through React.Children, and it is an empty prop and is not added to the script loader, so the script is not executed. ### How? Added item to script loader when `child.props.strategy` is undefined. --------- Co-authored-by: JJ Kasper <[email protected]>
1 parent 1c81480 commit 337666b

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

packages/next/src/pages/_document.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,9 @@ function handleDocumentScriptLoaderItems(
989989
) {
990990
scriptLoaderItems.push(child.props)
991991
return
992+
} else if (typeof child.props.strategy === 'undefined') {
993+
scriptLoaderItems.push({ ...child.props, strategy: 'afterInteractive' })
994+
return
992995
}
993996
}
994997
})

test/e2e/next-script/index.test.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,125 @@ describe('beforeInteractive in document body', () => {
124124
}
125125
})
126126
})
127+
128+
describe('empty strategy in document Head', () => {
129+
let next: NextInstance
130+
131+
beforeAll(async () => {
132+
next = await createNext({
133+
files: {
134+
'pages/_document.js': `
135+
import { Html, Head, Main, NextScript } from 'next/document'
136+
import Script from 'next/script'
137+
138+
export default function Document() {
139+
return (
140+
<Html>
141+
<Head>
142+
<Script
143+
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
144+
></Script>
145+
</Head>
146+
<body>
147+
<Main />
148+
<NextScript />
149+
</body>
150+
</Html>
151+
)
152+
}
153+
`,
154+
'pages/index.js': `
155+
export default function Home() {
156+
return (
157+
<>
158+
<p>Home page</p>
159+
</>
160+
)
161+
}
162+
`,
163+
},
164+
dependencies: {
165+
react: '19.0.0-beta-4508873393-20240430',
166+
'react-dom': '19.0.0-beta-4508873393-20240430',
167+
},
168+
})
169+
})
170+
afterAll(() => next.destroy())
171+
172+
it('Script is injected server-side', async () => {
173+
let browser: BrowserInterface
174+
175+
try {
176+
browser = await webdriver(next.url, '/')
177+
178+
const script = await browser.eval(
179+
`document.querySelector('script[data-nscript="afterInteractive"]')`
180+
)
181+
expect(script).not.toBeNull()
182+
} finally {
183+
if (browser) await browser.close()
184+
}
185+
})
186+
})
187+
188+
describe('empty strategy in document body', () => {
189+
let next: NextInstance
190+
191+
beforeAll(async () => {
192+
next = await createNext({
193+
files: {
194+
'pages/_document.js': `
195+
import { Html, Head, Main, NextScript } from 'next/document'
196+
import Script from 'next/script'
197+
198+
export default function Document() {
199+
return (
200+
<Html>
201+
<Head/>
202+
<body>
203+
<Main />
204+
<NextScript />
205+
<Script
206+
src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"
207+
/>
208+
</body>
209+
</Html>
210+
)
211+
}
212+
`,
213+
'pages/index.js': `
214+
export default function Home() {
215+
return (
216+
<>
217+
<p>Home page</p>
218+
</>
219+
)
220+
}
221+
`,
222+
},
223+
dependencies: {
224+
react: '19.0.0-beta-4508873393-20240430',
225+
'react-dom': '19.0.0-beta-4508873393-20240430',
226+
},
227+
})
228+
})
229+
afterAll(() => next.destroy())
230+
231+
it('Script is injected server-side', async () => {
232+
let browser: BrowserInterface
233+
234+
try {
235+
browser = await webdriver(next.url, '/')
236+
237+
const script = await browser.eval(
238+
`document.querySelector('script[data-nscript="afterInteractive"]')`
239+
)
240+
expect(script).not.toBeNull()
241+
} finally {
242+
if (browser) await browser.close()
243+
}
244+
})
245+
})
127246
;(process.env.TURBOPACK ? describe.skip : describe)(
128247
'experimental.nextScriptWorkers',
129248
() => {

0 commit comments

Comments
 (0)