Unverified Commit c432d07a authored by sangwook's avatar sangwook Committed by GitHub

Fix before interactive incorrectly render css (#81146)

Fixing a bug

  - Related issues linked using fixes #81130
- Tests added. See:
https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see
https://github.com/vercel/next.js/blob/canary/contributing.md

  What?

This PR fixes a bug where Next.js Script components with
strategy="beforeInteractive" incorrectly render classname attribute
instead of the HTML standard class attribute.

  Why?

The beforeInteractive script strategy in App Router bypassed the
existing setAttributesFromProps utility function that properly maps
React prop names to DOM attribute names. This caused className props to
be directly set as classname attributes in the HTML, which is
non-standard and breaks CSS styling.

  Before (broken):
```
  <script classname="example-class" id="example">...</script>
```
  After (fixed):
```
  <script class="example-class" id="example">...</script>
```
  How?

1. Identified the root cause: In
/packages/next/src/client/app-bootstrap.ts, the loadScriptsInSequence
function manually set attributes using a simple loop that didn't handle
DOM attribute name mapping.
  2. Applied the fix:
    - Imported the existing setAttributesFromProps utility function
- Replaced the manual attribute setting loop with the proper utility
function call
    - This ensures className → class mapping works correctly
3. Added comprehensive tests: Created e2e tests in
/test/e2e/app-dir/script-before-interactive/ that verify:
    - Single scripts render with correct class attribute
    - Multiple scripts work correctly
    - Script execution remains unaffected
    - Scripts are properly placed in document head

  Code changes:
```
  // Before (buggy)
  if (props) {
    for (const key in props) {
      if (key !== 'children') {
        el.setAttribute(key, props[key])  // ❌ className becomes classname
      }
    }
  }
```
  // After (fixed)  
```
  if (props) {
    setAttributesFromProps(el, props)  // ✅ className becomes class
  }
```
  Fixes #81130

---------
Co-authored-by: 's avatarJJ Kasper <jj@jjsweb.site>
parent a0d15704
......@@ -5,6 +5,8 @@
* - next/script with `beforeInteractive` strategy
*/
import { setAttributesFromProps } from './set-attributes-from-props'
const version = process.env.__NEXT_VERSION
window.next = {
......@@ -27,11 +29,7 @@ function loadScriptsInSequence(
const el = document.createElement('script')
if (props) {
for (const key in props) {
if (key !== 'children') {
el.setAttribute(key, props[key])
}
}
setAttributesFromProps(el, props)
}
if (src) {
......
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
import Script from 'next/script'
export default function MultiplePage() {
return (
<div>
<h1>Multiple beforeInteractive Scripts Test</h1>
<Script
id="first-script"
strategy="beforeInteractive"
className="first-script"
dangerouslySetInnerHTML={{
__html: `
console.log('First beforeInteractive script executed');
window.firstScriptExecuted = true;
`,
}}
/>
<Script
id="second-script"
strategy="beforeInteractive"
className="second-script"
dangerouslySetInnerHTML={{
__html: `
console.log('Second beforeInteractive script executed');
window.secondScriptExecuted = true;
`,
}}
/>
<p>
This page tests multiple beforeInteractive scripts with CSS classes.
</p>
</div>
)
}
import Script from 'next/script'
export default function Page() {
return (
<div>
<h1>Script beforeInteractive Test</h1>
<Script
id="example-script"
strategy="beforeInteractive"
className="example-class"
dangerouslySetInnerHTML={{
__html: `
console.log('beforeInteractive script executed');
window.beforeInteractiveExecuted = true;
`,
}}
/>
<p>
This page tests the beforeInteractive script strategy with CSS classes.
</p>
</div>
)
}
/** @type {import('next').NextConfig} */
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('Script component with beforeInteractive strategy CSS class rendering', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should render script tag with correct class attribute instead of classname', async () => {
const browser = await next.browser('/')
// Wait for the page to fully load
await browser.waitForElementByCss('#example-script')
// Get the HTML content to check the actual rendered attributes
const html = await browser.eval(() => document.documentElement.innerHTML)
// Check that the script tag has 'class' attribute, not 'classname'
expect(html).toContain('class="example-class"')
expect(html).not.toContain('classname="example-class"')
// Also verify the script element directly
const scriptElement = await browser.elementByCss('#example-script')
const className = await scriptElement.getAttribute('class')
expect(className).toBe('example-class')
})
it('should execute beforeInteractive script correctly', async () => {
const browser = await next.browser('/')
// Check that the script executed by looking for its side effects
const hasExecuted = await browser.eval(() => {
return (window as any).beforeInteractiveExecuted === true
})
expect(hasExecuted).toBe(true)
})
it('should render script in document head with beforeInteractive strategy', async () => {
const browser = await next.browser('/')
// Check that the script is in the head section
const scriptInHead = await browser.eval(() => {
return document.head.querySelector('#example-script') !== null
})
expect(scriptInHead).toBe(true)
})
it('should render multiple beforeInteractive scripts with correct class attributes', async () => {
const browser = await next.browser('/multiple')
const html = await browser.eval(() => document.documentElement.innerHTML)
// Check that both scripts have correct class attributes
expect(html).toContain('class="first-script"')
expect(html).toContain('class="second-script"')
expect(html).not.toContain('classname="first-script"')
expect(html).not.toContain('classname="second-script"')
})
})
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