Skip to content

Commit

Permalink
Fill basic twitter metadata with opengraph when missing (vercel#50854)
Browse files Browse the repository at this point in the history
When twitter metadata is not provided but opengraph metadata is, fill the opengraph basic information for twitter metadata.
Twitter card can't be displayed if there's no information from twitter meta tags, at least the `twitter:card`. We fill the `title` `description` and `images` these 3 overlapped properties from opengraph image so they can be displayed properly

Closes NEXT-1111
  • Loading branch information
huozhi authored and hydRAnger committed Jun 12, 2023
1 parent faf7a38 commit d3abccc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
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

0 comments on commit d3abccc

Please sign in to comment.