Unverified Commit 747d3650 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

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 
parent 196dab66
...@@ -319,13 +319,11 @@ async function generateFlight( ...@@ -319,13 +319,11 @@ async function generateFlight(
flightRouterState, flightRouterState,
isFirst: true, isFirst: true,
// For flight, render metadata inside leaf page // For flight, render metadata inside leaf page
rscPayloadHead: ( // NOTE: in 14.2, fragment doesn't work well with React, using array instead
<> rscPayloadHead: [
<NonIndex ctx={ctx} /> <MetadataTree key={requestId} />,
{/* Adding requestId as react key to make metadata remount for each render */} <NonIndex key="noindex" ctx={ctx} />,
<MetadataTree key={requestId} /> ],
</>
),
injectedCSS: new Set(), injectedCSS: new Set(),
injectedJS: new Set(), injectedJS: new Set(),
injectedFontPreloadTags: new Set(), injectedFontPreloadTags: new Set(),
...@@ -531,12 +529,12 @@ async function ReactServerError({ ...@@ -531,12 +529,12 @@ async function ReactServerError({
const head = ( const head = (
<> <>
<NonIndex ctx={ctx} />
{/* Adding requestId as react key to make metadata remount for each render */} {/* Adding requestId as react key to make metadata remount for each render */}
<MetadataTree key={requestId} /> <MetadataTree key={requestId} />
{process.env.NODE_ENV === 'development' && ( {process.env.NODE_ENV === 'development' && (
<meta name="next-error" content="not-found" /> <meta name="next-error" content="not-found" />
)} )}
<NonIndex ctx={ctx} />
</> </>
) )
......
import { Metadata } from 'next'
export const metadata: Metadata = {
title: 'Product Layout',
}
export default function Layout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return <>{children}</>
}
export default function Page() {
return <div>Product page content</div>
}
type LayoutProps = {
children: React.ReactNode
}
export default function Layout({ children }: LayoutProps) {
return <div>{children}</div>
}
import Link from 'next/link'
export default function Page() {
return (
<div>
<div>Home header</div>
<Link href="/product" id="product-link">
Product
</Link>
</div>
)
}
type LayoutProps = {
children: React.ReactNode
}
export default function Layout({ children }: LayoutProps) {
return <>{children}</>
}
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1 id="product-title">Product header</h1>
<Link href="/" id="home-link">
Go to Home page
</Link>
</div>
)
}
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: 'Home Layout',
description: 'Generated by create next app',
}
export default function RootLayout({
header,
children,
}: Readonly<{
header: React.ReactNode
children: React.ReactNode
}>) {
return (
<html lang="en">
<body className={`antialiased bg-gray-50 text-black`}>
{header}
{children}
</body>
</html>
)
}
export default function Home() {
return (
<div>
<h3 id="home-title">Home page content</h3>
</div>
)
}
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app-dir - metadata-navigation',
{
files: __dirname,
},
({ next }) => {
it('should show the index title', async () => {
const browser = await next.browser('/')
expect(await browser.elementByCss('title').text()).toBe('Home Layout')
})
it('should show target page metadata after navigation', async () => {
const browser = await next.browser('/')
await browser.elementByCss('#product-link').click()
await browser.waitForElementByCss('#product-title')
expect(await browser.elementByCss('title').text()).toBe('Product Layout')
})
it('should show target page metadata after navigation with back', async () => {
const browser = await next.browser('/')
await browser.elementByCss('#product-link').click()
await browser.waitForElementByCss('#product-title')
await browser.elementByCss('#home-link').click()
await browser.waitForElementByCss('#home-title')
expect(await browser.elementByCss('title').text()).toBe('Home Layout')
})
}
)
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