Commit 26ddcdf2 authored by Jiachi Liu's avatar Jiachi Liu Committed by Janka Uryga

Always collect static icons for all segments (#68712)

### What

Collect static metadata icons from the most leaf node in the component
tree, and then add them into resolved metadata icons if there's no icons
presented in exported metadata

### Why

Previously we collected the icons from the last item from collected
metadata items as last segment in the tree. But it doesn't act like that
when there's parallel routes, so we collect the last presented static
metadata icons from the metadata item list and then merge into the
resolved metadata at the end.

Fixes #68650
parent ce309b4c
...@@ -16,6 +16,7 @@ import type { LoaderTree } from '../../server/lib/app-dir-module' ...@@ -16,6 +16,7 @@ import type { LoaderTree } from '../../server/lib/app-dir-module'
import type { import type {
AbsoluteTemplateString, AbsoluteTemplateString,
IconDescriptor, IconDescriptor,
ResolvedIcons,
} from './types/metadata-types' } from './types/metadata-types'
import type { ParsedUrlQuery } from 'querystring' import type { ParsedUrlQuery } from 'querystring'
import type { StaticMetadata } from './types/icons' import type { StaticMetadata } from './types/icons'
...@@ -48,6 +49,8 @@ import { ResolveMetadataSpan } from '../../server/lib/trace/constants' ...@@ -48,6 +49,8 @@ import { ResolveMetadataSpan } from '../../server/lib/trace/constants'
import { PAGE_SEGMENT_KEY } from '../../shared/lib/segment' import { PAGE_SEGMENT_KEY } from '../../shared/lib/segment'
import * as Log from '../../build/output/log' import * as Log from '../../build/output/log'
type StaticIcons = Pick<ResolvedIcons, 'icon' | 'apple'>
type MetadataResolver = ( type MetadataResolver = (
parent: ResolvingMetadata parent: ResolvingMetadata
) => Metadata | Promise<Metadata> ) => Metadata | Promise<Metadata>
...@@ -98,27 +101,18 @@ function mergeStaticMetadata( ...@@ -98,27 +101,18 @@ function mergeStaticMetadata(
staticFilesMetadata: StaticMetadata, staticFilesMetadata: StaticMetadata,
metadataContext: MetadataContext, metadataContext: MetadataContext,
titleTemplates: TitleTemplates, titleTemplates: TitleTemplates,
isLastSegment: boolean leafSegmentStaticIcons: StaticIcons
) { ) {
if (!staticFilesMetadata) return if (!staticFilesMetadata) return
const { icon, apple, openGraph, twitter, manifest } = staticFilesMetadata const { icon, apple, openGraph, twitter, manifest } = staticFilesMetadata
// Only pick up the static metadata if the current level is the last segment // Keep updating the static icons in the most leaf node
if (isLastSegment) {
// file based metadata is specified and current level metadata icons is not specified if (icon) {
if (target.icons) { leafSegmentStaticIcons.icon = icon
if (icon) { }
target.icons.icon.unshift(...icon) if (apple) {
} leafSegmentStaticIcons.apple = apple
if (apple) {
target.icons.apple.unshift(...apple)
}
} else if (icon || apple) {
target.icons = {
icon: icon || [],
apple: apple || [],
}
}
} }
// file based metadata is specified and current level metadata twitter.images is not specified // file based metadata is specified and current level metadata twitter.images is not specified
...@@ -157,7 +151,7 @@ function mergeMetadata({ ...@@ -157,7 +151,7 @@ function mergeMetadata({
titleTemplates, titleTemplates,
metadataContext, metadataContext,
buildState, buildState,
isLastSegment, leafSegmentStaticIcons,
}: { }: {
source: Metadata | null source: Metadata | null
target: ResolvedMetadata target: ResolvedMetadata
...@@ -165,7 +159,7 @@ function mergeMetadata({ ...@@ -165,7 +159,7 @@ function mergeMetadata({
titleTemplates: TitleTemplates titleTemplates: TitleTemplates
metadataContext: MetadataContext metadataContext: MetadataContext
buildState: BuildState buildState: BuildState
isLastSegment: boolean leafSegmentStaticIcons: StaticIcons
}): void { }): void {
// If there's override metadata, prefer it otherwise fallback to the default metadata. // If there's override metadata, prefer it otherwise fallback to the default metadata.
const metadataBase = const metadataBase =
...@@ -285,7 +279,7 @@ function mergeMetadata({ ...@@ -285,7 +279,7 @@ function mergeMetadata({
staticFilesMetadata, staticFilesMetadata,
metadataContext, metadataContext,
titleTemplates, titleTemplates,
isLastSegment leafSegmentStaticIcons
) )
} }
...@@ -769,6 +763,13 @@ export async function accumulateMetadata( ...@@ -769,6 +763,13 @@ export async function accumulateMetadata(
} }
let favicon let favicon
// Collect the static icons in the most leaf node,
// since we don't collect all the static metadata icons in the parent segments.
const leafSegmentStaticIcons = {
icon: [],
apple: [],
}
for (let i = 0; i < metadataItems.length; i++) { for (let i = 0; i < metadataItems.length; i++) {
const staticFilesMetadata = metadataItems[i][1] const staticFilesMetadata = metadataItems[i][1]
...@@ -795,7 +796,7 @@ export async function accumulateMetadata( ...@@ -795,7 +796,7 @@ export async function accumulateMetadata(
staticFilesMetadata, staticFilesMetadata,
titleTemplates, titleTemplates,
buildState, buildState,
isLastSegment: i === metadataItems.length - 1, leafSegmentStaticIcons,
}) })
// If the layout is the same layer with page, skip the leaf layout and leaf page // If the layout is the same layer with page, skip the leaf layout and leaf page
...@@ -809,6 +810,24 @@ export async function accumulateMetadata( ...@@ -809,6 +810,24 @@ export async function accumulateMetadata(
} }
} }
if (
leafSegmentStaticIcons.icon.length > 0 ||
leafSegmentStaticIcons.apple.length > 0
) {
if (!resolvedMetadata.icons) {
resolvedMetadata.icons = {
icon: [],
apple: [],
}
if (leafSegmentStaticIcons.icon.length > 0) {
resolvedMetadata.icons.icon.unshift(...leafSegmentStaticIcons.icon)
}
if (leafSegmentStaticIcons.apple.length > 0) {
resolvedMetadata.icons.apple.unshift(...leafSegmentStaticIcons.apple)
}
}
}
// Only log warnings if there are any, and only once after the metadata resolving process is finished // Only log warnings if there are any, and only once after the metadata resolving process is finished
if (buildState.warnings.size > 0) { if (buildState.warnings.size > 0) {
for (const warning of buildState.warnings) { for (const warning of buildState.warnings) {
......
export default function Modal() {
return <p>modal</p>
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<!-- Created with Vectornator (http://vectornator.io/) -->
<svg height="100%" stroke-miterlimit="10" style="fill-rule:nonzero;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;" version="1.1" viewBox="0 0 48 48" width="100%" xml:space="preserve" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<defs/>
<g id="Background">
<path d="M9.6 0L38.4 0C43.7019 0 48 4.29807 48 9.6L48 38.4C48 43.7019 43.7019 48 38.4 48L9.6 48C4.29807 48 0 43.7019 0 38.4L0 9.6C0 4.29807 4.29807 0 9.6 0Z" fill="#ffffff" fill-rule="nonzero" opacity="1" stroke="none"/>
</g>
<g id="Icon">
<path d="M19.2 24C16.549 24 14.4 26.149 14.4 28.8C14.4 31.451 16.549 33.6 19.2 33.6C21.851 33.6 24 31.451 24 28.8C24 26.149 21.851 24 19.2 24Z" fill="#000000" fill-rule="nonzero" opacity="1" stroke="none"/>
<path d="M24 4.8C13.44 4.8 4.8 13.3978 4.8 24.096C4.8 34.7942 13.44 43.2 24 43.2C34.56 43.2 43.2 34.6022 43.2 24.096C43.2 13.5898 34.752 4.8 24 4.8ZM22.464 39.3792C14.784 38.615 8.64 32.1197 8.64 24.096C8.64 16.0723 14.592 9.76704 22.464 8.8128C24.576 9.95904 26.496 11.4874 28.032 13.3978C28.8 13.0157 29.76 12.6336 30.72 12.6336C33.408 12.6336 35.52 14.736 35.52 17.4106C35.52 19.703 33.792 21.8054 31.488 22.1875C31.488 22.7616 31.68 23.5258 31.68 24.0979C31.68 30.7814 27.84 36.5126 22.464 39.3792ZM35.52 33.648C34.368 33.648 33.6 32.8838 33.6 31.7376C33.6 30.5914 34.368 29.8272 35.52 29.8272C36.672 29.8272 37.44 30.5914 37.44 31.7376C37.44 32.8838 36.672 33.648 35.52 33.648Z" fill="#000000" fill-rule="nonzero" opacity="1" stroke="none"/>
<path d="M15.36 14.4C13.7694 14.4 12.48 15.6894 12.48 17.28C12.48 18.8706 13.7694 20.16 15.36 20.16C16.9506 20.16 18.24 18.8706 18.24 17.28C18.24 15.6894 16.9506 14.4 15.36 14.4Z" fill="#000000" fill-rule="nonzero" opacity="1" stroke="none"/>
</g>
</svg>
export default function Root({ children, modal }) {
return (
<html>
<body>
{modal}
{children}
</body>
</html>
)
}
export default function Page() {
return <p>page</p>
}
export default function Page() {
return <p>page</p>
}
import { nextTestSetup } from 'e2e-utils'
describe('app-dir - metadata-icons-parallel-routes', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should present favicon with other icons when parallel routes are presented', async () => {
const $ = await next.render$('/')
expect($('link[type="image/x-icon"]').length).toBe(1)
expect($('link[type="image/svg+xml"]').length).toBe(1)
expect($('link[rel="apple-touch-icon"]').length).toBe(1)
})
it('should override parent icon when both static icon presented', async () => {
const $ = await next.render$('/nested')
expect($('link[type="image/x-icon"]').length).toBe(1)
expect($('link[rel="icon"][type="image/png"]').length).toBe(1)
})
it('should inherit parent apple icon when child does not present but parent contain static apple icon', async () => {
const $ = await next.render$('/nested')
expect($('link[rel="apple-touch-icon"][type="image/png"]').length).toBe(1)
})
})
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