diff --git a/apps/marketing/src/app/(marketing)/blog/[post]/opengraph-image.tsx b/apps/marketing/src/app/(marketing)/blog/[post]/opengraph/route.tsx similarity index 77% rename from apps/marketing/src/app/(marketing)/blog/[post]/opengraph-image.tsx rename to apps/marketing/src/app/(marketing)/blog/[post]/opengraph/route.tsx index 4c01967d2..70233bbdd 100644 --- a/apps/marketing/src/app/(marketing)/blog/[post]/opengraph-image.tsx +++ b/apps/marketing/src/app/(marketing)/blog/[post]/opengraph/route.tsx @@ -1,25 +1,21 @@ import { ImageResponse } from 'next/og'; - -import { allBlogPosts } from 'contentlayer/generated'; +import { NextResponse } from 'next/server'; export const runtime = 'edge'; -export const contentType = 'image/png'; - -export const IMAGE_SIZE = { +const IMAGE_SIZE = { width: 1200, height: 630, }; -type BlogPostOpenGraphImageProps = { - params: { post: string }; -}; +export async function GET(_request: Request) { + const url = new URL(_request.url); -export default async function BlogPostOpenGraphImage({ params }: BlogPostOpenGraphImageProps) { - const blogPost = allBlogPosts.find((post) => post._raw.flattenedPath === `blog/${params.post}`); + const title = url.searchParams.get('title'); + const author = url.searchParams.get('author'); - if (!blogPost) { - return null; + if (!title || !author) { + return NextResponse.json({ error: 'Not found' }, { status: 404 }); } // The long urls are needed for a compiler optimisation on the Next.js side, lifting this up @@ -49,10 +45,10 @@ export default async function BlogPostOpenGraphImage({ params }: BlogPostOpenGra logo

- {blogPost.title} + {title}

-

Written by {blogPost.authorName}

+

Written by {author}

), { diff --git a/apps/marketing/src/app/(marketing)/blog/[post]/page.tsx b/apps/marketing/src/app/(marketing)/blog/[post]/page.tsx index bd5fdb2da..3e50f8305 100644 --- a/apps/marketing/src/app/(marketing)/blog/[post]/page.tsx +++ b/apps/marketing/src/app/(marketing)/blog/[post]/page.tsx @@ -20,11 +20,23 @@ export const generateMetadata = ({ params }: { params: { post: string } }) => { }; } + // Use the url constructor to ensure that things are escaped as they should be + const searchParams = new URLSearchParams({ + title: blogPost.title, + author: blogPost.authorName, + }); + return { title: { absolute: `${blogPost.title} - Documenso Blog`, }, description: blogPost.description, + openGraph: { + images: [`${blogPost.href}/opengraph?${searchParams.toString()}`], + }, + twitter: { + images: [`${blogPost.href}/opengraph?${searchParams.toString()}`], + }, }; }; @@ -94,7 +106,7 @@ export default function BlogPostPage({ params }: { params: { post: string } }) { - {post.cta && } + {post.cta && } ); }