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

Fix metadata nested twitter props rendering #47972

Merged
merged 3 commits into from
Apr 5, 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
29 changes: 20 additions & 9 deletions packages/next/src/lib/metadata/generate/meta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ type MultiMetaContent =
| null
| undefined

function camelToSnake(camelCaseStr: string) {
return camelCaseStr.replace(/([A-Z])/g, function (match) {
return '_' + match.toLowerCase()
})
}

function getMetaKey(prefix: string, key: string) {
// Use `twitter:image` and `og:image` instead of `twitter:image:url` and `og:image:url`
// to be more compatible as it's a more common format
if ((prefix === 'og:image' || prefix === 'twitter:image') && key === 'url') {
return prefix
}
if (prefix.startsWith('og:') || prefix.startsWith('twitter:')) {
key = camelToSnake(key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are only limited possibilities maybe we can inline them all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good, I can patch this later in other PRs

}
return prefix + ':' + key
}

function ExtendMeta({
content,
namePrefix,
Expand All @@ -49,15 +67,8 @@ function ExtendMeta({
return typeof v === 'undefined' ? null : (
<Meta
key={keyPrefix + ':' + k + '_' + index}
{...(propertyPrefix
? // Use `og:image` instead of `og:image:url` to be more compatible as it's a more common format
{
property:
propertyPrefix === 'og:image' && k === 'url'
? 'og:image'
: propertyPrefix + ':' + k,
}
: { name: namePrefix + ':' + k })}
{...(propertyPrefix && { property: getMetaKey(propertyPrefix, k) })}
{...(namePrefix && { name: getMetaKey(namePrefix, k) })}
content={typeof v === 'string' ? v : v?.toString()}
/>
)
Expand Down
9 changes: 1 addition & 8 deletions packages/next/src/lib/metadata/generate/opengraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,14 +259,7 @@ export function TwitterMetadata({
<Meta name="twitter:creator:id" content={twitter.creatorId} />
<Meta name="twitter:title" content={twitter.title?.absolute} />
<Meta name="twitter:description" content={twitter.description} />
{twitter.images
? twitter.images.map((image, index) => (
<React.Fragment key={index}>
<Meta name="twitter:image" content={image.url} />
<Meta name="twitter:image:alt" content={image.alt} />
</React.Fragment>
))
: null}
<MultiMeta namePrefix="twitter:image" contents={twitter.images} />
{card === 'player'
? twitter.players.map((player, index) => (
<React.Fragment key={index}>
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/lib/metadata/types/opengraph-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ type OGImageDescriptor = {
type OGAudio = string | OGAudioDescriptor | URL
type OGAudioDescriptor = {
url: string | URL
secure_url?: string | URL
secureUrl?: string | URL
type?: string
}
type OGVideo = string | OGVideoDescriptor | URL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const metadata = {
creator: 'creator',
creatorId: 'creatorId',
images: {
url: 'https://twitter.com/image.png',
url: 'https://twitter.com/large-image.png',
alt: 'image-alt',
},
},
Expand Down
7 changes: 6 additions & 1 deletion test/e2e/app-dir/metadata/app/twitter/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ export const metadata = {
siteId: 'siteId',
creator: 'creator',
creatorId: 'creatorId',
images: 'https://twitter.com/image.png',
images: [
{
url: 'https://twitter.com/image.png',
secureUrl: 'https://twitter.com/secure.png',
},
],
},
}
3 changes: 2 additions & 1 deletion test/e2e/app-dir/metadata/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,7 @@ createNextDescribe(
'twitter:creator': 'creator',
'twitter:creator:id': 'creatorId',
'twitter:image': 'https://twitter.com/image.png',
'twitter:image:secure_url': 'https://twitter.com/secure.png',
'twitter:card': 'summary',
})
})
Expand All @@ -638,7 +639,7 @@ createNextDescribe(
'twitter:site:id': 'siteId',
'twitter:creator': 'creator',
'twitter:creator:id': 'creatorId',
'twitter:image': 'https://twitter.com/image.png',
'twitter:image': 'https://twitter.com/large-image.png',
'twitter:image:alt': 'image-alt',
'twitter:card': 'summary_large_image',
})
Expand Down