How to add metadata

Here is a guide on how to add metadata. This entire documentation is referenced from Nextjs, so for a better view, please check this Nextjs tutorial - https://nextjs.org/docs/app/getting-started/metadata-and-og-imagesarrow-up-right

The Metadata APIs can be used to define your application metadata for improved SEO and web shareability and include:

  1. Special file conventionsarrow-up-right that can be used to add static or dynamically generated faviconsarrow-up-right and OG imagesarrow-up-right.

With all the options above, Next.js will automatically generate the relevant <head> tags for your page, which can be inspected in the browser's developer tools.

There are two default meta tags that are always added even if a route doesn't define metadata:

<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

The other metadata fields can be defined with the Metadata object (for static metadataarrow-up-right) or the generateMetadata function (for generated metadataarrow-up-right).

To define static metadata, export a Metadata objectarrow-up-right from a static layout.jsarrow-up-right or page.jsarrow-up-right file. For example, to add a title and description to the blog route:

app/blog/layout.tsx

You can view a full list of available options, in the generate metadata documentationarrow-up-right.

You can use generateMetadataarrow-up-right function to fetch metadata that depends on data. For example, to fetch the title and description for a specific blog post:

app/blog/[slug]/page.tsx

Behind-the-scenes, Next.js will stream metadata separately from the UI and inject the metadata into the HTML as soon as it's resolved.

Last updated

Was this helpful?