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

fix unhandled runtime error from generateMetadata in parallel routes (#72153)

### What

Backport #65102, #65343 (as they are fixing the same issue)

x-ref:
[slack](https://vercel.slack.com/archives/C0676QZBWKS/p1730402565599979?thread_ts=1729588414.805619&cid=C0676QZBWKS)

---------
Co-authored-by: 's avatarZack Tanner <1939140+ztanner@users.noreply.github.com>
parent 75482671
...@@ -104,9 +104,10 @@ export function createMetadataComponents({ ...@@ -104,9 +104,10 @@ export function createMetadataComponents({
resolve(undefined) resolve(undefined)
} else { } else {
error = resolvedError error = resolvedError
// If the error triggers in initial metadata resolving, re-resolve with proper error type. // If a not-found error is triggered during metadata resolution, we want to capture the metadata
// They'll be saved for flight data, when hydrates, it will replaces the SSR'd metadata with this. // for the not-found route instead of whatever triggered the error. For all error types, we resolve an
// for not-found error: resolve not-found metadata // error, which will cause the outlet to throw it so it'll be handled by an error boundary
// (either an actual error, or an internal error that renders UI such as the NotFoundBoundary).
if (!errorType && isNotFoundError(resolvedError)) { if (!errorType && isNotFoundError(resolvedError)) {
const [notFoundMetadataError, notFoundMetadata, notFoundViewport] = const [notFoundMetadataError, notFoundMetadata, notFoundViewport] =
await resolveMetadata({ await resolveMetadata({
......
...@@ -436,7 +436,7 @@ async function createComponentTreeInternal({ ...@@ -436,7 +436,7 @@ async function createComponentTreeInternal({
injectedJS: injectedJSWithCurrentLayout, injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout, injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
asNotFound, asNotFound,
metadataOutlet, metadataOutlet: isChildrenRouteKey ? metadataOutlet : undefined,
ctx, ctx,
missingSlots, missingSlots,
}) })
......
export default function Page() {
return <div>@bar slot</div>
}
export default function Page() {
return <div>@bar slot</div>
}
import { notFound } from 'next/navigation'
export function generateMetadata() {
notFound()
}
export default function Page() {
return <div>@bar slot</div>
}
export default function Page() {
return <div>@foo slot</div>
}
export function generateMetadata() {
return {
title: 'Create Next App',
}
}
export default function Page() {
return <div>@foo slot</div>
}
export default function Page() {
return <div>@foo slot</div>
}
import { notFound } from 'next/navigation'
export function generateMetadata() {
notFound()
}
export default function Page() {
return <div>@foobar slot</div>
}
export default function Layout(props: { foobar: React.ReactNode }) {
return <>{props.foobar}</>
}
export default function Page() {
return <div>Custom Not Found!</div>
}
import { notFound } from 'next/navigation'
export function generateMetadata() {
notFound()
}
export default function Page() {
return <h1>Hello from Page</h1>
}
export function generateMetadata() {
return {
title: 'Create Next App',
}
}
export default function Page() {
return <h1>Hello from Page</h1>
}
...@@ -58,6 +58,38 @@ createNextDescribe( ...@@ -58,6 +58,38 @@ createNextDescribe(
expect(warnings.length).toBe(0) expect(warnings.length).toBe(0)
}) })
it('should handle `notFound()` in generateMetadata on a page that also renders a parallel route', async () => {
const browser = await next.browser('/not-found-metadata/page-error')
// The page's `generateMetadata` function threw a `notFound()` error,
// so we should see the not found page.
expect(await browser.elementByCss('body').text()).toContain(
'Custom Not Found!'
)
})
it('should handle `notFound()` in a slot', async () => {
const browser = await next.browser('/not-found-metadata/slot-error')
// The page's `generateMetadata` function threw a `notFound()` error,
// so we should see the not found page.
expect(await browser.elementByCss('body').text()).toContain(
'Custom Not Found!'
)
})
// TODO-APP: This test should probably work. But we only provide a not-found boundary for the children slot.
// This means that if a parallel route throws a notFound() in generateMetadata, it won't be properly handled.
it.skip('should handle `notFound()` in a slot with no `children` slot', async () => {
const browser = await next.browser('/not-found-metadata/no-page')
// The page's `generateMetadata` function threw a `notFound()` error,
// so we should see the not found page.
expect(await browser.elementByCss('body').text()).toContain(
'Custom Not Found!'
)
})
if (isNextDev) { if (isNextDev) {
it('should not log any warnings for a regular not found page', async () => { it('should not log any warnings for a regular not found page', async () => {
const browser = await next.browser('/this-page-doesnt-exist') const browser = await next.browser('/this-page-doesnt-exist')
......
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