Skip to content

Commit

Permalink
fix unhandled runtime error when notFound() triggered in generateMeta…
Browse files Browse the repository at this point in the history
…data w/ parallel routes
  • Loading branch information
ztanner committed Apr 27, 2024
1 parent 3050f45 commit 1f1c943
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,10 @@ async function createComponentTreeInternal({
injectedJS: injectedJSWithCurrentLayout,
injectedFontPreloadTags: injectedFontPreloadTagsWithCurrentLayout,
asNotFound,
metadataOutlet,
// The metadataOutlet is responsible for throwing any errors that were caught during metadata resolution.
// We only want to render an outlet once per segment, as otherwise the error will be triggered
// multiple times causing an uncaught error.
metadataOutlet: isChildrenRouteKey ? metadataOutlet : undefined,
ctx,
missingSlots,
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>@bar slot</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { notFound } from 'next/navigation'

export function generateMetadata() {
notFound()
}

export default function Page() {
return <div>@bar slot</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function generateMetadata() {
return {
title: 'Create Next App',
}
}

export default function Page() {
return <div>@foo slot</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>@foo slot</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>Custom Not Found!</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { notFound } from 'next/navigation'

export function generateMetadata() {
notFound()
}

export default function Page() {
return <h1>Hello from Page</h1>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function generateMetadata() {
return {
title: 'Create Next App',
}
}

export default function Page() {
return <h1>Hello from Page</h1>
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,34 @@ describe('parallel-route-not-found', () => {
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!'
)

// The slots should still render
expect(await browser.elementByCss('body').text()).toContain('@bar slot')
expect(await browser.elementByCss('body').text()).toContain('@foo slot')
})

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!'
)

// The slots should still render
expect(await browser.elementByCss('body').text()).toContain('@bar slot')
expect(await browser.elementByCss('body').text()).toContain('@foo slot')
})

if (isNextDev) {
it('should not log any warnings for a regular not found page', async () => {
const browser = await next.browser('/this-page-doesnt-exist')
Expand Down

0 comments on commit 1f1c943

Please sign in to comment.