Skip to solution
mediumFrontend

How does the Metadata API work in Next.js (static and generateMetadata)?

497 views
01

Understand the problem

Declaring head tags from layouts/pages.

nextjsmetadataseogeneratemetadata
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Export a static metadata object or an async generateMetadata() function from a layout/page; Next.js renders the corresponding

Solution ready — 2 min read

Classified // press E to declassify

04

Read the code

Static + dynamic metadata
// static (layout.tsx)
export const metadata = {
  title: { default: 'My Site', template: '%s | My Site' },
  description: 'Welcome',
};

// dynamic (blog/[slug]/page.tsx) — fetch deduped with the page
export async function generateMetadata({ params }) {
  const { slug } = await params;
  const post = await getPost(slug);
  return { title: post.title, openGraph: { images: [post.cover] } };
}
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 55 of 95 decoded in the Next.js track. One more won't hurt.

Back to track