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

Auto fill title/images/description from openGraph for twitter if missing #51453

Merged
merged 5 commits into from
Jun 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 43 additions & 5 deletions packages/next/src/lib/metadata/resolve-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ describe('accumulateMetadata', () => {
const items: [Metadata[], Metadata][] = [
[
[{ openGraph: { type: 'article', images: 'https://test1.com' } }],
{ openGraph: { images: [{ url: 'https://test1.com' }] } },
{ openGraph: { images: [{ url: new URL('https://test1.com') }] } },
],
[
[{ openGraph: { type: 'book', images: 'https://test2.com' } }],
{ openGraph: { images: [{ url: 'https://test2.com' }] } },
{ openGraph: { images: [{ url: new URL('https://test2.com/') }] } },
],
[
[
Expand All @@ -90,7 +90,7 @@ describe('accumulateMetadata', () => {
},
},
],
{ openGraph: { images: [{ url: 'https://test4.com' }] } },
{ openGraph: { images: [{ url: new URL('https://test4.com') }] } },
],
[
[
Expand All @@ -101,11 +101,11 @@ describe('accumulateMetadata', () => {
},
},
],
{ openGraph: { images: [{ url: 'https://test5.com' }] } },
{ openGraph: { images: [{ url: new URL('https://test5.com') }] } },
],
[
[{ openGraph: { type: 'video.movie', images: 'https://test6.com' } }],
{ openGraph: { images: [{ url: 'https://test6.com' }] } },
{ openGraph: { images: [{ url: new URL('https://test6.com') }] } },
],
]

Expand All @@ -117,6 +117,44 @@ describe('accumulateMetadata', () => {
expect(metadata).toMatchObject(result)
})
})

it('should fill twitter with partial existing openGraph metadata', async () => {
const metadataItems: MetadataItems = [
[
{
openGraph: {
title: 'title',
description: 'description',
images: 'https://test.com',
},
twitter: {
card: 'summary_large_image',
},
},
null,
],
]
const metadata = await accumulateMetadata(metadataItems)
expect(metadata).toMatchObject({
openGraph: {
title: {
absolute: 'title',
template: null,
},
description: 'description',
images: [{ url: new URL('https://test.com') }],
},
twitter: {
card: 'summary_large_image',
title: {
absolute: 'title',
template: null,
},
description: 'description',
images: [{ url: new URL('https://test.com') }],
},
})
})
})

describe('themeColor', () => {
Expand Down
36 changes: 30 additions & 6 deletions packages/next/src/lib/metadata/resolve-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,15 +358,39 @@ export async function resolveMetadata({
return metadataItems
}

const commonOgKeys = ['title', 'description', 'images'] as const
function postProcessMetadata(metadata: ResolvedMetadata): ResolvedMetadata {
const { openGraph, twitter } = metadata
if (openGraph && !twitter) {
const overlappedProps = {
title: openGraph.title,
description: openGraph.description,
images: openGraph.images,
if (openGraph) {
let autoFillProps: Partial<{
[Key in (typeof commonOgKeys)[number]]: NonNullable<
ResolvedMetadata['openGraph']
>[Key]
}> = {}
const hasTwTitle = twitter?.title.absolute
const hasTwDescription = twitter?.description
const hasTwImages = twitter?.images
if (!hasTwTitle) autoFillProps.title = openGraph.title
if (!hasTwDescription) autoFillProps.description = openGraph.description
if (!hasTwImages) autoFillProps.images = openGraph.images

if (Object.keys(autoFillProps).length > 0) {
const partialTwitter = resolveTwitter(
autoFillProps,
metadata.metadataBase
)
if (metadata.twitter) {
metadata.twitter = Object.assign({}, metadata.twitter, {
...(!hasTwTitle && { title: partialTwitter?.title }),
...(!hasTwDescription && {
description: partialTwitter?.description,
}),
...(!hasTwImages && { images: partialTwitter?.images }),
})
} else {
metadata.twitter = partialTwitter
}
}
metadata.twitter = resolveTwitter(overlappedProps, metadata.metadataBase)
}
return metadata
}
Expand Down