Assets, Metadata, and CSS

Metadata

What if we wanted to modify the metadata of the page, such as the <title> HTML tag?

<title> is part of the <head> HTML tag, so let's dive into how we can modify the <head> tag in a Next.js page.

Open pages/index.js in your editor and take a look at the following lines:

<Head>
  <title>Create Next App</title>
  <link rel="icon" href="/favicon.ico" />
</Head>

Notice that <Head> is used instead of the lowercase <head>. <Head> is a React Component that is built into Next.js. It allows you to modify the <head> of a page.

You can import the Head component from the next/head module.

Adding <Head> to first-post.js

We haven't added a <title> to our /posts/first-post route. Let's add one.

Open the pages/posts/first-post.js file.

First, import Head from next/head:

import Head from 'next/head'

Then, add it to the FirstPost component. For now, we‘ll add just the title tag:

export default function FirstPost() {
  return (
    <>
      <Head>
        <title>First Post</title>
      </Head></>
  )
}

Try accessing http://localhost:3000/posts/first-post. The browser tab should now say “First Post”. By using your browser’s developer tools, you should see that the title tag is added to <head>.

To learn more about next/head, check out the API reference documentation.

If you want to customize the <html>, for example to add the lang attribute, you can do so by creating a custom Document component. Learn more in the custom document documentation.