The Metadata API lets you add information about your web page that helps browsers and search engines understand it better.
Metadata API (static metadata) in NextJS
export const metadata = { title: 'Page Title', description: 'Page description here', icons: { icon: '/favicon.ico' }, openGraph: { title: 'Open Graph Title', description: 'Open Graph description', url: 'https://example.com/page', siteName: 'Site Name', images: [ { url: 'https://example.com/image.png', width: 800, height: 600 } ], locale: 'en_US', type: 'website' } };
This metadata object is exported from a page or layout file in Next.js.
Next.js uses this static metadata to generate <head> tags automatically.
export const metadata = { title: 'Home Page', description: 'Welcome to our homepage' };
export const metadata = { title: 'About Us', icons: { icon: '/about-icon.ico' } };
export const metadata = { title: 'Blog Post', openGraph: { title: 'Blog Post Title', description: 'Summary of the blog post', url: 'https://example.com/blog/post', images: [ { url: 'https://example.com/blog/post-image.png', width: 1200, height: 630 } ] } };
This Next.js page exports static metadata that sets the page title, description, icon, and Open Graph data. The metadata helps browsers and social media show the page nicely.
export const metadata = { title: 'My Next.js Page', description: 'This is a sample page using static metadata.', icons: { icon: '/favicon.ico' }, openGraph: { title: 'My Next.js Page', description: 'This is a sample page using static metadata.', url: 'https://mywebsite.com', siteName: 'My Website', images: [ { url: 'https://mywebsite.com/og-image.png', width: 1200, height: 630 } ], locale: 'en_US', type: 'website' } }; export default function Page() { return ( <main> <h1>Welcome to My Next.js Page</h1> <p>This page uses static metadata for SEO and social sharing.</p> </main> ); }
Static metadata is defined outside the component and exported as a constant named metadata.
Next.js automatically adds this metadata to the HTML <head> when rendering the page.
Use absolute URLs for images in Open Graph metadata to ensure they display correctly on social platforms.
The Metadata API lets you add important info about your page like title and description.
Export a metadata object from your page or layout for static metadata.
This helps with SEO and how your page looks when shared on social media.
Practice
metadata object in a Next.js page or layout?Solution
Step 1: Understand the role of metadata in Next.js
Themetadataobject 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 AQuick Check:
Metadata = SEO info [OK]
- Confusing metadata with routing or styling
- Thinking metadata handles user login
- Assuming metadata is dynamic by default
Solution
Step 1: Recall the syntax for static metadata export
Next.js expects a named export calledmetadataas an object.Step 2: Check each option's syntax
export const metadata = { title: 'Home' }correctly exports a constant object namedmetadata. The other options use incorrect export forms or syntax.Final Answer:
export const metadata = { title: 'Home' } -> Option CQuick Check:
Named export of object = export const metadata = { title: 'Home' } [OK]
- Using default export instead of named export
- Exporting a function instead of an object
- Incorrect export syntax causing errors
export const metadata = { title: 'About Us', description: 'Learn about our team' }What will be the page's title shown in the browser tab?
Solution
Step 1: Identify the title property in metadata
Themetadataobject has atitleset to 'About Us'.Step 2: Understand how Next.js uses metadata
Next.js uses thetitleproperty to set the browser tab's page title.Final Answer:
About Us -> Option AQuick Check:
Page title = metadata.title [OK]
- Confusing description with title
- Expecting metadata object name as title
- Assuming no title is set without explicit <title> tag
export const metadata = { title: 'Contact', description: Contact us page }Why does this cause an error?
Solution
Step 1: Check the syntax of the description value
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 BQuick Check:
Strings need quotes [OK]
- Forgetting quotes around strings
- Thinking metadata must be a function
- Misplacing export keyword
metadata export?Solution
Step 1: Understand openGraph image format in Next.js metadata
The openGraph images property expects an array of objects with aurlkey.Step 2: Check each option's openGraph structure
export const metadata = { title: 'Blog', description: 'Latest posts', openGraph: { images: [{ url: '/blog.png' }] } }correctly uses an array with an object containingurl. The other options use incorrect formats.Final Answer:
export const metadata = { title: 'Blog', description: 'Latest posts', openGraph: { images: [{ url: '/blog.png' }] } } -> Option DQuick Check:
openGraph images = array of {url} objects [OK]
- Using string instead of array for images
- Using wrong property name like image instead of images
- Not wrapping image url in an object
Ready to prove it?
Pass the test and this pattern moves unknown
