Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix metadata prop merging #69807

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,11 @@ async function generateFlight(
flightRouterState,
isFirst: true,
// For flight, render metadata inside leaf page
rscPayloadHead: (
<>
<NonIndex ctx={ctx} />
{/* Adding requestId as react key to make metadata remount for each render */}
<MetadataTree key={requestId} />
</>
),
// NOTE: in 14.2, fragment doesn't work well with React, using array instead
rscPayloadHead: [
<MetadataTree key={requestId} />,
<NonIndex key="noindex" ctx={ctx} />,
],
injectedCSS: new Set(),
injectedJS: new Set(),
injectedFontPreloadTags: new Set(),
Expand Down Expand Up @@ -531,12 +529,12 @@ async function ReactServerError({

const head = (
<>
<NonIndex ctx={ctx} />
{/* Adding requestId as react key to make metadata remount for each render */}
<MetadataTree key={requestId} />
{process.env.NODE_ENV === 'development' && (
<meta name="next-error" content="not-found" />
)}
<NonIndex ctx={ctx} />
</>
)

Expand Down
13 changes: 13 additions & 0 deletions test/e2e/app-dir/metadata-navigation/app/(cart)/product/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Metadata } from 'next'

export const metadata: Metadata = {
title: 'Product Layout',
}

export default function Layout({
children,
}: Readonly<{
children: React.ReactNode
}>) {
return <>{children}</>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function Page() {
return <div>Product page content</div>
}
7 changes: 7 additions & 0 deletions test/e2e/app-dir/metadata-navigation/app/@header/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type LayoutProps = {
children: React.ReactNode
}

export default function Layout({ children }: LayoutProps) {
return <div>{children}</div>
}
12 changes: 12 additions & 0 deletions test/e2e/app-dir/metadata-navigation/app/@header/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Link from 'next/link'

export default function Page() {
return (
<div>
<div>Home header</div>
<Link href="/product" id="product-link">
Product
</Link>
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type LayoutProps = {
children: React.ReactNode
}

export default function Layout({ children }: LayoutProps) {
return <>{children}</>
}
12 changes: 12 additions & 0 deletions test/e2e/app-dir/metadata-navigation/app/@header/product/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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>
)
}
23 changes: 23 additions & 0 deletions test/e2e/app-dir/metadata-navigation/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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>
)
}
7 changes: 7 additions & 0 deletions test/e2e/app-dir/metadata-navigation/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function Home() {
return (
<div>
<h3 id="home-title">Home page content</h3>
</div>
)
}
30 changes: 30 additions & 0 deletions test/e2e/app-dir/metadata-navigation/metadata-navigation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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')
})
}
)
Loading