To assist with generating dynamic Open Graph (OG) images, you can use the Vercel @vercel/og
library to compute and generate social card images using Vercel Functions.
@vercel/og
automatically adds the correct headers to cache computed images at the edge, helping reduce cost and recomputationVercel OG image generation is supported on the Node.js runtime.
Local resources can be loaded directly using fs.readFile
. Alternatively, fetch
can be used to load remote resources.
const fs = require('fs').promises;
const loadLocalImage = async () => {
const imageData = await fs.readFile('/path/to/image.png');
// Process image data
};
There are limitations when using vercel/og
with the Next.js Pages Router and the Node.js runtime. Specifically, this combination does not support the return new Response(…)
syntax. The table below provides a breakdown of the supported syntaxes for different configurations.
pages/
+ Edge runtime return new Response(…)
Fully supported. app/
+ Node.js runtime return new Response(…)
Fully supported. app/
+ Edge runtime return new Response(…)
Fully supported. pages/
+ Node.js runtime Not supported Does not support return new Response(…)
syntax with vercel/og
.
@vercel/og
by running the following command inside your project directory. This isn't required for Next.js App Router projects, as the package is already included:ImageResponse
function, the use of .jsx
or .tsx
files is recommended as they are designed to handle this kind of syntaxAllow
inside your robots.txt
file. For example, if your OG image API route is /api/og/
, you can add the following line: If you are using Next.js, review robots.txt to learn how to add or generate a robots.txt
file.Get started with an example that generates an image from static text using Next.js by setting up a new app with the following command:
Create an API endpoint by adding route.tsx
under the app/api/og
directory in the root of your project.
Then paste the following code:
import { ImageResponse } from 'next/og';
// App router includes @vercel/og.
// No need to install it.
export async function GET() {
return new ImageResponse(
(
<div
style={{
fontSize: 40,
color: 'black',
background: 'white',
width: '100%',
height: '100%',
padding: '50px 200px',
textAlign: 'center',
justifyContent: 'center',
alignItems: 'center',
}}
>
👋 Hello
</div>
),
{
width: 1200,
height: 630,
},
);
}
If you're not using a framework, you must either add "type": "module"
to your package.json
or change your JavaScript Functions' file extensions from .js
to .mjs
Run the following command:
Then, browse to http://localhost:3000/api/og
. You will see the following image:
Deploy your project to obtain a publicly accessible path to the OG image API endpoint. You can find an example deployment at https://og-examples.vercel.sh/api/static.
Then, based on the Open Graph Protocol, create the web content for your social media post as follows:
<meta>
tag inside the <head>
of the webpageproperty
attribute with value og:image
to the <meta>
tagcontent
attribute with value as the absolute path of the /api/og
endpoint to the <meta>
tagWith the example deployment at https://og-examples.vercel.sh/api/static, use the following code:
<head>
<title>Hello world</title>
<meta
property="og:image"
content="https://og-examples.vercel.sh/api/static"
/>
</head>
Every time you create a new social media post, you need to update the API endpoint with the new content. However, if you identify which parts of your ImageResponse
will change for each post, you can then pass those values as parameters of the endpoint so that you can use the same endpoint for all your posts.
In the examples below, we explore using parameters and including other types of content with ImageResponse
.
@vercel/og
uses Satori and Resvg to convert HTML and CSS into PNG@vercel/og
API referencettf
, otf
, and woff
font formats are supported. To maximize the font parsing speed, ttf
or otf
are preferred over woff
display: flex
) and a subset of CSS properties are supported. Advanced layouts (display: grid
) will not work. See Satori's documentation for more details on supported CSS propertiesRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4