Dynamic social previews
A request needs a social card whose text or imagery depends on route data.
Next.js 16+ · Module
next/og exports ImageResponse for generating dynamic Open Graph, Twitter, and other images from JSX and CSS. It can be returned from Route Handlers or metadata image files.
next/og provides ImageResponse, which turns a React element and optional rendering or HTTP options into an image response. It supports JSX with a documented subset of CSS, and can be used in Route Handlers or metadata image files for generated Open Graph and Twitter images.
new ImageResponse(element: ReactElement, options?: { width?: number; height?: number; emoji?: 'twemoji' | 'blobmoji' | 'noto' | 'openmoji'; fonts?: { name: string; data: ArrayBuffer; weight: number; style: 'normal' | 'italic' }[]; debug?: boolean; status?: number; statusText?: string; headers?: Record<string, string> })| Name | Type | Description |
|---|---|---|
element | ReactElement | React element used as the image's rendered content. |
options | object | Optional image and response settings, including width, height, emoji, fonts, debug, status, statusText, and headers. |
A Response containing the generated image.
ImageResponse to render a dynamic image from a JSX tree and CSS styles.next/og, not its former next/server location.import { ImageResponse } from 'next/og'
export const runtime = 'edge'
export async function GET() {
return new ImageResponse(
(
<div
style={{
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
background: '#111827',
color: 'white',
fontSize: 64,
}}
>
Hello world!
</div>
),
{ width: 1200, height: 630 },
)
}A Route Handler returns a dynamically generated 1200×630 image containing styled JSX.
A request needs a social card whose text or imagery depends on route data.
A route segment needs a generated Open Graph or Twitter image without storing static files.
An application needs branded typography in generated cards.
ImageResponse do?Import ImageResponse from next/og and construct it with a React element plus optional settings such as dimensions, fonts, and response metadata. The result is a web Response containing the generated image.
Follow-up: How would you generate an image for a dynamic blog post? Place an opengraph-image.tsx file in the [slug] segment, load the post data, and return an ImageResponse using the resolved route parameter.
The constructor accepts a React element and an options object. Options include width and height, an emoji set, custom fonts, debugging, and HTTP response fields such as status and headers.
Follow-up: How can custom fonts be supplied? Pass font records containing a name, ArrayBuffer data, numeric weight, and normal or italic style.
It supports common CSS features such as flexbox, absolute positioning, text wrapping, centering, nested images, and custom fonts. It is intended for generated social images and integrates with Route Handlers and metadata file conventions.
Follow-up: What should be checked if an image fails during deployment? Verify the bundle remains under 500 KB and that the implementation uses APIs compatible with the Edge Runtime.
Route Handlersopengraph-imagetwitter-imagegenerateImageMetadataMetadata Files