Unverified Commit baf3d2fa authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix title crashing while post processing metadata (#52196)

Fixes #52191

Resolved `twitter?.title` could be `null`, adding optional chain to access it
parent 0b470753
...@@ -80,7 +80,7 @@ describe('accumulateMetadata', () => { ...@@ -80,7 +80,7 @@ describe('accumulateMetadata', () => {
}) })
}) })
describe('openGraph', () => { describe('openGraph and twitter', () => {
it('should convert string or URL images field to array, not only for basic og type', async () => { it('should convert string or URL images field to array, not only for basic og type', async () => {
const items: [Metadata[], Metadata][] = [ const items: [Metadata[], Metadata][] = [
[ [
...@@ -177,6 +177,44 @@ describe('accumulateMetadata', () => { ...@@ -177,6 +177,44 @@ describe('accumulateMetadata', () => {
}) })
}) })
it('should fill only the existing props from openGraph to twitter', async () => {
const metadataItems: MetadataItems = [
[
{
openGraph: {
// skip title
description: 'description',
},
},
// has static metadata files
{
icon: undefined,
apple: undefined,
twitter: ['/og/twitter.png'],
openGraph: undefined,
manifest: undefined,
},
],
]
const metadata = await accumulateMetadata(metadataItems)
expect(metadata).toMatchObject({
openGraph: {
title: {
absolute: '',
template: null,
},
description: 'description',
},
twitter: {
title: {
absolute: '',
template: null,
},
description: 'description',
},
})
})
it('should resolve relative url starting with ./ with pathname for openGraph.url', async () => { it('should resolve relative url starting with ./ with pathname for openGraph.url', async () => {
const metadataItems: MetadataItems = [ const metadataItems: MetadataItems = [
[ [
......
...@@ -376,7 +376,7 @@ function postProcessMetadata(metadata: ResolvedMetadata): ResolvedMetadata { ...@@ -376,7 +376,7 @@ function postProcessMetadata(metadata: ResolvedMetadata): ResolvedMetadata {
ResolvedMetadata['openGraph'] ResolvedMetadata['openGraph']
>[Key] >[Key]
}> = {} }> = {}
const hasTwTitle = twitter?.title.absolute const hasTwTitle = twitter?.title?.absolute
const hasTwDescription = twitter?.description const hasTwDescription = twitter?.description
const hasTwImages = twitter?.images const hasTwImages = twitter?.images
if (!hasTwTitle) autoFillProps.title = openGraph.title if (!hasTwTitle) autoFillProps.title = openGraph.title
......
...@@ -77,7 +77,7 @@ type ResolvedTwitterSummary = { ...@@ -77,7 +77,7 @@ type ResolvedTwitterSummary = {
creator: string | null creator: string | null
creatorId: string | null creatorId: string | null
description: string | null description: string | null
title: AbsoluteTemplateString title?: AbsoluteTemplateString
images?: Array<ResolvedTwitterImage> images?: Array<ResolvedTwitterImage>
} }
type ResolvedTwitterPlayer = ResolvedTwitterSummary & { type ResolvedTwitterPlayer = ResolvedTwitterSummary & {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment