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

metadata: prefer og title rather than metadata title for fallback twitter title #64331

Merged
merged 1 commit into from
Apr 11, 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
36 changes: 36 additions & 0 deletions packages/next/src/lib/metadata/resolve-metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,42 @@ describe('accumulateMetadata', () => {
})
})

it('should prefer title and description from openGraph rather than metadata for twitter', async () => {
const metadataItems: MetadataItems = [
[
{
title: 'doc title',
openGraph: {
title: 'og-title',
description: 'og-description',
images: 'https://test.com',
},
},
null,
],
]
const metadata = await accumulateMetadata(metadataItems)
expect(metadata).toMatchObject({
openGraph: {
title: {
absolute: 'og-title',
template: null,
},
description: 'og-description',
images: [{ url: new URL('https://test.com') }],
},
twitter: {
card: 'summary_large_image',
title: {
absolute: 'og-title',
template: null,
},
description: 'og-description',
images: [{ url: new URL('https://test.com') }],
},
})
})

it('should fill only the existing props from openGraph to twitter', async () => {
const metadataItems: MetadataItems = [
[
Expand Down
31 changes: 21 additions & 10 deletions packages/next/src/lib/metadata/resolve-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,13 @@ export async function resolveMetadataItems({
type WithTitle = { title?: AbsoluteTemplateString | null }
type WithDescription = { description?: string | null }

const hasTitle = (metadata: WithTitle | null) => !!metadata?.title?.absolute
const isTitleTruthy = (title: AbsoluteTemplateString | null | undefined) =>
!!title?.absolute
const hasTitle = (metadata: WithTitle | null) => isTitleTruthy(metadata?.title)

function inheritFromMetadata(
metadata: ResolvedMetadata,
target: (WithTitle & WithDescription) | null
target: (WithTitle & WithDescription) | null,
metadata: ResolvedMetadata
) {
if (target) {
if (!hasTitle(target) && hasTitle(metadata)) {
Expand All @@ -568,11 +570,6 @@ function postProcessMetadata(
): ResolvedMetadata {
const { openGraph, twitter } = metadata

// If there's no title and description configured in openGraph or twitter,
// use the title and description from metadata.
inheritFromMetadata(metadata, openGraph)
inheritFromMetadata(metadata, twitter)

if (openGraph) {
// If there's openGraph information but not configured in twitter,
// inherit them from openGraph metadata.
Expand All @@ -586,8 +583,16 @@ function postProcessMetadata(
const hasTwImages = Boolean(
twitter?.hasOwnProperty('images') && twitter.images
)
if (!hasTwTitle) autoFillProps.title = openGraph.title
if (!hasTwDescription) autoFillProps.description = openGraph.description
if (!hasTwTitle) {
if (isTitleTruthy(openGraph.title)) {
autoFillProps.title = openGraph.title
} else if (metadata.title && isTitleTruthy(metadata.title)) {
autoFillProps.title = metadata.title
}
}
if (!hasTwDescription)
autoFillProps.description =
openGraph.description || metadata.description || undefined
if (!hasTwImages) autoFillProps.images = openGraph.images

if (Object.keys(autoFillProps).length > 0) {
Expand All @@ -609,6 +614,12 @@ function postProcessMetadata(
}
}
}

// If there's no title and description configured in openGraph or twitter,
// use the title and description from metadata.
inheritFromMetadata(openGraph, metadata)
inheritFromMetadata(twitter, metadata)

return metadata
}

Expand Down
Loading