From 6ac733a8a3e8dfd97adaeefcc9cfc3c6b48dd8e8 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 6 Jun 2023 17:08:26 +0200 Subject: [PATCH 1/2] Fill basic twitter metadata with opengraph when missing --- .../next/src/lib/metadata/resolve-metadata.ts | 16 +++++++++++++++- test/e2e/app-dir/metadata/metadata.test.ts | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/next/src/lib/metadata/resolve-metadata.ts b/packages/next/src/lib/metadata/resolve-metadata.ts index b1c06450fd73c..8fb681f1f5a5b 100644 --- a/packages/next/src/lib/metadata/resolve-metadata.ts +++ b/packages/next/src/lib/metadata/resolve-metadata.ts @@ -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 } diff --git a/test/e2e/app-dir/metadata/metadata.test.ts b/test/e2e/app-dir/metadata/metadata.test.ts index d388a99731f3a..f5bfa000413c6 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -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 () => { From a2b3f68ad142ff2b4b8e5a949990bbdb6d3c29b5 Mon Sep 17 00:00:00 2001 From: Jiachi Liu Date: Tue, 6 Jun 2023 21:30:17 +0200 Subject: [PATCH 2/2] fix lint --- .../next/src/lib/metadata/resolve-metadata.ts | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/next/src/lib/metadata/resolve-metadata.ts b/packages/next/src/lib/metadata/resolve-metadata.ts index 8fb681f1f5a5b..ed9c28f9c7b97 100644 --- a/packages/next/src/lib/metadata/resolve-metadata.ts +++ b/packages/next/src/lib/metadata/resolve-metadata.ts @@ -42,6 +42,10 @@ export type MetadataItems = [ StaticMetadata ][] +type MetadataAccumulationOptions = { + pathname: string +} + function mergeStaticMetadata( metadata: ResolvedMetadata, staticFilesMetadata: StaticMetadata @@ -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( @@ -446,17 +459,3 @@ export async function accumulateMetadata( 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 -}