Dynamic OG Images in Next.js

When someone shares a link to your site, the image that shows up on X, LinkedIn, Slack, or Discord comes from the og:image meta tag. A good preview image noticeably changes how a link looks in a feed, and a missing or broken one makes the link look unfinished.

Next.js includes good tooling for generating these images, so this guide starts there and treats it fairly. After that we show the hosted alternative: a single meta tag that points at a Unfurlkit URL. You can pick whichever suits your project.

Version note: This guide targets the Next.js App Router and was checked against the current Next.js docs (Oct 2026). opengraph-image arrived in 13.3, and ImageResponse moved from next/server to next/og in 14.0. Check the official docs for your version: https://nextjs.org/docs/app/api-reference/file-conventions/metadata/opengraph-image.

What a good OG image needs

Whatever tool you use, the image should meet these basics:

  • Size: 1200×630 px (about 1.91:1) is the standard across platforms.
  • Absolute URL: og:image must be a full https:// URL. Relative paths fail on many crawlers.
  • Reachable without auth: crawlers don't have your cookies, so no login walls and no bot-blocking on the image route.
  • Reasonable file size: keep it small. Platforms have upper limits, and large images can time out.
  • Both tag families: og:image plus twitter:card set to summary_large_image.

Option 1: Static opengraph-image file

The simplest approach: put an image file named opengraph-image.png (or .jpg, .jpeg, .gif) in a route segment:

app/
  opengraph-image.png        ← used for "/"
  blog/
    opengraph-image.png      ← used for "/blog"

Next.js detects the file and emits the og:image, og:image:width, og:image:height, and og:image:type tags for you. You can add an opengraph-image.alt.txt next to it for the alt text, and a twitter-image.png if you want a different image for X. Next.js fails the build if a static opengraph-image exceeds 8 MB or a twitter-image exceeds 5 MB.

This works well for a home page. For a blog with hundreds of posts, you need something dynamic.

Option 2: Generated opengraph-image.tsx with ImageResponse

Instead of a static file, create opengraph-image.tsx (or .js) in the segment. It exports a default function that returns an ImageResponse, a class from next/og that turns a JSX tree into a PNG. It uses Satori to lay out the JSX with a subset of CSS (flexbox, not grid) and Resvg to rasterize the result.

A root-level image

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

export const alt = 'My Site — Build faster'
export const size = { width: 1200, height: 630 }
export const contentType = 'image/png'

export default async function Image() {
  return new ImageResponse(
    (
      <div
        style={{
          width: '100%',
          height: '100%',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
          background: '#0b0d12',
          color: 'white',
          fontSize: 72,
          fontWeight: 700,
        }}
      >
        My Site
      </div>
    ),
    { ...size }
  )
}

A per-post image

For a dynamic route like app/blog/[slug]/, put opengraph-image.tsx in the same folder. The function receives the route params. In current Next.js, params is a Promise and must be awaited. The docs' version history for opengraph-image lists this change under v16.0.0.

Replace your-site.com with your own site.

// app/blog/[slug]/opengraph-image.tsx
import { ImageResponse } from 'next/og'
import { getPost } from '@/lib/posts'

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

export default async function Image({
  params,
}: {
  params: Promise<{ slug: string }>
}) {
  const { slug } = await params
  const post = await getPost(slug)

  return new ImageResponse(
    (
      <div
        style={{
          width: '100%',
          height: '100%',
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'space-between',
          padding: 64,
          background: 'linear-gradient(135deg, #111827, #1e3a8a)',
          color: 'white',
        }}
      >
        <div style={{ fontSize: 28, opacity: 0.8 }}>your-site.com/blog</div>
        <div style={{ fontSize: 64, fontWeight: 700, lineHeight: 1.1 }}>
          {post.title}
        </div>
        <div style={{ fontSize: 28 }}>{post.author}</div>
      </div>
    ),
    { ...size }
  )
}

On older versions, check the docs for your release: there, opengraph-image received params as a plain object. Note that page and generateMetadata params became async earlier, in Next.js 15.

Custom fonts

ImageResponse ships with a default font. To use your brand font, load the font data and pass it in. The Next.js docs list only ttf, otf, and woff as supported (ttf and otf parse faster), so WOFF2 won't work. Read the font once at module scope, as below:

import { readFile } from 'node:fs/promises'
import { join } from 'node:path'

const inter = await readFile(join(process.cwd(), 'assets/Inter-Bold.ttf'))

return new ImageResponse(jsx, {
  ...size,
  fonts: [{ name: 'Inter', data: inter, style: 'normal', weight: 700 }],
})

Option 2b: A route handler

If you'd rather control the URL yourself, for example /og?title=..., return an ImageResponse from a route handler and set the meta tag through generateMetadata:

// app/og/route.tsx
import { ImageResponse } from 'next/og'

export async function GET(req: Request) {
  const { searchParams } = new URL(req.url)
  const title = searchParams.get('title')?.slice(0, 120) ?? 'My Site'
  return new ImageResponse(
    <div style={{ display: 'flex', width: '100%', height: '100%',
      alignItems: 'center', justifyContent: 'center', fontSize: 64,
      background: 'white' }}>{title}</div>,
    { width: 1200, height: 630 }
  )
}

