Skip to content

Commit a98eb95

Browse files
fix: update metadata generation to include dynamic Open Graph and Twitter images
1 parent 62950b8 commit a98eb95

1 file changed

Lines changed: 18 additions & 2 deletions

File tree

app/blog/[slug]/page.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,25 @@ export async function generateMetadata({
3131
const title = `${post.frontmatter.title} | Inside React`;
3232
const description = post.frontmatter.description;
3333
const url = `https://inside-react.vercel.app/blog/${slug}`;
34+
const metadataBase = new URL("https://inside-react.vercel.app");
3435
const defaultOgImage = "/opengraph-image";
3536
const defaultTwitterImage = "/twitter-image";
3637
const ogImage = post.frontmatter.ogImage ?? defaultOgImage;
38+
const ogImageUrl = new URL(ogImage, metadataBase).toString();
39+
const twitterImageUrl = new URL(
40+
post.frontmatter.ogImage ?? defaultTwitterImage,
41+
metadataBase
42+
).toString();
43+
const ogImages = post.frontmatter.ogImage
44+
? [{ url: ogImageUrl }]
45+
: [
46+
{
47+
url: ogImageUrl,
48+
width: 1200,
49+
height: 630,
50+
alt: post.frontmatter.title,
51+
},
52+
];
3753

3854
return {
3955
title,
@@ -47,14 +63,14 @@ export async function generateMetadata({
4763
publishedTime: post.frontmatter.date,
4864
authors: ["Sankalpa Acharya"],
4965
tags: [post.frontmatter.topic],
50-
images: [ogImage],
66+
images: ogImages,
5167
},
5268
twitter: {
5369
card: "summary_large_image",
5470
title: post.frontmatter.title,
5571
description,
5672
creator: "@user_sankalpa",
57-
images: [post.frontmatter.ogImage ?? defaultTwitterImage],
73+
images: [twitterImageUrl],
5874
},
5975
alternates: {
6076
canonical: url,

0 commit comments

Comments
 (0)