Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Next.js 16+ · Directive

opengraph-image

The opengraph-image convention sets the Open Graph preview image for an App Router route segment. Use a supported image file for static metadata or a JavaScript, TypeScript, or TSX file to generate the image.

How it works

Place a static image or a code-based file named opengraph-image in an App Router route segment. Next.js uses the most specific matching segment, serves the asset, and adds the corresponding Open Graph metadata to the document head. Code-based variants can return an ImageResponse and export image metadata such as alt text, dimensions, and content type.

Syntax

app/**/opengraph-image.(jpg|jpeg|png|gif|js|ts|tsx)

Returns

A static image asset or a generated image response used as the route segment's Open Graph image.

Key points

  • The convention assigns a social preview image to an App Router route segment.
  • Use it to control the preview image shown when a page link is shared.
  • You can provide an image asset or generate the image with code.
  • Static files use the opengraph-image basename followed by a supported raster-image extension.
  • A dynamic implementation is a route file whose default export produces the shared image.

Common mistakes

  • Keep the generated or static Open Graph image within the documented 8 MB limit; exceeding it causes the build to fail.
  • A deeper route segment overrides an Open Graph image defined higher in the app directory.
  • The convention is metadata-aware, so manually adding equivalent head tags is unnecessary.

Example: Generated route-specific Open Graph image

// app/blog/opengraph-image.tsx
import { ImageResponse } from 'next/og'

export const alt = 'Blog post'
export const size = { width: 1200, height: 630 }
export const contentType = 'image/png'

export default function Image() {
  return new ImageResponse(
    <div
      style={{
        fontSize: 64,
        background: 'white',
        width: '100%',
        height: '100%',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
      }}
    >
      Blog post
    </div>,
  )
}

This route-specific file generates an Open Graph image with JSX and CSS for the /blog segment.

When to use it

Application-wide sharing image

A site needs a default social preview for all pages.

Route-specific static image

A blog or product route needs its own preview artwork.

Dynamic generated image

Preview text or branding depends on route data.

Hierarchical image overrides

Different nested routes need distinct social previews.

Interview questions

QHow do you add a static Open Graph image in the App Router?

Add an opengraph-image.jpg, .jpeg, .png, or .gif file to the relevant route segment. Next.js evaluates the convention and adds the appropriate Open Graph tags to the document head.

Follow-up: How would you make the image depend on route data? Create an opengraph-image.js, .ts, or .tsx file that default-exports a function returning an ImageResponse.

QHow are dynamic Open Graph images generated?

The route segment can contain a code-based opengraph-image file. Its default export generates the image, commonly by returning ImageResponse from next/og with JSX and CSS.

Follow-up: Which route wins when multiple images apply? The image located in the more specific, deeper segment takes precedence.

QWhich file types does opengraph-image support?

The convention supports .jpg, .jpeg, .png, and .gif image files. The generated-file form supports JavaScript, TypeScript, and TSX extensions.

Follow-up: What operational limit matters for static OG assets? An opengraph-image must not exceed 8 MB, or the build fails.

  • twitter-image
  • opengraph-image.alt
  • twitter-image.alt
  • ImageResponse
  • generateImageMetadata

Source: opengraph-image and twitter-image - Metadata Files. Checked 2026-10-15.