Open Graph metadata helps social media sites show nice previews when you share links. It makes your page look good with images and titles.
Open Graph metadata in NextJS
export const metadata = { title: 'Page Title', description: 'Page description here', openGraph: { title: 'Open Graph Title', description: 'Open Graph description', url: 'https://example.com/page', siteName: 'Site Name', images: [ { url: 'https://example.com/image.jpg', width: 800, height: 600, alt: 'Image description' } ], locale: 'en_US', type: 'website' } };
This metadata object is used in Next.js App Router to add Open Graph tags automatically.
The images array can have multiple images with details for better previews.
export const metadata = { title: 'My Blog Post', openGraph: { title: 'My Blog Post', description: 'A great post about coding.', url: 'https://myblog.com/post', images: [ { url: 'https://myblog.com/post-image.jpg', alt: 'Blog post image' } ], type: 'article' } };
export const metadata = { title: 'Home Page', openGraph: { title: 'Welcome to Our Site', description: 'Best site ever', url: 'https://oursite.com', images: [], type: 'website' } };
export const metadata = { title: 'Product Page', openGraph: { title: 'Cool Product', description: 'Buy this cool product now!', url: 'https://shop.com/product', images: [ { url: 'https://shop.com/product-image.jpg', width: 1200, height: 630, alt: 'Product image' } ], locale: 'en_GB', type: 'product' } };
This Next.js page component defines Open Graph metadata using the metadata export. When shared on social media, the link will show the title, description, and image specified.
import React from 'react'; import { Metadata } from 'next'; export const metadata: Metadata = { title: 'Sample Page', description: 'This is a sample page with Open Graph metadata.', openGraph: { title: 'Sample Page', description: 'This is a sample page with Open Graph metadata.', url: 'https://example.com/sample', siteName: 'Example Site', images: [ { url: 'https://example.com/sample-image.jpg', width: 1200, height: 630, alt: 'Sample image' } ], locale: 'en_US', type: 'website' } }; export default function SamplePage() { return ( <main> <h1>Welcome to the Sample Page</h1> <p>This page includes Open Graph metadata for social sharing.</p> </main> ); }
Open Graph metadata improves how links look on social media, increasing clicks.
Always provide an image with correct size (1200x630 px recommended) for best display.
Missing or incorrect metadata may cause social sites to show default or no preview.
Open Graph metadata controls how your page looks when shared on social media.
Next.js App Router uses the metadata export to add these tags automatically.
Include title, description, URL, and images for best results.
Practice
Solution
Step 1: Understand Open Graph metadata role
Open Graph metadata defines how a webpage looks when shared on social media platforms.Step 2: Relate to Next.js usage
Next.js uses themetadataexport to automatically add Open Graph tags for sharing previews.Final Answer:
To control how the page appears when shared on social media -> Option DQuick Check:
Open Graph = Social media preview [OK]
- Confusing Open Graph with performance optimization
- Thinking it manages fonts or styles
- Assuming it handles user login
Solution
Step 1: Recall Next.js metadata export syntax
Next.js App Router expects a named export calledmetadataas an object.Step 2: Check Open Graph nested object
Open Graph metadata is nested insideopenGraphproperty withinmetadata.Final Answer:
export const metadata = { openGraph: { title: 'My Page' } } -> Option AQuick Check:
metadata export with openGraph object = A [OK]
- Using default export instead of named export
- Defining metadata as a function instead of an object
- Trying to import and mutate metadata
export const metadata = {
openGraph: {
title: 'Welcome',
description: 'Home page',
url: 'https://example.com',
images: ['https://example.com/image.png']
}
}What will be the Open Graph
og:title content in the rendered HTML?Solution
Step 1: Identify the Open Graph title property
ThetitleinsideopenGraphsets theog:titletag content.Step 2: Match the value to the tag
The value is 'Welcome', soog:titlewill be 'Welcome'.Final Answer:
Welcome -> Option CQuick Check:
og:title = metadata.openGraph.title = 'Welcome' [OK]
- Confusing description or url as title
- Using images URL as title
- Not recognizing nested openGraph object
export const metadata = {
openGraph: {
title: 'Page',
images: 'https://example.com/img.png'
}
}Why might the Open Graph image not appear correctly when shared?
Solution
Step 1: Check images property type
Open Graph expectsimagesas an array of URLs, not a single string.Step 2: Identify the issue
Using a string instead of an array causes the image tag to be missing or invalid.Final Answer:
images should be an array, not a string -> Option AQuick Check:
images array required for Open Graph images [OK]
- Passing a string instead of array for images
- Forgetting to export metadata as named export
- Thinking openGraph should be a function
imageUrl. Which approach correctly sets the metadata in a Next.js App Router page?Solution
Step 1: Understand dynamic metadata in Next.js App Router
For dynamic metadata, Next.js uses agenerateMetadatafunction that receives page props.Step 2: Check options for correct dynamic usage
export function generateMetadata({ params }) { return { openGraph: { images: [params.imageUrl] } } } correctly definesgenerateMetadatareturning metadata usingparams.imageUrl.Final Answer:
export function generateMetadata({ params }) { return { openGraph: { images: [params.imageUrl] } } } -> Option BQuick Check:
Dynamic metadata uses generateMetadata function [OK]
- Trying to use static metadata export for dynamic data
- Using default export instead of named export
- Defining metadata as async function named 'metadata'
Ready to prove it?
Pass the test and this pattern moves unknown
