-
Notifications
You must be signed in to change notification settings - Fork 27.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
23 changed files
with
421 additions
and
246 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
25 changes: 25 additions & 0 deletions
25
packages/next/src/client/components/dev-root-not-found-boundary.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,25 @@ | ||
'use client' | ||
|
||
import React from 'react' | ||
import { NotFoundBoundary } from './not-found-boundary' | ||
|
||
export function bailOnNotFound() { | ||
throw new Error('notFound() is not allowed to use in root layout') | ||
} | ||
|
||
function NotAllowedRootNotFoundError() { | ||
bailOnNotFound() | ||
return null | ||
} | ||
|
||
export function DevRootNotFoundBoundary({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<NotFoundBoundary notFound={<NotAllowedRootNotFoundError />}> | ||
{children} | ||
</NotFoundBoundary> | ||
) | ||
} |
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
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
Oops, something went wrong.