--- title: generateMetadata description: Learn how to add Metadata to your Next.js application for improved search engine optimization (SEO) and web shareability. related: title: Next Steps description: View all the Metadata API options. links: - app/api-reference/file-conventions/metadata - app/api-reference/functions/generate-viewport --- You can use the `metadata` object or the `generateMetadata` function to define metadata. ## The `metadata` object To define static metadata, export a [`Metadata` object](#metadata-fields) from a `layout.js` or `page.js` file. ```tsx filename="layout.tsx | page.tsx" switcher import type { Metadata } from 'next' export const metadata: Metadata = { title: '...', description: '...', } export default function Page() {} ``` ```jsx filename="layout.js | page.js" switcher export const metadata = { title: '...', description: '...', } export default function Page() {} ``` > See the [Metadata Fields](#metadata-fields) for a complete list of supported options. ## `generateMetadata` function Dynamic metadata depends on **dynamic information**, such as the current route parameters, external data, or `metadata` in parent segments, can be set by exporting a `generateMetadata` function that returns a [`Metadata` object](#metadata-fields). ```tsx filename="app/products/[id]/page.tsx" switcher import type { Metadata, ResolvingMetadata } from 'next' type Props = { params: Promise<{ id: string }> searchParams: Promise<{ [key: string]: string | string[] | undefined }> } export async function generateMetadata( { params, searchParams }: Props, parent: ResolvingMetadata ): Promise { // read route params const { id } = await params // fetch data const product = await fetch(`https://.../${id}`).then((res) => res.json()) // optionally access and extend (rather than replace) parent metadata const previousImages = (await parent).openGraph?.images || [] return { title: product.title, openGraph: { images: ['/some-specific-page-image.jpg', ...previousImages], }, } } export default function Page({ params, searchParams }: Props) {} ``` ```jsx filename="app/products/[id]/page.js" switcher export async function generateMetadata({ params, searchParams }, parent) { // read route params const { id } = await params // fetch data const product = await fetch(`https://.../${id}`).then((res) => res.json()) // optionally access and extend (rather than replace) parent metadata const previousImages = (await parent).openGraph?.images || [] return { title: product.title, openGraph: { images: ['/some-specific-page-image.jpg', ...previousImages], }, } } export default function Page({ params, searchParams }) {} ``` > **Good to know**: > > - Metadata can be added to `layout.js` and `page.js` files. > - Next.js will automatically resolve the metadata, and create the relevant `` tags for the page. > - The `metadata` object and `generateMetadata` function exports are **only supported in Server Components**. > - You cannot export both the `metadata` object and `generateMetadata` function from the same route segment. > - `fetch` requests inside `generateMetadata` are automatically [memoized](/docs/app/guides/caching#request-memoization) for the same data across `generateMetadata`, `generateStaticParams`, Layouts, Pages, and Server Components. > - Resolving `generateMetadata` is part of rendering the page. If the page can be pre-rendered and `generateMetadata` doesn't introduce dynamic behavior, its result is included in the page’s initial HTML. > - React [`cache` can be used](/docs/app/guides/caching#react-cache-function) if `fetch` is unavailable. > - [File-based metadata](/docs/app/api-reference/file-conventions/metadata) has the higher priority and will override the `metadata` object and `generateMetadata` function. ## Reference ### Parameters `generateMetadata` function accepts the following parameters: - `props` - An object containing the parameters of the current route: - `params` - An object containing the [dynamic route parameters](/docs/app/api-reference/file-conventions/dynamic-routes) object from the root segment down to the segment `generateMetadata` is called from. Examples: | Route | URL | `params` | | ------------------------------- | ----------- | ------------------------- | | `app/shop/[slug]/page.js` | `/shop/1` | `{ slug: '1' }` | | `app/shop/[tag]/[item]/page.js` | `/shop/1/2` | `{ tag: '1', item: '2' }` | | `app/shop/[...slug]/page.js` | `/shop/1/2` | `{ slug: ['1', '2'] }` | - `searchParams` - An object containing the current URL's [search params](https://developer.mozilla.org/docs/Learn/Common_questions/What_is_a_URL#parameters). Examples: | URL | `searchParams` | | --------------- | -------------------- | | `/shop?a=1` | `{ a: '1' }` | | `/shop?a=1&b=2` | `{ a: '1', b: '2' }` | | `/shop?a=1&a=2` | `{ a: ['1', '2'] }` | - `parent` - A promise of the resolved metadata from parent route segments. ### Returns `generateMetadata` should return a [`Metadata` object](#metadata-fields) containing one or more metadata fields. > **Good to know**: > > - If metadata doesn't depend on runtime information, it should be defined using the static [`metadata` object](#the-metadata-object) rather than `generateMetadata`. > - `fetch` requests are automatically [memoized](/docs/app/guides/caching#request-memoization) for the same data across `generateMetadata`, `generateStaticParams`, Layouts, Pages, and Server Components. React [`cache` can be used](/docs/app/guides/caching#react-cache-function) if `fetch` is unavailable. > - `searchParams` are only available in `page.js` segments. > - The [`redirect()`](/docs/app/api-reference/functions/redirect) and [`notFound()`](/docs/app/api-reference/functions/not-found) Next.js methods can also be used inside `generateMetadata`. ### Metadata Fields The following fields are supported: #### `title` The `title` attribute is used to set the title of the document. It can be defined as a simple [string](#string) or an optional [template object](#template). ##### String ```jsx filename="layout.js | page.js" export const metadata = { title: 'Next.js', } ``` ```html filename=" output" hideLineNumbers Next.js ``` ##### `default` `title.default` can be used to provide a **fallback title** to child route segments that don't define a `title`. ```tsx filename="app/layout.tsx" import type { Metadata } from 'next' export const metadata: Metadata = { title: { default: 'Acme', }, } ``` ```tsx filename="app/about/page.tsx" import type { Metadata } from 'next' export const metadata: Metadata = {} // Output: Acme ``` ##### `template` `title.template` can be used to add a prefix or a suffix to `titles` defined in **child** route segments. ```tsx filename="app/layout.tsx" switcher import type { Metadata } from 'next' export const metadata: Metadata = { title: { template: '%s | Acme', default: 'Acme', // a default is required when creating a template }, } ``` ```jsx filename="app/layout.js" switcher export const metadata = { title: { template: '%s | Acme', default: 'Acme', // a default is required when creating a template }, } ``` ```tsx filename="app/about/page.tsx" switcher import type { Metadata } from 'next' export const metadata: Metadata = { title: 'About', } // Output: About | Acme ``` ```jsx filename="app/about/page.js" switcher export const metadata = { title: 'About', } // Output: About | Acme ``` > **Good to know**: > > - `title.template` applies to **child** route segments and **not** the segment it's defined in. This means: > > - `title.default` is **required** when you add a `title.template`. > - `title.template` defined in `layout.js` will not apply to a `title` defined in a `page.js` of the same route segment. > - `title.template` defined in `page.js` has no effect because a page is always the terminating segment (it doesn't have any children route segments). > > - `title.template` has **no effect** if a route has not defined a `title` or `title.default`. ##### `absolute` `title.absolute` can be used to provide a title that **ignores** `title.template` set in parent segments. ```tsx filename="app/layout.tsx" switcher import type { Metadata } from 'next' export const metadata: Metadata = { title: { template: '%s | Acme', }, } ``` ```jsx filename="app/layout.js" switcher export const metadata = { title: { template: '%s | Acme', }, } ``` ```tsx filename="app/about/page.tsx" switcher import type { Metadata } from 'next' export const metadata: Metadata = { title: { absolute: 'About', }, } // Output: About ``` ```jsx filename="app/about/page.js" switcher export const metadata = { title: { absolute: 'About', }, } // Output: About ``` > **Good to know**: > > - `layout.js` > > - `title` (string) and `title.default` define the default title for child segments (that do not define their own `title`). It will augment `title.template` from the closest parent segment if it exists. > - `title.absolute` defines the default title for child segments. It ignores `title.template` from parent segments. > - `title.template` defines a new title template for child segments. > > - `page.js` > - If a page does not define its own title the closest parents resolved title will be used. > - `title` (string) defines the routes title. It will augment `title.template` from the closest parent segment if it exists. > - `title.absolute` defines the route title. It ignores `title.template` from parent segments. > - `title.template` has no effect in `page.js` because a page is always the terminating segment of a route. ### `description` ```jsx filename="layout.js | page.js" export const metadata = { description: 'The React Framework for the Web', } ``` ```html filename=" output" hideLineNumbers ``` ### Other fields ```jsx filename="layout.js | page.js" export const metadata = { generator: 'Next.js', applicationName: 'Next.js', referrer: 'origin-when-cross-origin', keywords: ['Next.js', 'React', 'JavaScript'], authors: [{ name: 'Seb' }, { name: 'Josh', url: 'https://nextjs.org' }], creator: 'Jiachi Liu', publisher: 'Sebastian Markbåge', formatDetection: { email: false, address: false, telephone: false, }, } ``` ```html filename=" output" hideLineNumbers ``` #### `metadataBase` `metadataBase` is a convenience option to set a base URL prefix for `metadata` fields that require a fully qualified URL. - `metadataBase` allows URL-based `metadata` fields defined in the **current route segment and below** to use a **relative path** instead of an otherwise required absolute URL. - The field's relative path will be composed with `metadataBase` to form a fully qualified URL. ```jsx filename="layout.js | page.js" export const metadata = { metadataBase: new URL('https://acme.com'), alternates: { canonical: '/', languages: { 'en-US': '/en-US', 'de-DE': '/de-DE', }, }, openGraph: { images: '/og-image.png', }, } ``` ```html filename=" output" hideLineNumbers ``` > **Good to know**: > > - `metadataBase` is typically set in root `app/layout.js` to apply to URL-based `metadata` fields across all routes. > - All URL-based `metadata` fields that require absolute URLs can be configured with a `metadataBase` option. > - `metadataBase` can contain a subdomain e.g. `https://app.acme.com` or base path e.g. `https://acme.com/start/from/here` > - If a `metadata` field provides an absolute URL, `metadataBase` will be ignored. > - Using a relative path in a URL-based `metadata` field without configuring a `metadataBase` will cause a build error. > - Next.js will normalize duplicate slashes between `metadataBase` (e.g. `https://acme.com/`) and a relative field (e.g. `/path`) to a single slash (e.g. `https://acme.com/path`) #### URL Composition URL composition favors developer intent over default directory traversal semantics. - Trailing slashes between `metadataBase` and `metadata` fields are normalized. - An "absolute" path in a `metadata` field (that typically would replace the whole URL path) is treated as a "relative" path (starting from the end of `metadataBase`). For example, given the following `metadataBase`: ```tsx filename="app/layout.tsx" switcher import type { Metadata } from 'next' export const metadata: Metadata = { metadataBase: new URL('https://acme.com'), } ``` ```jsx filename="app/layout.js" switcher export const metadata = { metadataBase: new URL('https://acme.com'), } ``` Any `metadata` fields that inherit the above `metadataBase` and set their own value will be resolved as follows: | `metadata` field | Resolved URL | | -------------------------------- | -------------------------------- | | `/` | `https://acme.com` | | `./` | `https://acme.com` | | `payments` | `https://acme.com/payments` | | `/payments` | `https://acme.com/payments` | | `./payments` | `https://acme.com/payments` | | `../payments` | `https://acme.com/payments` | | `https://beta.acme.com/payments` | `https://beta.acme.com/payments` | ### `openGraph` ```jsx filename="layout.js | page.js" export const metadata = { openGraph: { title: 'Next.js', description: 'The React Framework for the Web', url: 'https://nextjs.org', siteName: 'Next.js', images: [ { url: 'https://nextjs.org/og.png', // Must be an absolute URL width: 800, height: 600, }, { url: 'https://nextjs.org/og-alt.png', // Must be an absolute URL width: 1800, height: 1600, alt: 'My custom alt', }, ], videos: [ { url: 'https://nextjs.org/video.mp4', // Must be an absolute URL width: 800, height: 600, }, ], audio: [ { url: 'https://nextjs.org/audio.mp3', // Must be an absolute URL }, ], locale: 'en_US', type: 'website', }, } ``` ```html filename=" output" hideLineNumbers ``` ```jsx filename="layout.js | page.js" export const metadata = { openGraph: { title: 'Next.js', description: 'The React Framework for the Web', type: 'article', publishedTime: '2023-01-01T00:00:00.000Z', authors: ['Seb', 'Josh'], }, } ``` ```html filename=" output" hideLineNumbers ``` > **Good to know**: > > - It may be more convenient to use the [file-based Metadata API](/docs/app/api-reference/file-conventions/metadata/opengraph-image#image-files-jpg-png-gif) for Open Graph images. Rather than having to sync the config export with actual files, the file-based API will automatically generate the correct metadata for you. ### `robots` ```tsx filename="layout.tsx | page.tsx" import type { Metadata } from 'next' export const metadata: Metadata = { robots: { index: true, follow: true, nocache: false, googleBot: { index: true, follow: true, noimageindex: false, 'max-video-preview': -1, 'max-image-preview': 'large', 'max-snippet': -1, }, }, } ``` ```html filename=" output" hideLineNumbers ``` ### `icons` > **Good to know**: We recommend using the [file-based Metadata API](/docs/app/api-reference/file-conventions/metadata/app-icons#image-files-ico-jpg-png) for icons where possible. Rather than having to sync the config export with actual files, the file-based API will automatically generate the correct metadata for you. ```jsx filename="layout.js | page.js" export const metadata = { icons: { icon: '/icon.png', shortcut: '/shortcut-icon.png', apple: '/apple-icon.png', other: { rel: 'apple-touch-icon-precomposed', url: '/apple-touch-icon-precomposed.png', }, }, } ``` ```html filename=" output" hideLineNumbers ``` ```jsx filename="layout.js | page.js" export const metadata = { icons: { icon: [ { url: '/icon.png' }, new URL('/icon.png', 'https://example.com'), { url: '/icon-dark.png', media: '(prefers-color-scheme: dark)' }, ], shortcut: ['/shortcut-icon.png'], apple: [ { url: '/apple-icon.png' }, { url: '/apple-icon-x3.png', sizes: '180x180', type: 'image/png' }, ], other: [ { rel: 'apple-touch-icon-precomposed', url: '/apple-touch-icon-precomposed.png', }, ], }, } ``` ```html filename=" output" hideLineNumbers ``` > **Good to know**: The `msapplication-*` meta tags are no longer supported in Chromium builds of Microsoft Edge, and thus no longer needed. ### `themeColor` > **Deprecated**: The `themeColor` option in `metadata` is deprecated as of Next.js 14. Please use the [`viewport` configuration](/docs/app/api-reference/functions/generate-viewport) instead. ### `colorScheme` > **Deprecated**: The `colorScheme` option in `metadata` is deprecated as of Next.js 14. Please use the [`viewport` configuration](/docs/app/api-reference/functions/generate-viewport) instead. ### `manifest` A web application manifest, as defined in the [Web Application Manifest specification](https://developer.mozilla.org/docs/Web/Manifest). ```jsx filename="layout.js | page.js" export const metadata = { manifest: 'https://nextjs.org/manifest.json', } ``` ```html filename=" output" hideLineNumbers ``` ### `twitter` The Twitter specification is (surprisingly) used for more than just X (formerly known as Twitter). Learn more about the [Twitter Card markup reference](https://developer.x.com/en/docs/twitter-for-websites/cards/overview/markup). ```jsx filename="layout.js | page.js" export const metadata = { twitter: { card: 'summary_large_image', title: 'Next.js', description: 'The React Framework for the Web', siteId: '1467726470533754880', creator: '@nextjs', creatorId: '1467726470533754880', images: ['https://nextjs.org/og.png'], // Must be an absolute URL }, } ``` ```html filename=" output" hideLineNumbers ``` ```jsx filename="layout.js | page.js" export const metadata = { twitter: { card: 'app', title: 'Next.js', description: 'The React Framework for the Web', siteId: '1467726470533754880', creator: '@nextjs', creatorId: '1467726470533754880', images: { url: 'https://nextjs.org/og.png', alt: 'Next.js Logo', }, app: { name: 'twitter_app', id: { iphone: 'twitter_app://iphone', ipad: 'twitter_app://ipad', googleplay: 'twitter_app://googleplay', }, url: { iphone: 'https://iphone_url', ipad: 'https://ipad_url', }, }, }, } ``` ```html filename=" output" hideLineNumbers ``` ### `viewport` > **Deprecated**: The `viewport` option in `metadata` is deprecated as of Next.js 14. Please use the [`viewport` configuration](/docs/app/api-reference/functions/generate-viewport) instead. ### `verification` ```jsx filename="layout.js | page.js" export const metadata = { verification: { google: 'google', yandex: 'yandex', yahoo: 'yahoo', other: { me: ['my-email', 'my-link'], }, }, } ``` ```html filename=" output" hideLineNumbers ``` ### `appleWebApp` ```jsx filename="layout.js | page.js" export const metadata = { itunes: { appId: 'myAppStoreID', appArgument: 'myAppArgument', }, appleWebApp: { title: 'Apple Web App', statusBarStyle: 'black-translucent', startupImage: [ '/assets/startup/apple-touch-startup-image-768x1004.png', { url: '/assets/startup/apple-touch-startup-image-1536x2008.png', media: '(device-width: 768px) and (device-height: 1024px)', }, ], }, } ``` ```html filename=" output" hideLineNumbers ``` ### `alternates` ```jsx filename="layout.js | page.js" export const metadata = { alternates: { canonical: 'https://nextjs.org', languages: { 'en-US': 'https://nextjs.org/en-US', 'de-DE': 'https://nextjs.org/de-DE', }, media: { 'only screen and (max-width: 600px)': 'https://nextjs.org/mobile', }, types: { 'application/rss+xml': 'https://nextjs.org/rss', }, }, } ``` ```html filename=" output" hideLineNumbers ``` ### `appLinks` ```jsx filename="layout.js | page.js" export const metadata = { appLinks: { ios: { url: 'https://nextjs.org/ios', app_store_id: 'app_store_id', }, android: { package: 'com.example.android/package', app_name: 'app_name_android', }, web: { url: 'https://nextjs.org/web', should_fallback: true, }, }, } ``` ```html filename=" output" hideLineNumbers ``` ### `archives` Describes a collection of records, documents, or other materials of historical interest ([source](https://www.w3.org/TR/2011/WD-html5-20110113/links.html#rel-archives)). ```jsx filename="layout.js | page.js" export const metadata = { archives: ['https://nextjs.org/13'], } ``` ```html filename=" output" hideLineNumbers ``` ### `assets` ```jsx filename="layout.js | page.js" export const metadata = { assets: ['https://nextjs.org/assets'], } ``` ```html filename=" output" hideLineNumbers ``` ### `bookmarks` ```jsx filename="layout.js | page.js" export const metadata = { bookmarks: ['https://nextjs.org/13'], } ``` ```html filename=" output" hideLineNumbers ``` ### `category` ```jsx filename="layout.js | page.js" export const metadata = { category: 'technology', } ``` ```html filename=" output" hideLineNumbers ``` ### `facebook` You can connect a Facebook app or Facebook account to you webpage for certain Facebook Social Plugins [Facebook Documentation](https://developers.facebook.com/docs/plugins/comments/#moderation-setup-instructions) > **Good to know**: You can specify either appId or admins, but not both. ```jsx filename="layout.js | page.js" export const metadata = { facebook: { appId: '12345678', }, } ``` ```html filename=" output" hideLineNumbers ``` ```jsx filename="layout.js | page.js" export const metadata = { facebook: { admins: '12345678', }, } ``` ```html filename=" output" hideLineNumbers ``` If you want to generate multiple fb:admins meta tags you can use array value. ```jsx filename="layout.js | page.js" export const metadata = { facebook: { admins: ['12345678', '87654321'], }, } ``` ```html filename=" output" hideLineNumbers ``` ### `pinterest` You can enable or disable [Pinterest Rich Pins](https://developers.pinterest.com/docs/web-features/rich-pins-overview/) on your webpage. ```jsx filename="layout.js | page.js" export const metadata = { pinterest: { richPin: true, }, } ``` ```html filename=" output" hideLineNumbers ``` ### `other` All metadata options should be covered using the built-in support. However, there may be custom metadata tags specific to your site, or brand new metadata tags just released. You can use the `other` option to render any custom metadata tag. ```jsx filename="layout.js | page.js" export const metadata = { other: { custom: 'meta', }, } ``` ```html filename=" output" hideLineNumbers ``` If you want to generate multiple same key meta tags you can use array value. ```jsx filename="layout.js | page.js" export const metadata = { other: { custom: ['meta1', 'meta2'], }, } ``` ```html filename=" output" hideLineNumbers ``` ### Types You can add type safety to your metadata by using the `Metadata` type. If you are using the [built-in TypeScript plugin](/docs/app/api-reference/config/typescript) in your IDE, you do not need to manually add the type, but you can still explicitly add it if you want. #### `metadata` object ```tsx filename="layout.tsx | page.tsx" import type { Metadata } from 'next' export const metadata: Metadata = { title: 'Next.js', } ``` #### `generateMetadata` function ##### Regular function ```tsx filename="layout.tsx | page.tsx" import type { Metadata } from 'next' export function generateMetadata(): Metadata { return { title: 'Next.js', } } ``` ##### Async function ```tsx filename="layout.tsx | page.tsx" import type { Metadata } from 'next' export async function generateMetadata(): Promise { return { title: 'Next.js', } } ``` ##### With segment props ```tsx filename="layout.tsx | page.tsx" import type { Metadata } from 'next' type Props = { params: Promise<{ id: string }> searchParams: Promise<{ [key: string]: string | string[] | undefined }> } export function generateMetadata({ params, searchParams }: Props): Metadata { return { title: 'Next.js', } } export default function Page({ params, searchParams }: Props) {} ``` ##### With parent metadata ```tsx filename="layout.tsx | page.tsx" import type { Metadata, ResolvingMetadata } from 'next' export async function generateMetadata( { params, searchParams }: Props, parent: ResolvingMetadata ): Promise { return { title: 'Next.js', } } ``` ##### JavaScript Projects For JavaScript projects, you can use JSDoc to add type safety. ```js filename="layout.js | page.js" /** @type {import("next").Metadata} */ export const metadata = { title: 'Next.js', } ``` ### Unsupported Metadata The following metadata types do not currently have built-in support. However, they can still be rendered in the layout or page itself. | Metadata | Recommendation | | ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | `` | Use appropriate HTTP Headers via [`redirect()`](/docs/app/api-reference/functions/redirect), [Middleware](/docs/app/api-reference/file-conventions/middleware#nextresponse), [Security Headers](/docs/app/api-reference/config/next-config-js/headers) | | `` | Render the tag in the layout or page itself. | | `