📖 Scenario: You are building a simple Next.js website. You want to improve how search engines see your pages by adding metadata like page titles and descriptions.
🎯 Goal: Create a Next.js page that uses the Metadata API to set the page title and description for SEO.
📋 What You'll Learn
Create a Next.js page component
Define a metadata object with title and description
Export the metadata object from the page
Render a simple heading in the page
💡 Why This Matters
🌍 Real World
SEO metadata helps search engines understand your page content better, improving your website's visibility and ranking.
💼 Career
Knowing how to add metadata in Next.js is important for frontend developers to build SEO-friendly web applications.
Progress0 / 4 steps
1
Create the Next.js page component
Create a file named page.tsx and write a React functional component called Page that returns a <main> element with a heading <h1>Welcome to My Site</h1>.
NextJS
Hint
Define a function named Page that returns JSX with a <main> and <h1> inside.
2
Add metadata object with title and description
Above the Page component, create a constant called metadata that is an object with title set to 'My Site Home' and description set to 'This is the homepage of my Next.js site.'.
NextJS
Hint
Create a constant named metadata with title and description properties as strings.
3
Export the metadata object
Make sure the metadata object is exported using export const metadata so Next.js can use it for SEO.
NextJS
Hint
Use export const metadata to export the metadata object.
4
Complete the page with metadata for SEO
Ensure the file page.tsx exports the metadata object and the default Page component with the heading inside <main>. This completes the SEO metadata setup.
NextJS
Hint
Check that both metadata and Page are exported correctly and the JSX is inside <main>.
Practice
(1/5)
1. What is the main purpose of the metadata export in a Next.js page?
easy
A. To define the page's layout and styling
B. To provide SEO information like title and description for the page
C. To handle user authentication on the page
D. To manage API routes for the page
Solution
Step 1: Understand the role of metadata in Next.js
The metadata export is used to set SEO-related information such as the page title and description.
Step 2: Differentiate from other page features
Layout, styling, authentication, and API routes are handled differently and not via metadata.
Final Answer:
To provide SEO information like title and description for the page -> Option B
Quick Check:
Metadata = SEO info [OK]
Hint: Metadata sets SEO info like title and description [OK]
Common Mistakes:
Confusing metadata with layout or styling
Thinking metadata handles API routes
Assuming metadata manages authentication
2. Which of the following is the correct way to export metadata in a Next.js page file?
easy
A. export const metadata = { title: 'Home' }
B. export default metadata = { title: 'Home' }
C. const metadata = { title: 'Home' }; export metadata
D. export metadata = { title: 'Home' }
Solution
Step 1: Recall correct export syntax for named exports
In Next.js, metadata is exported as a named export using export const metadata = {...}.
Step 2: Identify syntax errors in other options
Options A, B, and C use invalid export syntax or missing keywords.
A. metadata object must be wrapped in a React component
B. title property must be inside a function
C. title property should be 'pageTitle' instead of 'title'
D. metadata must be exported as a named export, not default
Solution
Step 1: Check export type for metadata
Next.js requires metadata to be a named export, not a default export.
Step 2: Verify other options
Title property does not need a function, React component, or different key name.
Final Answer:
metadata must be exported as a named export, not default -> Option D
Quick Check:
Named export required for metadata [OK]
Hint: Use 'export const metadata', not 'export default' [OK]
Common Mistakes:
Using default export instead of named export
Thinking metadata needs to be inside a component
Changing property names incorrectly
5. You want to add Open Graph metadata for social sharing in Next.js using the Metadata API. Which is the correct way to add the Open Graph title and image?