📖 Scenario: You are building a simple Next.js website for a local bakery. You want to make sure that when people share your website link on social media, the preview shows the bakery's name, a description, and a nice image.
🎯 Goal: Learn how to add Open Graph metadata to a Next.js page using the Head component so social media platforms show a rich preview when the page is shared.
📋 What You'll Learn
Create a Next.js page component with a basic HTML structure
Add a configuration variable for Open Graph image URL
Use the Head component to add Open Graph meta tags
Complete the page with a title and description meta tags
💡 Why This Matters
🌍 Real World
Adding Open Graph metadata helps social media platforms show rich previews when users share your website link, improving click rates and user engagement.
💼 Career
Web developers often need to optimize websites for social sharing and SEO by adding proper metadata in frameworks like Next.js.
Progress0 / 4 steps
1
Create the Next.js page component
Create a Next.js page component called HomePage that returns a main element with a heading Welcome to Sweet Treats Bakery.
NextJS
Hint
Use a function named HomePage that returns JSX with a main and h1 inside.
2
Add Open Graph image URL variable
Inside the HomePage component, create a constant called ogImageUrl and set it to "https://example.com/images/bakery-og.jpg".
NextJS
Hint
Declare a constant named ogImageUrl with the exact URL string inside the component.
3
Add Open Graph meta tags with Head
Import Head from next/head at the top. Inside the HomePage component's return, add a <Head> element with these meta tags: og:title set to "Sweet Treats Bakery", og:description set to "Delicious homemade pastries and cakes.", and og:image set to the ogImageUrl variable.
NextJS
Hint
Use the Head component to add meta tags with the exact properties and content values.
4
Add page title and description meta tags
Inside the Head component, add a <title> tag with text "Sweet Treats Bakery" and a meta tag with name="description" and content "Freshly baked goods every day." to complete the page metadata.
NextJS
Hint
Add a <title> tag and a meta tag with name="description" inside the Head component.
Practice
(1/5)
1. What is the main purpose of Open Graph metadata in a Next.js app?
easy
A. To improve the page loading speed
B. To handle user authentication
C. To add custom fonts to the page
D. To control how the page appears when shared on social media
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 the metadata export to automatically add Open Graph tags for sharing previews.
Final Answer:
To control how the page appears when shared on social media -> Option D
Quick Check:
Open Graph = Social media preview [OK]
Hint: Open Graph controls social media preview appearance [OK]
Common Mistakes:
Confusing Open Graph with performance optimization
Thinking it manages fonts or styles
Assuming it handles user login
2. Which is the correct way to define Open Graph metadata in a Next.js App Router page?
Why might the Open Graph image not appear correctly when shared?
medium
A. images should be an array, not a string
B. title property is missing
C. openGraph must be a function, not an object
D. metadata must be default export
Solution
Step 1: Check images property type
Open Graph expects images as 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 A
Quick Check:
images array required for Open Graph images [OK]
Hint: images must be an array even for one image [OK]
Common Mistakes:
Passing a string instead of array for images
Forgetting to export metadata as named export
Thinking openGraph should be a function
5. You want to add Open Graph metadata with a dynamic image URL based on a page prop imageUrl. Which approach correctly sets the metadata in a Next.js App Router page?