id
stringlengths
14
15
text
stringlengths
49
1.09k
source
stringlengths
46
101
4a5a91177da5-0
draftModeThe draftMode function allows you to detect Draft Mode inside a Server Component. app/page.js import { draftMode } from 'next/headers' export default function Page() { const { isEnabled } = draftMode() return ( <main> <h1>My Blog Post</h1> <p>Draft Mode is currently {isEnabled ? 'Enabled' : 'Disabled'}</p> </main> ) } Version History VersionChangesv13.4.0draftMode introduced.
https://nextjs.org/docs/app/api-reference/functions/draft-mode
3debed7a35b1-0
fetchNext.js extends the native Web fetch() API to allow each request on the server to set its own persistent caching semantics. In the browser, the cache option indicates how a fetch request will interact with the browser's HTTP cache. With this extension, cache indicates how a server-side fetch request will interact with the framework's persistent HTTP cache. You can call fetch with async and await directly within Server Components. app/page.tsx export default async function Page() { // This request should be cached until manually invalidated. // Similar to `getStaticProps`. // `force-cache` is the default and can be omitted. const staticData = await fetch(`https://...`, { cache: 'force-cache' }) // This request should be refetched on every request. // Similar to `getServerSideProps`. const dynamicData = await fetch(`https://...`, { cache: 'no-store' })
https://nextjs.org/docs/app/api-reference/functions/fetch
3debed7a35b1-1
// This request should be cached with a lifetime of 10 seconds. // Similar to `getStaticProps` with the `revalidate` option. const revalidatedData = await fetch(`https://...`, { next: { revalidate: 10 }, }) return <div>...</div> } fetch(url, options) Since Next.js extends the Web fetch() API, you can use any of the native options available. Further, Next.js polyfills fetch on both the client and the server, so you can use fetch in both Server and Client Components. options.cache Configure how the request should interact with Next.js HTTP cache. fetch(`https://...`, { cache: 'force-cache' | 'no-store' }) force-cache (default) - Next.js looks for a matching request in its HTTP cache.
https://nextjs.org/docs/app/api-reference/functions/fetch
3debed7a35b1-2
force-cache (default) - Next.js looks for a matching request in its HTTP cache. If there is a match and it is fresh, it will be returned from the cache. If there is no match or a stale match, Next.js will fetch the resource from the remote server and update the cache with the downloaded resource. no-store - Next.js fetches the resource from the remote server on every request without looking in the cache, and it will not update the cache with the downloaded resource. Good to know: If you don't provide a cache option, Next.js will default to force-cache, unless a dynamic function such as cookies() is used, in which case it will default to no-store. The no-cache option behaves the same way as no-store in Next.js. options.next.revalidate fetch(`https://...`, { next: { revalidate: false | 0 | number } })
https://nextjs.org/docs/app/api-reference/functions/fetch
3debed7a35b1-3
Set the cache lifetime of a resource (in seconds). false - Cache the resource indefinitely. Semantically equivalent to revalidate: Infinity. The HTTP cache may evict older resources over time. 0 - Prevent the resource from being cached. number - (in seconds) Specify the resource should have a cache lifetime of at most n seconds. Good to know: If an individual fetch() request sets a revalidate number lower than the default revalidate of a route, the whole route revalidation interval will be decreased. If two fetch requests with the same URL in the same route have different revalidate values, the lower value will be used. As a convenience, it is not necessary to set the cache option if revalidate is set to a number since 0 implies cache: 'no-store' and a positive value implies cache: 'force-cache'.
https://nextjs.org/docs/app/api-reference/functions/fetch
3debed7a35b1-4
Conflicting options such as { revalidate: 0, cache: 'force-cache' } or { revalidate: 10, cache: 'no-store' } will cause an error. Version History VersionChangesv13.0.0fetch introduced.
https://nextjs.org/docs/app/api-reference/functions/fetch
b35f8327fbd0-0
generateImageMetadataYou can use generateImageMetadata to generate different versions of one image or return multiple images for one route segment. This is useful for when you want to avoid hard-coding metadata values, such as for icons. Parameters generateImageMetadata function accepts the following parameters: params (optional) An object containing the dynamic route parameters object from the root segment down to the segment generateImageMetadata is called from. icon.tsx export function generateImageMetadata({ params, }: { params: { slug: string } }) { // ... } RouteURLparamsapp/shop/icon.js/shopundefinedapp/shop/[slug]/icon.js/shop/1{ slug: '1' }app/shop/[tag]/[item]/icon.js/shop/1/2{ tag: '1', item: '2' }app/shop/[...slug]/icon.js/shop/1/2{ slug: ['1', '2'] } Returns
https://nextjs.org/docs/app/api-reference/functions/generate-image-metadata
b35f8327fbd0-1
Returns The generateImageMetadata function should return an array of objects containing the image's metadata such as alt and size. In addition, each item must include an id value will be passed to the props of the image generating function. Image Metadata ObjectTypeidstring (required)altstringsize{ width: number; height: number }contentTypestring icon.tsx import { ImageResponse } from 'next/server' export function generateImageMetadata() { return [ { contentType: 'image/png', size: { width: 48, height: 48 }, id: 'small', }, { contentType: 'image/png', size: { width: 72, height: 72 }, id: 'medium', }, ] } export default function Icon({ id }: { id: string }) { return new ImageResponse(
https://nextjs.org/docs/app/api-reference/functions/generate-image-metadata
b35f8327fbd0-2
return new ImageResponse( ( <div style={{ width: '100%', height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', fontSize: 88, background: '#000', color: '#fafafa', }} > Icon {id} </div> ) ) } Examples Using external data This example uses the params object and external data to generate multiple Open Graph images for a route segment. app/products/[id]/opengraph-image.tsx import { ImageResponse } from 'next/server' import { getCaptionForImage, getOGImages } from '@/app/utils/images' export async function generateImageMetadata({ params, }: { params: { id: string } }) {
https://nextjs.org/docs/app/api-reference/functions/generate-image-metadata
b35f8327fbd0-3
params, }: { params: { id: string } }) { const images = await getOGImages(params.id) return images.map((image, idx) => ({ id: idx, size: { width: 1200, height: 600 }, alt: image.text, contentType: 'image/png', })) } export default async function Image({ params, id, }: { params: { id: string } id: number }) { const productId = params.id const imageId = id const text = await getCaptionForImage(productId, imageId) return new ImageResponse( ( <div style={ { // ... } } > {text} </div>
https://nextjs.org/docs/app/api-reference/functions/generate-image-metadata
b35f8327fbd0-4
} } > {text} </div> ) ) } Version History VersionChangesv13.3.0generateImageMetadata introduced.
https://nextjs.org/docs/app/api-reference/functions/generate-image-metadata
1d048b70a39d-0
Metadata Object and generateMetadata OptionsThis page covers all Config-based Metadata options with generateMetadata and the static metadata object. layout.tsx / page.tsx import { Metadata } from 'next' // either Static metadata export const metadata: Metadata = { title: '...', } // or Dynamic metadata export async function generateMetadata({ params }) { return { title: '...', } } Good to know: 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. The metadata object To define static metadata, export a Metadata object from a layout.js or page.js file. layout.tsx / page.tsx import { Metadata } from 'next' export const metadata: Metadata = { title: '...', description: '...', }
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-1
title: '...', description: '...', } export default function Page() {} See the 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. app/products/[id]/page.tsx import { Metadata, ResolvingMetadata } from 'next' type Props = { params: { id: string } searchParams: { [key: string]: string | string[] | undefined } } export async function generateMetadata( { params, searchParams }: Props, parent: ResolvingMetadata ): Promise<Metadata> { // read route params const id = params.id // fetch data
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-2
// read route params const id = params.id // 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) {} 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 object from the root segment down to the segment generateMetadata is called from. Examples:
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-3
RouteURLparamsapp/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. Examples: URLsearchParams/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 containing one or more metadata fields. Good to know:
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-4
generateMetadata should return a Metadata object 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 rather than generateMetadata. When rendering a route, Next.js will automatically deduplicate fetch requests for the same data across generateMetadata, generateStaticParams, Layouts, Pages, and Server Components. React cache can be used if fetch is unavailable. searchParams are only available in page.js segments. The redirect() and notFound() Next.js methods can also be used inside generateMetadata. Metadata Fields title The title attribute is used to set the title of the document. It can be defined as a simple string or an optional template object. String layout.js / page.js export const metadata = { title: 'Next.js', } <head> output <title>Next.js</title> Template object
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-5
} <head> output <title>Next.js</title> Template object app/layout.tsx import { Metadata } from 'next' export const metadata: Metadata = { title: { template: '...', default: '...', absolute: '...', }, } Default title.default can be used to provide a fallback title to child route segments that don't define a title. app/layout.tsx import type { Metadata } from 'next' export const metadata: Metadata = { title: { default: 'Acme', }, } app/about/page.tsx import type { Metadata } from 'next' export const metadata: Metadata = {} // Output: <title>Acme</title> Template title.template can be used to add a prefix or a suffix to titles defined in child route segments.
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-6
app/layout.tsx import { Metadata } from 'next' export const metadata: Metadata = { title: { template: '%s | Acme', default: 'Acme', // a default is required when creating a template }, } app/about/page.tsx import { Metadata } from 'next' export const metadata: Metadata = { title: 'About', } // Output: <title>About | Acme</title> 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).
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-7
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. app/layout.tsx import { Metadata } from 'next' export const metadata: Metadata = { title: { template: '%s | Acme', }, } app/about/page.tsx import { Metadata } from 'next' export const metadata: Metadata = { title: { absolute: 'About', }, } // Output: <title>About</title> 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.
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-8
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 layout.js / page.js export const metadata = { description: 'The React Framework for the Web', } <head> output <meta name="description" content="The React Framework for the Web" /> Basic Fields layout.js / page.js export const metadata = { generator: 'Next.js', applicationName: 'Next.js',
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-9
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' }], colorScheme: 'dark', creator: 'Jiachi Liu', publisher: 'Sebastian Markbåge', formatDetection: { email: false, address: false, telephone: false, }, } <head> output <meta name="application-name" content="Next.js" /> <meta name="author" content="Seb" /> <link rel="author" href="https://nextjs.org" /> <meta name="author" content="Josh" /> <meta name="generator" content="Next.js" />
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-10
<meta name="generator" content="Next.js" /> <meta name="keywords" content="Next.js,React,JavaScript" /> <meta name="referrer" content="origin-when-cross-origin" /> <meta name="color-scheme" content="dark" /> <meta name="creator" content="Jiachi Liu" /> <meta name="publisher" content="Sebastian Markbåge" /> <meta name="format-detection" content="telephone=no, address=no, email=no" /> 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. If not configured, metadataBase is automatically populated with a default value.
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-11
If not configured, metadataBase is automatically populated with a default value. 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', }, } <head> output <link rel="canonical" href="https://acme.com" /> <link rel="alternate" hreflang="en-US" href="https://acme.com/en-US" /> <link rel="alternate" hreflang="de-DE" href="https://acme.com/de-DE" /> <meta property="og:image" content="https://acme.com/og-image.png" /> Good to know:
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-12
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) Default value If not configured, metadataBase has a default value
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-13
Default value If not configured, metadataBase has a default value When VERCEL_URL is detected: https://${process.env.VERCEL_URL} otherwise it falls back to http://localhost:${process.env.PORT || 3000}. When overriding the default, we recommend using environment variables to compute the URL. This allows configuring a URL for local development, staging, and production environments. 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: app/layout.tsx import { Metadata } from 'next' export const metadata: Metadata = { metadataBase: new URL('https://acme.com'), }
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-14
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 fieldResolved URL/https://acme.com./https://acme.compaymentshttps://acme.com/payments/paymentshttps://acme.com/payments./paymentshttps://acme.com/payments../paymentshttps://acme.com/paymentshttps://beta.acme.com/paymentshttps://beta.acme.com/payments openGraph 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', width: 800,
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-15
width: 800, height: 600, }, { url: 'https://nextjs.org/og-alt.png', width: 1800, height: 1600, alt: 'My custom alt', }, ], locale: 'en_US', type: 'website', }, } <head> output <meta property="og:title" content="Next.js" /> <meta property="og:description" content="The React Framework for the Web" /> <meta property="og:url" content="https://nextjs.org/" /> <meta property="og:site_name" content="Next.js" /> <meta property="og:locale" content="en_US" /> <meta property="og:image:url" content="https://nextjs.org/og.png" /> <meta property="og:image:width" content="800" />
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-16
<meta property="og:image:width" content="800" /> <meta property="og:image:height" content="600" /> <meta property="og:image:url" content="https://nextjs.org/og-alt.png" /> <meta property="og:image:width" content="1800" /> <meta property="og:image:height" content="1600" /> <meta property="og:image:alt" content="My custom alt" /> <meta property="og:type" content="website" /> 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'], }, }
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-17
authors: ['Seb', 'Josh'], }, } <head> output <meta property="og:title" content="Next.js" /> <meta property="og:description" content="The React Framework for the Web" /> <meta property="og:type" content="article" /> <meta property="article:published_time" content="2023-01-01T00:00:00.000Z" /> <meta property="article:author" content="Seb" /> <meta property="article:author" content="Josh" /> Good to know: It may be more convenient to use the file-based Metadata API 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 import type { Metadata } from 'next' export const metadata: Metadata = { robots: { index: false,
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-18
export const metadata: Metadata = { robots: { index: false, follow: true, nocache: true, googleBot: { index: true, follow: false, noimageindex: true, 'max-video-preview': -1, 'max-image-preview': 'large', 'max-snippet': -1, }, }, } <head> output <meta name="robots" content="noindex, follow, nocache" /> <meta name="googlebot" content="index, nofollow, noimageindex, max-video-preview:-1, max-image-preview:large, max-snippet:-1" /> icons
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-19
/> icons Good to know: We recommend using the file-based Metadata API 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. 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', }, }, } <head> output <link rel="shortcut icon" href="/shortcut-icon.png" /> <link rel="icon" href="/icon.png" /> <link rel="apple-touch-icon" href="/apple-icon.png" /> <link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-precomposed.png" />
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-20
href="/apple-touch-icon-precomposed.png" /> layout.js / page.js export const metadata = { icons: { icon: [{ url: '/icon.png' }, new URL('/icon.png', 'https://example.com')], 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', }, ], }, } <head> output <link rel="shortcut icon" href="/shortcut-icon.png" /> <link rel="icon" href="/icon.png" /> <link rel="apple-touch-icon" href="/apple-icon.png" /> <link
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-21
<link rel="apple-touch-icon" href="/apple-icon.png" /> <link rel="apple-touch-icon-precomposed" href="/apple-touch-icon-precomposed.png" /> <link rel="icon" href="https://example.com/icon.png" /> <link rel="apple-touch-icon" href="/apple-icon-x3.png" sizes="180x180" type="image/png" /> Good to know: The msapplication-* meta tags are no longer supported in Chromium builds of Microsoft Edge, and thus no longer needed. themeColor Learn more about theme-color. Simple theme color layout.js / page.js export const metadata = { themeColor: 'black', } <head> output <meta name="theme-color" content="black" /> With media attribute layout.js / page.js export const metadata = { themeColor: [
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-22
layout.js / page.js export const metadata = { themeColor: [ { media: '(prefers-color-scheme: light)', color: 'cyan' }, { media: '(prefers-color-scheme: dark)', color: 'black' }, ], } <head> output <meta name="theme-color" media="(prefers-color-scheme: light)" content="cyan" /> <meta name="theme-color" media="(prefers-color-scheme: dark)" content="black" /> manifest A web application manifest, as defined in the Web Application Manifest specification. layout.js / page.js export const metadata = { manifest: 'https://nextjs.org/manifest.json', } <head> output <link rel="manifest" href="https://nextjs.org/manifest.json" /> twitter Learn more about the Twitter Card markup reference. layout.js / page.js export const metadata = {
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-23
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'], }, } <head> output <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:site:id" content="1467726470533754880" /> <meta name="twitter:creator" content="@nextjs" /> <meta name="twitter:creator:id" content="1467726470533754880" /> <meta name="twitter:title" content="Next.js" />
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-24
<meta name="twitter:title" content="Next.js" /> <meta name="twitter:description" content="The React Framework for the Web" /> <meta name="twitter:image" content="https://nextjs.org/og.png" /> 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',
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-25
ipad: 'twitter_app://ipad', googleplay: 'twitter_app://googleplay', }, url: { iphone: 'https://iphone_url', ipad: 'https://ipad_url', }, }, }, } <head> output <meta name="twitter:site:id" content="1467726470533754880" /> <meta name="twitter:creator" content="@nextjs" /> <meta name="twitter:creator:id" content="1467726470533754880" /> <meta name="twitter:title" content="Next.js" /> <meta name="twitter:description" content="The React Framework for the Web" /> <meta name="twitter:card" content="app" /> <meta name="twitter:image" content="https://nextjs.org/og.png" /> <meta name="twitter:image:alt" content="Next.js Logo" />
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-26
<meta name="twitter:image:alt" content="Next.js Logo" /> <meta name="twitter:app:name:iphone" content="twitter_app" /> <meta name="twitter:app:id:iphone" content="twitter_app://iphone" /> <meta name="twitter:app:id:ipad" content="twitter_app://ipad" /> <meta name="twitter:app:id:googleplay" content="twitter_app://googleplay" /> <meta name="twitter:app:url:iphone" content="https://iphone_url" /> <meta name="twitter:app:url:ipad" content="https://ipad_url" /> <meta name="twitter:app:name:ipad" content="twitter_app" /> <meta name="twitter:app:name:googleplay" content="twitter_app" /> viewport
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-27
<meta name="twitter:app:name:googleplay" content="twitter_app" /> viewport Good to know: The viewport meta tag is automatically set with the following default values. Usually, manual configuration is unnecessary as the default is sufficient. However, the information is provided for completeness. layout.js / page.js export const metadata = { viewport: { width: 'device-width', initialScale: 1, maximumScale: 1, }, } <head> output <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> verification layout.js / page.js export const metadata = { verification: { google: 'google', yandex: 'yandex', yahoo: 'yahoo', other: { me: ['my-email', 'my-link'], }, },
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-28
me: ['my-email', 'my-link'], }, }, } <head> output <meta name="google-site-verification" content="google" /> <meta name="y_key" content="yahoo" /> <meta name="yandex-verification" content="yandex" /> <meta name="me" content="my-email" /> <meta name="me" content="my-link" /> appleWebApp 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', {
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-29
{ url: '/assets/startup/apple-touch-startup-image-1536x2008.png', media: '(device-width: 768px) and (device-height: 1024px)', }, ], }, } <head> output <meta name="apple-itunes-app" content="app-id=myAppStoreID, app-argument=myAppArgument" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-title" content="Apple Web App" /> <link href="/assets/startup/apple-touch-startup-image-768x1004.png" rel="apple-touch-startup-image" /> <link href="/assets/startup/apple-touch-startup-image-1536x2008.png"
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-30
href="/assets/startup/apple-touch-startup-image-1536x2008.png" media="(device-width: 768px) and (device-height: 1024px)" rel="apple-touch-startup-image" /> <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" /> alternates 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',
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-31
types: { 'application/rss+xml': 'https://nextjs.org/rss', }, }, } <head> output <link rel="canonical" href="https://nextjs.org" /> <link rel="alternate" hreflang="en-US" href="https://nextjs.org/en-US" /> <link rel="alternate" hreflang="de-DE" href="https://nextjs.org/de-DE" /> <link rel="alternate" media="only screen and (max-width: 600px)" href="https://nextjs.org/mobile" /> <link rel="alternate" type="application/rss+xml" href="https://nextjs.org/rss" /> appLinks layout.js / page.js export const metadata = { appLinks: { ios: { url: 'https://nextjs.org/ios',
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-32
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, }, }, } <head> output <meta property="al:ios:url" content="https://nextjs.org/ios" /> <meta property="al:ios:app_store_id" content="app_store_id" /> <meta property="al:android:package" content="com.example.android/package" /> <meta property="al:android:app_name" content="app_name_android" /> <meta property="al:web:url" content="https://nextjs.org/web" />
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-33
<meta property="al:web:url" content="https://nextjs.org/web" /> <meta property="al:web:should_fallback" content="true" /> archives Describes a collection of records, documents, or other materials of historical interest (source). layout.js / page.js export const metadata = { archives: ['https://nextjs.org/13'], } <head> output <link rel="archives" href="https://nextjs.org/13" /> assets layout.js / page.js export const metadata = { assets: ['https://nextjs.org/assets'], } <head> output <link rel="assets" href="https://nextjs.org/assets" /> bookmarks layout.js / page.js export const metadata = { bookmarks: ['https://nextjs.org/13'], }
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-34
bookmarks: ['https://nextjs.org/13'], } <head> output <link rel="bookmarks" href="https://nextjs.org/13" /> category layout.js / page.js export const metadata = { category: 'technology', } <head> output <meta name="category" content="technology" /> 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. layout.js / page.js export const metadata = { other: { custom: 'meta', }, } <head> output <meta name="custom" content="meta" /> 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.
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-35
MetadataRecommendation<meta http-equiv="...">Use appropriate HTTP Headers via redirect(), Middleware, Security Headers<base>Render the tag in the layout or page itself.<noscript>Render the tag in the layout or page itself.<style>Learn more about styling in Next.js.<script>Learn more about using scripts.<link rel="stylesheet" />import stylesheets directly in the layout or page itself.<link rel="preload />Use ReactDOM preload method<link rel="preconnect" />Use ReactDOM preconnect method<link rel="dns-prefetch" />Use ReactDOM prefetchDNS method Resource hints The <link> element has a number of rel keywords that can be used to hint to the browser that a external resource is likely to be needed. The browser uses this information to apply preloading optimizations depending on the keyword. While the Metadata API doesn't directly support these hints, you can use new ReactDOM methods to safely insert them into the <head> of the document.
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-36
app/preload-resources.tsx 'use client' import ReactDOM from 'react-dom' export function PreloadResources() { ReactDOM.preload('...', { as: '...' }) ReactDOM.preconnect('...', { crossOrigin: '...' }) ReactDOM.prefetchDNS('...') return null } <link rel="preload"> Start loading a resource early in the page rendering (browser) lifecycle. MDN Docs. ReactDOM.preload(href: string, options: { as: string }) <head> output <link rel="preload" href="..." as="..." /> <link rel="preconnect"> Preemptively initiate a connection to an origin. MDN Docs. ReactDOM.preconnect(href: string, options?: { crossOrigin?: string }) <head> output <link rel="preconnect" href="..." crossorigin /> <link rel="dns-prefetch">
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-37
<link rel="dns-prefetch"> Attempt to resolve a domain name before resources get requested. MDN Docs. ReactDOM.prefetchDNS(href: string) <head> output <link rel="dns-prefetch" href="..." /> Good to know: These methods are currently only supported in Client Components, which are still Server Side Rendered on initial page load. Next.js in-built features such as next/font, next/image and next/script automatically handle relevant resource hints. React 18.3 does not yet include type definitions for ReactDOM.preload, ReactDOM.preconnect, and ReactDOM.preconnectDNS. You can use // @ts-ignore as a temporary solution to avoid type errors. Types You can add type safety to your metadata by using the Metadata type. If you are using the built-in TypeScript plugin in your IDE, you do not need to manually add the type, but you can still explicitly add it if you want. metadata object
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-38
metadata object import type { Metadata } from 'next' export const metadata: Metadata = { title: 'Next.js', } generateMetadata function Regular function import type { Metadata } from 'next' export function generateMetadata(): Metadata { return { title: 'Next.js', } } Async function import type { Metadata } from 'next' export async function generateMetadata(): Promise<Metadata> { return { title: 'Next.js', } } With segment props import type { Metadata } from 'next' type Props = { params: { id: string } searchParams: { [key: string]: string | string[] | undefined } } export function generateMetadata({ params, searchParams }: Props): Metadata { return { title: 'Next.js',
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
1d048b70a39d-39
return { title: 'Next.js', } } export default function Page({ params, searchParams }: Props) {} With parent metadata import type { Metadata, ResolvingMetadata } from 'next' export async function generateMetadata( { params, searchParams }: Props, parent: ResolvingMetadata ): Promise<Metadata> { return { title: 'Next.js', } } JavaScript Projects For JavaScript projects, you can use JSDoc to add type safety. /** @type {import("next").Metadata} */ export const metadata = { title: 'Next.js', } Version History VersionChangesv13.2.0metadata and generateMetadata introduced.
https://nextjs.org/docs/app/api-reference/functions/generate-metadata
6ea27c9f4feb-0
generateStaticParamsThe generateStaticParams function can be used in combination with dynamic route segments to statically generate routes at build time instead of on-demand at request time. app/blog/[slug]/page.js // Return a list of `params` to populate the [slug] dynamic segment export async function generateStaticParams() { const posts = await fetch('https://.../posts').then((res) => res.json()) return posts.map((post) => ({ slug: post.slug, })) } // Multiple versions of this page will be statically generated // using the `params` returned by `generateStaticParams` export default function Page({ params }) { const { slug } = params // ... } Good to know You can use the dynamicParams segment config option to control what happens when a dynamic segment is visited that was not generated with generateStaticParams.
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-1
During next dev, generateStaticParams will be called when you navigate to a route. During next build, generateStaticParams runs before the corresponding Layouts or Pages are generated. During revalidation (ISR), generateStaticParams will not be called again. generateStaticParams replaces the getStaticPaths function in the Pages Router. Parameters options.params (optional) If multiple dynamic segments in a route use generateStaticParams, the child generateStaticParams function is executed once for each set of params the parent generates. The params object contains the populated params from the parent generateStaticParams, which can be used to generate the params in a child segment. Returns generateStaticParams should return an array of objects where each object represents the populated dynamic segments of a single route. Each property in the object is a dynamic segment to be filled in for the route.
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-2
Each property in the object is a dynamic segment to be filled in for the route. The properties name is the segment's name, and the properties value is what that segment should be filled in with. Example RoutegenerateStaticParams Return Type/product/[id]{ id: string }[]/products/[category]/[product]{ category: string, product: string }[]/products/[...slug]{ slug: string[] }[] Single Dynamic Segment app/product/[id]/page.tsx export function generateStaticParams() { return [{ id: '1' }, { id: '2' }, { id: '3' }] } // Three versions of this page will be statically generated // using the `params` returned by `generateStaticParams` // - /product/1 // - /product/2 // - /product/3
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-3
// - /product/2 // - /product/3 export default function Page({ params }: { params: { id: string } }) { const { id } = params // ... } Multiple Dynamic Segments app/products/[category]/[product]/page.tsx export function generateStaticParams() { return [ { category: 'a', product: '1' }, { category: 'b', product: '2' }, { category: 'c', product: '3' }, ] } // Three versions of this page will be statically generated // using the `params` returned by `generateStaticParams` // - /products/a/1 // - /products/b/2 // - /products/c/3 export default function Page({ params, }: { params: { category: string; product: string }
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-4
params, }: { params: { category: string; product: string } }) { const { category, product } = params // ... } Catch-all Dynamic Segment app/product/[...slug]/page.tsx export function generateStaticParams() { return [{ slug: ['a', '1'] }, { slug: ['b', '2'] }, { slug: ['c', '3'] }] } // Three versions of this page will be statically generated // using the `params` returned by `generateStaticParams` // - /product/a/1 // - /product/b/2 // - /product/c/3 export default function Page({ params }: { params: { slug: string[] } }) { const { slug } = params // ... } Examples Multiple Dynamic Segments in a Route
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-5
// ... } Examples Multiple Dynamic Segments in a Route You can generate params for dynamic segments above the current layout or page, but not below. For example, given the app/products/[category]/[product] route: app/products/[category]/[product]/page.js can generate params for both [category] and [product]. app/products/[category]/layout.js can only generate params for [category]. There are two approaches to generating params for a route with multiple dynamic segments: Generate params from the bottom up Generate multiple dynamic segments from the child route segment. app/products/[category]/[product]/page.tsx // Generate segments for both [category] and [product] export async function generateStaticParams() { const products = await fetch('https://.../products').then((res) => res.json()) return products.map((product) => ({ category: product.category.slug,
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-6
return products.map((product) => ({ category: product.category.slug, product: product.id, })) } export default function Page({ params, }: { params: { category: string; product: string } }) { // ... } Generate params from the top down Generate the parent segments first and use the result to generate the child segments. app/products/[category]/layout.tsx // Generate segments for [category] export async function generateStaticParams() { const products = await fetch('https://.../products').then((res) => res.json()) return products.map((product) => ({ category: product.category.slug, })) } export default function Layout({ params }: { params: { category: string } }) { // ... }
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-7
// ... } A child route segment's generateStaticParams function is executed once for each segment a parent generateStaticParams generates. The child generateStaticParams function can use the params returned from the parent generateStaticParams function to dynamically generate its own segments. app/products/[category]/[product]/page.tsx // Generate segments for [product] using the `params` passed from // the parent segment's `generateStaticParams` function export async function generateStaticParams({ params: { category }, }: { params: { category: string } }) { const products = await fetch( `https://.../products?category=${category}` ).then((res) => res.json()) return products.map((product) => ({ product: product.id, })) } export default function Page({ params, }: {
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
6ea27c9f4feb-8
})) } export default function Page({ params, }: { params: { category: string; product: string } }) { // ... } Good to know: When rendering a route, Next.js will automatically deduplicate fetch requests for the same data across generateMetadata, generateStaticParams, Layouts, Pages, and Server Components. React cache can be used if fetch is unavailable. Version History VersionChangesv13.0.0generateStaticParams introduced.
https://nextjs.org/docs/app/api-reference/functions/generate-static-params
b4a14755a81b-0
headersThe headers function allows you to read the HTTP incoming request headers from a Server Component. headers() This API extends the Web Headers API. It is read-only, meaning you cannot set / delete the outgoing request headers. app/page.tsx import { headers } from 'next/headers' export default function Page() { const headersList = headers() const referer = headersList.get('referer') return <div>Referer: {referer}</div> } Good to know: headers() is a Dynamic Function whose returned values cannot be known ahead of time. Using it in a layout or page will opt a route into dynamic rendering at request time. API Reference const headersList = headers() Parameters headers does not take any parameters. Returns headers returns a read-only Web Headers object. Headers.entries(): Returns an iterator allowing to go through all key/value pairs contained in this object.
https://nextjs.org/docs/app/api-reference/functions/headers
b4a14755a81b-1
Headers.entries(): Returns an iterator allowing to go through all key/value pairs contained in this object. Headers.forEach(): Executes a provided function once for each key/value pair in this Headers object. Headers.get(): Returns a String sequence of all the values of a header within a Headers object with a given name. Headers.has(): Returns a boolean stating whether a Headers object contains a certain header. Headers.keys(): Returns an iterator allowing you to go through all keys of the key/value pairs contained in this object. Headers.values(): Returns an iterator allowing you to go through all values of the key/value pairs contained in this object. Examples Usage with Data Fetching headers() can be used in combination with Suspense for Data Fetching. app/page.js import { headers } from 'next/headers' async function getUser() { const headersInstance = headers() const authorization = headersInstance.get('authorization') // Forward the authorization header
https://nextjs.org/docs/app/api-reference/functions/headers
b4a14755a81b-2
const authorization = headersInstance.get('authorization') // Forward the authorization header const res = await fetch('...', { headers: { authorization }, }) return res.json() } export default async function UserPage() { const user = await getUser() return <h1>{user.name}</h1> } Version History VersionChangesv13.0.0headers introduced.
https://nextjs.org/docs/app/api-reference/functions/headers
9de461750099-0
ImageResponseThe ImageResponse constructor allows you to generate dynamic images using JSX and CSS. This is useful for generating social media images such as Open Graph images, Twitter cards, and more. The following options are available for ImageResponse: import { ImageResponse } from 'next/server' new ImageResponse( element: ReactElement, options: { width?: number = 1200 height?: number = 630 emoji?: 'twemoji' | 'blobmoji' | 'noto' | 'openmoji' = 'twemoji', fonts?: { name: string, data: ArrayBuffer, weight: number, style: 'normal' | 'italic' }[] debug?: boolean = false // Options that will be passed to the HTTP response status?: number = 200 statusText?: string
https://nextjs.org/docs/app/api-reference/functions/image-response
9de461750099-1
status?: number = 200 statusText?: string headers?: Record<string, string> }, ) Supported CSS Properties Please refer to Satori’s documentation for a list of supported HTML and CSS features. Version History VersionChangesv13.3.0ImageResponse can be imported from next/server.v13.0.0ImageResponse introduced via @vercel/og package.
https://nextjs.org/docs/app/api-reference/functions/image-response
66143d2debff-0
NextRequestNextRequest extends the Web Request API with additional convenience methods. cookies Read or mutate the Set-Cookie header of the request. set(name, value) Given a name, set a cookie with the given value on the request. // Given incoming request /home // Set a cookie to hide the banner // request will have a `Set-Cookie:show-banner=false;path=/home` header request.cookies.set('show-banner', 'false') get(name) Given a cookie name, return the value of the cookie. If the cookie is not found, undefined is returned. If multiple cookies are found, the first one is returned. // Given incoming request /home // { name: 'show-banner', value: 'false', Path: '/home' } request.cookies.get('show-banner') getAll()
https://nextjs.org/docs/app/api-reference/functions/next-request
66143d2debff-1
request.cookies.get('show-banner') getAll() Given a cookie name, return the values of the cookie. If no name is given, return all cookies on the request. // Given incoming request /home // [ // { name: 'experiments', value: 'new-pricing-page', Path: '/home' }, // { name: 'experiments', value: 'winter-launch', Path: '/home' }, // ] request.cookies.getAll('experiments') // Alternatively, get all cookies for the request request.cookies.getAll() delete(name) Given a cookie name, delete the cookie from the request. // Returns true for deleted, false is nothing is deleted request.cookies.delete('experiments') has(name) Given a cookie name, return true if the cookie exists on the request. // Returns true if cookie exists, false if it does not request.cookies.has('experiments') clear()
https://nextjs.org/docs/app/api-reference/functions/next-request
66143d2debff-2
request.cookies.has('experiments') clear() Remove the Set-Cookie header from the request. request.cookies.clear() nextUrl Extends the native URL API with additional convenience methods, including Next.js specific properties. // Given a request to /home, pathname is /home request.nextUrl.pathname // Given a request to /home?name=lee, searchParams is { 'name': 'lee' } request.nextUrl.searchParams Version History VersionChangesv13.0.0useSearchParams introduced.
https://nextjs.org/docs/app/api-reference/functions/next-request
db80150bc65b-0
NextResponseNextResponse extends the Web Response API with additional convenience methods. cookies Read or mutate the Set-Cookie header of the response. set(name, value) Given a name, set a cookie with the given value on the response. // Given incoming request /home let response = NextResponse.next() // Set a cookie to hide the banner response.cookies.set('show-banner', 'false') // Response will have a `Set-Cookie:show-banner=false;path=/home` header return response get(name) Given a cookie name, return the value of the cookie. If the cookie is not found, undefined is returned. If multiple cookies are found, the first one is returned. // Given incoming request /home let response = NextResponse.next() // { name: 'show-banner', value: 'false', Path: '/home' } response.cookies.get('show-banner') getAll()
https://nextjs.org/docs/app/api-reference/functions/next-response
db80150bc65b-1
response.cookies.get('show-banner') getAll() Given a cookie name, return the values of the cookie. If no name is given, return all cookies on the response. // Given incoming request /home let response = NextResponse.next() // [ // { name: 'experiments', value: 'new-pricing-page', Path: '/home' }, // { name: 'experiments', value: 'winter-launch', Path: '/home' }, // ] response.cookies.getAll('experiments') // Alternatively, get all cookies for the response response.cookies.getAll() delete(name) Given a cookie name, delete the cookie from the response. // Given incoming request /home let response = NextResponse.next() // Returns true for deleted, false is nothing is deleted response.cookies.delete('experiments') json() Produce a response with the given JSON body.
https://nextjs.org/docs/app/api-reference/functions/next-response
db80150bc65b-2
json() Produce a response with the given JSON body. app/api/route.ts import { NextResponse } from 'next/server' export async function GET(request: Request) { return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 }) } redirect() Produce a response that redirects to a URL. import { NextResponse } from 'next/server' return NextResponse.redirect(new URL('/new', request.url)) The URL can be created and modified before being used in the NextResponse.redirect() method. For example, you can use the request.nextUrl property to get the current URL, and then modify it to redirect to a different URL. import { NextResponse } from 'next/server' // Given an incoming request... const loginUrl = new URL('/login', request.url) // Add ?from=/incoming-url to the /login URL
https://nextjs.org/docs/app/api-reference/functions/next-response
db80150bc65b-3
// Add ?from=/incoming-url to the /login URL loginUrl.searchParams.set('from', request.nextUrl.pathname) // And redirect to the new URL return NextResponse.redirect(loginUrl) rewrite() Produce a response that rewrites (proxies) the given URL while preserving the original URL. import { NextResponse } from 'next/server' // Incoming request: /about, browser shows /about // Rewritten request: /proxy, browser shows /about return NextResponse.rewrite(new URL('/proxy', request.url)) next() The next() method is useful for Middleware, as it allows you to return early and continue routing. import { NextResponse } from 'next/server' return NextResponse.next() You can also forward headers when producing the response: import { NextResponse } from 'next/server' // Given an incoming request...
https://nextjs.org/docs/app/api-reference/functions/next-response
db80150bc65b-4
import { NextResponse } from 'next/server' // Given an incoming request... const newHeaders = new Headers(request.headers) // Add a new header newHeaders.set('x-version', '123') // And produce a response with the new headers return NextResponse.next({ request: { // New request headers headers: newHeaders, }, })
https://nextjs.org/docs/app/api-reference/functions/next-response
9eeda12cf0af-0
notFoundThe notFound function allows you to render the not-found file within a route segment as well as inject a <meta name="robots" content="noindex" /> tag. notFound() Invoking the notFound() function throws a NEXT_NOT_FOUND error and terminates rendering of the route segment in which it was thrown. Specifying a not-found file allows you to gracefully handle such errors by rendering a Not Found UI within the segment. app/user/[id]/page.js import { notFound } from 'next/navigation' async function fetchUser(id) { const res = await fetch('https://...') if (!res.ok) return undefined return res.json() } export default async function Profile({ params }) { const user = await fetchUser(params.id) if (!user) { notFound() } // ... }
https://nextjs.org/docs/app/api-reference/functions/not-found
9eeda12cf0af-1
notFound() } // ... } Good to know: notFound() does not require you to use return notFound() due to using the TypeScript never type. Version History VersionChangesv13.0.0notFound introduced.
https://nextjs.org/docs/app/api-reference/functions/not-found
eb3aba3a1e25-0
redirectThe redirect function allows you to redirect the user to another URL. redirect can be used in Server Components, Client Components, Route Handlers, and Server Actions. If you need to redirect to a 404, use the notFound function instead. Parameters The redirect function accepts two arguments: redirect(path, type) ParameterTypeDescriptionpathstringThe URL to redirect to. Can be a relative or absolute path.type'replace' (default) or 'push' (default in Server Actions)The type of redirect to perform. By default, redirect will use push (adding a new entry to the browser history stack) in Server Actions) and replace (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the type parameter. The type parameter has no effect when used in Server Components. Returns redirect does not return any value. Example
https://nextjs.org/docs/app/api-reference/functions/redirect
eb3aba3a1e25-1
Returns redirect does not return any value. Example Invoking the redirect() function throws a NEXT_REDIRECT error and terminates rendering of the route segment in which it was thrown. app/team/[id]/page.js import { redirect } from 'next/navigation' async function fetchTeam(id) { const res = await fetch('https://...') if (!res.ok) return undefined return res.json() } export default async function Profile({ params }) { const team = await fetchTeam(params.id) if (!team) { redirect('/login') } // ... } Good to know: redirect does not require you to use return redirect() as it uses the TypeScript never type. VersionChangesv13.0.0redirect introduced.
https://nextjs.org/docs/app/api-reference/functions/redirect
ea4d089e49b4-0
revalidatePathrevalidatePath allows you to revalidate data associated with a specific path. This is useful for scenarios where you want to update your cached data without waiting for a revalidation period to expire. app/api/revalidate/route.ts import { NextRequest, NextResponse } from 'next/server' import { revalidatePath } from 'next/cache' export async function GET(request: NextRequest) { const path = request.nextUrl.searchParams.get('path') || '/' revalidatePath(path) return NextResponse.json({ revalidated: true, now: Date.now() }) } Good to know: revalidatePath is available in both Node.js and Edge runtimes.
https://nextjs.org/docs/app/api-reference/functions/revalidatePath
ea4d089e49b4-1
Good to know: revalidatePath is available in both Node.js and Edge runtimes. revalidatePath will revalidate all segments under a dynamic route segment. For example, if you have a dynamic segment /product/[id] and you call revalidatePath('/product/[id]'), then all segments under /product/[id] will be revalidated as requested. revalidatePath only invalidates the cache when the path is next visited. This means calling revalidatePath with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited. Parameters revalidatePath(path: string): void; path: A string representing the filesystem path associated with the data you want to revalidate. This is not the literal route segment (e.g. /product/123) but instead the path on the filesystem (e.g. /product/[id]). Returns
https://nextjs.org/docs/app/api-reference/functions/revalidatePath
ea4d089e49b4-2
Returns revalidatePath does not return any value. Examples Node.js Runtime app/api/revalidate/route.ts import { NextRequest, NextResponse } from 'next/server' import { revalidatePath } from 'next/cache' export async function GET(request: NextRequest) { const path = request.nextUrl.searchParams.get('path') || '/' revalidatePath(path) return NextResponse.json({ revalidated: true, now: Date.now() }) } Edge Runtime app/api/revalidate/route.ts import { NextRequest, NextResponse } from 'next/server' import { revalidatePath } from 'next/cache' export const runtime = 'edge' export async function GET(request: NextRequest) { const path = request.nextUrl.searchParams.get('path') || '/' revalidatePath(path)
https://nextjs.org/docs/app/api-reference/functions/revalidatePath
ea4d089e49b4-3
revalidatePath(path) return NextResponse.json({ revalidated: true, now: Date.now() }) }
https://nextjs.org/docs/app/api-reference/functions/revalidatePath
84313f942a21-0
revalidateTagrevalidateTag allows you to revalidate data associated with a specific cache tag. This is useful for scenarios where you want to update your cached data without waiting for a revalidation period to expire. app/api/revalidate/route.ts import { NextRequest, NextResponse } from 'next/server' import { revalidateTag } from 'next/cache' export async function GET(request: NextRequest) { const tag = request.nextUrl.searchParams.get('tag') revalidateTag(tag) return NextResponse.json({ revalidated: true, now: Date.now() }) } Good to know: revalidateTag is available in both Node.js and Edge runtimes. Parameters revalidateTag(tag: string): void; tag: A string representing the cache tag associated with the data you want to revalidate. You can add tags to fetch as follows:
https://nextjs.org/docs/app/api-reference/functions/revalidateTag
84313f942a21-1
You can add tags to fetch as follows: fetch(url, { next: { tags: [...] } }); Returns revalidateTag does not return any value. Examples Node.js Runtime app/api/revalidate/route.ts import { NextRequest, NextResponse } from 'next/server' import { revalidateTag } from 'next/cache' export async function GET(request: NextRequest) { const tag = request.nextUrl.searchParams.get('tag') revalidateTag(tag) return NextResponse.json({ revalidated: true, now: Date.now() }) } Edge Runtime app/api/revalidate/route.ts import { NextRequest, NextResponse } from 'next/server' import { revalidateTag } from 'next/cache' export const runtime = 'edge' export async function GET(request: NextRequest) {
https://nextjs.org/docs/app/api-reference/functions/revalidateTag
84313f942a21-2
export async function GET(request: NextRequest) { const tag = request.nextUrl.searchParams.get('tag') revalidateTag(tag) return NextResponse.json({ revalidated: true, now: Date.now() }) }
https://nextjs.org/docs/app/api-reference/functions/revalidateTag
cb9ff5bbd24c-0
useParamsuseParams is a Client Component hook that lets you read a route's dynamic params filled in by the current URL. app/example-client-component.tsx 'use client' import { useParams } from 'next/navigation' export default function ExampleClientComponent() { const params = useParams() // Route -> /shop/[tag]/[item] // URL -> /shop/shoes/nike-air-max-97 // `params` -> { tag: 'shoes', item: 'nike-air-max-97' } console.log(params) return <></> } Parameters const params = useParams() useParams does not take any parameters. Returns useParams returns an object containing the current route's filled in dynamic parameters. Each property in the object is an active dynamic segment.
https://nextjs.org/docs/app/api-reference/functions/use-params
cb9ff5bbd24c-1
Each property in the object is an active dynamic segment. The properties name is the segment's name, and the properties value is what the segment is filled in with. The properties value will either be a string or array of string's depending on the type of dynamic segment. If the route contains no dynamic parameters, useParams returns an empty object. If used in pages, useParams will return null. For example: RouteURLuseParams()app/shop/page.js/shopnullapp/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'] } Version History VersionChangesv13.3.0useParams introduced.
https://nextjs.org/docs/app/api-reference/functions/use-params
9085570e8c89-0
usePathnameusePathname is a Client Component hook that lets you read the current URL's pathname. app/example-client-component.tsx 'use client' import { usePathname } from 'next/navigation' export default function ExampleClientComponent() { const pathname = usePathname() return <p>Current pathname: {pathname}</p> } usePathname intentionally requires using a Client Component. It's important to note Client Components are not a de-optimization. They are an integral part of the Server Components architecture. For example, a Client Component with usePathname will be rendered into HTML on the initial page load. When navigating to a new route, this component does not need to be re-fetched. Instead, the component is downloaded once (in the client JavaScript bundle), and re-renders based on the current state. Good to know:
https://nextjs.org/docs/app/api-reference/functions/use-pathname
9085570e8c89-1
Good to know: Reading the current URL from a Server Component is not supported. This design is intentional to support layout state being preserved across page navigations. Compatibility mode: usePathname can return null when a fallback route is being rendered or when a pages directory page has been automatically statically optimized by Next.js and the router is not ready. Next.js will automatically update your types if it detects both an app and pages directory in your project. Parameters const pathname = usePathname() usePathname does not take any parameters. Returns usePathname returns a string of the current URL's pathname. For example: URLReturned value/'/'/dashboard'/dashboard'/dashboard?v=2'/dashboard'/blog/hello-world'/blog/hello-world' Examples Do something in response to a route change app/example-client-component.tsx 'use client' import { usePathname, useSearchParams } from 'next/navigation'
https://nextjs.org/docs/app/api-reference/functions/use-pathname
9085570e8c89-2
import { usePathname, useSearchParams } from 'next/navigation' function ExampleClientComponent() { const pathname = usePathname() const searchParams = useSearchParams() useEffect(() => { // Do something here... }, [pathname, searchParams]) } VersionChangesv13.0.0usePathname introduced.
https://nextjs.org/docs/app/api-reference/functions/use-pathname
486dbdce1797-0
useReportWebVitals The useReportWebVitals hook allows you to report Core Web Vitals, and can be used in combination with your analytics service. app/_components/web-vitals.js 'use client' import { useReportWebVitals } from 'next/web-vitals' export function WebVitals() { useReportWebVitals((metric) => { console.log(metric) }) }app/layout.js import { WebVitals } from './_components/web-vitals' export default function Layout({ children }) { return ( <html> <body> <WebVitals /> {children} </body> </html> ) }
https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals
486dbdce1797-1
{children} </body> </html> ) } Since the useReportWebVitals hook requires the "use client" directive, the most performant approach is to create a separate component that the root layout imports. This confines the client boundary exclusively to the WebVitals component. useReportWebVitals The metric object passed as the hook's argument consists of a number of properties: id: Unique identifier for the metric in the context of the current page load name: The name of the performance metric. Possible values include names of Web Vitals metrics (TTFB, FCP, LCP, FID, CLS) specific to a web application. delta: The difference between the current value and the previous value of the metric. The value is typically in milliseconds and represents the change in the metric's value over time.
https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals
486dbdce1797-2
entries: An array of Performance Entries associated with the metric. These entries provide detailed information about the performance events related to the metric. navigationType: Indicates the type of navigation that triggered the metric collection. Possible values include "navigate", "reload", "back_forward", and "prerender". rating: A qualitative rating of the metric value, providing an assessment of the performance. Possible values are "good", "needs-improvement", and "poor". The rating is typically determined by comparing the metric value against predefined thresholds that indicate acceptable or suboptimal performance. value: The actual value or duration of the performance entry, typically in milliseconds. The value provides a quantitative measure of the performance aspect being tracked by the metric. The source of the value depends on the specific metric being measured and can come from various Performance APIs. Web Vitals Web Vitals are a set of useful metrics that aim to capture the user
https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals
486dbdce1797-3
Web Vitals Web Vitals are a set of useful metrics that aim to capture the user experience of a web page. The following web vitals are all included: Time to First Byte (TTFB) First Contentful Paint (FCP) Largest Contentful Paint (LCP) First Input Delay (FID) Cumulative Layout Shift (CLS) Interaction to Next Paint (INP) You can handle all the results of these metrics using the name property. app/components/web-vitals.tsx 'use client' import { useReportWebVitals } from 'next/web-vitals' export function WebVitals() { useReportWebVitals((metric) => { switch (metric.name) { case 'FCP': { // handle FCP results } case 'LCP': { // handle LCP results
https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals
486dbdce1797-4
} case 'LCP': { // handle LCP results } // ... } }) } Usage on Vercel Vercel Speed Insights are automatically configured on Vercel deployments, and don't require the use of useReportWebVitals. This hook is useful in local development, or if you're using a different analytics service. Sending results to external systems You can send results to any endpoint to measure and track real user performance on your site. For example: useReportWebVitals((metric) => { const body = JSON.stringify(metric) const url = 'https://example.com/analytics' // Use `navigator.sendBeacon()` if available, falling back to `fetch()`. if (navigator.sendBeacon) { navigator.sendBeacon(url, body) } else {
https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals
486dbdce1797-5
navigator.sendBeacon(url, body) } else { fetch(url, { body, method: 'POST', keepalive: true }) } }) Good to know: If you use Google Analytics, using the id value can allow you to construct metric distributions manually (to calculate percentiles, etc.) useReportWebVitals(metric => { // Use `window.gtag` if you initialized Google Analytics as this example: // https://github.com/vercel/next.js/blob/canary/examples/with-google-analytics/pages/_app.js window.gtag('event', metric.name, { value: Math.round(metric.name === 'CLS' ? metric.value * 1000 : metric.value), // values must be integers event_label: metric.id, // id unique to current page load non_interaction: true, // avoids affecting bounce rate. }); }
https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals
486dbdce1797-6
non_interaction: true, // avoids affecting bounce rate. }); } Read more about sending results to Google Analytics.
https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals
a32e123ba449-0
useRouterThe useRouter hook allows you to programmatically change routes inside Client Components. Recommendation: Use the <Link> component for navigation unless you have a specific requirement for using useRouter. app/example-client-component.tsx 'use client' import { useRouter } from 'next/navigation' export default function Page() { const router = useRouter() return ( <button type="button" onClick={() => router.push('/dashboard')}> Dashboard </button> ) } useRouter() router.push(href: string): Perform a client-side navigation to the provided route. Adds a new entry into the browser’s history stack. router.replace(href: string): Perform a client-side navigation to the provided route without adding a new entry into the browser’s history stack.
https://nextjs.org/docs/app/api-reference/functions/use-router
a32e123ba449-1
router.refresh(): Refresh the current route. Making a new request to the server, re-fetching data requests, and re-rendering Server Components. The client will merge the updated React Server Component payload without losing unaffected client-side React (e.g. useState) or browser state (e.g. scroll position). router.prefetch(href: string): Prefetch the provided route for faster client-side transitions. router.back(): Navigate back to the previous route in the browser’s history stack using soft navigation. router.forward(): Navigate forwards to the next page in the browser’s history stack using soft navigation. Good to know: The push() and replace() methods will perform a soft navigation if the new route has been prefetched, and either, doesn't include dynamic segments or has the same dynamic parameters as the current route. next/link automatically prefetch routes as they become visible in the viewport.
https://nextjs.org/docs/app/api-reference/functions/use-router
a32e123ba449-2
next/link automatically prefetch routes as they become visible in the viewport. refresh() could re-produce the same result if fetch requests are cached. Other dynamic functions like cookies and headers could also change the response. Migrating from the pages directory: The new useRouter hook should be imported from next/navigation and not next/router The pathname string has been removed and is replaced by usePathname() The query object has been removed and is replaced by useSearchParams() router.events is not currently supported. See below. View the full migration guide. Examples Router Events You can listen for page changes by composing other Client Component hooks like usePathname and useSearchParams. app/components/navigation-events.js 'use client' import { useEffect } from 'react' import { usePathname, useSearchParams } from 'next/navigation' export function NavigationEvents() { const pathname = usePathname()
https://nextjs.org/docs/app/api-reference/functions/use-router
a32e123ba449-3
export function NavigationEvents() { const pathname = usePathname() const searchParams = useSearchParams() useEffect(() => { const url = `${pathname}?${searchParams}` console.log(url) // You can now use the current URL // ... }, [pathname, searchParams]) return null } Which can be imported into a layout. app/layout.js import { Suspense } from 'react' import { NavigationEvents } from './components/navigation-events' export default function Layout({ children }) { return ( <html lang="en"> <body> {children} <Suspense fallback={null}> <NavigationEvents /> </Suspense> </body> </html> ) }
https://nextjs.org/docs/app/api-reference/functions/use-router