How to Use Head in Next.js for Page Metadata
In Next.js, use the
Head component from next/head to add elements like <title> and <meta> tags inside the HTML <head>. Place Head inside your page or component to customize the document head for that page.Syntax
The Head component is imported from next/head. You wrap any HTML elements you want to add to the document's <head> inside it. Common elements include <title>, <meta>, <link>, and <script>.
Example parts:
import Head from 'next/head': imports the component.<Head>...</Head>: wraps head elements.- Inside
Head, add tags like<title>or<meta>.
jsx
import Head from 'next/head' export default function Page() { return ( <> <Head> <title>Page Title</title> <meta name="description" content="Page description here" /> </Head> <main> <h1>Hello Next.js</h1> </main> </> ) }
Example
This example shows how to set a custom page title and meta description using the Head component in a Next.js page. When you open this page in a browser, the tab title changes and the meta description is added to the HTML head.
jsx
import Head from 'next/head' export default function Home() { return ( <> <Head> <title>Welcome to My Site</title> <meta name="description" content="This is a simple Next.js page with custom head tags." /> </Head> <main> <h1>Home Page</h1> <p>This page uses the Head component to set metadata.</p> </main> </> ) }
Output
The browser tab shows "Welcome to My Site" as the page title, and the page's HTML head includes the meta description tag.
Common Pitfalls
Some common mistakes when using Head in Next.js include:
- Not importing
Headfromnext/head. - Placing
Headoutside the React component return statement. - Adding multiple
<title>tags without realizing Next.js will keep the last one rendered. - Forgetting to wrap multiple elements inside a fragment
<></>or a parent element.
Correct usage ensures your page metadata updates properly.
jsx
/* Wrong way: Missing import and multiple titles */ export default function Wrong() { return ( <> <title>First Title</title> <title>Second Title</title> <main>Content</main> </> ) } /* Right way: Import Head and use it properly */ import Head from 'next/head' export default function Right() { return ( <> <Head> <title>Correct Title</title> </Head> <main>Content</main> </> ) }
Quick Reference
| Usage | Description |
|---|---|
| import Head from 'next/head' | Import the Head component to use in your page. |
| Set the page title inside the head. | |
| Add meta tags for SEO or social sharing. | |
| Place inside your component return | Ensure head tags update per page or component. |
| Multiple components merge | Next.js combines all head tags on the page. |
Key Takeaways
Use the Head component from next/head to add elements inside the HTML head.
Wrap your head tags like and inside within your component.
Next.js merges multiple Head components on the same page automatically.
Always import Head before using it to avoid errors.
Avoid multiple tags outside Head or outside component return.