Unverified Commit 0b136b27 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

[metadata] fix streaming metadata triggering error boundaries (#76280)

parent de535e48
......@@ -136,7 +136,7 @@ class HTTPAccessFallbackErrorBoundary extends React.Component<
<meta name="robots" content="noindex" />
{process.env.NODE_ENV === 'development' && (
<meta
name="next-error"
name="boundary-next-error"
content={getAccessFallbackErrorTypeByStatus(triggeredStatus)}
/>
)}
......
......@@ -27,7 +27,10 @@ function BrowserResolvedMetadata({
}: {
promise: Promise<StreamingMetadataResolvedState>
}) {
const { metadata } = use(promise)
const { metadata, error } = use(promise)
// If there's metadata error on client, discard the browser metadata
// and let metadata outlet deal with the error. This will avoid the duplication metadata.
if (error) return null
return metadata
}
......
......@@ -93,7 +93,7 @@ async function createComponentTreeInternal({
preloadCallbacks: PreloadCallbacks
authInterrupts: boolean
StreamingMetadata: React.ComponentType<{}> | null
StreamingMetadataOutlet: React.ComponentType
StreamingMetadataOutlet: React.ComponentType | null
}): Promise<CacheNodeSeedData> {
const {
renderOpts: { nextConfigOutput, experimental },
......@@ -397,9 +397,15 @@ async function createComponentTreeInternal({
// Only render metadata on the actual SSR'd segment not the `default` segment,
// as it's used as a placeholder for navigation.
const isNotDefaultSegment = actualSegment !== DEFAULT_SEGMENT_KEY
const metadata =
actualSegment !== DEFAULT_SEGMENT_KEY && StreamingMetadata ? (
<StreamingMetadata />
isNotDefaultSegment && StreamingMetadata ? <StreamingMetadata /> : undefined
// Use the same condition to render metadataOutlet as metadata
const metadataOutlet =
isNotDefaultSegment && StreamingMetadataOutlet ? (
<StreamingMetadataOutlet />
) : undefined
const notFoundElement = NotFound ? (
......@@ -522,7 +528,11 @@ async function createComponentTreeInternal({
preloadCallbacks,
authInterrupts,
StreamingMetadata,
StreamingMetadataOutlet,
// `StreamingMetadataOutlet` is used to conditionally throw. In the case of parallel routes we will have more than one page
// but we only want to throw on the first one.
StreamingMetadataOutlet: isChildrenRouteKey
? StreamingMetadataOutlet
: null,
})
childCacheNodeSeedData = seedData
......@@ -709,8 +719,10 @@ async function createComponentTreeInternal({
{layerAssets}
<OutletBoundary>
<MetadataOutlet ready={getViewportReady} />
{/* Blocking metadata outlet */}
<MetadataOutlet ready={getMetadataReady} />
<StreamingMetadataOutlet />
{/* Streaming metadata outlet */}
{metadataOutlet}
</OutletBoundary>
</React.Fragment>,
parallelRouteCacheNodeSeedData,
......
export default function NotFound() {
return <h1>Custom Not Found</h1>
}
export const metadata = {
title: 'notfound title',
}
import { connection } from 'next/server'
import { notFound } from 'next/navigation'
export default function Page() {
return <p>notfound page</p>
}
export async function generateMetadata() {
await connection()
await new Promise((resolve) => setTimeout(resolve, 2 * 1000))
notFound()
}
export const dynamic = 'force-dynamic'
......@@ -2,12 +2,12 @@ import { connection } from 'next/server'
import { notFound } from 'next/navigation'
export default function Page() {
return <p>notfound page</p>
return <p>boundary notfound page</p>
}
export async function generateMetadata() {
await connection()
await new Promise((resolve) => setTimeout(resolve, 2 * 1000))
await new Promise((resolve) => setTimeout(resolve, 500))
notFound()
}
......
......@@ -121,6 +121,19 @@ describe('app-dir - metadata-streaming', () => {
expect(await browser.elementByCss('p').text()).toBe('index page')
})
it('should trigger custom not-found in the boundary', async () => {
const browser = await next.browser('/notfound/boundary')
expect(await browser.elementByCss('h1').text()).toBe('Custom Not Found')
})
it('should not duplicate metadata with navigation API', async () => {
const browser = await next.browser('/notfound/boundary')
const titleTags = await browser.elementsByCss('title')
expect(titleTags.length).toBe(1)
})
it('should render blocking 404 response status when html limited bots access notFound', async () => {
const { status } = await next.fetch('/notfound', {
headers: {
......
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