Skip to content

Commit

Permalink
Fill basic twitter metadata with opengraph when missing
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Jun 6, 2023
1 parent 1e0566b commit 6ac733a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/next/src/lib/metadata/resolve-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -444,5 +444,19 @@ export async function accumulateMetadata(
}
}

return resolvedMetadata
return postProcessMetadata(resolvedMetadata)
}

function postProcessMetadata(metadata: ResolvedMetadata): ResolvedMetadata {
const { openGraph, twitter } = metadata
if (openGraph && !twitter) {
const overlappedProps = {
title: openGraph.title,
description: openGraph.description,
images: openGraph.images,
}
const twitter = resolveTwitter(overlappedProps, metadata.metadataBase)
metadata.twitter = twitter
}
return metadata
}
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 6ac733a

Please sign in to comment.