What is @vercel/og?
@vercel/og is a package designed to generate Open Graph images dynamically. It allows developers to create custom social media preview images on the fly using HTML and CSS, which can be particularly useful for generating unique images for each page or post on a website.
Generate Open Graph Images
This feature allows you to generate Open Graph images dynamically using HTML and CSS. The code sample demonstrates how to create an image with a custom title passed as a query parameter.
const { ImageResponse } = require('@vercel/og');
export default function handler(req, res) {
const { searchParams } = new URL(req.url, 'http://localhost');
const title = searchParams.get('title') || 'Default Title';
const image = new ImageResponse(
<div style={{
fontSize: 128,
color: 'white',
background: 'black',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}>
{title}
</div>,
{
width: 1200,
height: 630,
}
);
res.setHeader('Content-Type', 'image/png');
res.send(image);
}