fix: avoid opengraph image limit (#1027)

This commit is contained in:
Lucas Smith
2024-03-14 23:27:19 +11:00
committed by GitHub
2 changed files with 23 additions and 15 deletions

View File

@@ -1,25 +1,21 @@
import { ImageResponse } from 'next/og'; import { ImageResponse } from 'next/og';
import { NextResponse } from 'next/server';
import { allBlogPosts } from 'contentlayer/generated';
export const runtime = 'edge'; export const runtime = 'edge';
export const contentType = 'image/png'; const IMAGE_SIZE = {
export const IMAGE_SIZE = {
width: 1200, width: 1200,
height: 630, height: 630,
}; };
type BlogPostOpenGraphImageProps = { export async function GET(_request: Request) {
params: { post: string }; const url = new URL(_request.url);
};
export default async function BlogPostOpenGraphImage({ params }: BlogPostOpenGraphImageProps) { const title = url.searchParams.get('title');
const blogPost = allBlogPosts.find((post) => post._raw.flattenedPath === `blog/${params.post}`); const author = url.searchParams.get('author');
if (!blogPost) { if (!title || !author) {
return null; 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 // 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
<img src={logoImage} alt="logo" tw="h-8" /> <img src={logoImage} alt="logo" tw="h-8" />
<h1 tw="mt-8 text-6xl text-center flex items-center justify-center w-full max-w-[800px] font-bold text-center mx-auto"> <h1 tw="mt-8 text-6xl text-center flex items-center justify-center w-full max-w-[800px] font-bold text-center mx-auto">
{blogPost.title} {title}
</h1> </h1>
<p tw="font-normal">Written by {blogPost.authorName}</p> <p tw="font-normal">Written by {author}</p>
</div> </div>
), ),
{ {

View File

@@ -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 { return {
title: { title: {
absolute: `${blogPost.title} - Documenso Blog`, absolute: `${blogPost.title} - Documenso Blog`,
}, },
description: blogPost.description, 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 } }) {
</Link> </Link>
</article> </article>
{post.cta && <CallToAction className="mt-8" utmSource={`blog__${params.post}`} />} {post.cta && <CallToAction className="mt-8" utmSource={`blog_${params.post}`} />}
</div> </div>
); );
} }