Bird
Raised Fist0
NextJSframework~5 mins

Metadata in layouts in NextJS

Choose your learning style10 modes available
Introduction

Metadata helps describe your web page to browsers and search engines. Using metadata in layouts means you set this info once for many pages.

You want all pages in a section to share the same title prefix or description.
You want to add social media preview info for all pages inside a layout.
You want consistent keywords or author info across multiple pages.
You want to improve SEO by setting metadata in one place.
You want to avoid repeating metadata code on every page.
Syntax
NextJS
export const metadata = {
  title: 'Page Title',
  description: 'Page description here',
  openGraph: {
    title: 'OG Title',
    description: 'OG Description',
    url: 'https://example.com/page',
    siteName: 'Site Name',
    images: [
      {
        url: 'https://example.com/image.png',
        width: 800,
        height: 600,
        alt: 'Image description'
      }
    ],
    locale: 'en_US',
    type: 'website'
  },
  twitter: {
    card: 'summary_large_image',
    title: 'Twitter Title',
    description: 'Twitter Description',
    images: ['https://example.com/twitter-image.png']
  }
};

The metadata object is exported from a layout file in Next.js.

Next.js automatically uses this metadata for the HTML head of pages using this layout.

Examples
Simple metadata with just title and description.
NextJS
export const metadata = {
  title: 'My Blog',
  description: 'Welcome to my blog'
};
Adding Open Graph info for social media previews.
NextJS
export const metadata = {
  title: 'Shop - Electronics',
  openGraph: {
    title: 'Shop Electronics',
    description: 'Best electronics online',
    url: 'https://shop.com/electronics'
  }
};
Adding Twitter card metadata for better sharing on Twitter.
NextJS
export const metadata = {
  title: 'Portfolio',
  twitter: {
    card: 'summary_large_image',
    title: 'My Portfolio',
    images: ['https://portfolio.com/image.png']
  }
};
Sample Program

This layout sets metadata for all pages inside it. The metadata includes title, description, Open Graph, and Twitter info. The layout wraps page content with header and footer.

NextJS
import React from 'react';

export const metadata = {
  title: 'My Website',
  description: 'This is my website built with Next.js',
  openGraph: {
    title: 'My Website',
    description: 'This is my website built with Next.js',
    url: 'https://mywebsite.com',
    siteName: 'My Website',
    images: [
      {
        url: 'https://mywebsite.com/og-image.png',
        width: 1200,
        height: 630,
        alt: 'My Website Logo'
      }
    ],
    locale: 'en_US',
    type: 'website'
  },
  twitter: {
    card: 'summary_large_image',
    title: 'My Website',
    description: 'This is my website built with Next.js',
    images: ['https://mywebsite.com/twitter-image.png']
  }
};

export default function RootLayout({ children }) {
  return (
    <html lang="en">
      <body>
        <header>
          <h1>Welcome to My Website</h1>
        </header>
        <main>{children}</main>
        <footer>
          <p>Ā© 2024 My Website</p>
        </footer>
      </body>
    </html>
  );
}
OutputSuccess
Important Notes

Metadata in layouts applies to all pages using that layout automatically.

You can override metadata in individual pages if needed.

Use meaningful titles and descriptions to help users and search engines.

Summary

Metadata describes your page for browsers and search engines.

Setting metadata in layouts saves time and keeps info consistent.

Next.js uses the exported metadata object to add tags to the HTML head.

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

  1. 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.
  2. Step 2: Differentiate metadata from other layout tasks

    Styling, authentication, and state management are handled separately, not by metadata.
  3. Final Answer:

    To define page information like title and description for browsers and search engines -> Option C
  4. 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?
easy
A. export metadata = { title: 'Home' };
B. const metadata = { title: 'Home' }; export default metadata;
C. export function metadata() { return { title: 'Home' }; }
D. export const metadata = { title: 'Home' };

Solution

  1. Step 1: Recall Next.js metadata export syntax

    Next.js expects a named export called metadata as a constant object.
  2. Step 2: Check each option's syntax

    export const metadata = { title: 'Home' }; correctly exports a constant object. const metadata = { title: 'Home' }; export default metadata; exports default, which is incorrect. export function metadata() { return { title: 'Home' }; } exports a function, not an object. export metadata = { title: 'Home' }; has invalid export syntax.
  3. Final Answer:

    export const metadata = { title: 'Home' }; -> Option D
  4. Quick Check:

    Named const export = correct syntax [OK]
Hint: Use named const export for metadata object [OK]
Common Mistakes:
  • Using default export instead of named export
  • Exporting a function instead of an object
  • Incorrect export syntax without const
3. Given this layout file snippet, what will be the page title shown in the browser tab?
export const metadata = { title: 'Dashboard' };

export default function Layout({ children }) {
  return <main>{children}</main>;
}
medium
A. Dashboard
B. Home
C. Layout
D. No title will be shown

Solution

  1. Step 1: Identify the metadata title value

    The metadata object sets title to 'Dashboard'.
  2. Step 2: Understand Next.js behavior with metadata

    Next.js uses this metadata to set the HTML <title> tag, which appears in the browser tab.
  3. Final Answer:

    Dashboard -> Option A
  4. Quick Check:

    metadata.title = browser tab title [OK]
Hint: metadata.title sets browser tab title [OK]
Common Mistakes:
  • Assuming default title if metadata is present
  • Confusing layout function name with title
  • Thinking children content affects title
4. You wrote this layout metadata but the page title does not update:
const metadata = { title: 'Profile' };

export default function Layout({ children }) {
  return <main>{children}</main>;
}

What is the error?
medium
A. The title property must be inside a function
B. metadata must be exported with export const metadata
C. Layout must be a class component to use metadata
D. Children must be wrapped in a <section> tag

Solution

  1. Step 1: Check metadata export

    The metadata object is declared but not exported, so Next.js cannot read it.
  2. Step 2: Confirm correct export syntax

    Next.js requires export const metadata = { ... } to recognize metadata.
  3. Final Answer:

    metadata must be exported with export const metadata -> Option B
  4. 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

  1. Step 1: Understand metadata inheritance in Next.js

    Metadata in layouts is merged, so root layout metadata applies by default.
  2. Step 2: Override metadata in nested layouts or pages

    Exporting metadata in a page or nested layout overrides root metadata for that page.
  3. 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.
  4. Final Answer:

    Export metadata with description in root layout, then export metadata with a different description in the page layout -> Option A
  5. Quick Check:

    Root metadata sets default, page metadata overrides [OK]
Hint: Root metadata sets default; page metadata overrides [OK]
Common Mistakes:
  • Assuming page metadata is ignored if root exists
  • Trying to merge metadata manually in code
  • Using CSS to set metadata tags
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard