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

Shrink the react key size in metadata RSC payload #50739

Merged
merged 6 commits into from
Jun 6, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 11 additions & 18 deletions packages/next/src/lib/metadata/generate/basic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@ import { Meta, MetaFilter, MultiMeta } from './meta'

export function BasicMetadata({ metadata }: { metadata: ResolvedMetadata }) {
return MetaFilter([
<meta key="charset" charSet="utf-8" />,
<meta charSet="utf-8" />,
metadata.title !== null && metadata.title.absolute ? (
<title key="title">{metadata.title.absolute}</title>
<title>{metadata.title.absolute}</title>
) : null,
Meta({ name: 'description', content: metadata.description }),
Meta({ name: 'application-name', content: metadata.applicationName }),
...(metadata.authors
? metadata.authors.map((author, index) => [
? metadata.authors.map((author) => [
author.url ? (
<link
key={'author' + index}
rel="author"
href={author.url.toString()}
/>
<link rel="author" href={author.url.toString()} />
) : null,
Meta({ name: 'author', content: author.name }),
])
: []),
metadata.manifest ? (
<link key="manifest" rel="manifest" href={metadata.manifest.toString()} />
<link rel="manifest" href={metadata.manifest.toString()} />
) : null,
Meta({ name: 'generator', content: metadata.generator }),
Meta({ name: 'keywords', content: metadata.keywords?.join(',') }),
Expand All @@ -47,17 +43,15 @@ export function BasicMetadata({ metadata }: { metadata: ResolvedMetadata }) {
Meta({ name: 'abstract', content: metadata.abstract }),
...(metadata.archives
? metadata.archives.map((archive) => (
<link rel="archives" href={archive} key={archive} />
<link rel="archives" href={archive} />
))
: []),
...(metadata.assets
? metadata.assets.map((asset) => (
<link rel="assets" href={asset} key={asset} />
))
? metadata.assets.map((asset) => <link rel="assets" href={asset} />)
: []),
...(metadata.bookmarks
? metadata.bookmarks.map((bookmark) => (
<link rel="bookmarks" href={bookmark} key={bookmark} />
<link rel="bookmarks" href={bookmark} />
))
: []),
Meta({ name: 'category', content: metadata.category }),
Expand Down Expand Up @@ -120,16 +114,15 @@ export function AppleWebAppMeta({
? Meta({ name: 'apple-mobile-web-app-capable', content: 'yes' })
: null,
Meta({ name: 'apple-mobile-web-app-title', content: title }),
...(startupImage
? startupImage.map((image, index) => (
startupImage
? startupImage.map((image) => (
<link
key={index}
href={image.url}
media={image.media}
rel="apple-touch-startup-image"
/>
))
: []),
: null,
statusBarStyle
? Meta({
name: 'apple-mobile-web-app-status-bar-style',
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/metadata/generate/icons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function IconDescriptorLink({ icon }: { icon: IconDescriptor }) {
function IconLink({ rel, icon }: { rel?: string; icon: Icon }) {
if (typeof icon === 'object' && !(icon instanceof URL)) {
if (!icon.rel && rel) icon.rel = rel
return <IconDescriptorLink icon={icon} />
return IconDescriptorLink({ icon })
} else {
const href = icon.toString()
return <link rel={rel} href={href} />
Expand Down
32 changes: 17 additions & 15 deletions packages/next/src/lib/metadata/generate/meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export function Meta({
if (typeof content !== 'undefined' && content !== null && content !== '') {
return (
<meta
key={(name || property) + ':' + content}
{...(name ? { name } : { property })}
{...(media ? { media } : undefined)}
content={typeof content === 'string' ? content : content.toString()}
Expand All @@ -28,10 +27,15 @@ export function Meta({
export function MetaFilter<T extends {} | {}[]>(
items: (T | null)[]
): NonNullable<T>[] {
return items.filter(
(item): item is NonNullable<T> =>
nonNullable(item) && !(Array.isArray(item) && item.length === 0)
)
const acc: NonNullable<T>[] = []
for (const item of items) {
if (Array.isArray(item)) {
acc.push(...item.filter(nonNullable))
} else if (nonNullable(item)) {
acc.push(item)
}
}
return acc
}

type ExtendMetaContent = Record<
Expand Down Expand Up @@ -70,18 +74,16 @@ function ExtendMeta({
namePrefix?: string
propertyPrefix?: string
}) {
const keyPrefix = namePrefix || propertyPrefix
if (!content) return null
return MetaFilter(
Object.entries(content).map(([k, v], index) => {
return typeof v === 'undefined' ? null : (
<Meta
key={keyPrefix + ':' + k + '_' + index}
{...(propertyPrefix && { property: getMetaKey(propertyPrefix, k) })}
{...(namePrefix && { name: getMetaKey(namePrefix, k) })}
content={typeof v === 'string' ? v : v?.toString()}
/>
)
Object.entries(content).map(([k, v]) => {
return typeof v === 'undefined'
? null
: Meta({
...(propertyPrefix && { property: getMetaKey(propertyPrefix, k) }),
...(namePrefix && { name: getMetaKey(namePrefix, k) }),
content: typeof v === 'string' ? v : v?.toString(),
})
})
)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/next/src/lib/metadata/generate/opengraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,14 @@ function TwitterAppItem({
app: TwitterAppDescriptor
type: 'iphone' | 'ipad' | 'googleplay'
}) {
return MetaFilter([
return [
Meta({ name: `twitter:app:name:${type}`, content: app.name }),
Meta({ name: `twitter:app:id:${type}`, content: app.id[type] }),
Meta({
name: `twitter:app:url:${type}`,
content: app.url?.[type]?.toString(),
}),
])
]
}

export function TwitterMetadata({
Expand Down
9 changes: 7 additions & 2 deletions packages/next/src/lib/metadata/metadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ export async function MetadataTree({
IconsMetadata({ icons: metadata.icons }),
])

// Wrap with fragment and key to skip react array-key warnings.
return <>{elements}</>
return (
<>
{elements.map((el, index) => {
return React.cloneElement(el as React.ReactElement, { key: index })
})}
</>
)
}