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