Commit 4698ad64 authored by Jiachi Liu's avatar Jiachi Liu

[metadata] remove the default segement check for metadata rendering (#77119)

### What

Remove the default segment checks for metadata rendering. When the
`default.js` is rendered, it should still have the metadata. It was a
condition added due to possible of duplicated metadata, but it's
resolved by other conditions such as only rendering in child slot, and
hoisting metadata into rsc head for navigation.
parent 1e1ff403
...@@ -21,7 +21,6 @@ import type { LoadingModuleData } from '../../shared/lib/app-router-context.shar ...@@ -21,7 +21,6 @@ import type { LoadingModuleData } from '../../shared/lib/app-router-context.shar
import type { Params } from '../request/params' import type { Params } from '../request/params'
import { workUnitAsyncStorage } from './work-unit-async-storage.external' import { workUnitAsyncStorage } from './work-unit-async-storage.external'
import { OUTLET_BOUNDARY_NAME } from '../../lib/metadata/metadata-constants' import { OUTLET_BOUNDARY_NAME } from '../../lib/metadata/metadata-constants'
import { DEFAULT_SEGMENT_KEY } from '../../shared/lib/segment'
import type { UseCachePageComponentProps } from '../use-cache/use-cache-wrapper' import type { UseCachePageComponentProps } from '../use-cache/use-cache-wrapper'
/** /**
...@@ -394,19 +393,12 @@ async function createComponentTreeInternal({ ...@@ -394,19 +393,12 @@ async function createComponentTreeInternal({
// Resolve the segment param // Resolve the segment param
const actualSegment = segmentParam ? segmentParam.treeSegment : segment const actualSegment = segmentParam ? segmentParam.treeSegment : segment
const metadata = StreamingMetadata ? <StreamingMetadata /> : undefined
// 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 =
isNotDefaultSegment && StreamingMetadata ? <StreamingMetadata /> : undefined
// Use the same condition to render metadataOutlet as metadata // Use the same condition to render metadataOutlet as metadata
const metadataOutlet = const metadataOutlet = StreamingMetadataOutlet ? (
isNotDefaultSegment && StreamingMetadataOutlet ? ( <StreamingMetadataOutlet />
<StreamingMetadataOutlet /> ) : undefined
) : undefined
const notFoundElement = NotFound ? ( const notFoundElement = NotFound ? (
<> <>
......
...@@ -23,7 +23,7 @@ async function SubComponent() { ...@@ -23,7 +23,7 @@ async function SubComponent() {
export async function generateMetadata() { export async function generateMetadata() {
await connection() await connection()
await new Promise((resolve) => setTimeout(resolve, 3000)) await new Promise((resolve) => setTimeout(resolve, 800))
return { return {
title: `Dynamic api ${Math.random()}`, title: `Dynamic api ${Math.random()}`,
} }
......
export default function Page() {
return 'default @bar'
}
export default function Layout({ children }) {
return (
<div>
<h2>@bar Layout</h2>
<div id="bar-children">{children}</div>
</div>
)
}
export default function Page() {
return 'page @bar'
}
export default function Page() {
return 'default @foo'
}
export default function Layout({ children }) {
return (
<div>
<h2>@foo Layout</h2>
<div id="foo-children">{children}</div>
</div>
)
}
export default function Page() {
return 'page @foo'
}
export default function Page() {
return 'default no-children'
}
import { connection } from 'next/server'
export default function Layout({ bar, foo, children }) {
return (
<div>
<h1>Parallel Routes Layout - No Children</h1>
<div id="foo-slot">{foo}</div>
<div id="bar-slot">{bar}</div>
{/* hitting default.js */}
{children}
</div>
)
}
export async function generateMetadata() {
await connection()
await new Promise((resolve) => setTimeout(resolve, 300))
return {
title: 'parallel-routes-default layout title',
}
}
import { connection } from 'next/server'
export default function TestPage() {
return 'test page'
}
export async function generateMetadata() {
await connection()
await new Promise((resolve) => setTimeout(resolve, 3000))
return {
title: `Dynamic api ${Math.random()}`,
}
}
import { connection } from 'next/server'
export default function TestPage() {
return 'test page'
}
export async function generateMetadata() {
await connection()
await new Promise((resolve) => setTimeout(resolve, 1000))
return {
title: `Dynamic api ${Math.random()}`,
}
}
import Link from 'next/link'
import { connection } from 'next/server' import { connection } from 'next/server'
export default function TestPage() { export default function Page() {
return 'test page' return (
<div>
<p>no bar</p>
<Link href="/parallel-routes">Back to /parallel-routes</Link>
</div>
)
} }
export async function generateMetadata() { export async function generateMetadata() {
await connection() await connection()
await new Promise((resolve) => setTimeout(resolve, 3000)) await new Promise((resolve) => setTimeout(resolve, 1000))
return { return {
title: `Dynamic api ${Math.random()}`, title: `Dynamic api ${Math.random()}`,
description: 'no-bar description',
} }
} }
...@@ -3,10 +3,11 @@ import Link from 'next/link' ...@@ -3,10 +3,11 @@ import Link from 'next/link'
export default function Page() { export default function Page() {
return ( return (
<div> <div>
Hello from Nested{' '} Hello from Nested <br />
<Link href="/parallel-routes/test-page"> <Link href="/parallel-routes/test-page">
To /parallel-routes/test-page To /parallel-routes/test-page
</Link> </Link>
<br />
<Link href="/parallel-routes/no-bar">To /parallel-routes/no-bar</Link> <Link href="/parallel-routes/no-bar">To /parallel-routes/no-bar</Link>
</div> </div>
) )
...@@ -14,4 +15,5 @@ export default function Page() { ...@@ -14,4 +15,5 @@ export default function Page() {
export const metadata = { export const metadata = {
title: 'parallel title', title: 'parallel title',
description: 'parallel description',
} }
...@@ -6,7 +6,7 @@ export default function TestPage() { ...@@ -6,7 +6,7 @@ export default function TestPage() {
export async function generateMetadata() { export async function generateMetadata() {
await connection() await connection()
await new Promise((resolve) => setTimeout(resolve, 3000)) await new Promise((resolve) => setTimeout(resolve, 1000))
return { return {
title: `Dynamic api ${Math.random()}`, title: `Dynamic api ${Math.random()}`,
} }
......
...@@ -93,9 +93,11 @@ describe('app-dir - metadata-streaming', () => { ...@@ -93,9 +93,11 @@ describe('app-dir - metadata-streaming', () => {
expect((await browser.elementsByCss('head title')).length).toBe(1) expect((await browser.elementsByCss('head title')).length).toBe(1)
expect((await browser.elementsByCss('body title')).length).toBe(0) expect((await browser.elementsByCss('body title')).length).toBe(0)
expect(await browser.elementByCss('title').text()).toBe('parallel title')
const $ = await next.render$('/parallel-routes') const $ = await next.render$('/parallel-routes')
expect($('title').length).toBe(1) expect($('title').length).toBe(1)
expect($('head title').text()).toBe('parallel title')
// validate behavior remains the same on client navigations // validate behavior remains the same on client navigations
await browser.elementByCss('[href="/parallel-routes/test-page"]').click() await browser.elementByCss('[href="/parallel-routes/test-page"]').click()
...@@ -113,13 +115,29 @@ describe('app-dir - metadata-streaming', () => { ...@@ -113,13 +115,29 @@ describe('app-dir - metadata-streaming', () => {
const browser = await next.browser('/parallel-routes') const browser = await next.browser('/parallel-routes')
await browser.elementByCss('[href="/parallel-routes/no-bar"]').click() await browser.elementByCss('[href="/parallel-routes/no-bar"]').click()
// Wait for navigation is finished and metadata is updated
await retry(async () => { await retry(async () => {
expect(await browser.elementByCss('title').text()).toContain( expect(await browser.elementByCss('title').text()).toContain(
'Dynamic api' 'Dynamic api'
) )
}) })
await retry(async () => {
expect((await browser.elementsByCss('title')).length).toBe(1)
})
})
it('should still render metadata if children is not rendered in parallel routes layout', async () => {
const browser = await next.browser('/parallel-routes-default')
expect((await browser.elementsByCss('title')).length).toBe(1) expect((await browser.elementsByCss('title')).length).toBe(1)
expect(await browser.elementByCss('body title').text()).toBe(
'parallel-routes-default layout title'
)
const $ = await next.render$('/parallel-routes-default')
expect($('title').length).toBe(1)
expect($('body title').text()).toBe('parallel-routes-default layout title')
}) })
describe('dynamic api', () => { describe('dynamic api', () => {
......
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