Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add data attribute to script component #28310

Merged
merged 7 commits into from Aug 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/client/script.tsx
Expand Up @@ -35,6 +35,7 @@ const loadScript = (props: ScriptProps): void => {
onLoad = () => {},
dangerouslySetInnerHTML,
children = '',
strategy = 'afterInteractive',
onError,
} = props

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

el.setAttribute('data-nscript', strategy)

document.body.appendChild(el)
}

Expand Down
1 change: 1 addition & 0 deletions packages/next/pages/_document.tsx
Expand Up @@ -84,6 +84,7 @@ function getPreNextScripts(context: HtmlProps, props: OriginProps) {
key={scriptProps.src || index}
defer={!disableOptimizedLoading}
nonce={props.nonce}
data-nscript="beforeInteractive"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be data-nscript={strategy}?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this doesn't seem right since its not using next/script here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For beforeInteractive we pass the data via context so we can render it during SSR. Only beforeInteractive scripts are rendered here

crossOrigin={props.crossOrigin || process.env.__NEXT_CROSS_ORIGIN}
/>
)
Expand Down
7 changes: 7 additions & 0 deletions test/integration/script-loader/test/index.test.js
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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(
Expand Down