Unverified Commit e1a7de0d authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

feat(error-overlay): handle script under html hydration error (#63403)

script tag cannot be placed under html directly, users reported a case
in #51242 that having `<Script>` under html will cause hydration error,
this will display the React hydration error related warning of bad usage
for it.

You will see this warning in dev overlay instead of displaying nothing
```
In HTML, <script> cannot be a child of <html>.
This will cause a hydration error.
```

Added two other react warnings detection patterns  as well
* `Warning: In HTML, text nodes cannot be a child of <%s>.\nThis will
cause a hydration error.',`
* `Warning: In HTML, whitespace text nodes cann...`

But tested they're not generating hydration errors, only warnings in
console, so we don't need to have tests for them.

Closes NEXT-2835
parent a35a8904
...@@ -231,7 +231,8 @@ export function Errors({ ...@@ -231,7 +231,8 @@ export function Errors({
? warningTemplate ? warningTemplate
.replace('%s', serverContent) .replace('%s', serverContent)
.replace('%s', clientContent) .replace('%s', clientContent)
.replace('%s', '') // remove the last %s for stack .replace('%s', '') // remove the %s for stack
.replace(/%s$/, '') // If there's still a %s at the end, remove it
.replace(/^Warning: /, '') .replace(/^Warning: /, '')
: null : null
...@@ -269,16 +270,18 @@ export function Errors({ ...@@ -269,16 +270,18 @@ export function Errors({
> >
{error.name}: <HotlinkedText text={error.message} /> {error.name}: <HotlinkedText text={error.message} />
</p> </p>
{hydrationWarning && activeError.componentStackFrames && ( {hydrationWarning && (
<> <>
<p id="nextjs__container_errors__extra">{hydrationWarning}</p> <p id="nextjs__container_errors__extra">{hydrationWarning}</p>
<PseudoHtmlDiff {activeError.componentStackFrames?.length ? (
className="nextjs__container_errors__extra_code" <PseudoHtmlDiff
hydrationMismatchType={hydrationErrorType} className="nextjs__container_errors__extra_code"
componentStackFrames={activeError.componentStackFrames} hydrationMismatchType={hydrationErrorType}
firstContent={serverContent} componentStackFrames={activeError.componentStackFrames}
secondContent={clientContent} firstContent={serverContent}
/> secondContent={clientContent}
/>
) : null}
</> </>
)} )}
{isServerError ? ( {isServerError ? (
......
...@@ -32,7 +32,11 @@ export const hydrationErrorState: HydrationErrorState = {} ...@@ -32,7 +32,11 @@ export const hydrationErrorState: HydrationErrorState = {}
// https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference // https://github.com/facebook/react/blob/main/packages/react-dom/src/__tests__/ReactDOMHydrationDiff-test.js used as a reference
const htmlTagsWarnings = new Set([ const htmlTagsWarnings = new Set([
'Warning: Cannot render a sync or defer <script> outside the main document without knowing its order. Try adding async="" or moving it into the root <head> tag.%s',
'Warning: In HTML, %s cannot be a child of <%s>.%s\nThis will cause a hydration error.%s',
'Warning: In HTML, %s cannot be a descendant of <%s>.\nThis will cause a hydration error.%s', 'Warning: In HTML, %s cannot be a descendant of <%s>.\nThis will cause a hydration error.%s',
'Warning: In HTML, text nodes cannot be a child of <%s>.\nThis will cause a hydration error.',
"Warning: In HTML, whitespace text nodes cannot be a child of <%s>. Make sure you don't have any extra whitespace between tags on each line of your source code.\nThis will cause a hydration error.",
'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s', 'Warning: Expected server HTML to contain a matching <%s> in <%s>.%s',
'Warning: Did not expect server HTML to contain a <%s> in <%s>.%s', 'Warning: Did not expect server HTML to contain a <%s> in <%s>.%s',
]) ])
......
...@@ -537,6 +537,50 @@ describe('Error overlay for hydration errors', () => { ...@@ -537,6 +537,50 @@ describe('Error overlay for hydration errors', () => {
await cleanup() await cleanup()
}) })
it('should show error if script is directly placed under html instead of body', async () => {
const { cleanup, session } = await sandbox(
next,
new Map([
[
'app/layout.js',
outdent`
import Script from 'next/script'
export default function Layout({ children }) {
return (
<html>
<body>{children}</body>
<Script
src="https://example.com/script.js"
strategy="beforeInteractive"
/>
</html>
)
}
`,
],
[
'app/page.js',
outdent`
export default function Page() {
return <div>Hello World</div>
}
`,
],
])
)
await session.waitForAndOpenRuntimeError()
expect(await session.hasRedbox()).toBe(true)
const warning = await session.getRedboxDescriptionWarning()
expect(warning).toContain(
'In HTML, <script> cannot be a child of <html>.\nThis will cause a hydration error.'
)
await cleanup()
})
it('should collapse and uncollapse properly when there are many frames', async () => { it('should collapse and uncollapse properly when there are many frames', async () => {
const { cleanup, session } = await sandbox( const { cleanup, session } = await sandbox(
next, next,
......
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