Skip to content

Commit 4ce41c6

Browse files
committed
fix: ignore metadata from default.js
1 parent f998bdb commit 4ce41c6

File tree

17 files changed

+138
-1
lines changed

17 files changed

+138
-1
lines changed

packages/next/src/lib/metadata/resolve-metadata.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import {
5151
import { resolveIcons } from './resolvers/resolve-icons'
5252
import { getTracer } from '../../server/lib/trace/tracer'
5353
import { ResolveMetadataSpan } from '../../server/lib/trace/constants'
54-
import { PAGE_SEGMENT_KEY } from '../../shared/lib/segment'
54+
import { DEFAULT_SEGMENT_KEY, PAGE_SEGMENT_KEY } from '../../shared/lib/segment'
5555
import * as Log from '../../build/output/log'
5656
import type { WorkStore } from '../../server/app-render/work-async-storage.external'
5757
import type {
@@ -443,6 +443,10 @@ async function collectMetadata({
443443
const hasErrorConventionComponent = Boolean(
444444
errorConvention && tree[2][errorConvention]
445445
)
446+
// If it's default.js, ignore the metadata
447+
if (tree[0] === DEFAULT_SEGMENT_KEY) {
448+
return
449+
}
446450
if (errorConvention) {
447451
mod = await getComponentTypeModule(tree, 'layout')
448452
modType = errorConvention
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Link from 'next/link'
2+
import React from 'react'
3+
4+
export default function Root({ children }: { children: React.ReactNode }) {
5+
return (
6+
<html>
7+
<body>
8+
<div>
9+
<Link href="/nested">to nested</Link>
10+
</div>
11+
<div>
12+
<Link href="/nested/subroute">to nested subroute</Link>
13+
</div>
14+
<h1>Root Layout</h1>
15+
<div>{children}</div>
16+
</body>
17+
</html>
18+
)
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Default() {
2+
return '@bar default'
3+
}
4+
5+
export const metadata = {
6+
title: '@bar - page',
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function Layout({ children }) {
2+
return (
3+
<div>
4+
<h1>@bar Layout</h1>
5+
<div id="bar-children">{children}</div>
6+
</div>
7+
)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Page() {
2+
return <div>Bar Slot</div>
3+
}
4+
5+
export const metadata = {
6+
title: '@bar - page',
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Page() {
2+
return <div>Subroute</div>
3+
}
4+
5+
export const metadata = {
6+
title: 'subroute - page',
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Default() {
2+
return '@foo default'
3+
}
4+
5+
export const metadata = {
6+
title: '@foo - default',
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function Layout({ children }) {
2+
return (
3+
<div>
4+
<h1>@foo Layout</h1>
5+
<div id="foo-children">{children}</div>
6+
</div>
7+
)
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Page() {
2+
return <div>Foo Slot</div>
3+
}
4+
5+
export const metadata = {
6+
title: '@foo - page',
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Page() {
2+
return 'default page'
3+
}
4+
5+
export const metadata = {
6+
title: 'nested - default',
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export default function Layout({ children, bar, foo }) {
2+
return (
3+
<div>
4+
<h1>Nested Layout</h1>
5+
<div id="nested-children">{children}</div>
6+
<div id="foo-slot">{foo}</div>
7+
<div id="bar-slot">{bar}</div>
8+
</div>
9+
)
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export default function Page() {
2+
return <div>Hello from Nested</div>
3+
}
4+
5+
export const metadata = {
6+
title: 'nested - page',
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export default async function Home() {
2+
return <div>Hello World</div>
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('metadata-parallel-routes', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
})
7+
8+
it('should ignore metadata from default.js in normal route', async () => {
9+
const $ = await next.render$('/')
10+
// When there's no title, parallel routes should not affect it
11+
expect($('title').length).toBe(0)
12+
13+
// When there's defined title, parallel routes should not affect it
14+
const $nested = await next.render$('/nested')
15+
expect($nested('title').text()).toBe('nested - page')
16+
})
17+
18+
it('should ignore metadata from default.js in parallel routes', async () => {
19+
const $ = await next.render$('/nested/subroute')
20+
expect($('title').text()).toBe('subroute - page')
21+
})
22+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* @type {import('next').NextConfig}
3+
*/
4+
const nextConfig = {}
5+
6+
module.exports = nextConfig
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export default function Page() {
22
return <div>Bar Slot</div>
33
}
4+
5+
export const metadata = {
6+
title: 'nested - page',
7+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
export default function Page() {
22
return <div>Subroute</div>
33
}
4+
5+
export const metadata = {
6+
title: 'subroute - page',
7+
}

0 commit comments

Comments
 (0)