Skip to content

Commit 747d365

Browse files
authored
Fix metadata prop merging (#69807)
### What Fixes metadata merging issue on 14.2. There's a bug with React on 14.2 as it bit outdated: while generating rsc payload, if we use fragment to wrap the metadata and noindex component, it will miss all the data. It works well with separating them as an array. It also requires #64058 to be applied as well so the output not-found page will having correct status 404, and then we can generate noindex metatag based on correct status Fixes #69778
1 parent 196dab6 commit 747d365

File tree

10 files changed

+120
-8
lines changed

10 files changed

+120
-8
lines changed

packages/next/src/server/app-render/app-render.tsx

+6-8
Original file line numberDiff line numberDiff line change
@@ -319,13 +319,11 @@ async function generateFlight(
319319
flightRouterState,
320320
isFirst: true,
321321
// For flight, render metadata inside leaf page
322-
rscPayloadHead: (
323-
<>
324-
<NonIndex ctx={ctx} />
325-
{/* Adding requestId as react key to make metadata remount for each render */}
326-
<MetadataTree key={requestId} />
327-
</>
328-
),
322+
// NOTE: in 14.2, fragment doesn't work well with React, using array instead
323+
rscPayloadHead: [
324+
<MetadataTree key={requestId} />,
325+
<NonIndex key="noindex" ctx={ctx} />,
326+
],
329327
injectedCSS: new Set(),
330328
injectedJS: new Set(),
331329
injectedFontPreloadTags: new Set(),
@@ -531,12 +529,12 @@ async function ReactServerError({
531529

532530
const head = (
533531
<>
534-
<NonIndex ctx={ctx} />
535532
{/* Adding requestId as react key to make metadata remount for each render */}
536533
<MetadataTree key={requestId} />
537534
{process.env.NODE_ENV === 'development' && (
538535
<meta name="next-error" content="not-found" />
539536
)}
537+
<NonIndex ctx={ctx} />
540538
</>
541539
)
542540

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Metadata } from 'next'
2+
3+
export const metadata: Metadata = {
4+
title: 'Product Layout',
5+
}
6+
7+
export default function Layout({
8+
children,
9+
}: Readonly<{
10+
children: React.ReactNode
11+
}>) {
12+
return <>{children}</>
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default function Page() {
2+
return <div>Product page content</div>
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type LayoutProps = {
2+
children: React.ReactNode
3+
}
4+
5+
export default function Layout({ children }: LayoutProps) {
6+
return <div>{children}</div>
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Link from 'next/link'
2+
3+
export default function Page() {
4+
return (
5+
<div>
6+
<div>Home header</div>
7+
<Link href="/product" id="product-link">
8+
Product
9+
</Link>
10+
</div>
11+
)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
type LayoutProps = {
2+
children: React.ReactNode
3+
}
4+
5+
export default function Layout({ children }: LayoutProps) {
6+
return <>{children}</>
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Link from 'next/link'
2+
3+
export default function Page() {
4+
return (
5+
<div>
6+
<h1 id="product-title">Product header</h1>
7+
<Link href="/" id="home-link">
8+
Go to Home page
9+
</Link>
10+
</div>
11+
)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import type { Metadata } from 'next'
2+
3+
export const metadata: Metadata = {
4+
title: 'Home Layout',
5+
description: 'Generated by create next app',
6+
}
7+
8+
export default function RootLayout({
9+
header,
10+
children,
11+
}: Readonly<{
12+
header: React.ReactNode
13+
children: React.ReactNode
14+
}>) {
15+
return (
16+
<html lang="en">
17+
<body className={`antialiased bg-gray-50 text-black`}>
18+
{header}
19+
{children}
20+
</body>
21+
</html>
22+
)
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Home() {
2+
return (
3+
<div>
4+
<h3 id="home-title">Home page content</h3>
5+
</div>
6+
)
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { createNextDescribe } from 'e2e-utils'
2+
3+
createNextDescribe(
4+
'app-dir - metadata-navigation',
5+
{
6+
files: __dirname,
7+
},
8+
({ next }) => {
9+
it('should show the index title', async () => {
10+
const browser = await next.browser('/')
11+
expect(await browser.elementByCss('title').text()).toBe('Home Layout')
12+
})
13+
14+
it('should show target page metadata after navigation', async () => {
15+
const browser = await next.browser('/')
16+
await browser.elementByCss('#product-link').click()
17+
await browser.waitForElementByCss('#product-title')
18+
expect(await browser.elementByCss('title').text()).toBe('Product Layout')
19+
})
20+
21+
it('should show target page metadata after navigation with back', async () => {
22+
const browser = await next.browser('/')
23+
await browser.elementByCss('#product-link').click()
24+
await browser.waitForElementByCss('#product-title')
25+
await browser.elementByCss('#home-link').click()
26+
await browser.waitForElementByCss('#home-title')
27+
expect(await browser.elementByCss('title').text()).toBe('Home Layout')
28+
})
29+
}
30+
)

0 commit comments

Comments
 (0)