Skip to content

Commit 26255a6

Browse files
Add data attribute to script component (#28310)
Add data attribute `data-nscript` to the script component
1 parent 8737ac0 commit 26255a6

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

packages/next/client/script.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const loadScript = (props: ScriptProps): void => {
3535
onLoad = () => {},
3636
dangerouslySetInnerHTML,
3737
children = '',
38+
strategy = 'afterInteractive',
3839
onError,
3940
} = props
4041

@@ -98,6 +99,8 @@ const loadScript = (props: ScriptProps): void => {
9899
el.setAttribute(attr, value)
99100
}
100101

102+
el.setAttribute('data-nscript', strategy)
103+
101104
document.body.appendChild(el)
102105
}
103106

packages/next/pages/_document.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ function getPreNextScripts(context: HtmlProps, props: OriginProps) {
8484
key={scriptProps.src || index}
8585
defer={!disableOptimizedLoading}
8686
nonce={props.nonce}
87+
data-nscript="beforeInteractive"
8788
crossOrigin={props.crossOrigin || process.env.__NEXT_CROSS_ORIGIN}
8889
/>
8990
)

test/integration/script-loader/test/index.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ describe('Script Loader', () => {
4242

4343
async function test(id) {
4444
const script = await browser.elementById(id)
45+
const dataAttr = await script.getAttribute('data-nscript')
4546
const endScripts = await browser.elementsByCss(
4647
`#__NEXT_DATA__ ~ #${id}`
4748
)
4849

4950
// Renders script tag
5051
expect(script).toBeDefined()
52+
expect(dataAttr).toBeDefined()
53+
5154
// Script is inserted at the end
5255
expect(endScripts.length).toBe(1)
5356
}
@@ -77,12 +80,15 @@ describe('Script Loader', () => {
7780

7881
async function test(id) {
7982
const script = await browser.elementById(id)
83+
const dataAttr = await script.getAttribute('data-nscript')
8084
const endScripts = await browser.elementsByCss(
8185
`#__NEXT_DATA__ ~ #${id}`
8286
)
8387

8488
// Renders script tag
8589
expect(script).toBeDefined()
90+
expect(dataAttr).toBeDefined()
91+
8692
// Script is inserted at the end
8793
expect(endScripts.length).toBe(1)
8894
}
@@ -105,6 +111,7 @@ describe('Script Loader', () => {
105111

106112
// Renders script tag
107113
expect(script.length).toBe(1)
114+
expect(script.attr('data-nscript')).toBeDefined()
108115

109116
// Script is inserted before NextScripts
110117
expect(

0 commit comments

Comments
 (0)