No description
Find a file
Jiachi Liu cb24c555a6
Refine the not-found rendering process for app router (#52790)
### What

This PR changes the flow of not-found rendering process. 

### Why

`not-found.js` was rendered in two ways before:
* 1 is SSR rendering the not-found as 404
* 2 is triggering the error on RSC rendering then the error will be
preserved in inline flight data, on the client it will recover the error
and trigger the proper error boundary.

The solution has been through a jounery:
No top-level not found boundary -> introduce metadata API -> then we
create a top level root not found boundary -> then we delete it due to
duplicated rendering of root layout -> now this

So the solution before this PR is still having a root not found boundary
wrapped in the `AppRouter`, it's being used in a lot of places including
HMR. As we discovered it's doing duplicated rendering of root layout,
then we removed it and it started failing with rendering `not-found` but
missing root layout. In this PR we redesign the process.

### How

Now the rendering architecture looks like:

* For normal root not-found and certain level of not-found boundary
they're still covered by `LayoutRouter`
* For other error renderings including not-found
* Fully remove the top level not-found boundary, when it renders with
404 error it goes to render the fallback page
* During rendering the fallback page it will check if it should just
renders a 404 error page or render nothing and let the error from inline
flight data to trigger the error boundary

pseudo code
```
try {
  render AppRouter > PageComponent
} catch (err) {
  create ErrorComponent by determine err
  render AppRouter > ErrorComponent
}
```

In this way if the error is thrown from top-level like the page itself
or even from metadata, we can still catch them and render the proper
error page based on the error type.

The problematic is the HMR: introduces a new development mode meta tag
`<meta name="next-error">` to indicate it's 404 so that we don't do
refresh. This reverts the change brougt in #51637 as it will also has
the duplicated rendering problem for root layout if it's included in the
top level not found boundary.

Also fixes the root layout missing issue:

Fixes #52718
Fixes #52739

---------

Co-authored-by: Shu Ding <g@shud.in>
2023-07-20 14:12:06 -07:00
.cargo Revert "Next Build Turbo POC (#49942)" (#51538) 2023-06-19 21:54:04 -07:00
.config Update Next.js code owners (#51319) 2023-06-14 20:07:32 -07:00
.devcontainer re-enable next-dev-tests (#47087) 2023-03-14 16:05:19 +01:00
.github chore: add GitHub Action to manage "+1" comments (#52866) 2023-07-20 09:34:29 +00:00
.husky Replace pre-commit with husky (#38350) 2022-07-06 11:14:16 -05:00
.vscode [Next Docs] Update Git Workflow (#50579) 2023-06-08 12:01:55 +02:00
bench Replace var with const (#49379) 2023-05-09 10:25:39 +02:00
contributing Make sure DevEx can approve docs PRs / Remove Spaces (#51996) 2023-06-29 17:10:53 +00:00
docs Change "publically" to "publicly" in the routing docs (#52966) 2023-07-20 20:47:42 +00:00
errors docs: Add missing closing tag for react hydration error message. (#52607) 2023-07-13 00:22:10 +02:00
examples examples: export force-dynamic from all dynamic routes (#52916) 2023-07-20 13:33:43 +00:00
packages Refine the not-found rendering process for app router (#52790) 2023-07-20 14:12:06 -07:00
scripts perf: create an experimental bundled version of the next server (#52206) 2023-07-10 15:40:06 +00:00
test Refine the not-found rendering process for app router (#52790) 2023-07-20 14:12:06 -07:00
.alexignore Integrate next-* crates from Turbopack (#47019) 2023-03-13 14:33:17 +01:00
.alexrc docs: Add "special" to the Alex allowlist (#48021) 2023-04-06 16:32:06 +02:00
.eslintignore chore: add GitHub Action to manage "+1" comments (#52866) 2023-07-20 09:34:29 +00:00
.eslintrc.json perf: create an experimental bundled version of the next server (#52206) 2023-07-10 15:40:06 +00:00
.gitattributes chore: make next/src/build files indexable on github (#51647) 2023-06-22 11:32:42 +00:00
.gitignore chore: add GitHub Action to manage "+1" comments (#52866) 2023-07-20 09:34:29 +00:00
.npmrc fix: npm publish provenance permissions (#48757) 2023-04-24 14:08:55 +00:00
.prettierignore chore: add GitHub Action to manage "+1" comments (#52866) 2023-07-20 09:34:29 +00:00
.prettierignore_staged chore: add GitHub Action to manage "+1" comments (#52866) 2023-07-20 09:34:29 +00:00
.prettierrc.json Prettier trailingComma default value to es5 since 2.0 (#14391) 2020-06-22 13:25:24 +02:00
.rustfmt.toml build(cargo): move workspaces manifest to top level (#48198) 2023-04-19 18:38:36 +02:00
azure-pipelines.yml Revert "Fix pnpm ERR_INVALID_THIS on Node 20" (#51539) 2023-06-19 23:38:15 -07:00
Cargo.lock update turbopack (#52899) 2023-07-19 18:17:57 +00:00
Cargo.toml update turbopack (#52899) 2023-07-19 18:17:57 +00:00
CODE_OF_CONDUCT.md updated code of conduct to v2.1 (#34208) 2022-02-10 18:11:42 -06:00
contributing.md chore: improve repo templates (#46629) 2023-03-01 09:25:51 -08:00
jest.config.js use env var to switch next.js to turbopack mode (#51353) 2023-06-21 13:52:14 +00:00
jest.replay.config.js Add Replay integration for dev e2e tests (#40955) 2022-09-29 14:45:10 -07:00
lerna.json v13.4.11-canary.0 2023-07-18 17:12:06 +00:00
license.md Update license year (#44403) 2023-01-01 11:12:49 +01:00
lint-staged.config.js Run dev tests against default CNA templates (#45211) 2023-01-24 11:01:03 -08:00
package.json Update vendor @vercel/og (#52897) 2023-07-19 21:04:27 +00:00
plopfile.js Opt-in to dynamic rendering when reading searchParams (#46205) 2023-03-16 21:46:35 +01:00
pnpm-lock.yaml Update vendor @vercel/og (#52897) 2023-07-19 21:04:27 +00:00
pnpm-workspace.yaml Integrate next-* crates from Turbopack (#47019) 2023-03-13 14:33:17 +01:00
readme.md Monorepo (#5341) 2018-10-01 01:02:10 +02:00
release.js Fix labels for release sections 2021-11-21 13:11:54 +01:00
run-tests.js ci(workflow): enable test trace upload (#51107) 2023-06-12 17:14:13 +00:00
rust-toolchain Turbopack: Vc<T> and Turbo Engine type system improvements (#51792) 2023-07-16 13:56:20 +00:00
test-file.txt Add additional file serving tests (#12479) 2020-05-04 11:58:19 -05:00
tsconfig-tsec.json Integrate tsec into the linting process (#33746) 2022-02-24 16:59:18 -08:00
tsconfig.json type check tests (and convert next-test-utils.js to ts) (#51071) 2023-06-23 17:42:50 +00:00
tsec-exemptions.json Move core files to src folder and move JS files to TypeScript (#44405) 2023-01-03 10:05:50 +01:00
turbo.json chore(ci): fix turbo input path globs (#52414) 2023-07-08 01:57:24 +00:00
UPGRADING.md Move upgrading guide to /docs (#10727) 2020-02-28 23:46:18 +01:00
vercel.json Silence GH Comments for Preview URLs (#18766) 2020-11-03 21:59:47 +00:00

Next.js

Getting Started

Visit https://nextjs.org/learn to get started with Next.js.

Documentation

Visit https://nextjs.org/docs to view the full documentation.

Who is using Next.js?

Next.js is used by the world's leading companies. Check out the Next.js Showcase to learn more.

Community

The Next.js community can be found on GitHub Discussions, where you can ask questions, voice ideas, and share your projects.

To chat with other community members you can join the Next.js Discord.

Our Code of Conduct applies to all Next.js community channels.

Contributing

Please see our contributing.md.

Good First Issues

We have a list of good first issues that contain bugs that have a relatively limited scope. This is a great place to get started, gain experience, and get familiar with our contribution process.

Authors

Security

If you believe you have found a security vulnerability in Next.js, we encourage you to responsibly disclose this and not open a public issue. We will investigate all legitimate reports. Email security@vercel.com to disclose any security vulnerabilities.

https://vercel.com/security