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

fix: <Script> with beforeInteractive strategy ignores additional attributes in App Router #59779

Merged
merged 2 commits into from Jan 3, 2024
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: 2 additions & 1 deletion packages/next/src/client/script.tsx
Expand Up @@ -334,7 +334,7 @@ function Script(props: ScriptProps): JSX.Element | null {
dangerouslySetInnerHTML={{
__html: `(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
0,
{ ...restProps },
{ ...restProps, id },
])})`,
}}
/>
Expand All @@ -353,6 +353,7 @@ function Script(props: ScriptProps): JSX.Element | null {
dangerouslySetInnerHTML={{
__html: `(self.__next_s=self.__next_s||[]).push(${JSON.stringify([
src,
{ ...restProps, id },
])})`,
}}
/>
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/app-dir/app/app/script/page.js
Expand Up @@ -33,6 +33,22 @@ export default function Page() {
`,
}}
/>
<Script
strategy="beforeInteractive"
src="/noop-test.js"
id="script-with-src-noop-test"
data-extra-prop="script-with-src"
/>
<Script
strategy="beforeInteractive"
dangerouslySetInnerHTML={{
__html: `
console.log('noop-test-dangerouslySetInnerHTML')
`,
}}
id="script-without-src-noop-test-dangerouslySetInnerHTML"
data-extra-prop="script-without-src"
/>
</div>
)
}
20 changes: 20 additions & 0 deletions test/e2e/app-dir/app/index.test.ts
Expand Up @@ -1751,6 +1751,26 @@ createNextDescribe(
return 'yes'
}, 'yes')
})

it('should pass on extra props for beforeInteractive scripts with a src prop', async () => {
const browser = await next.browser('/script')

const foundProps = await browser.eval(
`document.querySelector('#script-with-src-noop-test').getAttribute('data-extra-prop')`
)

expect(foundProps).toBe('script-with-src')
})

it('should pass on extra props for beforeInteractive scripts without a src prop', async () => {
const browser = await next.browser('/script')

const foundProps = await browser.eval(
`document.querySelector('#script-without-src-noop-test-dangerouslySetInnerHTML').getAttribute('data-extra-prop')`
)

expect(foundProps).toBe('script-without-src')
})
}

it('should insert preload tags for beforeInteractive and afterInteractive scripts', async () => {
Expand Down
1 change: 1 addition & 0 deletions test/e2e/app-dir/app/public/noop-test.js
@@ -0,0 +1 @@
console.log('test-noop')