Back to Blog
Next.jsOpen GraphWeb Development

Next.js Open Graph & Dynamic OG Image Optimization Guide

Master Next.js App Router metadata generation, dynamic @vercel/og image rendering, and social card debugging for maximum CTR.

SS
Sanjay Samanta
July 20, 2026
7 min read

Open Graph meta tags are essential for driving referral traffic from social platforms, messaging apps, and community forums. In Next.js (App Router), managing metadata and generating dynamic social share graphics has been completely reimagined with native TypeScript APIs and the Edge Runtime.

This guide walks you through configuring static and dynamic Open Graph tags, programmatically generating custom OG images with @vercel/og, and preventing common social preview caching pitfalls.


1. Static Metadata Configuration in Next.js App Router

Next.js provides a built-in metadata object exported directly from any layout.tsx or page.tsx file.

Basic Implementation Example

import type { Metadata } from 'next';

export const metadata: Metadata = {
  title: 'Mastering Open Graph Meta Tags | Open Graph Generator',
  description: 'Generate, validate, and preview Open Graph and Twitter Card tags in real-time.',
  openGraph: {
    title: 'Mastering Open Graph Meta Tags',
    description: 'Generate, validate, and preview Open Graph and Twitter Card tags in real-time.',
    url: 'https://opengraphgenerator.com/blog/open-graph-guide',
    siteName: 'Open Graph Generator',
    images: [
      {
        url: 'https://opengraphgenerator.com/og-image.png',
        width: 1200,
        height: 630,
        alt: 'Open Graph Meta Tags Guide Banner',
      },
    ],
    locale: 'en_US',
    type: 'article',
  },
  twitter: {
    card: 'summary_large_image',
    title: 'Mastering Open Graph Meta Tags',
    description: 'Generate, validate, and preview Open Graph and Twitter Card tags in real-time.',
    images: ['https://opengraphgenerator.com/og-image.png'],
    creator: '@opengraphgen',
  },
};

2. Dynamic Metadata for E-Commerce & Blog Routes

For dynamic routes like /blog/[slug] or /products/[id], export generateMetadata to fetch asynchronous database records before generating social tags.

import type { Metadata, ResolvingMetadata } from 'next';

type Props = {
  params: { slug: string };
};

export async function generateMetadata(
  { params }: Props,
  parent: ResolvingMetadata
): Promise<Metadata> {
  const slug = params.slug;
  const post = await fetchPostBySlug(slug);

  if (!post) {
    return { title: 'Post Not Found' };
  }

  const previousImages = (await parent).openGraph?.images || [];

  return {
    title: `${post.title} | Open Graph Blog`,
    description: post.excerpt,
    openGraph: {
      title: post.title,
      description: post.excerpt,
      url: `https://opengraphgenerator.com/blog/${slug}`,
      publishedTime: post.publishedAt,
      authors: [post.authorName],
      images: [post.coverImage, ...previousImages],
    },
    twitter: {
      card: 'summary_large_image',
      title: post.title,
      description: post.excerpt,
      images: [post.coverImage],
    },
  };
}

3. Generating Dynamic OG Images with Edge JSX (@vercel/og / ImageResponse)

Instead of designing static PNG images in Figma for every blog post, Next.js allows you to render HTML and CSS into PNG images at runtime using ImageResponse.

Create an opengraph-image.tsx file directly inside your page directory (e.g. app/blog/[slug]/opengraph-image.tsx):

import { ImageResponse } from 'next/og';

export const runtime = 'edge';
export const alt = 'Article Social Preview';
export const size = { width: 1200, height: 630 };
export const contentType = 'image/png';

export default async function Image({ params }: { params: { slug: string } }) {
  const post = await fetchPostBySlug(params.slug);

  return new ImageResponse(
    (
      <div
        style={{
          height: '100%',
          width: '100%',
          display: 'flex',
          flexDirection: 'column',
          alignItems: 'flex-start',
          justifyContent: 'space-between',
          backgroundColor: '#0a0d12',
          padding: '80px',
          color: '#ffffff',
          fontFamily: 'sans-serif',
        }}
      >
        <div style={{ display: 'flex', alignItems: 'center', gap: '12px' }}>
          <div
            style={{
              width: '16px',
              height: '16px',
              borderRadius: '50%',
              backgroundColor: '#0070f3',
            }}
          />
          <span style={{ fontSize: '24px', letterSpacing: '2px', textTransform: 'uppercase', color: '#888' }}>
            Open Graph Generator Blog
          </span>
        </div>

        <div style={{ fontSize: '56px', fontWeight: 'bold', lineHeight: '1.2' }}>
          {post?.title || 'SEO & Technical Web Guide'}
        </div>

        <div style={{ display: 'flex', justifyContent: 'space-between', width: '100%', color: '#666', fontSize: '20px' }}>
          <span>By {post?.authorName || 'Editorial Team'}</span>
          <span>opengraphgenerator.com</span>
        </div>
      </div>
    ),
    {
      ...size,
    }
  );
}

Next.js will automatically generate the corresponding <meta property="og:image" content="..." /> tag pointing to this edge function URL!


4. Key Open Graph Metadata Checklist for Next.js

Meta Property Purpose Optimal Standard
og:title Social card heading 55 - 60 characters
og:description Short card snippet 110 - 150 characters
og:image Shared graphic URL 1200 x 630 px (1.91:1 ratio)
og:url Canonical share destination Absolute HTTPS URL
og:type Content classification website or article
twitter:card Twitter display template summary_large_image

5. Testing and Debugging Next.js Meta Tags

Before sharing your Next.js application link in production, test your rendered tags with our Open Graph Inspector.

Key validation checks to run:

  1. Ensure og:image uses an absolute URL (e.g. https://example.com/og.png), not a relative path (/og.png).
  2. Verify HTTP response codes return 200 OK without blocking user-agents.
  3. Use platform debuggers (LinkedIn Post Inspector, Facebook Sharing Debugger) to clear cached preview thumbnails after major updates.