opengraph-image and twitter-image
API Reference for the Open Graph Image and Twitter Image file conventions.
The opengraph-image and twitter-image file conventions allow you to set Open Graph and Twitter images for a route segment.
They are useful for setting the images that appear on social networks and messaging apps when a user shares a link to your site.
There are two ways to set Open Graph and Twitter images:
Image files (.jpg, .png, .gif)
Use an image file to set a route segment's shared image by placing an opengraph-image or twitter-image image file in the segment.
Next.js will evaluate the file and automatically add the appropriate tags to your app's <head> element.
| File convention | Supported file types |
|---|---|
opengraph-image | .jpg, .jpeg, .png, .gif |
twitter-image | .jpg, .jpeg, .png, .gif |
opengraph-image.alt | .txt |
twitter-image.alt | .txt |
opengraph-image
Add an opengraph-image.(jpg|jpeg|png|gif) image file to any route segment.
twitter-image
Add a twitter-image.(jpg|jpeg|png|gif) image file to any route segment.
opengraph-image.alt.txt
Add an accompanying opengraph-image.alt.txt file in the same route segment as the opengraph-image.(jpg|jpeg|png|gif) image it's alt text.
twitter-image.alt.txt
Add an accompanying twitter-image.alt.txt file in the same route segment as the twitter-image.(jpg|jpeg|png|gif) image it's alt text.
Generate images using code (.js, .ts, .tsx)
In addition to using literal image files, you can programmatically generate images using code.
Generate a route segment's shared image by creating an opengraph-image or twitter-image route that default exports a function.
| File convention | Supported file types |
|---|---|
opengraph-image | .js, .ts, .tsx |
twitter-image | .js, .ts, .tsx |
Good to know:
- By default, generated images are statically optimized (generated at build time and cached) unless they use Dynamic APIs or uncached data.
- You can generate multiple Images in the same file using
generateImageMetadata. opengraph-image.jsandtwitter-image.jsare special Route Handlers that is cached by default unless it uses a Dynamic API or dynamic config option.
The easiest way to generate an image is to use the ImageResponse API from next/og.
Props
The default export function receives the following props:
params (optional)
A promise that resolves to an object containing the dynamic route parameters object from the root segment down to the segment opengraph-image or twitter-image is colocated in.
Good to know: If you use generateImageMetadata, the function will also receive an id prop that is a promise resolving to the id value from one of the items returned by generateImageMetadata.
| Route | URL | params |
|---|---|---|
app/shop/opengraph-image.js | /shop | undefined |
app/shop/[slug]/opengraph-image.js | /shop/1 | Promise<{ slug: '1' }> |
app/shop/[tag]/[item]/opengraph-image.js | /shop/1/2 | Promise<{ tag: '1', item: '2' }> |
Returns
The default export function should return a Blob | ArrayBuffer | TypedArray | DataView | ReadableStream | Response.
Good to know: ImageResponse satisfies this return type.
Config exports
You can optionally configure the image's metadata by exporting alt, size, and contentType variables from opengraph-image or twitter-image route.
| Option | Type |
|---|---|
alt | string |
size | { width: number; height: number } |
contentType | string - image MIME type |
alt
size
contentType
Route Segment Config
opengraph-image and twitter-image are specialized Route Handlers that can use the same route segment configuration options as Pages and Layouts.
Examples
Using external data
This example uses the params object and external data to generate the image.
Good to know:
By default, this generated image will be statically optimized. You can configure the individual fetch options or route segments options to change this behavior.
Using Node.js runtime with local assets
These examples use the Node.js runtime to fetch a local image from the file system and pass it to the <img> src attribute, either as a base64 string or an ArrayBuffer. Place the local asset relative to the project root, not the example source file.
Passing an ArrayBuffer to the src attribute of an <img> element is not part of the HTML spec. The rendering engine used by next/og supports it, but because TypeScript definitions follow the spec, you need a @ts-expect-error directive or similar to use this feature.
Version History
| Version | Changes |
|---|---|
v16.0.0 | params is now a promise that resolves to an object |
v13.3.0 | opengraph-image and twitter-image introduced. |