📖 Scenario: You are building a simple Next.js website with a consistent layout. You want to add metadata like the page title and description that applies to all pages using a layout component.
🎯 Goal: Create a Next.js layout component that defines metadata for the entire site using the metadata export. This metadata should include a title and description. Then use this layout in your app.
📋 What You'll Learn
Create a layout component file named layout.js in the app directory
Add a metadata export object with title and description properties
Use the children prop to render nested page content inside the layout
Export the layout component as default
💡 Why This Matters
🌍 Real World
Websites often need consistent metadata for SEO and social sharing. Using layouts to define metadata helps keep it organized and reusable.
💼 Career
Understanding Next.js layouts and metadata is important for frontend developers working on modern React-based web applications.
Progress0 / 4 steps
1
Create the layout component file
Create a file named layout.js inside the app directory. Inside it, define a React functional component named RootLayout that accepts a children prop and returns a html element with a body containing the children. Export this component as default.
NextJS
Hint
Remember to wrap the children inside <html> and <body> tags and set the language attribute.
2
Add metadata export with title and description
In the layout.js file, add an exported constant named metadata that is an object with two properties: title set to 'My Next.js Site' and description set to 'A simple Next.js app with metadata in layout.'.
NextJS
Hint
Use export const metadata = { title: '...', description: '...' } syntax at the top of the file.
3
Use the layout in the app directory
Ensure the layout.js file is inside the app directory so Next.js uses it as the root layout for all pages. Confirm the file exports the RootLayout component as default and the metadata object is exported.
NextJS
Hint
Make sure the file is named exactly layout.js inside the app folder and exports both metadata and RootLayout.
4
Add a simple page to test the layout
Create a file named page.js inside the app directory. Export a default React functional component named HomePage that returns a <main> element with the text 'Welcome to My Next.js Site'. This page will use the layout and metadata you created.
NextJS
Hint
Remember to export the page component as default and return the <main> element with the exact text.
Practice
(1/5)
1. What is the main purpose of the metadata object in a Next.js layout?
easy
A. To manage client-side state in the layout
B. To style the layout components with CSS
C. To define page information like title and description for browsers and search engines
D. To handle user authentication in the layout
Solution
Step 1: Understand the role of metadata in Next.js
The metadata object is used to provide page details such as title and description that browsers and search engines use.
Step 2: Differentiate metadata from other layout tasks
Styling, authentication, and state management are handled separately, not by metadata.
Final Answer:
To define page information like title and description for browsers and search engines -> Option C
Quick Check:
Metadata = page info for browsers and SEO [OK]
Hint: Metadata sets page info, not styles or logic [OK]
Common Mistakes:
Confusing metadata with CSS styling
Thinking metadata manages user data
Assuming metadata controls client state
2. Which of the following is the correct way to export metadata in a Next.js layout file?
metadata must be exported with export const metadata -> Option B
Quick Check:
Export metadata for Next.js to use it [OK]
Hint: Always export metadata as named const [OK]
Common Mistakes:
Forgetting to export metadata
Using function or class for metadata
Changing children wrapper tags unnecessarily
5. You want to set a default description for all pages using a root layout metadata, but override it on a specific page. How should you structure your Next.js metadata exports?
hard
A. Export metadata with description in root layout, then export metadata with a different description in the page layout
B. Only export metadata in the root layout; page metadata is ignored
C. Merge metadata objects manually inside the root layout component
D. Use a global CSS file to set description meta tag
Solution
Step 1: Understand metadata inheritance in Next.js
Metadata in layouts is merged, so root layout metadata applies by default.
Step 2: Override metadata in nested layouts or pages
Exporting metadata in a page or nested layout overrides root metadata for that page.
Step 3: Confirm correct approach
Export metadata with description in root layout, then export metadata with a different description in the page layout correctly describes exporting metadata in root and overriding in page layout.
Final Answer:
Export metadata with description in root layout, then export metadata with a different description in the page layout -> Option A