Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/next/client/script.tsx
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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