Metadata and SEO
Set titles and social tags with the metadata object and generateMetadata, add sitemap, robots and OG image files, and understand streaming metadata.
Metadata and SEO
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Export a static metadata object from a layout or page for fixed titles and descriptions, and export generateMetadata for values that depend on data or params.
- Both exports work only in Server Components, and Next.js turns them into the matching head tags for you.
- Wrap data functions used by both generateMetadata and the page in React.cache so the data is fetched once per request.
- File conventions such as sitemap.ts, robots.ts, opengraph-image.tsx, and icon files generate the SEO and sharing assets without hand-written route handlers.
- For dynamic pages Next.js streams metadata, but it waits for bots that expect metadata in the head, so crawlers still receive complete HTML.
What the head is for
Everything a search engine, a chat app, or a browser tab shows about your page before the user opens it comes from the <head>: the title, the description, the image on a link preview, the canonical URL. A page can be perfect and invisible if that markup is missing.
Next.js generates these tags from exports in your route files so you describe the page in TypeScript and the framework writes the HTML. Two tags are always added: the charset and viewport meta tags.
Static metadata
For a fixed title and description, export a Metadata object from a layout or page:
// app/blog/layout.tsx
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: 'My Blog',
description: 'Notes on building things.',
}
Metadata merges down the tree, with the more specific segment winning for the same field.
Metadata from data
A post page’s title is the post’s title, which the layout cannot know. Use generateMetadata. It receives params and searchParams, which are promises in Next.js 16:
// app/blog/[slug]/page.tsx
import type { Metadata } from 'next'
export async function generateMetadata({ params }: PageProps<'/blog/[slug]'>): Promise<Metadata> {
const { slug } = await params
const post = await getPost(slug)
return { title: post.title, description: post.description }
}
Both forms only work in Server Components. If a page needs a client hook, keep the metadata export in a server layout or page above it.
Fetching once
The page and generateMetadata often want the same post. For fetch, identical requests are memoized per render. For a database call, wrap the function in React.cache, as in the memoization lesson:
import { cache } from 'react'
export const getPost = cache(async (slug: string) => {
return db.query.posts.findFirst({ where: eq(posts.slug, slug) })
})
getPost is called twice per request but runs once.
Files for the rest of SEO
Some of SEO is not tags but files. Next.js has conventions for them:
app/sitemap.tsgeneratessitemap.xmlapp/robots.tsgeneratesrobots.txtopengraph-imageandtwitter-imagefiles (static, or generated with JSX throughImageResponsefromnext/og) produce social previews, with deeper folders overriding shallower onesfavicon.ico,icon, andapple-iconfiles for icons
The docs note that these special files stay static by default unless they use request-time APIs or dynamic config.
Metadata under Cache Components
Because generateMetadata renders as part of the route, the same rules apply. The docs say that inside generateMetadata, uncached fetches or runtime data access surface the same insights and errors as in your page. If it fetches a post, cache that fetch with use cache so the metadata can be part of the static shell, or accept a streamed metadata.
Streaming metadata and crawlers
On dynamically rendered pages Next.js streams metadata separately and injects it into the HTML when generateMetadata resolves, so slow metadata does not block the UI. It turns this off for bots that expect metadata in the <head> in the first bytes, such as Twitterbot, Slackbot, and Bingbot, detected through the User-Agent header. You can adjust the list or disable streaming entirely with the htmlLimitedBots option. Prerendered pages are not streamed, because their metadata is resolved at build time.
For deeper structured data such as JSON-LD, the docs have a dedicated guide; this lesson only covers the metadata API itself.
What the video covers
PedroTech’s October 2025 video is a broad Next.js 16 SEO course. By its chapters: why SEO matters, project setup and approach, static metadata, dynamic metadata, robots.txt, sitemaps, canonical URLs and JSON-LD, and semantic HTML and testing. It runs 44 minutes and the description carries a sponsorship for a hosting platform. Codevolution’s eight-minute Next.js 15 runner-up covers static and dynamic metadata, plus metadata in client components, and is a good first pass.
Where this fits
That finishes the rendering strategies. The data side is covered, the mutation side is covered, and pages now have titles and previews. Next: how they look, starting with CSS Modules and global CSS.
Check yourself
When do you use generateMetadata instead of the metadata object?
When the title, description, or images depend on data or route params, such as a blog post's title, since the static object cannot know them and generateMetadata can await params and fetch.
Why wrap a post lookup in React.cache when both generateMetadata and the page call it?
A database function is not memoized automatically, so React.cache makes both calls in one request share a single query result.
Can a Client Component export a metadata object?
No. The metadata object and generateMetadata export are only supported in Server Components, so a page that needs a client-side hook should keep its metadata in a server layout or page.
What does Next.js do about streaming metadata for crawlers?
It streams metadata separately on dynamic pages so visual content is not blocked, but it disables streaming for bots like Twitterbot, Slackbot, and Bingbot that expect it in the head and serves the full resolved HTML.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.