Metadata helps search engines understand your page. It improves how your site shows up in search results.
Metadata API for SEO in NextJS
import { Metadata } from 'next'; export const metadata: Metadata = { title: 'Page Title', description: 'Page description for SEO', openGraph: { title: 'Open Graph Title', description: 'Open Graph description', url: 'https://example.com/page', images: [{ url: 'https://example.com/image.png' }], }, };
The metadata object is exported from your page or layout file.
Next.js automatically uses this metadata to set HTML meta tags.
export const metadata = { title: 'Home Page', description: 'Welcome to our homepage', };
export const metadata = { title: 'Blog Post', description: 'Read our latest blog post', openGraph: { title: 'Blog Post', description: 'Read our latest blog post', url: 'https://example.com/blog/post', images: [{ url: 'https://example.com/blog-image.png' }], }, };
export const metadata = { title: 'Contact Us', description: 'Get in touch with us', twitter: { card: 'summary_large_image', title: 'Contact Us', description: 'Get in touch with us', images: ['https://example.com/contact-image.png'], }, };
This Next.js page exports metadata for SEO. The page shows a heading and paragraph. The metadata sets the page title, description, and social preview info automatically.
import React from 'react'; export const metadata = { title: 'About Us', description: 'Learn more about our company', openGraph: { title: 'About Us', description: 'Learn more about our company', url: 'https://example.com/about', images: [{ url: 'https://example.com/about-image.png' }], }, }; export default function AboutPage() { return ( <main> <h1>About Us</h1> <p>Welcome to our company page.</p> </main> ); }
Metadata API works with Next.js App Router pages and layouts.
Always provide a descriptive title and description for better SEO.
Use Open Graph and Twitter metadata to improve social media sharing previews.
Metadata API lets you set SEO info easily in Next.js pages.
It improves search engine results and social media previews.
Export a metadata object with title, description, and social info.
Practice
metadata export in a Next.js page?Solution
Step 1: Understand the role of metadata in Next.js
Themetadataexport 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 viametadata.Final Answer:
To provide SEO information like title and description for the page -> Option BQuick Check:
Metadata = SEO info [OK]
- Confusing metadata with layout or styling
- Thinking metadata handles API routes
- Assuming metadata manages authentication
Solution
Step 1: Recall correct export syntax for named exports
In Next.js, metadata is exported as a named export usingexport const metadata = {...}.Step 2: Identify syntax errors in other options
Options A, B, and C use invalid export syntax or missing keywords.Final Answer:
export const metadata = { title: 'Home' } -> Option AQuick Check:
Named export with const = export const metadata = { title: 'Home' } [OK]
- Using default export instead of named export
- Missing 'const' keyword
- Incorrect export statement syntax
export const metadata = {
title: 'About Us - MySite',
description: 'Learn more about us'
}Solution
Step 1: Identify the title property in metadata
Thetitleproperty is set to 'About Us - MySite'. This is what appears in the browser tab.Step 2: Understand description does not affect title
Thedescriptionis for SEO and social previews, not the browser tab title.Final Answer:
About Us - MySite -> Option CQuick Check:
Title property sets browser tab title [OK]
- Confusing description with title
- Assuming default title if metadata is present
- Thinking title is empty if not set in layout
const metadata = {
title: 'Contact'
}
export default metadataWhat is the error?
Solution
Step 1: Check export type for metadata
Next.js requiresmetadatato 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 DQuick Check:
Named export required for metadata [OK]
- Using default export instead of named export
- Thinking metadata needs to be inside a component
- Changing property names incorrectly
Solution
Step 1: Recall Open Graph structure in Next.js metadata
Open Graph data is nested under theopenGraphkey with properties liketitleandimages(array of URLs).Step 2: Compare options for correct property names and structure
Only export const metadata = { openGraph: { title: 'My Page', images: ['https://example.com/image.png'] } } uses the correct nestedopenGraphobject withtitleandimagesarray.Final Answer:
export const metadata = { openGraph: { title: 'My Page', images: ['https://example.com/image.png'] } } -> Option AQuick Check:
Open Graph uses openGraph key with images array [OK]
- Using flat keys like ogTitle instead of nested openGraph
- Passing image as string instead of array
- Using incorrect property names like social or openGraphTitle
Ready to prove it?
Pass the test and this pattern moves unknown
