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< ...@@ -136,7 +136,7 @@ class HTTPAccessFallbackErrorBoundary extends React.Component<
<meta name="robots" content="noindex" /> <meta name="robots" content="noindex" />
{process.env.NODE_ENV === 'development' && ( {process.env.NODE_ENV === 'development' && (
<meta <meta
name="next-error" name="boundary-next-error"
content={getAccessFallbackErrorTypeByStatus(triggeredStatus)} content={getAccessFallbackErrorTypeByStatus(triggeredStatus)}
/> />
)} )}
......
...@@ -27,7 +27,10 @@ function BrowserResolvedMetadata({ ...@@ -27,7 +27,10 @@ function BrowserResolvedMetadata({
}: { }: {
promise: Promise<StreamingMetadataResolvedState> 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 return metadata
} }
......
...@@ -93,7 +93,7 @@ async function createComponentTreeInternal({ ...@@ -93,7 +93,7 @@ async function createComponentTreeInternal({
preloadCallbacks: PreloadCallbacks preloadCallbacks: PreloadCallbacks
authInterrupts: boolean authInterrupts: boolean
StreamingMetadata: React.ComponentType<{}> | null StreamingMetadata: React.ComponentType<{}> | null
StreamingMetadataOutlet: React.ComponentType StreamingMetadataOutlet: React.ComponentType | null
}): Promise<CacheNodeSeedData> { }): Promise<CacheNodeSeedData> {
const { const {
renderOpts: { nextConfigOutput, experimental }, renderOpts: { nextConfigOutput, experimental },
...@@ -397,9 +397,15 @@ async function createComponentTreeInternal({ ...@@ -397,9 +397,15 @@ async function createComponentTreeInternal({
// Only render metadata on the actual SSR'd segment not the `default` segment, // Only render metadata on the actual SSR'd segment not the `default` segment,
// as it's used as a placeholder for navigation. // as it's used as a placeholder for navigation.
const isNotDefaultSegment = actualSegment !== DEFAULT_SEGMENT_KEY
const metadata = const metadata =
actualSegment !== DEFAULT_SEGMENT_KEY && StreamingMetadata ? ( isNotDefaultSegment && StreamingMetadata ? <StreamingMetadata /> : undefined
<StreamingMetadata />
// Use the same condition to render metadataOutlet as metadata
const metadataOutlet =
isNotDefaultSegment && StreamingMetadataOutlet ? (
<StreamingMetadataOutlet />
) : undefined ) : undefined
const notFoundElement = NotFound ? ( const notFoundElement = NotFound ? (
...@@ -522,7 +528,11 @@ async function createComponentTreeInternal({ ...@@ -522,7 +528,11 @@ async function createComponentTreeInternal({
preloadCallbacks, preloadCallbacks,
authInterrupts, authInterrupts,
StreamingMetadata, 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 childCacheNodeSeedData = seedData
...@@ -709,8 +719,10 @@ async function createComponentTreeInternal({ ...@@ -709,8 +719,10 @@ async function createComponentTreeInternal({
{layerAssets} {layerAssets}
<OutletBoundary> <OutletBoundary>
<MetadataOutlet ready={getViewportReady} /> <MetadataOutlet ready={getViewportReady} />
{/* Blocking metadata outlet */}
<MetadataOutlet ready={getMetadataReady} /> <MetadataOutlet ready={getMetadataReady} />
<StreamingMetadataOutlet /> {/* Streaming metadata outlet */}
{metadataOutlet}
</OutletBoundary> </OutletBoundary>
</React.Fragment>, </React.Fragment>,
parallelRouteCacheNodeSeedData, 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' ...@@ -2,12 +2,12 @@ import { connection } from 'next/server'
import { notFound } from 'next/navigation' import { notFound } from 'next/navigation'
export default function Page() { export default function Page() {
return <p>notfound page</p> return <p>boundary notfound page</p>
} }
export async function generateMetadata() { export async function generateMetadata() {
await connection() await connection()
await new Promise((resolve) => setTimeout(resolve, 2 * 1000)) await new Promise((resolve) => setTimeout(resolve, 500))
notFound() notFound()
} }
......
...@@ -121,6 +121,19 @@ describe('app-dir - metadata-streaming', () => { ...@@ -121,6 +121,19 @@ describe('app-dir - metadata-streaming', () => {
expect(await browser.elementByCss('p').text()).toBe('index page') 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 () => { it('should render blocking 404 response status when html limited bots access notFound', async () => {
const { status } = await next.fetch('/notfound', { const { status } = await next.fetch('/notfound', {
headers: { 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