Unverified Commit 4b598a56 authored by Pete Hunt's avatar Pete Hunt Committed by GitHub

test: reproduce dev-mode <html> class stripped after hydration

Adds an e2e fixture using the classic no-flash-of-incorrect-theme
pattern: an inline <head> script seeds a class + attribute onto <html>
from localStorage before React hydrates.

In next dev on the 16.3 line the seeded class/attribute is stripped by
a React commit shortly after hydration (a MutationObserver shows
add -> reset). Production is unaffected, and 16.2.11 is unaffected in
both modes. Bisecting published canaries narrows the regression to
16.3.0-canary.3 (canary.2 is good).
parent 0be0ebe5
export const metadata = { title: 'html-class-hydration' }
// The classic "no flash of incorrect theme" pattern: an inline script in the
// document <head> seeds a class (and attribute) onto <html> from localStorage
// before React hydrates. The server never renders this class, so it only
// exists in the DOM after this script runs. React must not strip it after
// hydration.
const seedScript = `try {
var el = document.documentElement
var theme = localStorage.getItem('theme') || 'dark'
el.classList.add(theme)
el.setAttribute('data-seeded', '1')
} catch (e) {}`
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" suppressHydrationWarning>
<head>
<script dangerouslySetInnerHTML={{ __html: seedScript }} />
</head>
<body>{children}</body>
</html>
)
}
import Link from 'next/link'
export default function Page() {
return (
<main>
<p id="home">home</p>
<Link href="/second" id="to-second">
to second
</Link>
</main>
)
}
import Link from 'next/link'
export default function Second() {
return (
<main>
<p id="second">second</p>
<Link href="/" id="to-home">
to home
</Link>
</main>
)
}
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('html-class-hydration', () => {
const { next } = nextTestSetup({
files: __dirname,
})
// Regression test for a dev-mode issue where a class seeded onto <html> by
// an inline script (before hydration) was stripped shortly after hydration.
it('keeps a script-seeded <html> class after hydration', async () => {
const browser = await next.browser('/')
await browser.elementById('home')
await retry(async () => {
expect(
await browser.eval(
'document.documentElement.getAttribute("data-seeded")'
)
).toBe('1')
expect(
await browser.eval('document.documentElement.className')
).toContain('dark')
})
// Ensure it stays put after post-hydration effects/commits have settled.
await retry(async () => {
expect(
await browser.eval('document.documentElement.className')
).toContain('dark')
expect(
await browser.eval(
'document.documentElement.getAttribute("data-seeded")'
)
).toBe('1')
})
})
it('keeps the class across a client-side navigation', async () => {
const browser = await next.browser('/')
await browser.elementById('home')
await browser.elementById('to-second').click()
await browser.elementById('second')
await retry(async () => {
expect(
await browser.eval('document.documentElement.className')
).toContain('dark')
})
})
})
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