Unverified Commit 26255a68 authored by Janicklas Ralph's avatar Janicklas Ralph Committed by GitHub

Add data attribute to script component (#28310)

Add data attribute `data-nscript` to the script component
parent 8737ac08
......@@ -35,6 +35,7 @@ const loadScript = (props: ScriptProps): void => {
onLoad = () => {},
dangerouslySetInnerHTML,
children = '',
strategy = 'afterInteractive',
onError,
} = props
......@@ -98,6 +99,8 @@ const loadScript = (props: ScriptProps): void => {
el.setAttribute(attr, value)
}
el.setAttribute('data-nscript', strategy)
document.body.appendChild(el)
}
......
......@@ -84,6 +84,7 @@ function getPreNextScripts(context: HtmlProps, props: OriginProps) {
key={scriptProps.src || index}
defer={!disableOptimizedLoading}
nonce={props.nonce}
data-nscript="beforeInteractive"
crossOrigin={props.crossOrigin || process.env.__NEXT_CROSS_ORIGIN}
/>
)
......
......@@ -42,12 +42,15 @@ describe('Script Loader', () => {
async function test(id) {
const script = await browser.elementById(id)
const dataAttr = await script.getAttribute('data-nscript')
const endScripts = await browser.elementsByCss(
`#__NEXT_DATA__ ~ #${id}`
)
// Renders script tag
expect(script).toBeDefined()
expect(dataAttr).toBeDefined()
// Script is inserted at the end
expect(endScripts.length).toBe(1)
}
......@@ -77,12 +80,15 @@ describe('Script Loader', () => {
async function test(id) {
const script = await browser.elementById(id)
const dataAttr = await script.getAttribute('data-nscript')
const endScripts = await browser.elementsByCss(
`#__NEXT_DATA__ ~ #${id}`
)
// Renders script tag
expect(script).toBeDefined()
expect(dataAttr).toBeDefined()
// Script is inserted at the end
expect(endScripts.length).toBe(1)
}
......@@ -105,6 +111,7 @@ describe('Script Loader', () => {
// Renders script tag
expect(script.length).toBe(1)
expect(script.attr('data-nscript')).toBeDefined()
// Script is inserted before NextScripts
expect(
......
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