-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[error overlay] move missing tags error inside error overlay (#62993)
### What * Move missing html tags error into error overlay, from outside we don't have to manually determine when to render a dummy component with runtime missing tag error or error overlay. * Add brackets `<>` to the html tags in the error ![image](https://github.com/vercel/next.js/assets/4800338/cd3467b7-74c2-477e-8516-c31761adb064) ### Why In #62815, we're having throwing an missing required error, this will trigger another runtime error. Then when error overlay caught it through error event listener, it will render it as an unhandled runtime error: You will see the below message in the overlay. ``` Unhandled Runtime Error Error: The following tas are missing... [Error stack] ``` This error message will bring a message that the error is happened on client during runtime, but actually we already know that is a user side mistake which doesn't have a error trace. This couldn't hmr as you fix the error as well. This PR moves the rendering into error overlay that we're aware of the errors and can render the correct html on client, with the `html` tag attached with error id and `body` wrapping the error overlay. We tell overlay that there're missing tags through props, let it handle everything inside. It can also hmr once you fix the error. One drawback is that when you re-introduce the error, it might trigger react DOM updates exception (`Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.`) instead of the "missing tags" message again. Besides that the HMR works properly. Closes NEXT-2741
- Loading branch information
Showing
12 changed files
with
147 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
...client/components/react-dev-overlay/internal/container/root-layout-missing-tags-error.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as React from 'react' | ||
import type { VersionInfo } from '../../../../../server/dev/parse-version-info' | ||
import { Dialog, DialogContent, DialogHeader } from '../components/Dialog' | ||
import { Overlay } from '../components/Overlay' | ||
import { VersionStalenessInfo } from '../components/VersionStalenessInfo' | ||
import { HotlinkedText } from '../components/hot-linked-text' | ||
|
||
export type RootLayoutMissingTagsErrorProps = { | ||
missingTags: string[] | ||
versionInfo?: VersionInfo | ||
} | ||
|
||
export const RootLayoutMissingTagsError: React.FC<RootLayoutMissingTagsErrorProps> = | ||
function RootLayoutMissingTagsError({ missingTags, versionInfo }) { | ||
const noop = React.useCallback(() => {}, []) | ||
return ( | ||
<Overlay> | ||
<Dialog | ||
type="error" | ||
aria-labelledby="nextjs__container_errors_label" | ||
aria-describedby="nextjs__container_errors_desc" | ||
onClose={noop} | ||
> | ||
<DialogContent> | ||
<DialogHeader className="nextjs-container-errors-header"> | ||
<h3 id="nextjs__container_errors_label"> | ||
Missing required html tags | ||
</h3> | ||
{versionInfo ? <VersionStalenessInfo {...versionInfo} /> : null} | ||
<p | ||
id="nextjs__container_errors_desc" | ||
className="nextjs__container_errors_desc nextjs__container_errors_desc--error" | ||
> | ||
<HotlinkedText | ||
text={`The following tags are missing in the Root Layout: ${missingTags | ||
.map((tagName) => `<${tagName}>`) | ||
.join( | ||
', ' | ||
)}.\nRead more at https://nextjs.org/docs/messages/missing-root-layout-tags`} | ||
/> | ||
</p> | ||
</DialogHeader> | ||
</DialogContent> | ||
</Dialog> | ||
</Overlay> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/development/app-dir/missing-required-html-tags/app/layout.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Root({ children }) { | ||
return children | ||
} |
File renamed without changes.
64 changes: 64 additions & 0 deletions
64
test/development/app-dir/missing-required-html-tags/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { nextTestSetup } from 'e2e-utils' | ||
import { getRedboxDescription, hasRedbox, retry } from 'next-test-utils' | ||
|
||
describe('app-dir - missing required html tags', () => { | ||
const { next } = nextTestSetup({ files: __dirname }) | ||
|
||
it('should show error overlay', async () => { | ||
const browser = await next.browser('/') | ||
|
||
expect(await hasRedbox(browser)).toBe(true) | ||
expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(` | ||
"The following tags are missing in the Root Layout: <html>, <body>. | ||
Read more at https://nextjs.org/docs/messages/missing-root-layout-tags" | ||
`) | ||
}) | ||
|
||
it('should hmr when you fix the error', async () => { | ||
const browser = await next.browser('/') | ||
|
||
await next.patchFile('app/layout.js', (code) => | ||
code.replace('return children', 'return <body>{children}</body>') | ||
) | ||
|
||
expect(await hasRedbox(browser)).toBe(true) | ||
expect(await getRedboxDescription(browser)).toMatchInlineSnapshot(` | ||
"The following tags are missing in the Root Layout: <html>. | ||
Read more at https://nextjs.org/docs/messages/missing-root-layout-tags" | ||
`) | ||
|
||
await next.patchFile('app/layout.js', (code) => | ||
code.replace( | ||
'return <body>{children}</body>', | ||
'return <html><body>{children}</body></html>' | ||
) | ||
) | ||
|
||
expect(await hasRedbox(browser)).toBe(false) | ||
expect(await browser.elementByCss('p').text()).toBe('hello world') | ||
|
||
// Reintroduce the bug, but only missing html tag | ||
await next.patchFile('app/layout.js', (code) => | ||
code.replace( | ||
'return <html><body>{children}</body></html>', | ||
'return children' | ||
) | ||
) | ||
|
||
await retry(async () => { | ||
expect(await hasRedbox(browser)).toBe(true) | ||
}) | ||
|
||
// Fix the issue again | ||
await next.patchFile('app/layout.js', (code) => | ||
code.replace( | ||
'return children', | ||
'return <html><body>{children}</body></html>' | ||
) | ||
) | ||
|
||
await retry(async () => { | ||
expect(await hasRedbox(browser)).toBe(false) | ||
}) | ||
}) | ||
}) |
3 changes: 0 additions & 3 deletions
3
test/development/app-dir/show-error-when-missing-html-or-body-in-root-layout/app/layout.tsx
This file was deleted.
Oops, something went wrong.
6 changes: 0 additions & 6 deletions
6
test/development/app-dir/show-error-when-missing-html-or-body-in-root-layout/next.config.js
This file was deleted.
Oops, something went wrong.
33 changes: 0 additions & 33 deletions
33
...g-html-or-body-in-root-layout/show-error-when-missing-html-or-body-in-root-layout.test.ts
This file was deleted.
Oops, something went wrong.
23 changes: 0 additions & 23 deletions
23
test/development/app-dir/show-error-when-missing-html-or-body-in-root-layout/tsconfig.json
This file was deleted.
Oops, something went wrong.