Replace your-site.com with your own site.

// app/blog/[slug]/page.tsx
import type { Metadata } from 'next'

export async function generateMetadata(
  { params }: { params: Promise<{ slug: string }> }
): Promise<Metadata> {
  const { slug } = await params
  const post = await getPost(slug)
  const og = `https://your-site.com/og?title=${encodeURIComponent(post.title)}`
  return {
    metadataBase: new URL('https://your-site.com'),
    openGraph: { title: post.title, images: [{ url: og, width: 1200, height: 630 }] },
    twitter: { card: 'summary_large_image', images: [og] },
  }
}

Note the metadataBase. Without it, Next.js can't turn relative image paths into absolute URLs. Set it once in your root layout.

The honest trade-offs of DIY

The built-in approach is well documented and costs nothing beyond your hosting bill. If you're on Vercel and need a few templates, it's often the right choice. Owning the route also means owning a few ongoing tasks:

  • Bundle size cap. The docs set a 500 KB maximum for the bundle, which includes your JSX, CSS, fonts, and images. Large fonts eat into that quickly.
  • Satori's CSS subset. It supports flexbox but not grid, and every div with more than one child needs display: flex. Your existing components won't render as they are.
  • Fonts. You load font files yourself, WOFF2 isn't supported, and you need extra work for emoji and non-Latin scripts.
  • Caching and cost. Images generated on request run as functions. By default, generated images are statically optimized (built and cached at build time) unless they use request-time APIs, which, which adds to build duration as the number of pages grows.
  • Hosting assumptions. next/og works on self-hosted Node too, but edge caching behavior depends on your platform.
  • Debugging. When a preview is broken, the cause is usually not the image code. It's a relative URL, a missing twitter:card, a bot blocked by middleware, or a platform caching an old version.
  • It only covers Next.js. If your docs run on Astro and your marketing site runs on something else, you have to build it again for each.

None of these block you, but they all take time to maintain.

Option 3: One URL with Unfurlkit (hosted)

If you'd rather not maintain your own image route, Unfurlkit is the hosted option: one URL and done. It renders the image on its servers, so there's no opengraph-image.tsx or route handler in your app. This isn't a judgment on ImageResponse. It's a choice about who maintains the rendering. You put a URL in a meta tag:

// app/blog/[slug]/page.tsx
import type { Metadata } from 'next'

const ogUrl = (title: string) =>
  `https://unfurlkit.com/v1/basic.png?title=${encodeURIComponent(title || 'My Site')}&theme=dark`

export async function generateMetadata(
  { params }: { params: Promise<{ slug: string }> }
): Promise<Metadata> {
  const { slug } = await params
  const post = await getPost(slug)
  return {
    openGraph: { title: post.title, images: [{ url: ogUrl(post.title), width: 1200, height: 630 }] },
    twitter: { card: 'summary_large_image', images: [ogUrl(post.title)] },
  }
}

The rendered HTML looks like this:

<meta property="og:image" content="https://unfurlkit.com/v1/basic.png?title=My%20Post&theme=dark" />
<meta name="twitter:card" content="summary_large_image" />

What changes compared with DIY:

  • No rendering code in your repo. You don't have an opengraph-image.tsx, font files, or Satori quirks to manage.
  • Absolute and correctly sized by default. The URL is already absolute, and the image is 1200×630.
  • Cached at the edge. The same URL returns the same image every time.
  • The same pattern on every site. You use the same meta tag in Next.js, Astro, and Hugo.

Every image URL needs your key. A Free key gets 10 renders in total (no monthly reset), with a small "unfurlkit" watermark, and needs no card. Pro starts $9/mo or $90/yr (10k renders/month) and removes it, adding custom fonts (Inter, Roboto, Playfair Display, JetBrains Mono) and logo.

Choosing between them

You… Use
Need one image for the whole site Static opengraph-image.png
Like owning the code and the route (on Vercel or self-hosted) ImageResponse
Want a hosted URL instead of maintaining a route, or run several frameworks Unfurlkit

Debugging checklist

Whichever approach you choose, if a preview doesn't appear, work through these:

  1. View source (not DevTools) and confirm og:image is in the server-rendered HTML.
  2. Is the URL absolute? If not, set metadataBase.
  3. Open the image URL in a private window. It should return 200 with content-type: image/png.
  4. Check middleware and auth. Make sure crawlers can reach both the page and the image.
  5. Check twitter:card. It should be summary_large_image.
  6. Bust caches. LinkedIn's Post Inspector and Facebook's Sharing Debugger can re-scrape. Otherwise, add a ?v=2 to the image URL.

You can also run the page through the free Free OG Image Checker, which renders how the link will look on X, LinkedIn, Facebook, Slack, Discord, and iMessage and lists specific fixes.

FAQ

Which languages and scripts are supported? Right-to-left scripts such as Arabic and Hebrew aren't supported yet at launch.

Summary

Next.js gives you a solid native way to generate OG images: opengraph-image files for simple cases and ImageResponse for dynamic ones. If you prefer not to maintain that code, one meta tag pointing at a Unfurlkit URL does the same job.

See Unfurlkit pricing · Check any page with the Free OG Image Checker.

Updated 2026-10-21

Try it freePricing