Fullstack CourseLearn by building
Back to week 3

Topic

Add metadata and Open Graph images

Definition

Next.js metadata exports describe document and social-preview information, while Open Graph image routes can generate share images for a route.

In simpler words

Metadata is the title, description, and preview other tools see when people open, bookmark, search, or share a page.

Good metadata represents the current route accurately rather than repeating generic application copy.

After this you can

  • Set static title and description metadata
  • Generate data-dependent metadata
  • Explain the purpose of Open Graph images

Describe the route, not just the app

Static metadata is appropriate when a route’s description never depends on parameters. generateMetadata is appropriate when the title or description depends on a dynamic segment or server-rendered data.

Metadata should be concise, specific, and consistent with the visible page. Search engines and social clients may display it without any of the route’s page body.

Static Course metadata

import type { Metadata } from 'next';

export const metadata: Metadata = {
  title: 'Course | Next.js foundations',
  description: 'Learn App Router foundations in this full-stack monorepo.',
};

A stable route can export metadata directly from its page or layout.

Mistake: using page text as metadata

// Wrong — a generic or misleading preview
export const metadata = { title: 'Welcome' };

// Right — name the route’s actual purpose
export const metadata = { title: 'Tickets | Course' };

Metadata must identify the destination when it is seen outside the app.

Generate social previews honestly

Definition

An Open Graph image is a visual summary that social platforms may show beside a shared URL.

In simpler words

It should identify the route and brand without promising content or status the page cannot deliver.

Use a route-specific image when sharing behavior justifies the work; a clear default is better than a misleading custom image.

Keep generated image text large enough to remain useful in small social cards.

Live playground

Metadata playground

Write metadata for a ticket route and predict its social preview.

Export metadata / generateMetadata per route.

next-metadata-og

Keep in mind

  • Write a title that distinguishes the route.
  • Use generateMetadata only when route data changes metadata.
  • Make OG images truthful and legible at small sizes.

Test

Check your understanding

At least 10 questions — mix of concept, syntax, practical, and logic. Score ≥80% (enforced by the API) to save progress.

Checking your session…

15 questions · concept 5 · syntax 3 · practical 4 · logic 3

1. How do you set titles in App Router?
Concept
2. generateMetadata use?
Syntax
3. Open Graph images?
Practical
4. Why metadata matters?
Logic
5. Icons/favicons?
Concept
6. Per-segment metadata merges?
Practical
7. Don’t put secrets in metadata?
Syntax
8. Twitter/card fields?
Logic
9. Absolute canonical URLs?
Concept
10. Which is correct?
Practical
11. What does exporting metadata do?
Conceptintermediate
export const metadata = { title: 'Tickets' };
12. Why use generateMetadata instead of a static export?
Syntaxadvanced
export async function generateMetadata({ params }) {
  const t = await getTicket(params.id);
  return { title: t.title };
}
13. What is openGraph used for?
Practicalintermediate
export const metadata = {
  openGraph: { title: 'Post', images: ['/og.png'] },
};
14. What is wrong with this metadata?
Logicadvanced
export const metadata = { title: 'Dashboard', description: process.env.SECRET_KEY };
15. Why set metadataBase?
Conceptintermediate
export const metadata = { metadataBase: new URL('https://example.com') };

Checking your session…