OG Preview Tool

Guides › Frameworks & CMS

How to add Open Graph tags in Next.js

In the App Router, export metadata (or generateMetadata) from a layout or page. Set metadataBase once so relative image paths become absolute URLs.

// app/layout.tsx
export const metadata = {
  metadataBase: new URL('https://example.com'),
  openGraph: { siteName: 'Example', type: 'website' },
  twitter: { card: 'summary_large_image' },
}

// app/blog/[slug]/page.tsx
export async function generateMetadata({ params }) {
  const post = await getPost((await params).slug)
  return {
    title: post.title,
    description: post.excerpt,
    openGraph: { title: post.title, description: post.excerpt, images: [post.cover] },
  }
}

Or drop an opengraph-image.png (or a generated opengraph-image.tsx) in the route folder, and Next.js adds the tags for you. Note: child openGraph objects replace the parent's rather than merging, so repeat shared fields.

Verify

Deploy, then run the live URL through the OG previewer. It fetches the page like a social crawler (no JavaScript), so tags added only on the client side won't show up. That matches what Facebook, LinkedIn and X see.

Preview your link now

Related

How to add Open Graph tags in NuxtHow to add Open Graph tags in AstroHow to add Open Graph tags in WordPressHow to add Open Graph tags in ShopifyHow to add Open Graph tags in HugoHow to add Open Graph tags in SvelteKit

More free tools from Swift Labs

Robots & Sitemap Checker
Validate robots.txt and XML sitemaps, test any path, see which AI crawlers you block.
Email DNS Check
Check SPF, DKIM, DMARC and MX records in seconds.
DeadLinkPatrol
Find broken links across your whole site.