Per-page Open Graph images
A blog needs a distinct social preview for every article.
Next.js 16+ · Class
ImageResponse generates dynamic images from JSX and CSS in Next.js App Router applications. It is useful for Open Graph images, Twitter cards, and other request-time image endpoints.
ImageResponse turns a React element into a dynamically rendered image. Import it from next/og, optionally configure dimensions, fonts, emoji, debugging, and HTTP response properties, then return the instance from a Route Handler or metadata image file.
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 containing the image's JSX layout and styling. |
options | object | Optional rendering and HTTP response configuration, including dimensions, emoji, fonts, debugging, status, status text, and headers. |
An image response suitable for returning from a Route Handler or metadata image function.
import { ImageResponse } from 'next/og'
export const runtime = 'edge'
export async function GET() {
return new ImageResponse(
(
<div
style={{
display: 'flex',
width: '100%',
height: '100%',
alignItems: 'center',
justifyContent: 'center',
background: 'white',
fontSize: 64,
}}
>
Hello, Next.js!
</div>
),
{ width: 1200, height: 630 },
)
}This Route Handler returns a generated PNG containing JSX-styled text.
A blog needs a distinct social preview for every article.
A service exposes an endpoint that renders branded visuals from request parameters.
A project needs a generated favicon or app icon.
ImageResponse accepts a React element and an options object, then produces a response suitable for an image endpoint. It is commonly used with JSX and CSS for generated social images.
Follow-up: How would you expose it? Return the ImageResponse instance from a Route Handler, or from a metadata image file such as opengraph-image.tsx.
Import ImageResponse from next/og and return new ImageResponse(...) from a Route Handler. The element supplies the visual structure, while options can set width and height.
Follow-up: What must be checked when adding custom fonts or images? Include their data in the bundle and ensure the total remains at or below 500 KB.
The constructor defaults to 1200 by 630 pixels, uses twemoji for emoji, and has debugging disabled. Options also support fonts plus HTTP status, status text, and headers.
Follow-up: How can response metadata be customized? Pass status, statusText, or headers in the options object; these values are forwarded to the HTTP response.
Route Handlersnext/ogopengraph-image.tsxtwitter-imageMetadata