Before you launch your Next.js application into the world, there’s one essential concept you need to master: SEO and metadata.
Metadata determines how your site:
Next.js provides a powerful, modern, and intuitive system for handling metadata. In this guide, we’ll break down:
Let’s get started.
Metadata controls:
This directly affects how your content is perceived by:
Next.js makes this extremely easy using two approaches:
/appBoth are powerful, but serve different use cases.
This is the most common approach.
In any layout.tsx or page.tsx, you can export a simple metadata object:
export const metadata = {title: "My Awesome Page",description: "This is an example of SEO-friendly metadata in Next.js.",openGraph: {title: "My Awesome Page",description: "Learn how to optimize SEO in Next.js.",images: ["/og-image.png"],},};
✔ Easy to control ✔ Versionable (inside code) ✔ Per-route metadata override ✔ Works perfectly for static pages
But what if you need metadata that changes based on dynamic content?
generateMetadata()Dynamic routes (like blogs, product pages, user profiles) often need metadata based on the content they load.
Next.js solves this with:
export async function generateMetadata({ params }) {const post = await getPost(params.id);return {title: post.title,description: post.summary,openGraph: {images: [post.thumbnail],},};}
page.tsxparams (like [id])This is ideal for:
Next.js also supports metadata defined by simply placing special files in the app/ directory.
Examples:
| File | Purpose |
|---|---|
favicon.ico | Browser tab icon |
icon.png | General site icon |
apple-touch-icon.png | iOS home screen icon |
opengraph-image.png | Social sharing image |
twitter-image.png | Twitter card image |
robots.txt | Crawling rules |
sitemap.xml | Search engine sitemap |
app/├── favicon.ico├── icon.png├── apple-touch-icon.png├── opengraph-image.png├── twitter-image.png├── robots.txt└── sitemap.xml
Next.js automatically generates appropriate <meta> tags from these files.
File-based metadata overrides config-based metadata.
If you have:
favicon.ico in app/
ANDmetadata.icon in codeThe file wins.
Understanding priority helps avoid conflicts:
page.tsx, nested layout.tsx)app/layout.tsx)So:
By combining:
You ensure your website:
This results in:
✔ Better visibility ✔ Higher click-through rates ✔ Stronger SEO performance ✔ Professional branding on social platforms
You’ve now learned:
With all this in place, you’re now fully prepared to build and deploy a professional, fully optimized Next.js application.