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

Fill basic twitter metadata with opengraph when missing #50854

Merged
merged 3 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
19 changes: 16 additions & 3 deletions packages/next/src/lib/metadata/resolve-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export type MetadataItems = [
StaticMetadata
][]

type MetadataAccumulationOptions = {
pathname: string
}

function mergeStaticMetadata(
metadata: ResolvedMetadata,
staticFilesMetadata: StaticMetadata
Expand Down Expand Up @@ -354,8 +358,17 @@ export async function resolveMetadata({
return metadataItems
}

type MetadataAccumulationOptions = {
pathname: string
function postProcessMetadata(metadata: ResolvedMetadata): ResolvedMetadata {
const { openGraph, twitter } = metadata
if (openGraph && !twitter) {
const overlappedProps = {
title: openGraph.title,
description: openGraph.description,
images: openGraph.images,
}
metadata.twitter = resolveTwitter(overlappedProps, metadata.metadataBase)
}
return metadata
}

export async function accumulateMetadata(
Expand Down Expand Up @@ -444,5 +457,5 @@ export async function accumulateMetadata(
}
}

return resolvedMetadata
return postProcessMetadata(resolvedMetadata)
}
13 changes: 13 additions & 0 deletions test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,19 @@ createNextDescribe(
'og:image:height': ['600', '1600'],
'og:image:alt': 'My custom alt',
})

await matchMultiDom('meta', 'name', 'content', {
'twitter:card': 'summary',
'twitter:title': 'My custom title',
'twitter:description': 'My custom description',
'twitter:image': [
'https://example.com/image.png',
'https://example.com/image2.png',
],
'twitter:image:width': ['800', '1800'],
'twitter:image:height': ['600', '1600'],
'twitter:image:alt': 'My custom alt',
})
})

it('should support opengraph with article type', async () => {
Expand Down