đˇď¸ Next.js Metadata Configuration: Your Websiteâs Name Tag
Imagine youâre at a big party with hundreds of people. Everyone is wearing a name tag that tells others who they are, what they do, and why they should come talk to them. Websites work the same way! When your website goes to the big party called âThe Internet,â it needs its own special name tag so search engines (like Google) and social media can introduce it properly.
In Next.js, we call this name tag Metadata. Letâs learn how to make the best name tag ever! đ
đ The Metadata API: Your Name Tag Factory
The Metadata API is like a factory that creates name tags for your website. Next.js gives you special tools to make these name tags easily.
Think of it this way:
- Your website = A person at a party
- Metadata = The name tag they wear
- Metadata API = The machine that prints the name tags
How It Works
The Metadata API lets you add information like:
- Whatâs your pageâs title?
- Whatâs your page about?
- What picture should show when shared?
// This is how we use the API
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: 'My Awesome Page',
description: 'This page is cool!'
}
đ Static Metadata: The Pre-Printed Name Tag
Static Metadata is like printing your name tag before the party. You know exactly what it will say, and it never changes.
When to Use Static Metadata?
Use it when your page content stays the same for everyone:
- Your homepage
- Your âAbout Usâ page
- Your âContactâ page
Example: A Static Name Tag
// app/about/page.tsx
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: 'About Our Company',
description: 'Learn about our story'
}
export default function AboutPage() {
return <h1>About Us</h1>
}
This page will always have the same title: âAbout Our Companyâ. Simple and easy! â¨
đ Dynamic Metadata: The Custom Name Tag
What if youâre at a party where your name tag needs to change based on who asks? Thatâs Dynamic Metadata!
Real Life Example
Imagine a store with 1000 products. Each product page needs its own unique name tag:
- Product 1: âBlue Shoes - $50â
- Product 2: âRed Hat - $25â
You canât print 1000 name tags by hand! You need a machine that creates them automatically.
graph TD A["User visits product page"] --> B["Check which product"] B --> C["Create custom metadata"] C --> D["Show the right name tag"]
âď¸ generateMetadata Function: The Smart Name Tag Maker
The generateMetadata function is your smart helper that creates name tags on the fly. Itâs like having a robot that looks at your data and makes the perfect name tag!
How It Works
// app/products/[id]/page.tsx
import type { Metadata } from 'next'
// The smart name tag maker
export async function generateMetadata(
{ params }: { params: { id: string } }
): Promise<Metadata> {
// Get product info from database
const product = await getProduct(params.id)
return {
title: product.name,
description: product.summary
}
}
export default function ProductPage() {
return <div>Product Details Here</div>
}
The Magic Explained đŞ
- Someone visits
/products/shoe-123 generateMetadatawakes up- It fetches the shoe info from your database
- It creates a perfect name tag: âCool Running Shoesâ
- Google sees this and knows what the page is about!
đ¸ OpenGraph Metadata: The Social Media Photo
When you share a link on Facebook, LinkedIn, or other sites, you see a nice preview with an image, title, and description. Thatâs OpenGraph Metadata!
Think of It Like This
OpenGraph is like attaching a photo to your name tag. Instead of just text, people can see a picture of you!
graph TD A["Share link on Facebook"] --> B["Facebook reads OpenGraph"] B --> C["Shows nice preview"] C --> D["People want to click!"]
Example: Making a Beautiful Share Preview
export const metadata: Metadata = {
title: 'Amazing Recipe',
description: 'Best chocolate cake ever',
openGraph: {
title: 'Amazing Chocolate Cake',
description: 'Learn to bake the best cake!',
images: ['/cake-photo.jpg'],
type: 'article'
}
}
What Each Part Means
| Property | What It Does | Example |
|---|---|---|
title |
Big text at top | âAmazing Cakeâ |
description |
Small text below | âBest recipeâŚâ |
images |
The preview picture | cake-photo.jpg |
type |
Kind of content | article, website |
đŚ Twitter Metadata: The Tweet-Ready Card
Twitter has its own special name tag system called Twitter Cards. Itâs like OpenGraphâs cousin, but specifically for Twitter!
Why Do We Need Both?
Twitter sometimes prefers its own tags. So we give Twitter exactly what it wants!
Example: Twitter-Perfect Sharing
export const metadata: Metadata = {
title: 'My Blog Post',
twitter: {
card: 'summary_large_image',
title: 'Read My Amazing Blog Post',
description: 'You will love this!',
images: ['/blog-banner.jpg'],
creator: '@myhandle'
}
}
Twitter Card Types
| Card Type | What You See |
|---|---|
summary |
Small image + text |
summary_large_image |
Big banner image |
player |
Video/audio player |
app |
App download button |
đą Viewport Export: The Screen Size Helper
The viewport tells phones and tablets how to display your page. Itâs like giving instructions: âHey phone, hereâs how big to make things!â
Without Viewport
Imagine reading a book meant for giants. The text is huge, you have to scroll sideways, and everything is a mess. Thatâs a website without viewport settings!
With Viewport
Everything fits perfectly on your screen. Magic! â¨
Example: Perfect Mobile Display
// app/layout.tsx
export const viewport = {
width: 'device-width',
initialScale: 1,
maximumScale: 1
}
What Each Part Means
| Setting | What It Does |
|---|---|
width: 'device-width' |
Use the screenâs real width |
initialScale: 1 |
Start at normal zoom (100%) |
maximumScale: 1 |
Donât let users zoom too much |
đ¨ Icons Metadata: Your Websiteâs Face
Icons are the tiny pictures that represent your website. You see them in:
- Browser tabs
- Bookmarks
- Phone home screens
- Search results
Think of It Like This
Icons are like your websiteâs profile picture. When someone saves your site to their phone, they see your icon!
Example: Adding Icons
export const metadata: Metadata = {
icons: {
icon: '/favicon.ico',
shortcut: '/shortcut-icon.png',
apple: '/apple-touch-icon.png',
other: {
rel: 'apple-touch-icon-precomposed',
url: '/apple-touch-icon-precomposed.png'
}
}
}
Icon Types Explained
| Icon Type | Where It Shows |
|---|---|
icon |
Browser tab |
shortcut |
Desktop shortcut |
apple |
iPhone/iPad home screen |
đ Putting It All Together
Hereâs a complete example using everything we learned:
// app/blog/[slug]/page.tsx
import type { Metadata } from 'next'
// Dynamic metadata for each blog post
export async function generateMetadata(
{ params }: { params: { slug: string } }
): Promise<Metadata> {
const post = await getPost(params.slug)
return {
title: post.title,
description: post.summary,
// Social sharing (OpenGraph)
openGraph: {
title: post.title,
description: post.summary,
images: [post.coverImage],
type: 'article'
},
// Twitter sharing
twitter: {
card: 'summary_large_image',
title: post.title,
images: [post.coverImage]
},
// Website icons
icons: {
icon: '/favicon.ico',
apple: '/apple-icon.png'
}
}
}
đŻ Quick Summary
| Feature | Purpose | When to Use |
|---|---|---|
| Metadata API | The system for adding metadata | Always! |
| Static Metadata | Fixed, unchanging info | Same content for everyone |
| Dynamic Metadata | Changes based on data | Product pages, blog posts |
| generateMetadata | Function to create dynamic metadata | When content comes from database |
| OpenGraph | Social media previews | Sharing on Facebook, LinkedIn |
| Twitter-specific previews | Sharing on Twitter | |
| Viewport | Mobile screen settings | Every website! |
| Icons | Favicons & app icons | Every website! |
đ You Did It!
Now you understand how to give your Next.js website the perfect name tag! Your pages will:
- â Look great in Google search results
- â Have beautiful social media previews
- â Work perfectly on mobile phones
- â Have nice icons everywhere
Remember: Good metadata = More people finding and clicking your website!
Youâre now a Metadata Master! đ
