📖 Scenario: You are building a simple Next.js page for a blog. You want to add static metadata like the page title and description to improve SEO and browser tab labels.
🎯 Goal: Create a Next.js page component that uses the Metadata API to define static metadata including title and description.
📋 What You'll Learn
Create a page component file named page.tsx
Define a metadata export with title and description properties
Create a default exported React functional component that renders a heading
Use the Next.js Metadata API static metadata pattern
💡 Why This Matters
🌍 Real World
Static metadata helps search engines and browsers understand your page content, improving SEO and user experience.
💼 Career
Knowing how to use Next.js Metadata API is essential for frontend developers working on modern React apps with good SEO practices.
Progress0 / 4 steps
1
Create the page component file with a basic React component
Create a file named page.tsx and write a default exported React functional component called Page that returns an <h1> with the text Welcome to My Blog.
NextJS
Hint
Use a function named Page and return an <h1> element with the exact text.
2
Add a static metadata export with title and description
Add a named export called metadata as a constant object with title set to 'My Blog' and description set to 'A blog about coding and tech.' above the Page component.
NextJS
Hint
Define metadata as a constant object with the exact title and description values.
3
Ensure the metadata export is static and outside the component
Make sure the metadata export is declared outside and above the Page component function, so it is static and not inside the component body.
NextJS
Hint
Check that metadata is not inside the Page function but above it.
4
Complete the page with static metadata for Next.js
Confirm the file page.tsx exports the static metadata object with title and description, and the default Page component rendering the heading. This completes the static metadata setup for Next.js.
NextJS
Hint
Make sure all parts are present exactly as specified to complete the static metadata setup.
Practice
(1/5)
1. What is the primary purpose of exporting a metadata object in a Next.js page or layout?
easy
A. To provide static information like title and description for SEO and social sharing
B. To define dynamic routes for the page
C. To handle user authentication on the page
D. To style the page with CSS variables
Solution
Step 1: Understand the role of metadata in Next.js
The metadata object is used to add static information such as page title and description.
Step 2: Connect metadata to SEO and social sharing
This information helps search engines and social media platforms display the page correctly.
Final Answer:
To provide static information like title and description for SEO and social sharing -> Option A
Quick Check:
Metadata = SEO info [OK]
Hint: Metadata sets page info for SEO and sharing [OK]
Common Mistakes:
Confusing metadata with routing or styling
Thinking metadata handles user login
Assuming metadata is dynamic by default
2. Which of the following is the correct way to export static metadata in a Next.js page?
easy
A. export default { metadata: { title: 'Home' } }
B. export function metadata() { return { title: 'Home' } }
C. export const metadata = { title: 'Home' }
D. const metadata = { title: 'Home' }; export metadata
Solution
Step 1: Recall the syntax for static metadata export
Next.js expects a named export called metadata as an object.
Step 2: Check each option's syntax
export const metadata = { title: 'Home' } correctly exports a constant object named metadata. The other options use incorrect export forms or syntax.
The description value is written without quotes, which is invalid for strings in JavaScript.
Step 2: Understand JavaScript string syntax
Strings must be enclosed in quotes (' or ") to be valid. Missing quotes cause a syntax error.
Final Answer:
Missing quotes around the description string -> Option B
Quick Check:
Strings need quotes [OK]
Hint: Always quote string values in metadata [OK]
Common Mistakes:
Forgetting quotes around strings
Thinking metadata must be a function
Misplacing export keyword
5. You want to add static metadata with a title, description, and open graph image to a Next.js layout. Which is the correct way to include all these in the metadata export?