metadata
dict | url
stringlengths 19
103
| markdown
stringlengths 199
68.6k
|
---|---|---|
{
"contentLength": null,
"etag": "W/\"daab4bcb4fe0f706343b81cb874b21a1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/error | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [
error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Props](#props)
* [error](#error)
* [error.message](#errormessage)
* [error.digest](#errordigest)
* [reset](#reset)
* [global-error.js](#global-errorjs)
* [not-found.js](#not-foundjs)
* [Version History](#version-history)
* [Learn more about error handling](#learn-more-about-error-handling)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/error.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)error.js
error.js
========
An **error** file defines an error UI boundary for a route segment.
It is useful for catching **unexpected** errors that occur in Server Components and Client Components and displaying a fallback UI.
app/dashboard/error.tsx
TypeScript
JavaScriptTypeScript
'use client' // Error components must be Client Components
import { useEffect } from 'react'
export default function Error({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
useEffect(() => {
// Log the error to an error reporting service
console.error(error)
}, [error])
return (
<div>
<h2>Something went wrong!</h2>
<button
onClick={
// Attempt to recover by trying to re-render the segment
() => reset()
}
>
Try again
</button>
</div>
)
}
[Props](#props)
---------------
### [`error`](#error)
An instance of an [`Error`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error) object forwarded to the `error.js` Client Component.
#### [`error.message`](#errormessage)
The error message.
* For errors forwarded from Client Components, this will be the original Error's message.
* For errors forwarded from Server Components, this will be a generic error message to avoid leaking sensitive details. `errors.digest` can be used to match the corresponding error in server-side logs.
#### [`error.digest`](#errordigest)
An automatically generated hash of the error thrown in a Server Component. It can be used to match the corresponding error in server-side logs.
### [`reset`](#reset)
A function to reset the error boundary. When executed, the function will try to re-render the Error boundary's contents. If successful, the fallback error component is replaced with the result of the re-render.
Can be used to prompt the user to attempt to recover from the error.
> **Good to know**:
>
> * `error.js` boundaries must be **[Client Components](/docs/app/building-your-application/rendering/client-components)**.
> * In Production builds, errors forwarded from Server Components will be stripped of specific error details to avoid leaking sensitive information.
> * An `error.js` boundary will **not** handle errors thrown in a `layout.js` component in the **same** segment because the error boundary is nested **inside** that layouts component.
> * To handle errors for a specific layout, place an `error.js` file in the layouts parent segment.
> * To handle errors within the root layout or template, use a variation of `error.js` called `app/global-error.js`.
[`global-error.js`](#global-errorjs)
------------------------------------
To specifically handle errors in root `layout.js`, use a variation of `error.js` called `app/global-error.js` located in the root `app` directory.
app/global-error.tsx
TypeScript
JavaScriptTypeScript
'use client'
export default function GlobalError({
error,
reset,
}: {
error: Error & { digest?: string }
reset: () => void
}) {
return (
<html>
<body>
<h2>Something went wrong!</h2>
<button onClick={() => reset()}>Try again</button>
</body>
</html>
)
}
> **Good to know**:
>
> * `global-error.js` replaces the root `layout.js` when active and so **must** define its own `<html>` and `<body>` tags.
> * While designing error UI, you may find it helpful to use the [React Developer Tools](https://react.dev/learn/react-developer-tools) to manually toggle Error boundaries.
[not-found.js](#not-foundjs)
----------------------------
The [`not-found`](https://nextjs.org/docs/app/api-reference/file-conventions/not-found) file is used to render UI when the `notFound()` function is thrown within a route segment.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.1.0`
`global-error` introduced.
`v13.0.0`
`error` introduced.
Learn more about error handling
-------------------------------
[
App Router
...
Routing
### Error Handling
Handle runtime errors by automatically wrapping route segments and their nested children in a React Error Boundary.
](/docs/app/building-your-application/routing/error-handling)
[Previous
default.js
](/docs/app/api-reference/file-conventions/default)
[Next
instrumentation.js
](/docs/app/api-reference/file-conventions/instrumentation)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2f84d4ab1289d99aedf648369947a1ec\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/optimizing/fonts | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [
Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Google Fonts](#google-fonts)
* [Specifying a subset](#specifying-a-subset)
* [Using Multiple Fonts](#using-multiple-fonts)
* [Local Fonts](#local-fonts)
* [With Tailwind CSS](#with-tailwind-css)
* [Preloading](#preloading)
* [Reusing fonts](#reusing-fonts)
* [API Reference](#api-reference)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/06-optimizing/03-fonts.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Optimizing](/docs/app/building-your-application/optimizing)Fonts
Font Optimization
=================
[**`next/font`**](/docs/app/api-reference/components/font) will automatically optimize your fonts (including custom fonts) and remove external network requests for improved privacy and performance.
> **🎥 Watch:** Learn more about using `next/font` → [YouTube (6 minutes)](https://www.youtube.com/watch?v=L8_98i_bMMA).
`next/font` includes **built-in automatic self-hosting** for _any_ font file. This means you can optimally load web fonts with zero layout shift, thanks to the underlying CSS `size-adjust` property used.
This new font system also allows you to conveniently use all Google Fonts with performance and privacy in mind. CSS and font files are downloaded at build time and self-hosted with the rest of your static assets. **No requests are sent to Google by the browser.**
[Google Fonts](#google-fonts)
-----------------------------
Automatically self-host any Google Font. Fonts are included in the deployment and served from the same domain as your deployment. **No requests are sent to Google by the browser.**
Get started by importing the font you would like to use from `next/font/google` as a function. We recommend using [variable fonts](https://fonts.google.com/variablefonts) for the best performance and flexibility.
app/layout.tsx
TypeScript
JavaScriptTypeScript
import { Inter } from 'next/font/google'
// If loading a variable font, you don't need to specify the font weight
const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}
If you can't use a variable font, you will **need to specify a weight**:
app/layout.tsx
TypeScript
JavaScriptTypeScript
import { Roboto } from 'next/font/google'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={roboto.className}>
<body>{children}</body>
</html>
)
}
You can specify multiple weights and/or styles by using an array:
app/layout.js
const roboto = Roboto({
weight: ['400', '700'],
style: ['normal', 'italic'],
subsets: ['latin'],
display: 'swap',
})
> **Good to know**: Use an underscore (\_) for font names with multiple words. E.g. `Roboto Mono` should be imported as `Roboto_Mono`.
### [Specifying a subset](#specifying-a-subset)
Google Fonts are automatically [subset](https://fonts.google.com/knowledge/glossary/subsetting). This reduces the size of the font file and improves performance. You'll need to define which of these subsets you want to preload. Failing to specify any subsets while [`preload`](/docs/app/api-reference/components/font#preload) is `true` will result in a warning.
This can be done by adding it to the function call:
app/layout.tsx
TypeScript
JavaScriptTypeScript
const inter = Inter({ subsets: ['latin'] })
View the [Font API Reference](/docs/app/api-reference/components/font) for more information.
### [Using Multiple Fonts](#using-multiple-fonts)
You can import and use multiple fonts in your application. There are two approaches you can take.
The first approach is to create a utility function that exports a font, imports it, and applies its `className` where needed. This ensures the font is preloaded only when it's rendered:
app/fonts.ts
TypeScript
JavaScriptTypeScript
import { Inter, Roboto_Mono } from 'next/font/google'
export const inter = Inter({
subsets: ['latin'],
display: 'swap',
})
export const roboto_mono = Roboto_Mono({
subsets: ['latin'],
display: 'swap',
})
app/layout.tsx
TypeScript
JavaScriptTypeScript
import { inter } from './fonts'
export default function Layout({ children }: { children: React.ReactNode }) {
return (
<html lang="en" className={inter.className}>
<body>
<div>{children}</div>
</body>
</html>
)
}
app/page.tsx
TypeScript
JavaScriptTypeScript
import { roboto_mono } from './fonts'
export default function Page() {
return (
<>
<h1 className={roboto_mono.className}>My page</h1>
</>
)
}
In the example above, `Inter` will be applied globally, and `Roboto Mono` can be imported and applied as needed.
Alternatively, you can create a [CSS variable](/docs/app/api-reference/components/font#variable) and use it with your preferred CSS solution:
app/layout.tsx
TypeScript
JavaScriptTypeScript
import { Inter, Roboto_Mono } from 'next/font/google'
import styles from './global.css'
const inter = Inter({
subsets: ['latin'],
variable: '--font-inter',
display: 'swap',
})
const roboto_mono = Roboto_Mono({
subsets: ['latin'],
variable: '--font-roboto-mono',
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={`${inter.variable} ${roboto_mono.variable}`}>
<body>
<h1>My App</h1>
<div>{children}</div>
</body>
</html>
)
}
app/global.css
html {
font-family: var(--font-inter);
}
h1 {
font-family: var(--font-roboto-mono);
}
In the example above, `Inter` will be applied globally, and any `<h1>` tags will be styled with `Roboto Mono`.
> **Recommendation**: Use multiple fonts conservatively since each new font is an additional resource the client has to download.
[Local Fonts](#local-fonts)
---------------------------
Import `next/font/local` and specify the `src` of your local font file. We recommend using [variable fonts](https://fonts.google.com/variablefonts) for the best performance and flexibility.
app/layout.tsx
TypeScript
JavaScriptTypeScript
import localFont from 'next/font/local'
// Font files can be colocated inside of `app`
const myFont = localFont({
src: './my-font.woff2',
display: 'swap',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={myFont.className}>
<body>{children}</body>
</html>
)
}
If you want to use multiple files for a single font family, `src` can be an array:
const roboto = localFont({
src: [
{
path: './Roboto-Regular.woff2',
weight: '400',
style: 'normal',
},
{
path: './Roboto-Italic.woff2',
weight: '400',
style: 'italic',
},
{
path: './Roboto-Bold.woff2',
weight: '700',
style: 'normal',
},
{
path: './Roboto-BoldItalic.woff2',
weight: '700',
style: 'italic',
},
],
})
View the [Font API Reference](/docs/app/api-reference/components/font) for more information.
[With Tailwind CSS](#with-tailwind-css)
---------------------------------------
`next/font` can be used with [Tailwind CSS](https://tailwindcss.com/) through a [CSS variable](/docs/app/api-reference/components/font#css-variables).
In the example below, we use the font `Inter` from `next/font/google` (you can use any font from Google or Local Fonts). Load your font with the `variable` option to define your CSS variable name and assign it to `inter`. Then, use `inter.variable` to add the CSS variable to your HTML document.
app/layout.tsx
TypeScript
JavaScriptTypeScript
import { Inter, Roboto_Mono } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
display: 'swap',
variable: '--font-inter',
})
const roboto_mono = Roboto_Mono({
subsets: ['latin'],
display: 'swap',
variable: '--font-roboto-mono',
})
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en" className={`${inter.variable} ${roboto_mono.variable}`}>
<body>{children}</body>
</html>
)
}
Finally, add the CSS variable to your [Tailwind CSS config](/docs/app/building-your-application/styling/tailwind-css#configuring-tailwind):
tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
'./app/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
fontFamily: {
sans: ['var(--font-inter)'],
mono: ['var(--font-roboto-mono)'],
},
},
},
plugins: [],
}
You can now use the `font-sans` and `font-mono` utility classes to apply the font to your elements.
[Preloading](#preloading)
-------------------------
When a font function is called on a page of your site, it is not globally available and preloaded on all routes. Rather, the font is only preloaded on the related routes based on the type of file where it is used:
* If it's a [unique page](/docs/app/building-your-application/routing/pages-and-layouts#pages), it is preloaded on the unique route for that page.
* If it's a [layout](/docs/app/building-your-application/routing/pages-and-layouts#layouts), it is preloaded on all the routes wrapped by the layout.
* If it's the [root layout](/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required), it is preloaded on all routes.
[Reusing fonts](#reusing-fonts)
-------------------------------
Every time you call the `localFont` or Google font function, that font is hosted as one instance in your application. Therefore, if you load the same font function in multiple files, multiple instances of the same font are hosted. In this situation, it is recommended to do the following:
* Call the font loader function in one shared file
* Export it as a constant
* Import the constant in each file where you would like to use this font
API Reference
-------------
Learn more about the next/font API.
[
App Router
...
Components
### Font
Optimizing loading web fonts with the built-in \`next/font\` loaders.
](/docs/app/api-reference/components/font)
[Previous
Videos
](/docs/app/building-your-application/optimizing/videos)
[Next
Metadata
](/docs/app/building-your-application/optimizing/metadata)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"90bcdb06ff936a21adb419cddcabd6ff\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/configuring/draft-mode | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [
Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Step 1: Create and access the Route Handler](#step-1-create-and-access-the-route-handler)
* [Securely accessing it from your Headless CMS](#securely-accessing-it-from-your-headless-cms)
* [Step 2: Update page](#step-2-update-page)
* [More Details](#more-details)
* [Clear the Draft Mode cookie](#clear-the-draft-mode-cookie)
* [Unique per next build](#unique-per-next-build)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/07-configuring/11-draft-mode.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Configuring](/docs/app/building-your-application/configuring)Draft Mode
Draft Mode
==========
Static rendering is useful when your pages fetch data from a headless CMS. However, it’s not ideal when you’re writing a draft on your headless CMS and want to view the draft immediately on your page. You’d want Next.js to render these pages at **request time** instead of build time and fetch the draft content instead of the published content. You’d want Next.js to switch to [dynamic rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering) only for this specific case.
Next.js has a feature called **Draft Mode** which solves this problem. Here are instructions on how to use it.
[Step 1: Create and access the Route Handler](#step-1-create-and-access-the-route-handler)
------------------------------------------------------------------------------------------
First, create a [Route Handler](/docs/app/building-your-application/routing/route-handlers). It can have any name - e.g. `app/api/draft/route.ts`
Then, import `draftMode` from `next/headers` and call the `enable()` method.
app/api/draft/route.ts
TypeScript
JavaScriptTypeScript
// route handler enabling draft mode
import { draftMode } from 'next/headers'
export async function GET(request: Request) {
draftMode().enable()
return new Response('Draft mode is enabled')
}
This will set a **cookie** to enable draft mode. Subsequent requests containing this cookie will trigger **Draft Mode** changing the behavior for statically generated pages (more on this later).
You can test this manually by visiting `/api/draft` and looking at your browser’s developer tools. Notice the `Set-Cookie` response header with a cookie named `__prerender_bypass`.
### [Securely accessing it from your Headless CMS](#securely-accessing-it-from-your-headless-cms)
In practice, you’d want to call this Route Handler _securely_ from your headless CMS. The specific steps will vary depending on which headless CMS you’re using, but here are some common steps you could take.
These steps assume that the headless CMS you’re using supports setting **custom draft URLs**. If it doesn’t, you can still use this method to secure your draft URLs, but you’ll need to construct and access the draft URL manually.
**First**, you should create a **secret token string** using a token generator of your choice. This secret will only be known by your Next.js app and your headless CMS. This secret prevents people who don’t have access to your CMS from accessing draft URLs.
**Second**, if your headless CMS supports setting custom draft URLs, specify the following as the draft URL. This assumes that your Route Handler is located at `app/api/draft/route.ts`
Terminal
https://<your-site>/api/draft?secret=<token>&slug=<path>
* `<your-site>` should be your deployment domain.
* `<token>` should be replaced with the secret token you generated.
* `<path>` should be the path for the page that you want to view. If you want to view `/posts/foo`, then you should use `&slug=/posts/foo`.
Your headless CMS might allow you to include a variable in the draft URL so that `<path>` can be set dynamically based on the CMS’s data like so: `&slug=/posts/{entry.fields.slug}`
**Finally**, in the Route Handler:
* Check that the secret matches and that the `slug` parameter exists (if not, the request should fail).
* Call `draftMode.enable()` to set the cookie.
* Then redirect the browser to the path specified by `slug`.
app/api/draft/route.ts
TypeScript
JavaScriptTypeScript
// route handler with secret and slug
import { draftMode } from 'next/headers'
import { redirect } from 'next/navigation'
export async function GET(request: Request) {
// Parse query string parameters
const { searchParams } = new URL(request.url)
const secret = searchParams.get('secret')
const slug = searchParams.get('slug')
// Check the secret and next parameters
// This secret should only be known to this route handler and the CMS
if (secret !== 'MY_SECRET_TOKEN' || !slug) {
return new Response('Invalid token', { status: 401 })
}
// Fetch the headless CMS to check if the provided `slug` exists
// getPostBySlug would implement the required fetching logic to the headless CMS
const post = await getPostBySlug(slug)
// If the slug doesn't exist prevent draft mode from being enabled
if (!post) {
return new Response('Invalid slug', { status: 401 })
}
// Enable Draft Mode by setting the cookie
draftMode().enable()
// Redirect to the path from the fetched post
// We don't redirect to searchParams.slug as that might lead to open redirect vulnerabilities
redirect(post.slug)
}
If it succeeds, then the browser will be redirected to the path you want to view with the draft mode cookie.
[Step 2: Update page](#step-2-update-page)
------------------------------------------
The next step is to update your page to check the value of `draftMode().isEnabled`.
If you request a page which has the cookie set, then data will be fetched at **request time** (instead of at build time).
Furthermore, the value of `isEnabled` will be `true`.
app/page.tsx
TypeScript
JavaScriptTypeScript
// page that fetches data
import { draftMode } from 'next/headers'
async function getData() {
const { isEnabled } = draftMode()
const url = isEnabled
? 'https://draft.example.com'
: 'https://production.example.com'
const res = await fetch(url)
return res.json()
}
export default async function Page() {
const { title, desc } = await getData()
return (
<main>
<h1>{title}</h1>
<p>{desc}</p>
</main>
)
}
That's it! If you access the draft Route Handler (with `secret` and `slug`) from your headless CMS or manually, you should now be able to see the draft content. And if you update your draft without publishing, you should be able to view the draft.
Set this as the draft URL on your headless CMS or access manually, and you should be able to see the draft.
Terminal
https://<your-site>/api/draft?secret=<token>&slug=<path>
[More Details](#more-details)
-----------------------------
### [Clear the Draft Mode cookie](#clear-the-draft-mode-cookie)
By default, the Draft Mode session ends when the browser is closed.
To clear the Draft Mode cookie manually, create a Route Handler that calls `draftMode().disable()`:
app/api/disable-draft/route.ts
TypeScript
JavaScriptTypeScript
import { draftMode } from 'next/headers'
export async function GET(request: Request) {
draftMode().disable()
return new Response('Draft mode is disabled')
}
Then, send a request to `/api/disable-draft` to invoke the Route Handler. If calling this route using [`next/link`](/docs/app/api-reference/components/link), you must pass `prefetch={false}` to prevent accidentally deleting the cookie on prefetch.
### [Unique per `next build`](#unique-per-next-build)
A new bypass cookie value will be generated each time you run `next build`.
This ensures that the bypass cookie can’t be guessed.
> **Good to know**: To test Draft Mode locally over HTTP, your browser will need to allow third-party cookies and local storage access.
[Previous
src Directory
](/docs/app/building-your-application/configuring/src-directory)
[Next
Content Security Policy
](/docs/app/building-your-application/configuring/content-security-policy)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"ebcdff7e9d2e8cc6f74f0c68d1895287\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/revalidateTag | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [
revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [Returns](#returns)
* [Examples](#examples)
* [Server Action](#server-action)
* [Route Handler](#route-handler)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/revalidateTag.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)revalidateTag
revalidateTag
=============
`revalidateTag` allows you to purge [cached data](/docs/app/building-your-application/caching) on-demand for a specific cache tag.
> **Good to know**:
>
> * `revalidateTag` is available in both [Node.js and Edge runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes).
> * `revalidateTag` only invalidates the cache when the path is next visited. This means calling `revalidateTag` with a dynamic route segment will not immediately trigger many revalidations at once. The invalidation only happens when the path is next visited.
[Parameters](#parameters)
-------------------------
revalidateTag(tag: string): void;
* `tag`: A string representing the cache tag associated with the data you want to revalidate. Must be less than or equal to 256 characters. This value is case-sensitive.
You can add tags to `fetch` as follows:
fetch(url, { next: { tags: [...] } });
[Returns](#returns)
-------------------
`revalidateTag` does not return any value.
[Examples](#examples)
---------------------
### [Server Action](#server-action)
app/actions.ts
TypeScript
JavaScriptTypeScript
'use server'
import { revalidateTag } from 'next/cache'
export default async function submit() {
await addPost()
revalidateTag('posts')
}
### [Route Handler](#route-handler)
app/api/revalidate/route.ts
TypeScript
JavaScriptTypeScript
import { NextRequest } from 'next/server'
import { revalidateTag } from 'next/cache'
export async function GET(request: NextRequest) {
const tag = request.nextUrl.searchParams.get('tag')
revalidateTag(tag)
return Response.json({ revalidated: true, now: Date.now() })
}
[Previous
revalidatePath
](/docs/app/api-reference/functions/revalidatePath)
[Next
unstable\_cache
](/docs/app/api-reference/functions/unstable_cache)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"db875d574478ce950f493a50a22c4e65\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/optimizing/bundle-analyzer | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [
Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Installation](#installation)
* [Analyzing your bundles](#analyzing-your-bundles)
* [Next Steps](#next-steps)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/06-optimizing/06-bundle-analyzer.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Optimizing](/docs/app/building-your-application/optimizing)Bundle Analyzer
Bundle Analyzer
===============
[`@next/bundle-analyzer`](https://www.npmjs.com/package/@next/bundle-analyzer) is a plugin for Next.js that helps you manage the size of your JavaScript modules. It generates a visual report of the size of each module and their dependencies. You can use the information to remove large dependencies, split your code, or only load some parts when needed, reducing the amount of data transferred to the client.
[Installation](#installation)
-----------------------------
Install the plugin by running the following command:
npm i @next/bundle-analyzer
# or
yarn add @next/bundle-analyzer
# or
pnpm add @next/bundle-analyzer
Then, add the bundle analyzer's settings to your `next.config.js`.
next.config.js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
/** @type {import('next').NextConfig} */
const nextConfig = {}
module.exports = withBundleAnalyzer(nextConfig)
[Analyzing your bundles](#analyzing-your-bundles)
-------------------------------------------------
Run the following command to analyze your bundles:
ANALYZE=true npm run build
# or
ANALYZE=true yarn build
# or
ANALYZE=true pnpm build
The report will open three new tabs in your browser, which you can inspect. Doing this regularly while you develop and before deploying your site can help you identify large bundles earlier, and architect your application to be more performant.
Next Steps
----------
Learn more about optimizing your application for production.
[
App Router
...
Deploying
### Production Checklist
Recommendations to ensure the best performance and user experience before taking your Next.js application to production.
](/docs/app/building-your-application/deploying/production-checklist)
[Previous
Scripts
](/docs/app/building-your-application/optimizing/scripts)
[Next
Lazy Loading
](/docs/app/building-your-application/optimizing/lazy-loading)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"1f2eabf8c6c05a0531cc1f5cb7f9abd3\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/layout | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [
layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Props](#props)
* [children (required)](#children-required)
* [params (optional)](#params-optional)
* [Good to know](#good-to-know)
* [Layouts do not receive searchParams](#layouts-do-not-receive-searchparams)
* [Root Layouts](#root-layouts)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/layout.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)layout.js
layout.js
=========
A **layout** is UI that is shared between routes.
app/dashboard/layout.tsx
TypeScript
JavaScriptTypeScript
export default function DashboardLayout({
children,
}: {
children: React.ReactNode
}) {
return <section>{children}</section>
}
A **root layout** is the top-most layout in the root `app` directory. It is used to define the `<html>` and `<body>` tags and other globally shared UI.
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
[Props](#props)
---------------
### [`children` (required)](#children-required)
Layout components should accept and use a `children` prop. During rendering, `children` will be populated with the route segments the layout is wrapping. These will primarily be the component of a child [Layout](/docs/app/building-your-application/routing/pages-and-layouts#pages) (if it exists) or [Page](/docs/app/building-your-application/routing/pages-and-layouts#pages), but could also be other special files like [Loading](/docs/app/building-your-application/routing/loading-ui-and-streaming) or [Error](/docs/app/building-your-application/routing/error-handling) when applicable.
### [`params` (optional)](#params-optional)
The [dynamic route parameters](/docs/app/building-your-application/routing/dynamic-routes) object from the root segment down to that layout.
Example
URL
`params`
`app/dashboard/[team]/layout.js`
`/dashboard/1`
`{ team: '1' }`
`app/shop/[tag]/[item]/layout.js`
`/shop/1/2`
`{ tag: '1', item: '2' }`
`app/blog/[...slug]/layout.js`
`/blog/1/2`
`{ slug: ['1', '2'] }`
For example:
app/shop/\[tag\]/\[item\]/layout.tsx
TypeScript
JavaScriptTypeScript
export default function ShopLayout({
children,
params,
}: {
children: React.ReactNode
params: {
tag: string
item: string
}
}) {
// URL -> /shop/shoes/nike-air-max-97
// `params` -> { tag: 'shoes', item: 'nike-air-max-97' }
return <section>{children}</section>
}
[Good to know](#good-to-know)
-----------------------------
### [Layouts do not receive `searchParams`](#layouts-do-not-receive-searchparams)
Unlike [Pages](/docs/app/api-reference/file-conventions/page), Layout components **do not** receive the `searchParams` prop. This is because a shared layout is [not re-rendered during navigation](/docs/app/building-your-application/routing/linking-and-navigating#4-partial-rendering) which could lead to stale `searchParams` between navigations.
When using client-side navigation, Next.js automatically only renders the part of the page below the common layout between two routes.
For example, in the following directory structure, `dashboard/layout.tsx` is the common layout for both `/dashboard/settings` and `/dashboard/analytics`:
![File structure showing a dashboard folder nesting a layout.tsx file, and settings and analytics folders with their own pages](/_next/image?url=%2Fdocs%2Flight%2Fshared-dashboard-layout.png&w=3840&q=75)![File structure showing a dashboard folder nesting a layout.tsx file, and settings and analytics folders with their own pages](/_next/image?url=%2Fdocs%2Fdark%2Fshared-dashboard-layout.png&w=3840&q=75)
When navigating from `/dashboard/settings` to `/dashboard/analytics`, `page.tsx` in `/dashboard/analytics` will rerender on the server, while `dashboard/layout.tsx` will **not** rerender because it's a common UI shared between the two routes.
This performance optimization allows navigation between pages that share a layout to be quicker as only the data fetching and rendering for the page has to run, instead of the entire route that could include shared layouts that fetch their own data.
Because `dashboard/layout.tsx` doesn't re-render, the `searchParams` prop in the layout Server Component might become **stale** after navigation.
* Instead, use the Page [`searchParams`](/docs/app/api-reference/file-conventions/page#searchparams-optional) prop or the [`useSearchParams`](/docs/app/api-reference/functions/use-search-params) hook in a Client Component, which is re-rendered on the client with the latest `searchParams`.
### [Root Layouts](#root-layouts)
* The `app` directory **must** include a root `app/layout.js`.
* The root layout **must** define `<html>` and `<body>` tags.
* You should **not** manually add `<head>` tags such as `<title>` and `<meta>` to root layouts. Instead, you should use the [Metadata API](/docs/app/api-reference/functions/generate-metadata) which automatically handles advanced requirements such as streaming and de-duplicating `<head>` elements.
* You can use [route groups](/docs/app/building-your-application/routing/route-groups) to create multiple root layouts.
* Navigating **across multiple root layouts** will cause a **full page load** (as opposed to a client-side navigation). For example, navigating from `/cart` that uses `app/(shop)/layout.js` to `/blog` that uses `app/(marketing)/layout.js` will cause a full page load. This **only** applies to multiple root layouts.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`layout` introduced.
[Previous
instrumentation.js
](/docs/app/api-reference/file-conventions/instrumentation)
[Next
loading.js
](/docs/app/api-reference/file-conventions/loading)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a7e59ebf467d028c02234dbc2323eb98\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/optimizing/scripts | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [
Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Layout Scripts](#layout-scripts)
* [Application Scripts](#application-scripts)
* [Strategy](#strategy)
* [Offloading Scripts To A Web Worker (Experimental)](#offloading-scripts-to-a-web-worker-experimental)
* [Inline Scripts](#inline-scripts)
* [Executing Additional Code](#executing-additional-code)
* [Additional Attributes](#additional-attributes)
* [API Reference](#api-reference)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/06-optimizing/05-scripts.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Optimizing](/docs/app/building-your-application/optimizing)Scripts
Script Optimization
===================
### [Layout Scripts](#layout-scripts)
To load a third-party script for multiple routes, import `next/script` and include the script directly in your layout component:
app/dashboard/layout.tsx
TypeScript
JavaScriptTypeScript
import Script from 'next/script'
export default function DashboardLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<>
<section>{children}</section>
<Script src="https://example.com/script.js" />
</>
)
}
The third-party script is fetched when the folder route (e.g. `dashboard/page.js`) or any nested route (e.g. `dashboard/settings/page.js`) is accessed by the user. Next.js will ensure the script will **only load once**, even if a user navigates between multiple routes in the same layout.
### [Application Scripts](#application-scripts)
To load a third-party script for all routes, import `next/script` and include the script directly in your root layout:
app/layout.tsx
TypeScript
JavaScriptTypeScript
import Script from 'next/script'
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
<Script src="https://example.com/script.js" />
</html>
)
}
This script will load and execute when _any_ route in your application is accessed. Next.js will ensure the script will **only load once**, even if a user navigates between multiple pages.
> **Recommendation**: We recommend only including third-party scripts in specific pages or layouts in order to minimize any unnecessary impact to performance.
### [Strategy](#strategy)
Although the default behavior of `next/script` allows you to load third-party scripts in any page or layout, you can fine-tune its loading behavior by using the `strategy` property:
* `beforeInteractive`: Load the script before any Next.js code and before any page hydration occurs.
* `afterInteractive`: (**default**) Load the script early but after some hydration on the page occurs.
* `lazyOnload`: Load the script later during browser idle time.
* `worker`: (experimental) Load the script in a web worker.
Refer to the [`next/script`](/docs/app/api-reference/components/script#strategy) API reference documentation to learn more about each strategy and their use cases.
### [Offloading Scripts To A Web Worker (Experimental)](#offloading-scripts-to-a-web-worker-experimental)
> **Warning:** The `worker` strategy is not yet stable and does not yet work with the [`app`](/docs/app/building-your-application/routing/defining-routes) directory. Use with caution.
Scripts that use the `worker` strategy are offloaded and executed in a web worker with [Partytown](https://partytown.builder.io/). This can improve the performance of your site by dedicating the main thread to the rest of your application code.
This strategy is still experimental and can only be used if the `nextScriptWorkers` flag is enabled in `next.config.js`:
next.config.js
module.exports = {
experimental: {
nextScriptWorkers: true,
},
}
Then, run `next` (normally `npm run dev` or `yarn dev`) and Next.js will guide you through the installation of the required packages to finish the setup:
Terminal
npm run dev
You'll see instructions like these: Please install Partytown by running `npm install @builder.io/partytown`
Once setup is complete, defining `strategy="worker"` will automatically instantiate Partytown in your application and offload the script to a web worker.
pages/home.tsx
TypeScript
JavaScriptTypeScript
import Script from 'next/script'
export default function Home() {
return (
<>
<Script src="https://example.com/script.js" strategy="worker" />
</>
)
}
There are a number of trade-offs that need to be considered when loading a third-party script in a web worker. Please see Partytown's [tradeoffs](https://partytown.builder.io/trade-offs) documentation for more information.
### [Inline Scripts](#inline-scripts)
Inline scripts, or scripts not loaded from an external file, are also supported by the Script component. They can be written by placing the JavaScript within curly braces:
<Script id="show-banner">
{`document.getElementById('banner').classList.remove('hidden')`}
</Script>
Or by using the `dangerouslySetInnerHTML` property:
<Script
id="show-banner"
dangerouslySetInnerHTML={{
__html: `document.getElementById('banner').classList.remove('hidden')`,
}}
/>
> **Warning**: An `id` property must be assigned for inline scripts in order for Next.js to track and optimize the script.
### [Executing Additional Code](#executing-additional-code)
Event handlers can be used with the Script component to execute additional code after a certain event occurs:
* `onLoad`: Execute code after the script has finished loading.
* `onReady`: Execute code after the script has finished loading and every time the component is mounted.
* `onError`: Execute code if the script fails to load.
These handlers will only work when `next/script` is imported and used inside of a [Client Component](/docs/app/building-your-application/rendering/client-components) where `"use client"` is defined as the first line of code:
app/page.tsx
TypeScript
JavaScriptTypeScript
'use client'
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://example.com/script.js"
onLoad={() => {
console.log('Script has loaded')
}}
/>
</>
)
}
Refer to the [`next/script`](/docs/app/api-reference/components/script#onload) API reference to learn more about each event handler and view examples.
### [Additional Attributes](#additional-attributes)
There are many DOM attributes that can be assigned to a `<script>` element that are not used by the Script component, like [`nonce`](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/nonce) or [custom data attributes](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/data-*). Including any additional attributes will automatically forward it to the final, optimized `<script>` element that is included in the HTML.
app/page.tsx
TypeScript
JavaScriptTypeScript
import Script from 'next/script'
export default function Page() {
return (
<>
<Script
src="https://example.com/script.js"
id="example-script"
nonce="XUENAJFW"
data-test="script"
/>
</>
)
}
API Reference
-------------
Learn more about the next/script API.
[
App Router
...
Components
### <Script>
Optimize third-party scripts in your Next.js application using the built-in \`next/script\` Component.
](/docs/app/api-reference/components/script)
[Previous
Metadata
](/docs/app/building-your-application/optimizing/metadata)
[Next
Bundle Analyzer
](/docs/app/building-your-application/optimizing/bundle-analyzer)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"0427af96d8e525525013d0d6be7b2283\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/routing/route-handlers | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [
Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Convention](#convention)
* [Supported HTTP Methods](#supported-http-methods)
* [Extended NextRequest and NextResponse APIs](#extended-nextrequest-and-nextresponse-apis)
* [Behavior](#behavior)
* [Caching](#caching)
* [Opting out of caching](#opting-out-of-caching)
* [Route Resolution](#route-resolution)
* [Examples](#examples)
* [Revalidating Cached Data](#revalidating-cached-data)
* [Dynamic Functions](#dynamic-functions)
* [Cookies](#cookies)
* [Headers](#headers)
* [Redirects](#redirects)
* [Dynamic Route Segments](#dynamic-route-segments)
* [URL Query Parameters](#url-query-parameters)
* [Streaming](#streaming)
* [Request Body](#request-body)
* [Request Body FormData](#request-body-formdata)
* [CORS](#cors)
* [Webhooks](#webhooks)
* [Edge and Node.js Runtimes](#edge-and-nodejs-runtimes)
* [Non-UI Responses](#non-ui-responses)
* [Segment Config Options](#segment-config-options)
* [API Reference](#api-reference)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/01-routing/12-route-handlers.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Routing](/docs/app/building-your-application/routing)Route Handlers
Route Handlers
==============
Route Handlers allow you to create custom request handlers for a given route using the Web [Request](https://developer.mozilla.org/docs/Web/API/Request) and [Response](https://developer.mozilla.org/docs/Web/API/Response) APIs.
![Route.js Special File](/_next/image?url=%2Fdocs%2Flight%2Froute-special-file.png&w=3840&q=75)![Route.js Special File](/_next/image?url=%2Fdocs%2Fdark%2Froute-special-file.png&w=3840&q=75)
> **Good to know**: Route Handlers are only available inside the `app` directory. They are the equivalent of [API Routes](/docs/pages/building-your-application/routing/api-routes) inside the `pages` directory meaning you **do not** need to use API Routes and Route Handlers together.
[Convention](#convention)
-------------------------
Route Handlers are defined in a [`route.js|ts` file](/docs/app/api-reference/file-conventions/route) inside the `app` directory:
app/api/route.ts
TypeScript
JavaScriptTypeScript
export const dynamic = 'force-dynamic' // defaults to auto
export async function GET(request: Request) {}
Route Handlers can be nested inside the `app` directory, similar to `page.js` and `layout.js`. But there **cannot** be a `route.js` file at the same route segment level as `page.js`.
### [Supported HTTP Methods](#supported-http-methods)
The following [HTTP methods](https://developer.mozilla.org/docs/Web/HTTP/Methods) are supported: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, and `OPTIONS`. If an unsupported method is called, Next.js will return a `405 Method Not Allowed` response.
### [Extended `NextRequest` and `NextResponse` APIs](#extended-nextrequest-and-nextresponse-apis)
In addition to supporting native [Request](https://developer.mozilla.org/docs/Web/API/Request) and [Response](https://developer.mozilla.org/docs/Web/API/Response). Next.js extends them with [`NextRequest`](/docs/app/api-reference/functions/next-request) and [`NextResponse`](/docs/app/api-reference/functions/next-response) to provide convenient helpers for advanced use cases.
[Behavior](#behavior)
---------------------
### [Caching](#caching)
Route Handlers are cached by default when using the `GET` method with the `Response` object.
app/items/route.ts
TypeScript
JavaScriptTypeScript
export async function GET() {
const res = await fetch('https://data.mongodb-api.com/...', {
headers: {
'Content-Type': 'application/json',
'API-Key': process.env.DATA_API_KEY,
},
})
const data = await res.json()
return Response.json({ data })
}
> **TypeScript Warning:** `Response.json()` is only valid from TypeScript 5.2. If you use a lower TypeScript version, you can use [`NextResponse.json()`](/docs/app/api-reference/functions/next-response#json) for typed responses instead.
### [Opting out of caching](#opting-out-of-caching)
You can opt out of caching by:
* Using the `Request` object with the `GET` method.
* Using any of the other HTTP methods.
* Using [Dynamic Functions](#dynamic-functions) like `cookies` and `headers`.
* The [Segment Config Options](#segment-config-options) manually specifies dynamic mode.
For example:
app/products/api/route.ts
TypeScript
JavaScriptTypeScript
export async function GET(request: Request) {
const { searchParams } = new URL(request.url)
const id = searchParams.get('id')
const res = await fetch(`https://data.mongodb-api.com/product/${id}`, {
headers: {
'Content-Type': 'application/json',
'API-Key': process.env.DATA_API_KEY!,
},
})
const product = await res.json()
return Response.json({ product })
}
Similarly, the `POST` method will cause the Route Handler to be evaluated dynamically.
app/items/route.ts
TypeScript
JavaScriptTypeScript
export async function POST() {
const res = await fetch('https://data.mongodb-api.com/...', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'API-Key': process.env.DATA_API_KEY!,
},
body: JSON.stringify({ time: new Date().toISOString() }),
})
const data = await res.json()
return Response.json(data)
}
> **Good to know**: Like API Routes, Route Handlers can be used for cases like handling form submissions. A new abstraction for [handling forms and mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) that integrates deeply with React is being worked on.
### [Route Resolution](#route-resolution)
You can consider a `route` the lowest level routing primitive.
* They **do not** participate in layouts or client-side navigations like `page`.
* There **cannot** be a `route.js` file at the same route as `page.js`.
Page
Route
Result
`app/page.js`
`app/route.js`
Conflict
`app/page.js`
`app/api/route.js`
Valid
`app/[user]/page.js`
`app/api/route.js`
Valid
Each `route.js` or `page.js` file takes over all HTTP verbs for that route.
app/page.js
export default function Page() {
return <h1>Hello, Next.js!</h1>
}
// ❌ Conflict
// `app/route.js`
export async function POST(request) {}
[Examples](#examples)
---------------------
The following examples show how to combine Route Handlers with other Next.js APIs and features.
### [Revalidating Cached Data](#revalidating-cached-data)
You can [revalidate cached data](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) using the [`next.revalidate`](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) option:
app/items/route.ts
TypeScript
JavaScriptTypeScript
export async function GET() {
const res = await fetch('https://data.mongodb-api.com/...', {
next: { revalidate: 60 }, // Revalidate every 60 seconds
})
const data = await res.json()
return Response.json(data)
}
Alternatively, you can use the [`revalidate` segment config option](/docs/app/api-reference/file-conventions/route-segment-config#revalidate):
export const revalidate = 60
### [Dynamic Functions](#dynamic-functions)
Route Handlers can be used with dynamic functions from Next.js, like [`cookies`](/docs/app/api-reference/functions/cookies) and [`headers`](/docs/app/api-reference/functions/headers).
#### [Cookies](#cookies)
You can read or set cookies with [`cookies`](/docs/app/api-reference/functions/cookies) from `next/headers`. This server function can be called directly in a Route Handler, or nested inside of another function.
Alternatively, you can return a new `Response` using the [`Set-Cookie`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Set-Cookie) header.
app/api/route.ts
TypeScript
JavaScriptTypeScript
import { cookies } from 'next/headers'
export async function GET(request: Request) {
const cookieStore = cookies()
const token = cookieStore.get('token')
return new Response('Hello, Next.js!', {
status: 200,
headers: { 'Set-Cookie': `token=${token.value}` },
})
}
You can also use the underlying Web APIs to read cookies from the request ([`NextRequest`](/docs/app/api-reference/functions/next-request)):
app/api/route.ts
TypeScript
JavaScriptTypeScript
import { type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
const token = request.cookies.get('token')
}
#### [Headers](#headers)
You can read headers with [`headers`](/docs/app/api-reference/functions/headers) from `next/headers`. This server function can be called directly in a Route Handler, or nested inside of another function.
This `headers` instance is read-only. To set headers, you need to return a new `Response` with new `headers`.
app/api/route.ts
TypeScript
JavaScriptTypeScript
import { headers } from 'next/headers'
export async function GET(request: Request) {
const headersList = headers()
const referer = headersList.get('referer')
return new Response('Hello, Next.js!', {
status: 200,
headers: { referer: referer },
})
}
You can also use the underlying Web APIs to read headers from the request ([`NextRequest`](/docs/app/api-reference/functions/next-request)):
app/api/route.ts
TypeScript
JavaScriptTypeScript
import { type NextRequest } from 'next/server'
export async function GET(request: NextRequest) {
const requestHeaders = new Headers(request.headers)
}
### [Redirects](#redirects)
app/api/route.ts
TypeScript
JavaScriptTypeScript
import { redirect } from 'next/navigation'
export async function GET(request: Request) {
redirect('https://nextjs.org/')
}
### [Dynamic Route Segments](#dynamic-route-segments)
> We recommend reading the [Defining Routes](/docs/app/building-your-application/routing/defining-routes) page before continuing.
Route Handlers can use [Dynamic Segments](/docs/app/building-your-application/routing/dynamic-routes) to create request handlers from dynamic data.
app/items/\[slug\]/route.ts
TypeScript
JavaScriptTypeScript
export async function GET(
request: Request,
{ params }: { params: { slug: string } }
) {
const slug = params.slug // 'a', 'b', or 'c'
}
Route
Example URL
`params`
`app/items/[slug]/route.js`
`/items/a`
`{ slug: 'a' }`
`app/items/[slug]/route.js`
`/items/b`
`{ slug: 'b' }`
`app/items/[slug]/route.js`
`/items/c`
`{ slug: 'c' }`
### [URL Query Parameters](#url-query-parameters)
The request object passed to the Route Handler is a `NextRequest` instance, which has [some additional convenience methods](/docs/app/api-reference/functions/next-request#nexturl), including for more easily handling query parameters.
app/api/search/route.ts
TypeScript
JavaScriptTypeScript
import { type NextRequest } from 'next/server'
export function GET(request: NextRequest) {
const searchParams = request.nextUrl.searchParams
const query = searchParams.get('query')
// query is "hello" for /api/search?query=hello
}
### [Streaming](#streaming)
Streaming is commonly used in combination with Large Language Models (LLMs), such as OpenAI, for AI-generated content. Learn more about the [AI SDK](https://sdk.vercel.ai/docs).
app/api/chat/route.ts
TypeScript
JavaScriptTypeScript
import OpenAI from 'openai'
import { OpenAIStream, StreamingTextResponse } from 'ai'
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
})
export const runtime = 'edge'
export async function POST(req: Request) {
const { messages } = await req.json()
const response = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
stream: true,
messages,
})
const stream = OpenAIStream(response)
return new StreamingTextResponse(stream)
}
These abstractions use the Web APIs to create a stream. You can also use the underlying Web APIs directly.
app/api/route.ts
TypeScript
JavaScriptTypeScript
// https://developer.mozilla.org/docs/Web/API/ReadableStream#convert_async_iterator_to_stream
function iteratorToStream(iterator: any) {
return new ReadableStream({
async pull(controller) {
const { value, done } = await iterator.next()
if (done) {
controller.close()
} else {
controller.enqueue(value)
}
},
})
}
function sleep(time: number) {
return new Promise((resolve) => {
setTimeout(resolve, time)
})
}
const encoder = new TextEncoder()
async function* makeIterator() {
yield encoder.encode('<p>One</p>')
await sleep(200)
yield encoder.encode('<p>Two</p>')
await sleep(200)
yield encoder.encode('<p>Three</p>')
}
export async function GET() {
const iterator = makeIterator()
const stream = iteratorToStream(iterator)
return new Response(stream)
}
### [Request Body](#request-body)
You can read the `Request` body using the standard Web API methods:
app/items/route.ts
TypeScript
JavaScriptTypeScript
export async function POST(request: Request) {
const res = await request.json()
return Response.json({ res })
}
### [Request Body FormData](#request-body-formdata)
You can read the `FormData` using the `request.formData()` function:
app/items/route.ts
TypeScript
JavaScriptTypeScript
export async function POST(request: Request) {
const formData = await request.formData()
const name = formData.get('name')
const email = formData.get('email')
return Response.json({ name, email })
}
Since `formData` data are all strings, you may want to use [`zod-form-data`](https://www.npmjs.com/zod-form-data) to validate the request and retrieve data in the format you prefer (e.g. `number`).
### [CORS](#cors)
You can set CORS headers for a specific Route Handler using the standard Web API methods:
app/api/route.ts
TypeScript
JavaScriptTypeScript
export const dynamic = 'force-dynamic' // defaults to auto
export async function GET(request: Request) {
return new Response('Hello, Next.js!', {
status: 200,
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
},
})
}
> **Good to know**:
>
> * To add CORS headers to multiple Route Handlers, you can use [Middleware](/docs/app/building-your-application/routing/middleware#cors) or the [`next.config.js` file](/docs/app/api-reference/next-config-js/headers#cors).
> * Alternatively, see our [CORS example](https://github.com/vercel/examples/blob/main/edge-functions/cors/lib/cors.ts) package.
### [Webhooks](#webhooks)
You can use a Route Handler to receive webhooks from third-party services:
app/api/route.ts
TypeScript
JavaScriptTypeScript
export async function POST(request: Request) {
try {
const text = await request.text()
// Process the webhook payload
} catch (error) {
return new Response(`Webhook error: ${error.message}`, {
status: 400,
})
}
return new Response('Success!', {
status: 200,
})
}
Notably, unlike API Routes with the Pages Router, you do not need to use `bodyParser` to use any additional configuration.
### [Edge and Node.js Runtimes](#edge-and-nodejs-runtimes)
Route Handlers have an isomorphic Web API to support both Edge and Node.js runtimes seamlessly, including support for streaming. Since Route Handlers use the same [route segment configuration](/docs/app/api-reference/file-conventions/route-segment-config) as Pages and Layouts, they support long-awaited features like general-purpose [statically regenerated](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data) Route Handlers.
You can use the `runtime` segment config option to specify the runtime:
export const runtime = 'edge' // 'nodejs' is the default
### [Non-UI Responses](#non-ui-responses)
You can use Route Handlers to return non-UI content. Note that [`sitemap.xml`](/docs/app/api-reference/file-conventions/metadata/sitemap#generating-a-sitemap-using-code-js-ts), [`robots.txt`](/docs/app/api-reference/file-conventions/metadata/robots#generate-a-robots-file), [`app icons`](/docs/app/api-reference/file-conventions/metadata/app-icons#generate-icons-using-code-js-ts-tsx), and [open graph images](/docs/app/api-reference/file-conventions/metadata/opengraph-image) all have built-in support.
app/rss.xml/route.ts
TypeScript
JavaScriptTypeScript
export const dynamic = 'force-dynamic' // defaults to auto
export async function GET() {
return new Response(
`<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Next.js Documentation</title>
<link>https://nextjs.org/docs</link>
<description>The React Framework for the Web</description>
</channel>
</rss>`,
{
headers: {
'Content-Type': 'text/xml',
},
}
)
}
### [Segment Config Options](#segment-config-options)
Route Handlers use the same [route segment configuration](/docs/app/api-reference/file-conventions/route-segment-config) as pages and layouts.
app/items/route.ts
TypeScript
JavaScriptTypeScript
export const dynamic = 'auto'
export const dynamicParams = true
export const revalidate = false
export const fetchCache = 'auto'
export const runtime = 'nodejs'
export const preferredRegion = 'auto'
See the [API reference](/docs/app/api-reference/file-conventions/route-segment-config) for more details.
API Reference
-------------
Learn more about the route.js file.
[
App Router
...
File Conventions
### route.js
API reference for the route.js special file.
](/docs/app/api-reference/file-conventions/route)
[Previous
Intercepting Routes
](/docs/app/building-your-application/routing/intercepting-routes)
[Next
Middleware
](/docs/app/building-your-application/routing/middleware)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"8c610902475c336d3d0a9718399e236d\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/instrumentation | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [
instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Config Option](#config-option)
* [Exports](#exports)
* [register (required)](#register-required)
* [Version History](#version-history)
* [Learn more about Instrumentation](#learn-more-about-instrumentation)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/instrumentation.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)instrumentation.js
instrumentation.js
==================
The `instrumentation.js|ts` file is used to integrate monitoring and logging tools into your application. This allows you to track the performance and behavior of your application, and to debug issues in production.
To use it, place the file in the **root** of your application or inside a [`src` folder](/docs/app/building-your-application/configuring/src-directory) if using one.
[Config Option](#config-option)
-------------------------------
Instrumentation is currently an experimental feature, to use the `instrumentation` file, you must explicitly opt-in by defining [`experimental.instrumentationHook = true;`](/docs/app/api-reference/next-config-js/instrumentationHook) in your `next.config.js`:
next.config.js
module.exports = {
experimental: {
instrumentationHook: true,
},
}
[Exports](#exports)
-------------------
### [`register` (required)](#register-required)
The file exports a `register` function that is called **once** when a new Next.js server instance is initiated. `register` can be an async function.
instrumentation.ts
TypeScript
JavaScriptTypeScript
import { registerOTel } from '@vercel/otel'
export function register() {
registerOTel('next-app')
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v14.0.4`
Turbopack support for `instrumentation`
`v13.2.0`
`instrumentation` introduced as an experimental feature
Learn more about Instrumentation
--------------------------------
[
App Router
...
Optimizing
### Instrumentation
Learn how to use instrumentation to run code at server startup in your Next.js app
](/docs/app/building-your-application/optimizing/instrumentation)
[Previous
error.js
](/docs/app/api-reference/file-conventions/error)
[Next
layout.js
](/docs/app/api-reference/file-conventions/layout)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"6b0d08a342164cd100d65dee472dd33f\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/unstable_cache | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [
unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [Returns](#returns)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/unstable_cache.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)unstable\_cache
unstable\_cache
===============
`unstable_cache` allows you to cache the results of expensive operations, like database queries, and reuse them across multiple requests.
import { getUser } from './data';
import { unstable_cache } from 'next/cache';
const getCachedUser = unstable_cache(
async (id) => getUser(id),
['my-app-user']
);
export default async function Component({ userID }) {
const user = await getCachedUser(userID);
...
}
> **Good to know**: Accessing dynamic data sources such as `headers` or `cookies` inside a cache scope is not supported. If you need this data inside a cached function use `headers` outside of the cached function and pass the required dynamic data in as an argument.
> **Warning**: This API is unstable and may change in the future. We will provide migration documentation and codemods, if needed, as this API stabilizes.
[Parameters](#parameters)
-------------------------
const data = unstable_cache(fetchData, keyParts, options)()
* `fetchData`: This is an asynchronous function that fetches the data you want to cache. It must be a function that returns a `Promise`.
* `keyParts`: This is an array that identifies the cached key. It must contain globally unique values that together identify the key of the data being cached. The cache key also includes the arguments passed to the function.
* `options`: This is an object that controls how the cache behaves. It can contain the following properties:
* `tags`: An array of tags that can be used to control cache invalidation.
* `revalidate`: The number of seconds after which the cache should be revalidated. Omit or pass `false` to cache indefinitely or until matching `revalidateTag()` or `revalidatePath()` methods are called.
[Returns](#returns)
-------------------
`unstable_cache` returns a function that when invoked, returns a Promise that resolves to the cached data. If the data is not in the cache, the provided function will be invoked, and its result will be cached and returned.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v14.0.0`
`unstable_cache` introduced.
[Previous
revalidateTag
](/docs/app/api-reference/functions/revalidateTag)
[Next
unstable\_noStore
](/docs/app/api-reference/functions/unstable_noStore)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9bbc9ccb01016523599ce131fcf62828\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/optimizing/lazy-loading | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [
Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [next/dynamic](#nextdynamic)
* [Examples](#examples)
* [Skipping SSR](#skipping-ssr)
* [Importing Server Components](#importing-server-components)
* [Loading External Libraries](#loading-external-libraries)
* [Adding a custom loading component](#adding-a-custom-loading-component)
* [Importing Named Exports](#importing-named-exports)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/06-optimizing/07-lazy-loading.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Optimizing](/docs/app/building-your-application/optimizing)Lazy Loading
Lazy Loading
============
[Lazy loading](https://developer.mozilla.org/docs/Web/Performance/Lazy_loading) in Next.js helps improve the initial loading performance of an application by decreasing the amount of JavaScript needed to render a route.
It allows you to defer loading of **Client Components** and imported libraries, and only include them in the client bundle when they're needed. For example, you might want to defer loading a modal until a user clicks to open it.
There are two ways you can implement lazy loading in Next.js:
1. Using [Dynamic Imports](#nextdynamic) with `next/dynamic`
2. Using [`React.lazy()`](https://react.dev/reference/react/lazy) with [Suspense](https://react.dev/reference/react/Suspense)
By default, Server Components are automatically [code split](https://developer.mozilla.org/docs/Glossary/Code_splitting), and you can use [streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming) to progressively send pieces of UI from the server to the client. Lazy loading applies to Client Components.
[`next/dynamic`](#nextdynamic)
------------------------------
`next/dynamic` is a composite of [`React.lazy()`](https://react.dev/reference/react/lazy) and [Suspense](https://react.dev/reference/react/Suspense). It behaves the same way in the `app` and `pages` directories to allow for incremental migration.
[Examples](#examples)
---------------------
### [Importing Client Components](#importing-client-components)
app/page.js
'use client'
import { useState } from 'react'
import dynamic from 'next/dynamic'
// Client Components:
const ComponentA = dynamic(() => import('../components/A'))
const ComponentB = dynamic(() => import('../components/B'))
const ComponentC = dynamic(() => import('../components/C'), { ssr: false })
export default function ClientComponentExample() {
const [showMore, setShowMore] = useState(false)
return (
<div>
{/* Load immediately, but in a separate client bundle */}
<ComponentA />
{/* Load on demand, only when/if the condition is met */}
{showMore && <ComponentB />}
<button onClick={() => setShowMore(!showMore)}>Toggle</button>
{/* Load only on the client side */}
<ComponentC />
</div>
)
}
### [Skipping SSR](#skipping-ssr)
When using `React.lazy()` and Suspense, Client Components will be pre-rendered (SSR) by default.
If you want to disable pre-rendering for a Client Component, you can use the `ssr` option set to `false`:
const ComponentC = dynamic(() => import('../components/C'), { ssr: false })
### [Importing Server Components](#importing-server-components)
If you dynamically import a Server Component, only the Client Components that are children of the Server Component will be lazy-loaded - not the Server Component itself.
app/page.js
import dynamic from 'next/dynamic'
// Server Component:
const ServerComponent = dynamic(() => import('../components/ServerComponent'))
export default function ServerComponentExample() {
return (
<div>
<ServerComponent />
</div>
)
}
### [Loading External Libraries](#loading-external-libraries)
External libraries can be loaded on demand using the [`import()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import) function. This example uses the external library `fuse.js` for fuzzy search. The module is only loaded on the client after the user types in the search input.
app/page.js
'use client'
import { useState } from 'react'
const names = ['Tim', 'Joe', 'Bel', 'Lee']
export default function Page() {
const [results, setResults] = useState()
return (
<div>
<input
type="text"
placeholder="Search"
onChange={async (e) => {
const { value } = e.currentTarget
// Dynamically load fuse.js
const Fuse = (await import('fuse.js')).default
const fuse = new Fuse(names)
setResults(fuse.search(value))
}}
/>
<pre>Results: {JSON.stringify(results, null, 2)}</pre>
</div>
)
}
### [Adding a custom loading component](#adding-a-custom-loading-component)
app/page.js
import dynamic from 'next/dynamic'
const WithCustomLoading = dynamic(
() => import('../components/WithCustomLoading'),
{
loading: () => <p>Loading...</p>,
}
)
export default function Page() {
return (
<div>
{/* The loading component will be rendered while <WithCustomLoading/> is loading */}
<WithCustomLoading />
</div>
)
}
### [Importing Named Exports](#importing-named-exports)
To dynamically import a named export, you can return it from the Promise returned by [`import()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/import) function:
components/hello.js
'use client'
export function Hello() {
return <p>Hello!</p>
}
app/page.js
import dynamic from 'next/dynamic'
const ClientComponent = dynamic(() =>
import('../components/hello').then((mod) => mod.Hello)
)
[Previous
Bundle Analyzer
](/docs/app/building-your-application/optimizing/bundle-analyzer)
[Next
Analytics
](/docs/app/building-your-application/optimizing/analytics)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"51c1d32be6860f9f9fb853148e4e2df0\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/loading | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [
loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/loading.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)loading.js
loading.js
==========
A **loading** file can create instant loading states built on [Suspense](/docs/app/building-your-application/routing/loading-ui-and-streaming).
By default, this file is a [Server Component](/docs/app/building-your-application/rendering/server-components) - but can also be used as a Client Component through the `"use client"` directive.
app/feed/loading.tsx
TypeScript
JavaScriptTypeScript
export default function Loading() {
// Or a custom loading skeleton component
return <p>Loading...</p>
}
Loading UI components do not accept any parameters.
> **Good to know**
>
> * While designing loading UI, you may find it helpful to use the [React Developer Tools](https://react.dev/learn/react-developer-tools) to manually toggle Suspense boundaries.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`loading` introduced.
[Previous
layout.js
](/docs/app/api-reference/file-conventions/layout)
[Next
middleware.js
](/docs/app/api-reference/file-conventions/middleware)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"81adc876197e078ab449d3b3c5075a65\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/optimizing/analytics | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [
Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Build Your Own](#build-your-own)
* [Web Vitals](#web-vitals)
* [Sending results to external systems](#sending-results-to-external-systems)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/06-optimizing/08-analytics.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Optimizing](/docs/app/building-your-application/optimizing)Analytics
Analytics
=========
Next.js has built-in support for measuring and reporting performance metrics. You can either use the `useReportWebVitals` hook to manage reporting yourself, or alternatively, Vercel provides a [managed service](https://vercel.com/analytics?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) to automatically collect and visualize metrics for you.
[Build Your Own](#build-your-own)
---------------------------------
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>
)
}
> 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.
View the [API Reference](/docs/app/api-reference/functions/use-report-web-vitals) for more information.
[Web Vitals](#web-vitals)
-------------------------
[Web Vitals](https://web.dev/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](https://developer.mozilla.org/docs/Glossary/Time_to_first_byte) (TTFB)
* [First Contentful Paint](https://developer.mozilla.org/docs/Glossary/First_contentful_paint) (FCP)
* [Largest Contentful Paint](https://web.dev/lcp/) (LCP)
* [First Input Delay](https://web.dev/fid/) (FID)
* [Cumulative Layout Shift](https://web.dev/cls/) (CLS)
* [Interaction to Next Paint](https://web.dev/inp/) (INP)
You can handle all the results of these metrics using the `name` property.
app/\_components/web-vitals.tsx
TypeScript
JavaScriptTypeScript
'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
}
// ...
}
})
}
[Sending results to external systems](#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 {
fetch(url, { body, method: 'POST', keepalive: true })
}
})
> **Good to know**: If you use [Google Analytics](https://analytics.google.com/analytics/web/), 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.
> })
> })
>
> Read more about [sending results to Google Analytics](https://github.com/GoogleChrome/web-vitals#send-the-results-to-google-analytics).
[Previous
Lazy Loading
](/docs/app/building-your-application/optimizing/lazy-loading)
[Next
Instrumentation
](/docs/app/building-your-application/optimizing/instrumentation)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"3d771b2063692801d2d8f38180985d25\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/configuring/content-security-policy | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [
Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Nonces](#nonces)
* [Why use a nonce?](#why-use-a-nonce)
* [Adding a nonce with Middleware](#adding-a-nonce-with-middleware)
* [Reading the nonce](#reading-the-nonce)
* [Without Nonces](#without-nonces)
* [Version History](#version-history)
* [Next Steps](#next-steps)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/07-configuring/15-content-security-policy.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Configuring](/docs/app/building-your-application/configuring)Content Security Policy
Content Security Policy
=======================
[Content Security Policy (CSP)](https://developer.mozilla.org/docs/Web/HTTP/CSP) is important to guard your Next.js application against various security threats such as cross-site scripting (XSS), clickjacking, and other code injection attacks.
By using CSP, developers can specify which origins are permissible for content sources, scripts, stylesheets, images, fonts, objects, media (audio, video), iframes, and more.
Examples
* [Strict CSP](https://github.com/vercel/next.js/tree/canary/examples/with-strict-csp)
[Nonces](#nonces)
-----------------
A [nonce](https://developer.mozilla.org/docs/Web/HTML/Global_attributes/nonce) is a unique, random string of characters created for a one-time use. It is used in conjunction with CSP to selectively allow certain inline scripts or styles to execute, bypassing strict CSP directives.
### [Why use a nonce?](#why-use-a-nonce)
Even though CSPs are designed to block malicious scripts, there are legitimate scenarios where inline scripts are necessary. In such cases, nonces offer a way to allow these scripts to execute if they have the correct nonce.
### [Adding a nonce with Middleware](#adding-a-nonce-with-middleware)
[Middleware](/docs/app/building-your-application/routing/middleware) enables you to add headers and generate nonces before the page renders.
Every time a page is viewed, a fresh nonce should be generated. This means that you **must use dynamic rendering to add nonces**.
For example:
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
const nonce = Buffer.from(crypto.randomUUID()).toString('base64')
const cspHeader = `
default-src 'self';
script-src 'self' 'nonce-${nonce}' 'strict-dynamic';
style-src 'self' 'nonce-${nonce}';
img-src 'self' blob: data:;
font-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
upgrade-insecure-requests;
`
// Replace newline characters and spaces
const contentSecurityPolicyHeaderValue = cspHeader
.replace(/\s{2,}/g, ' ')
.trim()
const requestHeaders = new Headers(request.headers)
requestHeaders.set('x-nonce', nonce)
requestHeaders.set(
'Content-Security-Policy',
contentSecurityPolicyHeaderValue
)
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
})
response.headers.set(
'Content-Security-Policy',
contentSecurityPolicyHeaderValue
)
return response
}
By default, Middleware runs on all requests. You can filter Middleware to run on specific paths using a [`matcher`](/docs/app/building-your-application/routing/middleware#matcher).
We recommend ignoring matching prefetches (from `next/link`) and static assets that don't need the CSP header.
middleware.ts
TypeScript
JavaScriptTypeScript
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
],
}
### [Reading the nonce](#reading-the-nonce)
You can now read the nonce from a [Server Component](/docs/app/building-your-application/rendering/server-components) using [`headers`](/docs/app/api-reference/functions/headers):
app/page.tsx
TypeScript
JavaScriptTypeScript
import { headers } from 'next/headers'
import Script from 'next/script'
export default function Page() {
const nonce = headers().get('x-nonce')
return (
<Script
src="https://www.googletagmanager.com/gtag/js"
strategy="afterInteractive"
nonce={nonce}
/>
)
}
[Without Nonces](#without-nonces)
---------------------------------
For applications that do not require nonces, you can set the CSP header directly in your [`next.config.js`](/docs/app/api-reference/next-config-js) file:
next.config.js
const cspHeader = `
default-src 'self';
script-src 'self' 'unsafe-eval' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' blob: data:;
font-src 'self';
object-src 'none';
base-uri 'self';
form-action 'self';
frame-ancestors 'none';
upgrade-insecure-requests;
`
module.exports = {
async headers() {
return [
{
source: '/(.*)',
headers: [
{
key: 'Content-Security-Policy',
value: cspHeader.replace(/\n/g, ''),
},
],
},
]
},
}
[Version History](#version-history)
-----------------------------------
We recommend using `v13.4.20+` of Next.js to properly handle and apply nonces.
Next Steps
----------
[
App Router
...
Routing
### Middleware
Learn how to use Middleware to run code before a request is completed.
](/docs/app/building-your-application/routing/middleware)[
App Router
...
Functions
### headers
API reference for the headers function.
](/docs/app/api-reference/functions/headers)
[Previous
Draft Mode
](/docs/app/building-your-application/configuring/draft-mode)
[Next
Testing
](/docs/app/building-your-application/testing)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"3c3804010e3a5c8f2670de364ab647d9\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/unstable_noStore | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [
unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Usage](#usage)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/unstable_noStore.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)unstable\_noStore
unstable\_noStore
=================
`unstable_noStore` can be used to declaratively opt out of static rendering and indicate a particular component should not be cached.
import { unstable_noStore as noStore } from 'next/cache';
export default async function Component() {
noStore();
const result = await db.query(...);
...
}
> **Good to know**:
>
> * `unstable_noStore` is equivalent to `cache: 'no-store'` on a `fetch`
> * `unstable_noStore` is preferred over `export const dynamic = 'force-dynamic'` as it is more granular and can be used on a per-component basis
* Using `unstable_noStore` inside [`unstable_cache`](/docs/app/api-reference/functions/unstable_cache) will not opt out of static generation. Instead, it will defer to the cache configuration to determine whether to cache the result or not.
[Usage](#usage)
---------------
If you prefer not to pass additional options to `fetch`, like `cache: 'no-store'` or `next: { revalidate: 0 }`, you can use `noStore()` as a replacement for all of these use cases.
import { unstable_noStore as noStore } from 'next/cache';
export default async function Component() {
noStore();
const result = await db.query(...);
...
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v14.0.0`
`unstable_noStore` introduced.
[Previous
unstable\_cache
](/docs/app/api-reference/functions/unstable_cache)
[Next
useParams
](/docs/app/api-reference/functions/use-params)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9a55272e23fbeb1064439757ef55cdee\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/routing/internationalization | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [
Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Terminology](#terminology)
* [Routing Overview](#routing-overview)
* [Localization](#localization)
* [Static Generation](#static-generation)
* [Resources](#resources)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/01-routing/14-internationalization.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Routing](/docs/app/building-your-application/routing)Internationalization
Internationalization
====================
Next.js enables you to configure the routing and rendering of content to support multiple languages. Making your site adaptive to different locales includes translated content (localization) and internationalized routes.
[Terminology](#terminology)
---------------------------
* **Locale:** An identifier for a set of language and formatting preferences. This usually includes the preferred language of the user and possibly their geographic region.
* `en-US`: English as spoken in the United States
* `nl-NL`: Dutch as spoken in the Netherlands
* `nl`: Dutch, no specific region
[Routing Overview](#routing-overview)
-------------------------------------
It’s recommended to use the user’s language preferences in the browser to select which locale to use. Changing your preferred language will modify the incoming `Accept-Language` header to your application.
For example, using the following libraries, you can look at an incoming `Request` to determine which locale to select, based on the `Headers`, locales you plan to support, and the default locale.
middleware.js
import { match } from '@formatjs/intl-localematcher'
import Negotiator from 'negotiator'
let headers = { 'accept-language': 'en-US,en;q=0.5' }
let languages = new Negotiator({ headers }).languages()
let locales = ['en-US', 'nl-NL', 'nl']
let defaultLocale = 'en-US'
match(languages, locales, defaultLocale) // -> 'en-US'
Routing can be internationalized by either the sub-path (`/fr/products`) or domain (`my-site.fr/products`). With this information, you can now redirect the user based on the locale inside [Middleware](/docs/app/building-your-application/routing/middleware).
middleware.js
import { NextResponse } from "next/server";
let locales = ['en-US', 'nl-NL', 'nl']
// Get the preferred locale, similar to the above or using a library
function getLocale(request) { ... }
export function middleware(request) {
// Check if there is any supported locale in the pathname
const { pathname } = request.nextUrl
const pathnameHasLocale = locales.some(
(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`
)
if (pathnameHasLocale) return
// Redirect if there is no locale
const locale = getLocale(request)
request.nextUrl.pathname = `/${locale}${pathname}`
// e.g. incoming request is /products
// The new URL is now /en-US/products
return NextResponse.redirect(request.nextUrl)
}
export const config = {
matcher: [
// Skip all internal paths (_next)
'/((?!_next).*)',
// Optional: only run on root (/) URL
// '/'
],
}
Finally, ensure all special files inside `app/` are nested under `app/[lang]`. This enables the Next.js router to dynamically handle different locales in the route, and forward the `lang` parameter to every layout and page. For example:
app/\[lang\]/page.js
// You now have access to the current locale
// e.g. /en-US/products -> `lang` is "en-US"
export default async function Page({ params: { lang } }) {
return ...
}
The root layout can also be nested in the new folder (e.g. `app/[lang]/layout.js`).
[Localization](#localization)
-----------------------------
Changing displayed content based on the user’s preferred locale, or localization, is not something specific to Next.js. The patterns described below would work the same with any web application.
Let’s assume we want to support both English and Dutch content inside our application. We might maintain two different “dictionaries”, which are objects that give us a mapping from some key to a localized string. For example:
dictionaries/en.json
{
"products": {
"cart": "Add to Cart"
}
}
dictionaries/nl.json
{
"products": {
"cart": "Toevoegen aan Winkelwagen"
}
}
We can then create a `getDictionary` function to load the translations for the requested locale:
app/\[lang\]/dictionaries.js
import 'server-only'
const dictionaries = {
en: () => import('./dictionaries/en.json').then((module) => module.default),
nl: () => import('./dictionaries/nl.json').then((module) => module.default),
}
export const getDictionary = async (locale) => dictionaries[locale]()
Given the currently selected language, we can fetch the dictionary inside of a layout or page.
app/\[lang\]/page.js
import { getDictionary } from './dictionaries'
export default async function Page({ params: { lang } }) {
const dict = await getDictionary(lang) // en
return <button>{dict.products.cart}</button> // Add to Cart
}
Because all layouts and pages in the `app/` directory default to [Server Components](/docs/app/building-your-application/rendering/server-components), we do not need to worry about the size of the translation files affecting our client-side JavaScript bundle size. This code will **only run on the server**, and only the resulting HTML will be sent to the browser.
[Static Generation](#static-generation)
---------------------------------------
To generate static routes for a given set of locales, we can use `generateStaticParams` with any page or layout. This can be global, for example, in the root layout:
app/\[lang\]/layout.js
export async function generateStaticParams() {
return [{ lang: 'en-US' }, { lang: 'de' }]
}
export default function Root({ children, params }) {
return (
<html lang={params.lang}>
<body>{children}</body>
</html>
)
}
[Resources](#resources)
-----------------------
* [Minimal i18n routing and translations](https://github.com/vercel/next.js/tree/canary/examples/app-dir-i18n-routing)
* [`next-intl`](https://next-intl-docs.vercel.app/docs/next-13)
* [`next-international`](https://github.com/QuiiBz/next-international)
* [`next-i18n-router`](https://github.com/i18nexus/next-i18n-router)
[Previous
Middleware
](/docs/app/building-your-application/routing/middleware)
[Next
Data Fetching
](/docs/app/building-your-application/data-fetching)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"ffe93666bbe6a23502791ae5a2fa06e1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/routing/middleware | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [
Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Use Cases](#use-cases)
* [Convention](#convention)
* [Example](#example)
* [Matching Paths](#matching-paths)
* [Matcher](#matcher)
* [Conditional Statements](#conditional-statements)
* [NextResponse](#nextresponse)
* [Using Cookies](#using-cookies)
* [Setting Headers](#setting-headers)
* [CORS](#cors)
* [Producing a Response](#producing-a-response)
* [waitUntil and NextFetchEvent](#waituntil-and-nextfetchevent)
* [Advanced Middleware Flags](#advanced-middleware-flags)
* [Runtime](#runtime)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/01-routing/13-middleware.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Routing](/docs/app/building-your-application/routing)Middleware
Middleware
==========
Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.
Middleware runs before cached content and routes are matched. See [Matching Paths](#matching-paths) for more details.
[Use Cases](#use-cases)
-----------------------
Integrating Middleware into your application can lead to significant improvements in performance, security, and user experience. Some common scenarios where Middleware is particularly effective include:
* Authentication and Authorization: Ensure user identity and check session cookies before granting access to specific pages or API routes.
* Server-Side Redirects: Redirect users at the server level based on certain conditions (e.g., locale, user role).
* Path Rewriting: Support A/B testing, feature rollouts, or legacy paths by dynamically rewriting paths to API routes or pages based on request properties.
* Bot Detection: Protect your resources by detecting and blocking bot traffic.
* Logging and Analytics: Capture and analyze request data for insights before processing by the page or API.
* Feature Flagging: Enable or disable features dynamically for seamless feature rollouts or testing.
Recognizing situations where middleware may not be the optimal approach is just as crucial. Here are some scenarios to be mindful of:
* Complex Data Fetching and Manipulation: Middleware is not designed for direct data fetching or manipulation, this should be done within Route Handlers or server-side utilities instead.
* Heavy Computational Tasks: Middleware should be lightweight and respond quickly or it can cause delays in page load. Heavy computational tasks or long-running processes should be done within dedicated Route Handlers.
* Extensive Session Management: While Middleware can manage basic session tasks, extensive session management should be managed by dedicated authentication services or within Route Handlers.
* Direct Database Operations: Performing direct database operations within Middleware is not recommended. Database interactions should done within Route Handlers or server-side utilities.
[Convention](#convention)
-------------------------
Use the file `middleware.ts` (or `.js`) in the root of your project to define Middleware. For example, at the same level as `pages` or `app`, or inside `src` if applicable.
> **Note**: While only one `middleware.ts` file is supported per project, you can still organize your middleware logic modularly. Break out middleware functionalities into separate `.ts` or `.js` files and import them into your main `middleware.ts` file. This allows for cleaner management of route-specific middleware, aggregated in the `middleware.ts` for centralized control. By enforcing a single middleware file, it simplifies configuration, prevents potential conflicts, and optimizes performance by avoiding multiple middleware layers.
[Example](#example)
-------------------
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/home', request.url))
}
// See "Matching Paths" below to learn more
export const config = {
matcher: '/about/:path*',
}
[Matching Paths](#matching-paths)
---------------------------------
Middleware will be invoked for **every route in your project**. Given this, it's crucial to use matchers to precisely target or exclude specific routes. The following is the execution order:
1. `headers` from `next.config.js`
2. `redirects` from `next.config.js`
3. Middleware (`rewrites`, `redirects`, etc.)
4. `beforeFiles` (`rewrites`) from `next.config.js`
5. Filesystem routes (`public/`, `_next/static/`, `pages/`, `app/`, etc.)
6. `afterFiles` (`rewrites`) from `next.config.js`
7. Dynamic Routes (`/blog/[slug]`)
8. `fallback` (`rewrites`) from `next.config.js`
There are two ways to define which paths Middleware will run on:
1. [Custom matcher config](#matcher)
2. [Conditional statements](#conditional-statements)
### [Matcher](#matcher)
`matcher` allows you to filter Middleware to run on specific paths.
middleware.js
export const config = {
matcher: '/about/:path*',
}
You can match a single path or multiple paths with an array syntax:
middleware.js
export const config = {
matcher: ['/about/:path*', '/dashboard/:path*'],
}
The `matcher` config allows full regex so matching like negative lookaheads or character matching is supported. An example of a negative lookahead to match all except specific paths can be seen here:
middleware.js
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}
You can also bypass Middleware for certain requests by using the `missing` or `has` arrays, or a combination of both:
middleware.js
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
has: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
has: [{ type: 'header', key: 'x-present' }],
missing: [{ type: 'header', key: 'x-missing', value: 'prefetch' }],
},
],
}
> **Good to know**: The `matcher` values need to be constants so they can be statically analyzed at build-time. Dynamic values such as variables will be ignored.
Configured matchers:
1. MUST start with `/`
2. Can include named parameters: `/about/:path` matches `/about/a` and `/about/b` but not `/about/a/c`
3. Can have modifiers on named parameters (starting with `:`): `/about/:path*` matches `/about/a/b/c` because `*` is _zero or more_. `?` is _zero or one_ and `+` _one or more_
4. Can use regular expression enclosed in parenthesis: `/about/(.*)` is the same as `/about/:path*`
Read more details on [path-to-regexp](https://github.com/pillarjs/path-to-regexp#path-to-regexp-1) documentation.
> **Good to know**: For backward compatibility, Next.js always considers `/public` as `/public/index`. Therefore, a matcher of `/public/:path` will match.
### [Conditional Statements](#conditional-statements)
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/about')) {
return NextResponse.rewrite(new URL('/about-2', request.url))
}
if (request.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.rewrite(new URL('/dashboard/user', request.url))
}
}
[NextResponse](#nextresponse)
-----------------------------
The `NextResponse` API allows you to:
* `redirect` the incoming request to a different URL
* `rewrite` the response by displaying a given URL
* Set request headers for API Routes, `getServerSideProps`, and `rewrite` destinations
* Set response cookies
* Set response headers
To produce a response from Middleware, you can:
1. `rewrite` to a route ([Page](/docs/app/building-your-application/routing/pages-and-layouts) or [Route Handler](/docs/app/building-your-application/routing/route-handlers)) that produces a response
2. return a `NextResponse` directly. See [Producing a Response](#producing-a-response)
[Using Cookies](#using-cookies)
-------------------------------
Cookies are regular headers. On a `Request`, they are stored in the `Cookie` header. On a `Response` they are in the `Set-Cookie` header. Next.js provides a convenient way to access and manipulate these cookies through the `cookies` extension on `NextRequest` and `NextResponse`.
1. For incoming requests, `cookies` comes with the following methods: `get`, `getAll`, `set`, and `delete` cookies. You can check for the existence of a cookie with `has` or remove all cookies with `clear`.
2. For outgoing responses, `cookies` have the following methods `get`, `getAll`, `set`, and `delete`.
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
// Assume a "Cookie:nextjs=fast" header to be present on the incoming request
// Getting cookies from the request using the `RequestCookies` API
let cookie = request.cookies.get('nextjs')
console.log(cookie) // => { name: 'nextjs', value: 'fast', Path: '/' }
const allCookies = request.cookies.getAll()
console.log(allCookies) // => [{ name: 'nextjs', value: 'fast' }]
request.cookies.has('nextjs') // => true
request.cookies.delete('nextjs')
request.cookies.has('nextjs') // => false
// Setting cookies on the response using the `ResponseCookies` API
const response = NextResponse.next()
response.cookies.set('vercel', 'fast')
response.cookies.set({
name: 'vercel',
value: 'fast',
path: '/',
})
cookie = response.cookies.get('vercel')
console.log(cookie) // => { name: 'vercel', value: 'fast', Path: '/' }
// The outgoing response will have a `Set-Cookie:vercel=fast;path=/` header.
return response
}
[Setting Headers](#setting-headers)
-----------------------------------
You can set request and response headers using the `NextResponse` API (setting _request_ headers is available since Next.js v13.0.0).
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
// Clone the request headers and set a new header `x-hello-from-middleware1`
const requestHeaders = new Headers(request.headers)
requestHeaders.set('x-hello-from-middleware1', 'hello')
// You can also set request headers in NextResponse.rewrite
const response = NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
})
// Set a new response header `x-hello-from-middleware2`
response.headers.set('x-hello-from-middleware2', 'hello')
return response
}
> **Good to know**: Avoid setting large headers as it might cause [431 Request Header Fields Too Large](https://developer.mozilla.org/docs/Web/HTTP/Status/431) error depending on your backend web server configuration.
### [CORS](#cors)
You can set CORS headers in Middleware to allow cross-origin requests, including [simple](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests) and [preflighted](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests) requests.
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextRequest, NextResponse } from 'next/server'
const allowedOrigins = ['https://acme.com', 'https://my-app.org']
const corsOptions = {
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
}
export function middleware(request: NextRequest) {
// Check the origin from the request
const origin = request.headers.get('origin') ?? ''
const isAllowedOrigin = allowedOrigins.includes(origin)
// Handle preflighted requests
const isPreflight = request.method === 'OPTIONS'
if (isPreflight) {
const preflightHeaders = {
...(isAllowedOrigin && { 'Access-Control-Allow-Origin': origin }),
...corsOptions,
}
return NextResponse.json({}, { headers: preflightHeaders })
}
// Handle simple requests
const response = NextResponse.next()
if (isAllowedOrigin) {
response.headers.set('Access-Control-Allow-Origin', origin)
}
Object.entries(corsOptions).forEach(([key, value]) => {
response.headers.set(key, value)
})
return response
}
export const config = {
matcher: '/api/:path*',
}
> **Good to know:** You can configure CORS headers for individual routes in [Route Handlers](/docs/app/building-your-application/routing/route-handlers#cors).
[Producing a Response](#producing-a-response)
---------------------------------------------
You can respond from Middleware directly by returning a `Response` or `NextResponse` instance. (This is available since [Next.js v13.1.0](https://nextjs.org/blog/next-13-1#nextjs-advanced-middleware))
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextRequest } from 'next/server'
import { isAuthenticated } from '@lib/auth'
// Limit the middleware to paths starting with `/api/`
export const config = {
matcher: '/api/:function*',
}
export function middleware(request: NextRequest) {
// Call our authentication function to check the request
if (!isAuthenticated(request)) {
// Respond with JSON indicating an error message
return Response.json(
{ success: false, message: 'authentication failed' },
{ status: 401 }
)
}
}
### [`waitUntil` and `NextFetchEvent`](#waituntil-and-nextfetchevent)
The `NextFetchEvent` object extends the native [`FetchEvent`](https://developer.mozilla.org/docs/Web/API/FetchEvent) object, and includes the [`waitUntil()`](https://developer.mozilla.org/docs/Web/API/ExtendableEvent/waitUntil) method.
The `waitUntil()` method takes a promise as an argument, and extends the lifetime of the Middleware until the promise settles. This is useful for performing work in the background.
middleware.ts
import { NextResponse } from 'next/server'
import type { NextFetchEvent, NextRequest } from 'next/server'
export function middleware(req: NextRequest, event: NextFetchEvent) {
event.waitUntil(
fetch('https://my-analytics-platform.com', {
method: 'POST',
body: JSON.stringify({ pathname: req.nextUrl.pathname }),
})
)
return NextResponse.next()
}
[Advanced Middleware Flags](#advanced-middleware-flags)
-------------------------------------------------------
In `v13.1` of Next.js two additional flags were introduced for middleware, `skipMiddlewareUrlNormalize` and `skipTrailingSlashRedirect` to handle advanced use cases.
`skipTrailingSlashRedirect` disables Next.js redirects for adding or removing trailing slashes. This allows custom handling inside middleware to maintain the trailing slash for some paths but not others, which can make incremental migrations easier.
next.config.js
module.exports = {
skipTrailingSlashRedirect: true,
}
middleware.js
const legacyPrefixes = ['/docs', '/blog']
export default async function middleware(req) {
const { pathname } = req.nextUrl
if (legacyPrefixes.some((prefix) => pathname.startsWith(prefix))) {
return NextResponse.next()
}
// apply trailing slash handling
if (
!pathname.endsWith('/') &&
!pathname.match(/((?!\.well-known(?:\/.*)?)(?:[^/]+\/)*[^/]+\.\w+)/)
) {
req.nextUrl.pathname += '/'
return NextResponse.redirect(req.nextUrl)
}
}
`skipMiddlewareUrlNormalize` allows for disabling the URL normalization in Next.js to make handling direct visits and client-transitions the same. In some advanced cases, this option provides full control by using the original URL.
next.config.js
module.exports = {
skipMiddlewareUrlNormalize: true,
}
middleware.js
export default async function middleware(req) {
const { pathname } = req.nextUrl
// GET /_next/data/build-id/hello.json
console.log(pathname)
// with the flag this now /_next/data/build-id/hello.json
// without the flag this would be normalized to /hello
}
[Runtime](#runtime)
-------------------
Middleware currently only supports the [Edge runtime](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes). The Node.js runtime can not be used.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.1.0`
Advanced Middleware flags added
`v13.0.0`
Middleware can modify request headers, response headers, and send responses
`v12.2.0`
Middleware is stable, please see the [upgrade guide](/docs/messages/middleware-upgrade-guide)
`v12.0.9`
Enforce absolute URLs in Edge Runtime ([PR](https://github.com/vercel/next.js/pull/33410))
`v12.0.0`
Middleware (Beta) added
[Previous
Route Handlers
](/docs/app/building-your-application/routing/route-handlers)
[Next
Internationalization
](/docs/app/building-your-application/routing/internationalization)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9362a4d3916ed385bb013582c07ac25e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/testing/vitest | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [
Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Quickstart](#quickstart)
* [Manual Setup](#manual-setup)
* [Creating your first Vitest Unit Test](#creating-your-first-vitest-unit-test)
* [Running your tests](#running-your-tests)
* [Additional Resources](#additional-resources)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/08-testing/01-vitest.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Testing](/docs/app/building-your-application/testing)Vitest
Setting up Vitest with Next.js
==============================
Vite and React Testing Library are frequently used together for **Unit Testing**. This guide will show you how to setup Vitest with Next.js and write your first tests.
> **Good to know:** Since `async` Server Components are new to the React ecosystem, Vitest currently does not support them. While you can still run **unit tests** for synchronous Server and Client Components, we recommend using an **E2E tests** for `async` components.
[Quickstart](#quickstart)
-------------------------
You can use `create-next-app` with the Next.js [with-vitest](https://github.com/vercel/next.js/tree/canary/examples/with-vitest) example to quickly get started:
Terminal
npx create-next-app@latest --example with-vitest with-vitest-app
[Manual Setup](#manual-setup)
-----------------------------
To manually set up Vitest, install `vitest` and the following packages as dev dependencies:
Terminal
npm install -D vitest @vitejs/plugin-react jsdom @testing-library/react
# or
yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react
# or
pnpm install -D vitest @vitejs/plugin-react jsdom @testing-library/react
# or
bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react
Create a `vitest.config.ts|js` file in the root of your project, and add the following options:
vitest.config.ts
TypeScript
JavaScriptTypeScript
import { defineConfig } from 'vitest/config'
import react from '@vitejs/plugin-react'
export default defineConfig({
plugins: [react()],
test: {
environment: 'jsdom',
},
})
For more information on configuring Vitest, please refer to the [Vitest Configuration](https://vitest.dev/config/#configuration) docs.
Then, add a `test` script to your `package.json`:
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"test": "vitest"
}
}
When you run `npm run test`, Vitest will **watch** for changes in your project by default.
[Creating your first Vitest Unit Test](#creating-your-first-vitest-unit-test)
-----------------------------------------------------------------------------
Check that everything is working by creating a test to check if the `<Page />` component successfully renders a heading:
app/page.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
\_\_tests\_\_/page.test.tsx
TypeScript
JavaScriptTypeScript
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'
import Page from '../app/page'
test('Page', () => {
render(<Page />)
expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined()
})
> **Good to know**: The example above uses the common `__tests__` convention, but test files can also be colocated inside the `app` router.
[Running your tests](#running-your-tests)
-----------------------------------------
Then, run the following command to run your tests:
Terminal
npm run test
# or
yarn test
# or
pnpm test
# or
bun test
[Additional Resources](#additional-resources)
---------------------------------------------
You may find these resources helpful:
* [Next.js with Vitest example](https://github.com/vercel/next.js/tree/canary/examples/with-vitest)
* [Vitest Docs](https://vitest.dev/guide/)
* [React Testing Library Docs](https://testing-library.com/docs/react-testing-library/intro/)
[Previous
Testing
](/docs/app/building-your-application/testing)
[Next
Jest
](/docs/app/building-your-application/testing/jest)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"4382f9da40d3020f6d146c6da80d4aac\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/data-fetching | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/02-data-fetching/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[Building Your Application](/docs/app/building-your-application)Data Fetching
Data Fetching
=============
[
### Fetching, Caching, and Revalidating
Learn how to fetch, cache, and revalidate data in your Next.js application.
](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)[
### Server Actions and Mutations
Learn how to handle form submissions and data mutations with Next.js.
](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)[
### Data Fetching Patterns and Best Practices
Learn about common data fetching patterns in React and Next.js.
](/docs/app/building-your-application/data-fetching/patterns)
[Previous
Internationalization
](/docs/app/building-your-application/routing/internationalization)
[Next
Fetching, Caching, and Revalidating
](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"de84bec9a3fdf715615efeaca291ef18\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/middleware | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [
middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Exports](#exports)
* [Middleware function](#middleware-function)
* [Config object (optional)](#config-object-optional)
* [Matcher](#matcher)
* [Params](#params)
* [request](#request)
* [NextResponse](#nextresponse)
* [Runtime](#runtime)
* [Version History](#version-history)
* [Learn more about Middleware](#learn-more-about-middleware)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/middleware.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)middleware.js
middleware.js
=============
The `middleware.js|ts` file is used to write [Middleware](/docs/app/building-your-application/routing/middleware) and run code on the server before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.
Middleware executes before routes are rendered. It's particularly useful for implementing custom server-side logic like authentication, logging, or handling redirects.
Use the file `middleware.ts` (or .js) in the root of your project to define Middleware. For example, at the same level as `app` or `pages`, or inside `src` if applicable.
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse, NextRequest } from 'next/server'
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/home', request.url))
}
export const config = {
matcher: '/about/:path*',
}
[Exports](#exports)
-------------------
### [Middleware function](#middleware-function)
The file must export a single function, either as a default export or named `middleware`. Note that multiple middleware from the same file are not supported.
middleware.js
// Example of default export
export default function middleware(request) {
// Middleware logic
}
### [Config object (optional)](#config-object-optional)
Optionally, a config object can be exported alongside the Middleware function. This object includes the [matcher](#matcher) to specify paths where the Middleware applies.
#### [Matcher](#matcher)
The `matcher` option allows you to target specific paths for the Middleware to run on. You can specify these paths in several ways:
* For a single path: Directly use a string to define the path, like `'/about'`.
* For multiple paths: Use an array to list multiple paths, such as `matcher: ['/about', '/contact']`, which applies the Middleware to both `/about` and `/contact`.
Additionally, `matcher` supports complex path specifications through regular expressions, such as `matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)']`, enabling precise control over which paths to include or exclude.
The `matcher` option also accepts an array of objects with the following keys:
* `source`: The path or pattern used to match the request paths. It can be a string for direct path matching or a pattern for more complex matching.
* `regexp` (optional): A regular expression string that fine-tunes the matching based on the source. It provides additional control over which paths are included or excluded.
* `locale` (optional): A boolean that, when set to `false`, ignores locale-based routing in path matching.
* `has` (optional): Specifies conditions based on the presence of specific request elements such as headers, query parameters, or cookies.
* `missing` (optional): Focuses on conditions where certain request elements are absent, like missing headers or cookies.
middleware.js
export const config = {
matcher: [
{
source: '/api/*',
regexp: '^/api/(.*)',
locale: false,
has: [
{ type: 'header', key: 'Authorization', value: 'Bearer Token' },
{ type: 'query', key: 'userId', value: '123' },
],
missing: [{ type: 'cookie', key: 'session', value: 'active' }],
},
],
}
[Params](#params)
-----------------
### [`request`](#request)
When defining Middleware, the default export function accepts a single parameter, `request`. This parameter is an instance of `NextRequest`, which represents the incoming HTTP request.
middleware.ts
TypeScript
JavaScriptTypeScript
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
// Middleware logic goes here
}
> **Good to know**:
>
> * `NextRequest` is a type that represents incoming HTTP requests in Next.js Middleware, whereas [`NextResponse`](#nextresponse) is a class used to manipulate and send back HTTP responses.
[NextResponse](#nextresponse)
-----------------------------
Middleware can use the [`NextResponse`](/docs/app/building-your-application/routing/middleware#nextresponse) object which extends the [Web Response API](https://developer.mozilla.org/en-US/docs/Web/API/Response). By returning a `NextResponse` object, you can directly manipulate cookies, set headers, implement redirects, and rewrite paths.
> **Good to know**: For redirects, you can also use `Response.redirect` instead of `NextResponse.redirect`.
[Runtime](#runtime)
-------------------
Middleware only supports the [Edge runtime](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes). The Node.js runtime cannot be used.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.1.0`
Advanced Middleware flags added
`v13.0.0`
Middleware can modify request headers, response headers, and send responses
`v12.2.0`
Middleware is stable, please see the [upgrade guide](/docs/messages/middleware-upgrade-guide)
`v12.0.9`
Enforce absolute URLs in Edge Runtime ([PR](https://github.com/vercel/next.js/pull/33410))
`v12.0.0`
Middleware (Beta) added
Learn more about Middleware
---------------------------
[
App Router
...
Routing
### Middleware
Learn how to use Middleware to run code before a request is completed.
](/docs/app/building-your-application/routing/middleware)
[Previous
loading.js
](/docs/app/api-reference/file-conventions/loading)
[Next
not-found.js
](/docs/app/api-reference/file-conventions/not-found)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"b9353149d035c11c6d4302be2f0d26fc\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/testing | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Types of tests](#types-of-tests)
* [Async Server Components](#async-server-components)
* [Guides](#guides)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/08-testing/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[Building Your Application](/docs/app/building-your-application)Testing
Testing
=======
In React and Next.js, there are a few different types of tests you can write, each with its own purpose and use cases. This page provides an overview of types and commonly used tools you can use to test your application.
[Types of tests](#types-of-tests)
---------------------------------
* **Unit testing** involves testing individual units (or blocks of code) in isolation. In React, a unit can be a single function, hook, or component.
* **Component testing** is a more focused version of unit testing where the primary subject of the tests is React components. This may involve testing how components are rendered, their interaction with props, and their behavior in response to user events.
* **Integration testing** involves testing how multiple units work together. This can be a combination of components, hooks, and functions.
* **End-to-End (E2E) Testing** involves testing user flows in an environment that simulates real user scenarios, like the browser. This means testing specific tasks (e.g. signup flow) in a production-like environment.
* **Snapshot testing** involves capturing the rendered output of a component and saving it to a snapshot file. When tests run, the current rendered output of the component is compared against the saved snapshot. Changes in the snapshot are used to indicate unexpected changes in behavior.
[Async Server Components](#async-server-components)
---------------------------------------------------
Since `async` Server Components are new to the React ecosystem, some tools do not fully support them. In the meantime, we recommend using **End-to-End Testing** over **Unit Testing** for `async` components.
[Guides](#guides)
-----------------
See the guides below to learn how to set up Next.js with these commonly used testing tools:
[
### Vitest
Learn how to set up Vitest with Next.js for Unit Testing.
](/docs/app/building-your-application/testing/vitest)[
### Jest
Learn how to set up Jest with Next.js for Unit Testing and Snapshot Testing.
](/docs/app/building-your-application/testing/jest)[
### Playwright
Learn how to set up Playwright with Next.js for End-to-End (E2E) testing.
](/docs/app/building-your-application/testing/playwright)[
### Cypress
Learn how to set up Cypress with Next.js for End-to-End (E2E) and Component Testing.
](/docs/app/building-your-application/testing/cypress)
[Previous
Content Security Policy
](/docs/app/building-your-application/configuring/content-security-policy)
[Next
Vitest
](/docs/app/building-your-application/testing/vitest)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"afcf2a4976ea54ea5acb5c9b332238cf\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/optimizing/instrumentation | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [
Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Convention](#convention)
* [Examples](#examples)
* [Importing files with side effects](#importing-files-with-side-effects)
* [Importing runtime-specific code](#importing-runtime-specific-code)
* [Learn more about Instrumentation](#learn-more-about-instrumentation)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/06-optimizing/09-instrumentation.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Optimizing](/docs/app/building-your-application/optimizing)Instrumentation
Instrumentation
===============
Instrumentation is the process of using code to integrate monitoring and logging tools into your application. This allows you to track the performance and behavior of your application, and to debug issues in production.
[Convention](#convention)
-------------------------
To set up instrumentation, create `instrumentation.ts|js` file in the **root directory** of your project (or inside the [`src`](/docs/app/building-your-application/configuring/src-directory) folder if using one).
Then, export a `register` function in the file. This function will be called **once** when a new Next.js server instance is initiated.
For example, to use Next.js with [OpenTelemetry](https://opentelemetry.io/) and [@vercel/otel](https://vercel.com/docs/observability/otel-overview):
instrumentation.ts
TypeScript
JavaScriptTypeScript
import { registerOTel } from '@vercel/otel'
export function register() {
registerOTel('next-app')
}
See the [Next.js with OpenTelemetry example](https://github.com/vercel/next.js/tree/canary/examples/with-opentelemetry) for a complete implementation.
> **Good to know**
>
> * This feature is **experimental**. To use it, you must explicitly opt in by defining [`experimental.instrumentationHook = true;`](/docs/app/api-reference/next-config-js/instrumentationHook) in your `next.config.js`.
> * The `instrumentation` file should be in the root of your project and not inside the `app` or `pages` directory. If you're using the `src` folder, then place the file inside `src` alongside `pages` and `app`.
> * If you use the [`pageExtensions` config option](/docs/app/api-reference/next-config-js/pageExtensions) to add a suffix, you will also need to update the `instrumentation` filename to match.
[Examples](#examples)
---------------------
### [Importing files with side effects](#importing-files-with-side-effects)
Sometimes, it may be useful to import a file in your code because of the side effects it will cause. For example, you might import a file that defines a set of global variables, but never explicitly use the imported file in your code. You would still have access to the global variables the package has declared.
We recommend importing files using JavaScript `import` syntax within your `register` function. The following example demonstrates a basic usage of `import` in a `register` function:
instrumentation.ts
TypeScript
JavaScriptTypeScript
export async function register() {
await import('package-with-side-effect')
}
> **Good to know:**
>
> We recommend importing the file from within the `register` function, rather than at the top of the file. By doing this, you can colocate all of your side effects in one place in your code, and avoid any unintended consequences from importing globally at the top of the file.
### [Importing runtime-specific code](#importing-runtime-specific-code)
Next.js calls `register` in all environments, so it's important to conditionally import any code that doesn't support specific runtimes (e.g. [Edge](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes#edge-runtime) or [Node.js](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes#nodejs-runtime)). You can use the `NEXT_RUNTIME` environment variable to get the current environment:
instrumentation.ts
TypeScript
JavaScriptTypeScript
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./instrumentation-node')
}
if (process.env.NEXT_RUNTIME === 'edge') {
await import('./instrumentation-edge')
}
}
Learn more about Instrumentation
--------------------------------
[
App Router
...
File Conventions
### instrumentation.js
API reference for the instrumentation.js file.
](/docs/app/api-reference/file-conventions/instrumentation)[
App Router
...
next.config.js Options
### instrumentationHook
Use the instrumentationHook option to set up instrumentation in your Next.js App.
](/docs/app/api-reference/next-config-js/instrumentationHook)
[Previous
Analytics
](/docs/app/building-your-application/optimizing/analytics)
[Next
OpenTelemetry
](/docs/app/building-your-application/optimizing/open-telemetry)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"0e63a36eca6978ab7489822c1636eabd\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/use-params | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [
useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [Returns](#returns)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/use-params.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)useParams
useParams
=========
`useParams` is a **Client Component** hook that lets you read a route's [dynamic params](/docs/app/building-your-application/routing/dynamic-routes) filled in by the current URL.
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useParams } from 'next/navigation'
export default function ExampleClientComponent() {
const params = useParams<{ tag: string; item: string }>()
// Route -> /shop/[tag]/[item]
// URL -> /shop/shoes/nike-air-max-97
// `params` -> { tag: 'shoes', item: 'nike-air-max-97' }
console.log(params)
return <></>
}
[Parameters](#parameters)
-------------------------
const params = useParams()
`useParams` does not take any parameters.
[Returns](#returns)
-------------------
`useParams` returns an object containing the current route's filled in [dynamic parameters](/docs/app/building-your-application/routing/dynamic-routes).
* 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](/docs/app/building-your-application/routing/dynamic-routes).
* If the route contains no dynamic parameters, `useParams` returns an empty object.
* If used in Pages Router, `useParams` will return `null` on the initial render and updates with properties following the rules above once the router is ready.
For example:
Route
URL
`useParams()`
`app/shop/page.js`
`/shop`
`{}`
`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'] }`
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
`useParams` introduced.
[Previous
unstable\_noStore
](/docs/app/api-reference/functions/unstable_noStore)
[Next
usePathname
](/docs/app/api-reference/functions/use-pathname)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"8a5a58275178ffba8f3cee863511d818\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/optimizing/open-telemetry | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [
OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Getting Started](#getting-started)
* [Using @vercel/otel](#using-vercelotel)
* [Manual OpenTelemetry configuration](#manual-opentelemetry-configuration)
* [Testing your instrumentation](#testing-your-instrumentation)
* [Deployment](#deployment)
* [Using OpenTelemetry Collector](#using-opentelemetry-collector)
* [Deploying on Vercel](#deploying-on-vercel)
* [Self-hosting](#self-hosting)
* [Custom Exporters](#custom-exporters)
* [Custom Spans](#custom-spans)
* [Default Spans in Next.js](#default-spans-in-nextjs)
* [\[http.method\] \[next.route\]](#httpmethod-nextroute)
* [render route (app) \[next.route\]](#render-route-app-nextroute)
* [fetch \[http.method\] \[http.url\]](#fetch-httpmethod-httpurl)
* [executing api route (app) \[next.route\]](#executing-api-route-app-nextroute)
* [getServerSideProps \[next.route\]](#getserversideprops-nextroute)
* [getStaticProps \[next.route\]](#getstaticprops-nextroute)
* [render route (pages) \[next.route\]](#render-route-pages-nextroute)
* [generateMetadata \[next.page\]](#generatemetadata-nextpage)
* [resolve page components](#resolve-page-components)
* [resolve segment modules](#resolve-segment-modules)
* [start response](#start-response)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/06-optimizing/10-open-telemetry.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Optimizing](/docs/app/building-your-application/optimizing)OpenTelemetry
OpenTelemetry
=============
> **Good to know**: This feature is **experimental**, you need to explicitly opt-in by providing `experimental.instrumentationHook = true;` in your `next.config.js`.
Observability is crucial for understanding and optimizing the behavior and performance of your Next.js app.
As applications become more complex, it becomes increasingly difficult to identify and diagnose issues that may arise. By leveraging observability tools, such as logging and metrics, developers can gain insights into their application's behavior and identify areas for optimization. With observability, developers can proactively address issues before they become major problems and provide a better user experience. Therefore, it is highly recommended to use observability in your Next.js applications to improve performance, optimize resources, and enhance user experience.
We recommend using OpenTelemetry for instrumenting your apps. It's a platform-agnostic way to instrument apps that allows you to change your observability provider without changing your code. Read [Official OpenTelemetry docs](https://opentelemetry.io/docs/) for more information about OpenTelemetry and how it works.
This documentation uses terms like _Span_, _Trace_ or _Exporter_ throughout this doc, all of which can be found in [the OpenTelemetry Observability Primer](https://opentelemetry.io/docs/concepts/observability-primer/).
Next.js supports OpenTelemetry instrumentation out of the box, which means that we already instrumented Next.js itself. When you enable OpenTelemetry we will automatically wrap all your code like `getStaticProps` in _spans_ with helpful attributes.
[Getting Started](#getting-started)
-----------------------------------
OpenTelemetry is extensible but setting it up properly can be quite verbose. That's why we prepared a package `@vercel/otel` that helps you get started quickly.
### [Using `@vercel/otel`](#using-vercelotel)
To get started, you must install `@vercel/otel`:
Terminal
npm install @vercel/otel
Next, create a custom [`instrumentation.ts`](/docs/app/building-your-application/optimizing/instrumentation) (or `.js`) file in the **root directory** of the project (or inside `src` folder if using one):
your-project/instrumentation.ts
TypeScript
JavaScriptTypeScript
import { registerOTel } from '@vercel/otel'
export function register() {
registerOTel({ serviceName: 'next-app' })
}
See the [`@vercel/otel` documentation](https://www.npmjs.com/package/@vercel/otel) for additional configuration options.
> **Good to know**
>
> * The `instrumentation` file should be in the root of your project and not inside the `app` or `pages` directory. If you're using the `src` folder, then place the file inside `src` alongside `pages` and `app`.
> * If you use the [`pageExtensions` config option](/docs/app/api-reference/next-config-js/pageExtensions) to add a suffix, you will also need to update the `instrumentation` filename to match.
> * We have created a basic [with-opentelemetry](https://github.com/vercel/next.js/tree/canary/examples/with-opentelemetry) example that you can use.
### [Manual OpenTelemetry configuration](#manual-opentelemetry-configuration)
The `@vercel/otel` package provides many configuration options and should serve most of common use cases. But if it doesn't suit your needs, you can configure OpenTelemetry manually.
Firstly you need to install OpenTelemetry packages:
Terminal
npm install @opentelemetry/sdk-node @opentelemetry/resources @opentelemetry/semantic-conventions @opentelemetry/sdk-trace-node @opentelemetry/exporter-trace-otlp-http
Now you can initialize `NodeSDK` in your `instrumentation.ts`. Unlike `@vercel/otel`, `NodeSDK` is not compatible with edge runtime, so you need to make sure that you are importing them only when `process.env.NEXT_RUNTIME === 'nodejs'`. We recommend creating a new file `instrumentation.node.ts` which you conditionally import only when using node:
instrumentation.ts
TypeScript
JavaScriptTypeScript
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
await import('./instrumentation.node.ts')
}
}
instrumentation.node.ts
TypeScript
JavaScriptTypeScript
import { NodeSDK } from '@opentelemetry/sdk-node'
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'
import { Resource } from '@opentelemetry/resources'
import { SEMRESATTRS_SERVICE_NAME } from '@opentelemetry/semantic-conventions'
import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node'
const sdk = new NodeSDK({
resource: new Resource({
[SEMRESATTRS_SERVICE_NAME]: 'next-app',
}),
spanProcessor: new SimpleSpanProcessor(new OTLPTraceExporter()),
})
sdk.start()
Doing this is equivalent to using `@vercel/otel`, but it's possible to modify and extend some features that are not exposed by the `@vercel/otel`. If edge runtime support is necessary, you will have to use `@vercel/otel`.
[Testing your instrumentation](#testing-your-instrumentation)
-------------------------------------------------------------
You need an OpenTelemetry collector with a compatible backend to test OpenTelemetry traces locally. We recommend using our [OpenTelemetry dev environment](https://github.com/vercel/opentelemetry-collector-dev-setup).
If everything works well you should be able to see the root server span labeled as `GET /requested/pathname`. All other spans from that particular trace will be nested under it.
Next.js traces more spans than are emitted by default. To see more spans, you must set `NEXT_OTEL_VERBOSE=1`.
[Deployment](#deployment)
-------------------------
### [Using OpenTelemetry Collector](#using-opentelemetry-collector)
When you are deploying with OpenTelemetry Collector, you can use `@vercel/otel`. It will work both on Vercel and when self-hosted.
#### [Deploying on Vercel](#deploying-on-vercel)
We made sure that OpenTelemetry works out of the box on Vercel.
Follow [Vercel documentation](https://vercel.com/docs/concepts/observability/otel-overview/quickstart) to connect your project to an observability provider.
#### [Self-hosting](#self-hosting)
Deploying to other platforms is also straightforward. You will need to spin up your own OpenTelemetry Collector to receive and process the telemetry data from your Next.js app.
To do this, follow the [OpenTelemetry Collector Getting Started guide](https://opentelemetry.io/docs/collector/getting-started/), which will walk you through setting up the collector and configuring it to receive data from your Next.js app.
Once you have your collector up and running, you can deploy your Next.js app to your chosen platform following their respective deployment guides.
### [Custom Exporters](#custom-exporters)
OpenTelemetry Collector is not necessary. You can use a custom OpenTelemetry exporter with [`@vercel/otel`](#using-vercelotel) or [manual OpenTelemetry configuration](/docs/pages/building-your-application/optimizing/open-telemetry#manual-opentelemetry-configuration).
[Custom Spans](#custom-spans)
-----------------------------
You can add a custom span with [OpenTelemetry APIs](https://opentelemetry.io/docs/instrumentation/js/instrumentation).
Terminal
npm install @opentelemetry/api
The following example demonstrates a function that fetches GitHub stars and adds a custom `fetchGithubStars` span to track the fetch request's result:
import { trace } from '@opentelemetry/api'
export async function fetchGithubStars() {
return await trace
.getTracer('nextjs-example')
.startActiveSpan('fetchGithubStars', async (span) => {
try {
return await getValue()
} finally {
span.end()
}
})
}
The `register` function will execute before your code runs in a new environment. You can start creating new spans, and they should be correctly added to the exported trace.
[Default Spans in Next.js](#default-spans-in-nextjs)
----------------------------------------------------
Next.js automatically instruments several spans for you to provide useful insights into your application's performance.
Attributes on spans follow [OpenTelemetry semantic conventions](https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/). We also add some custom attributes under the `next` namespace:
* `next.span_name` - duplicates span name
* `next.span_type` - each span type has a unique identifier
* `next.route` - The route pattern of the request (e.g., `/[param]/user`).
* `next.rsc` (true/false) - Whether the request is an RSC request, such as prefetch.
* `next.page`
* This is an internal value used by an app router.
* You can think about it as a route to a special file (like `page.ts`, `layout.ts`, `loading.ts` and others)
* It can be used as a unique identifier only when paired with `next.route` because `/layout` can be used to identify both `/(groupA)/layout.ts` and `/(groupB)/layout.ts`
### [`[http.method] [next.route]`](#httpmethod-nextroute)
* `next.span_type`: `BaseServer.handleRequest`
This span represents the root span for each incoming request to your Next.js application. It tracks the HTTP method, route, target, and status code of the request.
Attributes:
* [Common HTTP attributes](https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#common-attributes)
* `http.method`
* `http.status_code`
* [Server HTTP attributes](https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#http-server-semantic-conventions)
* `http.route`
* `http.target`
* `next.span_name`
* `next.span_type`
* `next.route`
### [`render route (app) [next.route]`](#render-route-app-nextroute)
* `next.span_type`: `AppRender.getBodyResult`.
This span represents the process of rendering a route in the app router.
Attributes:
* `next.span_name`
* `next.span_type`
* `next.route`
### [`fetch [http.method] [http.url]`](#fetch-httpmethod-httpurl)
* `next.span_type`: `AppRender.fetch`
This span represents the fetch request executed in your code.
Attributes:
* [Common HTTP attributes](https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#common-attributes)
* `http.method`
* [Client HTTP attributes](https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#http-client)
* `http.url`
* `net.peer.name`
* `net.peer.port` (only if specified)
* `next.span_name`
* `next.span_type`
This span can be turned off by setting `NEXT_OTEL_FETCH_DISABLED=1` in your environment. This is useful when you want to use a custom fetch instrumentation library.
### [`executing api route (app) [next.route]`](#executing-api-route-app-nextroute)
* `next.span_type`: `AppRouteRouteHandlers.runHandler`.
This span represents the execution of an API route handler in the app router.
Attributes:
* `next.span_name`
* `next.span_type`
* `next.route`
### [`getServerSideProps [next.route]`](#getserversideprops-nextroute)
* `next.span_type`: `Render.getServerSideProps`.
This span represents the execution of `getServerSideProps` for a specific route.
Attributes:
* `next.span_name`
* `next.span_type`
* `next.route`
### [`getStaticProps [next.route]`](#getstaticprops-nextroute)
* `next.span_type`: `Render.getStaticProps`.
This span represents the execution of `getStaticProps` for a specific route.
Attributes:
* `next.span_name`
* `next.span_type`
* `next.route`
### [`render route (pages) [next.route]`](#render-route-pages-nextroute)
* `next.span_type`: `Render.renderDocument`.
This span represents the process of rendering the document for a specific route.
Attributes:
* `next.span_name`
* `next.span_type`
* `next.route`
### [`generateMetadata [next.page]`](#generatemetadata-nextpage)
* `next.span_type`: `ResolveMetadata.generateMetadata`.
This span represents the process of generating metadata for a specific page (a single route can have multiple of these spans).
Attributes:
* `next.span_name`
* `next.span_type`
* `next.page`
### [`resolve page components`](#resolve-page-components)
* `next.span_type`: `NextNodeServer.findPageComponents`.
This span represents the process of resolving page components for a specific page.
Attributes:
* `next.span_name`
* `next.span_type`
* `next.route`
### [`resolve segment modules`](#resolve-segment-modules)
* `next.span_type`: `NextNodeServer.getLayoutOrPageModule`.
This span represents loading of code modules for a layout or a page.
Attributes:
* `next.span_name`
* `next.span_type`
* `next.segment`
### [`start response`](#start-response)
* `next.span_type`: `NextNodeServer.startResponse`.
This zero-length span represents the time when the first byte has been sent in the response.
[Previous
Instrumentation
](/docs/app/building-your-application/optimizing/instrumentation)
[Next
Static Assets
](/docs/app/building-your-application/optimizing/static-assets)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"88cf392c10caf506f70596f821a2c065\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/route | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [
route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [HTTP Methods](#http-methods)
* [Parameters](#parameters)
* [request (optional)](#request-optional)
* [context (optional)](#context-optional)
* [NextResponse](#nextresponse)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/route.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)route.js
route.js
========
Route Handlers allow you to create custom request handlers for a given route using the Web [Request](https://developer.mozilla.org/docs/Web/API/Request) and [Response](https://developer.mozilla.org/docs/Web/API/Response) APIs.
[HTTP Methods](#http-methods)
-----------------------------
A **route** file allows you to create custom request handlers for a given route. The following [HTTP methods](https://developer.mozilla.org/docs/Web/HTTP/Methods) are supported: `GET`, `POST`, `PUT`, `PATCH`, `DELETE`, `HEAD`, and `OPTIONS`.
route.ts
TypeScript
JavaScriptTypeScript
export async function GET(request: Request) {}
export async function HEAD(request: Request) {}
export async function POST(request: Request) {}
export async function PUT(request: Request) {}
export async function DELETE(request: Request) {}
export async function PATCH(request: Request) {}
// If `OPTIONS` is not defined, Next.js will automatically implement `OPTIONS` and set the appropriate Response `Allow` header depending on the other methods defined in the route handler.
export async function OPTIONS(request: Request) {}
> **Good to know**: Route Handlers are only available inside the `app` directory. You **do not** need to use API Routes (`pages`) and Route Handlers (`app`) together, as Route Handlers should be able to handle all use cases.
[Parameters](#parameters)
-------------------------
### [`request` (optional)](#request-optional)
The `request` object is a [NextRequest](/docs/app/api-reference/functions/next-request) object, which is an extension of the Web [Request](https://developer.mozilla.org/docs/Web/API/Request) API. `NextRequest` gives you further control over the incoming request, including easily accessing `cookies` and an extended, parsed, URL object `nextUrl`.
### [`context` (optional)](#context-optional)
app/dashboard/\[team\]/route.ts
TypeScript
JavaScriptTypeScript
type Params = {
team: string
}
export async function GET(request: Request, context: { params: Params }) {
const team = context.params.team // '1'
}
// Define params type according to your route parameters (see table below)
Currently, the only value of `context` is `params`, which is an object containing the [dynamic route parameters](/docs/app/building-your-application/routing/dynamic-routes) for the current route.
Example
URL
`params`
`app/dashboard/[team]/route.js`
`/dashboard/1`
`{ team: '1' }`
`app/shop/[tag]/[item]/route.js`
`/shop/1/2`
`{ tag: '1', item: '2' }`
`app/blog/[...slug]/route.js`
`/blog/1/2`
`{ slug: ['1', '2'] }`
[NextResponse](#nextresponse)
-----------------------------
Route Handlers can extend the Web Response API by returning a `NextResponse` object. This allows you to easily set cookies, headers, redirect, and rewrite. [View the API reference](/docs/app/api-reference/functions/next-response).
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.2.0`
Route handlers are introduced.
[Previous
page.js
](/docs/app/api-reference/file-conventions/page)
[Next
Route Segment Config
](/docs/app/api-reference/file-conventions/route-segment-config)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"26e10475a882ba88a20f251e4356b867\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/not-found | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [
not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Props](#props)
* [Data Fetching](#data-fetching)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/not-found.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)not-found.js
not-found.js
============
The **not-found** file is used to render UI when the [`notFound`](/docs/app/api-reference/functions/not-found) function is thrown within a route segment. Along with serving a custom UI, Next.js will return a `200` HTTP status code for streamed responses, and `404` for non-streamed responses.
app/not-found.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
export default function NotFound() {
return (
<div>
<h2>Not Found</h2>
<p>Could not find requested resource</p>
<Link href="/">Return Home</Link>
</div>
)
}
> **Good to know**: In addition to catching expected `notFound()` errors, the root `app/not-found.js` file also handles any unmatched URLs for your whole application. This means users that visit a URL that is not handled by your app will be shown the UI exported by the `app/not-found.js` file.
[Props](#props)
---------------
`not-found.js` components do not accept any props.
[Data Fetching](#data-fetching)
-------------------------------
By default, `not-found` is a Server Component. You can mark it as `async` to fetch and display data:
app/not-found.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
import { headers } from 'next/headers'
export default async function NotFound() {
const headersList = headers()
const domain = headersList.get('host')
const data = await getSiteData(domain)
return (
<div>
<h2>Not Found: {data.name}</h2>
<p>Could not find requested resource</p>
<p>
View <Link href="/blog">all posts</Link>
</p>
</div>
)
}
If you need to use Client Component hooks like `usePathname` to display content based on the path, you must fetch data on the client-side instead.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
Root `app/not-found` handles global unmatched URLs.
`v13.0.0`
`not-found` introduced.
[Previous
middleware.js
](/docs/app/api-reference/file-conventions/middleware)
[Next
page.js
](/docs/app/api-reference/file-conventions/page)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"d8de548f6a9faab1ba274e68a95cd2bc\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/use-pathname | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [
usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [Returns](#returns)
* [Examples](#examples)
* [Do something in response to a route change](#do-something-in-response-to-a-route-change)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/use-pathname.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)usePathname
usePathname
===========
`usePathname` is a **Client Component** hook that lets you read the current URL's **pathname**.
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'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](/docs/app/building-your-application/rendering/client-components). It's important to note Client Components are not a de-optimization. They are an integral part of the [Server Components](/docs/app/building-your-application/rendering/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**:
>
> * Reading the current URL from a [Server Component](/docs/app/building-your-application/rendering/server-components) 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](/docs/pages/api-reference/functions/get-static-paths#fallback-true) is being rendered or when a `pages` directory page has been [automatically statically optimized](/docs/pages/building-your-application/rendering/automatic-static-optimization) 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](#parameters)
-------------------------
const pathname = usePathname()
`usePathname` does not take any parameters.
[Returns](#returns)
-------------------
`usePathname` returns a string of the current URL's pathname. For example:
URL
Returned value
`/`
`'/'`
`/dashboard`
`'/dashboard'`
`/dashboard?v=2`
`'/dashboard'`
`/blog/hello-world`
`'/blog/hello-world'`
[Examples](#examples)
---------------------
### [Do something in response to a route change](#do-something-in-response-to-a-route-change)
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { usePathname, useSearchParams } from 'next/navigation'
function ExampleClientComponent() {
const pathname = usePathname()
const searchParams = useSearchParams()
useEffect(() => {
// Do something here...
}, [pathname, searchParams])
}
Version
Changes
`v13.0.0`
`usePathname` introduced.
[Previous
useParams
](/docs/app/api-reference/functions/use-params)
[Next
useReportWebVitals
](/docs/app/api-reference/functions/use-report-web-vitals)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2b4c102516a40512c9b643e70d428236\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/use-report-web-vitals | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [
useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [useReportWebVitals](#usereportwebvitals)
* [Web Vitals](#web-vitals)
* [Usage on Vercel](#usage-on-vercel)
* [Sending results to external systems](#sending-results-to-external-systems)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/use-report-web-vitals.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)useReportWebVitals
useReportWebVitals
==================
The `useReportWebVitals` hook allows you to report [Core Web Vitals](https://web.dev/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>
)
}
> 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](#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](#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.
* `entries`: An array of [Performance Entries](https://developer.mozilla.org/docs/Web/API/PerformanceEntry) associated with the metric. These entries provide detailed information about the performance events related to the metric.
* `navigationType`: Indicates the [type of navigation](https://developer.mozilla.org/docs/Web/API/PerformanceNavigationTiming/type) 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 API](https://developer.mozilla.org/docs/Web/API/Performance_API)s.
[Web Vitals](#web-vitals)
-------------------------
[Web Vitals](https://web.dev/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](https://developer.mozilla.org/docs/Glossary/Time_to_first_byte) (TTFB)
* [First Contentful Paint](https://developer.mozilla.org/docs/Glossary/First_contentful_paint) (FCP)
* [Largest Contentful Paint](https://web.dev/lcp/) (LCP)
* [First Input Delay](https://web.dev/fid/) (FID)
* [Cumulative Layout Shift](https://web.dev/cls/) (CLS)
* [Interaction to Next Paint](https://web.dev/inp/) (INP)
You can handle all the results of these metrics using the `name` property.
app/components/web-vitals.tsx
TypeScript
JavaScriptTypeScript
'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
}
// ...
}
})
}
[Usage on Vercel](#usage-on-vercel)
-----------------------------------
[Vercel Speed Insights](https://vercel.com/docs/concepts/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](#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 {
fetch(url, { body, method: 'POST', keepalive: true })
}
})
> **Good to know**: If you use [Google Analytics](https://analytics.google.com/analytics/web/), 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.
> });
> }
>
> Read more about [sending results to Google Analytics](https://github.com/GoogleChrome/web-vitals#send-the-results-to-google-analytics).
[Previous
usePathname
](/docs/app/api-reference/functions/use-pathname)
[Next
useRouter
](/docs/app/api-reference/functions/use-router)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"ba12808a0bb084691f00f287b3ac652e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/testing/jest | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [
Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Quickstart](#quickstart)
* [Manual setup](#manual-setup)
* [Optional: Handling Absolute Imports and Module Path Aliases](#optional-handling-absolute-imports-and-module-path-aliases)
* [Optional: Extend Jest with custom matchers](#optional-extend-jest-with-custom-matchers)
* [Add a test script to package.json:](#add-a-test-script-to-packagejson)
* [Creating your first test:](#creating-your-first-test)
* [Running your tests](#running-your-tests)
* [Additional Resources](#additional-resources)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/08-testing/02-jest.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Testing](/docs/app/building-your-application/testing)Jest
Setting up Jest with Next.js
============================
Jest and React Testing Library are frequently used together for **Unit Testing** and **Snapshot Testing**. This guide will show you how to set up Jest with Next.js and write your first tests.
> **Good to know:** Since `async` Server Components are new to the React ecosystem, Jest currently does not support them. While you can still run **unit tests** for synchronous Server and Client Components, we recommend using an **E2E tests** for `async` components.
[Quickstart](#quickstart)
-------------------------
You can use `create-next-app` with the Next.js [with-jest](https://github.com/vercel/next.js/tree/canary/examples/with-jest) example to quickly get started:
Terminal
npx create-next-app@latest --example with-jest with-jest-app
[Manual setup](#manual-setup)
-----------------------------
Since the release of [Next.js 12](https://nextjs.org/blog/next-12), Next.js now has built-in configuration for Jest.
To set up Jest, install `jest` and the following packages as dev dependencies:
Terminal
npm install -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom
# or
yarn add -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom
# or
pnpm install -D jest jest-environment-jsdom @testing-library/react @testing-library/jest-dom
Generate a basic Jest configuration file by running the following command:
Terminal
npm init jest@latest
# or
yarn create jest@latest
# or
pnpm create jest@latest
This will take you through a series of prompts to setup Jest for your project, including automatically creating a `jest.config.ts|js` file.
Update your config file to use `next/jest`. This transformer has all the necessary configuration options for Jest to work with Next.js:
jest.config.ts
TypeScript
JavaScriptTypeScript
import type { Config } from 'jest'
import nextJest from 'next/jest.js'
const createJestConfig = nextJest({
// Provide the path to your Next.js app to load next.config.js and .env files in your test environment
dir: './',
})
// Add any custom config to be passed to Jest
const config: Config = {
coverageProvider: 'v8',
testEnvironment: 'jsdom',
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
}
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
export default createJestConfig(config)
Under the hood, `next/jest` is automatically configuring Jest for you, including:
* Setting up `transform` using the [Next.js Compiler](/docs/architecture/nextjs-compiler)
* Auto mocking stylesheets (`.css`, `.module.css`, and their scss variants), image imports and [`next/font`](/docs/pages/building-your-application/optimizing/fonts)
* Loading `.env` (and all variants) into `process.env`
* Ignoring `node_modules` from test resolving and transforms
* Ignoring `.next` from test resolving
* Loading `next.config.js` for flags that enable SWC transforms
> **Good to know**: To test environment variables directly, load them manually in a separate setup script or in your `jest.config.ts` file. For more information, please see [Test Environment Variables](/docs/pages/building-your-application/configuring/environment-variables#test-environment-variables).
[Optional: Handling Absolute Imports and Module Path Aliases](#optional-handling-absolute-imports-and-module-path-aliases)
--------------------------------------------------------------------------------------------------------------------------
If your project is using [Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases), you will need to configure Jest to resolve the imports by matching the paths option in the `jsconfig.json` file with the `moduleNameMapper` option in the `jest.config.js` file. For example:
tsconfig.json or jsconfig.json
{
"compilerOptions": {
"module": "esnext",
"moduleResolution": "bundler",
"baseUrl": "./",
"paths": {
"@/components/*": ["components/*"]
}
}
}
jest.config.js
moduleNameMapper: {
// ...
'^@/components/(.*)$': '<rootDir>/components/$1',
}
[Optional: Extend Jest with custom matchers](#optional-extend-jest-with-custom-matchers)
----------------------------------------------------------------------------------------
`@testing-library/jest-dom` includes a set of convenient [custom matchers](https://github.com/testing-library/jest-dom#custom-matchers) such as `.toBeInTheDocument()` making it easier to write tests. You can import the custom matchers for every test by adding the following option to the Jest configuration file:
jest.config.ts
TypeScript
JavaScriptTypeScript
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts']
Then, inside `jest.setup.ts`, add the following import:
jest.setup.ts
TypeScript
JavaScriptTypeScript
import '@testing-library/jest-dom'
> **Good to know:**[`extend-expect` was removed in `v6.0`](https://github.com/testing-library/jest-dom/releases/tag/v6.0.0), so if you are using `@testing-library/jest-dom` before version 6, you will need to import `@testing-library/jest-dom/extend-expect` instead.
If you need to add more setup options before each test, you can add them to the `jest.setup.js` file above.
[Add a test script to `package.json`:](#add-a-test-script-to-packagejson)
-------------------------------------------------------------------------
Finally, add a Jest `test` script to your `package.json` file:
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"test": "jest",
"test:watch": "jest --watch"
}
}
`jest --watch` will re-run tests when a file is changed. For more Jest CLI options, please refer to the [Jest Docs](https://jestjs.io/docs/cli#reference).
### [Creating your first test:](#creating-your-first-test)
Your project is now ready to run tests. Create a folder called `__tests__` in your project's root directory.
For example, we can add a test to check if the `<Page />` component successfully renders a heading:
import Link from 'next/link'
export default function Home() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
\_\_tests\_\_/page.test.jsx
import '@testing-library/jest-dom'
import { render, screen } from '@testing-library/react'
import Page from '../app/page'
describe('Page', () => {
it('renders a heading', () => {
render(<Page />)
const heading = screen.getByRole('heading', { level: 1 })
expect(heading).toBeInTheDocument()
})
})
Optionally, add a [snapshot test](https://jestjs.io/docs/snapshot-testing) to keep track of any unexpected changes in your component:
\_\_tests\_\_/snapshot.js
import { render } from '@testing-library/react'
import Page from '../app/page'
it('renders homepage unchanged', () => {
const { container } = render(<Page />)
expect(container).toMatchSnapshot()
})
[Running your tests](#running-your-tests)
-----------------------------------------
Then, run the following command to run your tests:
Terminal
npm run test
# or
yarn test
# or
pnpm test
[Additional Resources](#additional-resources)
---------------------------------------------
For further reading, you may find these resources helpful:
* [Next.js with Jest example](https://github.com/vercel/next.js/tree/canary/examples/with-jest)
* [Jest Docs](https://jestjs.io/docs/getting-started)
* [React Testing Library Docs](https://testing-library.com/docs/react-testing-library/intro/)
* [Testing Playground](https://testing-playground.com/) - use good testing practices to match elements.
[Previous
Vitest
](/docs/app/building-your-application/testing/vitest)
[Next
Playwright
](/docs/app/building-your-application/testing/playwright)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"faedb887e80298dbab6e0f36dd21f887\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/page | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [
page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Props](#props)
* [params (optional)](#params-optional)
* [searchParams (optional)](#searchparams-optional)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/page.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)page.js
page.js
=======
A **page** is UI that is unique to a route.
app/blog/\[slug\]/page.tsx
TypeScript
JavaScriptTypeScript
export default function Page({
params,
searchParams,
}: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
}) {
return <h1>My Page</h1>
}
[Props](#props)
---------------
### [`params` (optional)](#params-optional)
An object containing the [dynamic route parameters](/docs/app/building-your-application/routing/dynamic-routes) from the root segment down to that page. For example:
Example
URL
`params`
`app/shop/[slug]/page.js`
`/shop/1`
`{ slug: '1' }`
`app/shop/[category]/[item]/page.js`
`/shop/1/2`
`{ category: '1', item: '2' }`
`app/shop/[...slug]/page.js`
`/shop/1/2`
`{ slug: ['1', '2'] }`
### [`searchParams` (optional)](#searchparams-optional)
An object containing the [search parameters](https://developer.mozilla.org/docs/Learn/Common_questions/What_is_a_URL#parameters) of the current URL. For example:
URL
`searchParams`
`/shop?a=1`
`{ a: '1' }`
`/shop?a=1&b=2`
`{ a: '1', b: '2' }`
`/shop?a=1&a=2`
`{ a: ['1', '2'] }`
> **Good to know**:
>
> * `searchParams` is a **[Dynamic API](/docs/app/building-your-application/rendering/server-components#server-rendering-strategies#dynamic-functions)** whose values cannot be known ahead of time. Using it will opt the page into **[dynamic rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering)** at request time.
> * `searchParams` returns a plain JavaScript object and not a `URLSearchParams` instance.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`page` introduced.
[Previous
not-found.js
](/docs/app/api-reference/file-conventions/not-found)
[Next
route.js
](/docs/app/api-reference/file-conventions/route)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"6be467bc761dca22c824e9343b349a78\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/testing/playwright | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [
Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Quickstart](#quickstart)
* [Manual setup](#manual-setup)
* [Creating your first Playwright E2E test](#creating-your-first-playwright-e2e-test)
* [Running your Playwright tests](#running-your-playwright-tests)
* [Running Playwright on Continuous Integration (CI)](#running-playwright-on-continuous-integration-ci)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/08-testing/03-playwright.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Testing](/docs/app/building-your-application/testing)Playwright
Setting up Playwright with Next.js
==================================
Playwright is a testing framework that lets you automate Chromium, Firefox, and WebKit with a single API. You can use it to write **End-to-End (E2E)** testing. This guide will show you how to set up Playwright with Next.js and write your first tests.
[Quickstart](#quickstart)
-------------------------
The fastest way to get started is to use `create-next-app` with the [with-playwright example](https://github.com/vercel/next.js/tree/canary/examples/with-playwright). This will create a Next.js project complete with Playwright configured.
Terminal
npx create-next-app@latest --example with-playwright with-playwright-app
[Manual setup](#manual-setup)
-----------------------------
To install Playwright, run the following command:
Terminal
npm init playwright
# or
yarn create playwright
# or
pnpm create playwright
This will take you through a series of prompts to setup and configure Playwright for your project, including adding a `playwright.config.ts` file. Please refer to the [Playwright installation guide](https://playwright.dev/docs/intro#installation) for the step-by-step guide.
[Creating your first Playwright E2E test](#creating-your-first-playwright-e2e-test)
-----------------------------------------------------------------------------------
Create two new Next.js pages:
app/page.tsx
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
app/about/page.tsx
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>About</h1>
<Link href="/">Home</Link>
</div>
)
}
Then, add a test to verify that your navigation is working correctly:
tests/example.spec.ts
import { test, expect } from '@playwright/test'
test('should navigate to the about page', async ({ page }) => {
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
await page.goto('http://localhost:3000/')
// Find an element with the text 'About' and click on it
await page.click('text=About')
// The new URL should be "/about" (baseURL is used there)
await expect(page).toHaveURL('http://localhost:3000/about')
// The new page should contain an h1 with "About"
await expect(page.locator('h1')).toContainText('About')
})
> **Good to know**:
>
> You can use `page.goto("/")` instead of `page.goto("http://localhost:3000/")`, if you add [`"baseURL": "http://localhost:3000"`](https://playwright.dev/docs/api/class-testoptions#test-options-base-url) to the `playwright.config.ts` [configuration file](https://playwright.dev/docs/test-configuration).
### [Running your Playwright tests](#running-your-playwright-tests)
Playwright will simulate a user navigating your application using three browsers: Chromium, Firefox and Webkit, this requires your Next.js server to be running. We recommend running your tests against your production code to more closely resemble how your application will behave.
Run `npm run build` and `npm run start`, then run `npx playwright test` in another terminal window to run the Playwright tests.
> **Good to know**: Alternatively, you can use the [`webServer`](https://playwright.dev/docs/test-webserver/) feature to let Playwright start the development server and wait until it's fully available.
### [Running Playwright on Continuous Integration (CI)](#running-playwright-on-continuous-integration-ci)
Playwright will by default run your tests in the [headless mode](https://playwright.dev/docs/ci#running-headed). To install all the Playwright dependencies, run `npx playwright install-deps`.
You can learn more about Playwright and Continuous Integration from these resources:
* [Next.js with Playwright example](https://github.com/vercel/next.js/tree/canary/examples/with-playwright)
* [Playwright on your CI provider](https://playwright.dev/docs/ci)
* [Playwright Discord](https://discord.com/invite/playwright-807756831384403968)
[Previous
Jest
](/docs/app/building-your-application/testing/jest)
[Next
Cypress
](/docs/app/building-your-application/testing/cypress)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"8d974eeb8900d4432e3f0b4f71e2f237\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/testing/cypress | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [
Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Quickstart](#quickstart)
* [Manual setup](#manual-setup)
* [Creating your first Cypress E2E test](#creating-your-first-cypress-e2e-test)
* [Running E2E Tests](#running-e2e-tests)
* [Creating your first Cypress component test](#creating-your-first-cypress-component-test)
* [Running Component Tests](#running-component-tests)
* [Continuous Integration (CI)](#continuous-integration-ci)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/08-testing/04-cypress.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Testing](/docs/app/building-your-application/testing)Cypress
Setting up Cypress with Next.js
===============================
[Cypress](https://www.cypress.io/) is a test runner used for **End-to-End (E2E)** and **Component Testing**. This page will show you how to set up Cypress with Next.js and write your first tests.
> **Warning:**
>
> * For **component testing**, Cypress currently does not support [Next.js version 14](https://github.com/cypress-io/cypress/issues/28185) and `async` Server Components. These issues are being tracked. For now, component testing works with Next.js version 13, and we recommend E2E testing for `async` Server Components.
> * Cypress currently does not support [TypeScript version 5](https://github.com/cypress-io/cypress/issues/27731) with `moduleResolution:"bundler"`. This issue is being tracked.
[Quickstart](#quickstart)
-------------------------
You can use `create-next-app` with the [with-cypress example](https://github.com/vercel/next.js/tree/canary/examples/with-cypress) to quickly get started.
Terminal
npx create-next-app@latest --example with-cypress with-cypress-app
[Manual setup](#manual-setup)
-----------------------------
To manually set up Cypress, install `cypress` as a dev dependency:
Terminal
npm install -D cypress
# or
yarn add -D cypress
# or
pnpm install -D cypress
Add the Cypress `open` command to the `package.json` scripts field:
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"cypress:open": "cypress open"
}
}
Run Cypress for the first time to open the Cypress testing suite:
Terminal
npm run cypress:open
You can choose to configure **E2E Testing** and/or **Component Testing**. Selecting any of these options will automatically create a `cypress.config.js` file and a `cypress` folder in your project.
[Creating your first Cypress E2E test](#creating-your-first-cypress-e2e-test)
-----------------------------------------------------------------------------
Ensure your `cypress.config.js` file has the following configuration:
cypress.config.ts
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {},
},
})
cypress.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {},
},
})
Then, create two new Next.js files:
app/page.js
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
app/about/page.js
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>About</h1>
<Link href="/">Home</Link>
</div>
)
}
Add a test to check your navigation is working correctly:
cypress/e2e/app.cy.js
describe('Navigation', () => {
it('should navigate to the about page', () => {
// Start from the index page
cy.visit('http://localhost:3000/')
// Find a link with an href attribute containing "about" and click it
cy.get('a[href*="about"]').click()
// The new url should include "/about"
cy.url().should('include', '/about')
// The new page should contain an h1 with "About"
cy.get('h1').contains('About')
})
})
### [Running E2E Tests](#running-e2e-tests)
Cypress will simulate a user navigating your application, this requires your Next.js server to be running. We recommend running your tests against your production code to more closely resemble how your application will behave.
Run `npm run build && npm run start` to build your Next.js application, then run `npm run cypress:open` in another terminal window to start Cypress and run your E2E testing suite.
> **Good to know:**
>
> * You can use `cy.visit("/")` instead of `cy.visit("http://localhost:3000/")` by adding `baseUrl: 'http://localhost:3000'` to the `cypress.config.js` configuration file.
> * Alternatively, you can install the `start-server-and-test` package to run the Next.js production server in conjunction with Cypress. After installation, add `"test": "start-server-and-test start http://localhost:3000 cypress"` to your `package.json` scripts field. Remember to rebuild your application after new changes.
[Creating your first Cypress component test](#creating-your-first-cypress-component-test)
-----------------------------------------------------------------------------------------
Component tests build and mount a specific component without having to bundle your whole application or start a server.
Select **Component Testing** in the Cypress app, then select **Next.js** as your front-end framework. A `cypress/component` folder will be created in your project, and a `cypress.config.js` file will be updated to enable component testing.
Ensure your `cypress.config.js` file has the following configuration:
cypress.config.ts
import { defineConfig } from 'cypress'
export default defineConfig({
component: {
devServer: {
framework: 'next',
bundler: 'webpack',
},
},
})
cypress.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
component: {
devServer: {
framework: 'next',
bundler: 'webpack',
},
},
})
Assuming the same components from the previous section, add a test to validate a component is rendering the expected output:
cypress/component/about.cy.tsx
import Page from '../../app/page'
describe('<Page />', () => {
it('should render and display expected content', () => {
// Mount the React component for the Home page
cy.mount(<Page />)
// The new page should contain an h1 with "Home"
cy.get('h1').contains('Home')
// Validate that a link with the expected URL is present
// Following the link is better suited to an E2E test
cy.get('a[href="/about"]').should('be.visible')
})
})
> **Good to know**:
>
> * Cypress currently doesn't support component testing for `async` Server Components. We recommend using E2E testing.
> * Since component tests do not require a Next.js server, features like `<Image />` that rely on a server being available may not function out-of-the-box.
### [Running Component Tests](#running-component-tests)
Run `npm run cypress:open` in your terminal to start Cypress and run your component testing suite.
[Continuous Integration (CI)](#continuous-integration-ci)
---------------------------------------------------------
In addition to interactive testing, you can also run Cypress headlessly using the `cypress run` command, which is better suited for CI environments:
package.json
{
"scripts": {
//...
"e2e": "start-server-and-test dev http://localhost:3000 \"cypress open --e2e\"",
"e2e:headless": "start-server-and-test dev http://localhost:3000 \"cypress run --e2e\"",
"component": "cypress open --component",
"component:headless": "cypress run --component"
}
}
You can learn more about Cypress and Continuous Integration from these resources:
* [Next.js with Cypress example](https://github.com/vercel/next.js/tree/canary/examples/with-cypress)
* [Cypress Continuous Integration Docs](https://docs.cypress.io/guides/continuous-integration/introduction)
* [Cypress GitHub Actions Guide](https://on.cypress.io/github-actions)
* [Official Cypress GitHub Action](https://github.com/cypress-io/github-action)
* [Cypress Discord](https://discord.com/invite/cypress)
[Previous
Playwright
](/docs/app/building-your-application/testing/playwright)
[Next
Authentication
](/docs/app/building-your-application/authentication)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"8f58b62a2f94d235bc67d5244f390246\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/authentication | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Authentication](#authentication)
* [Authentication Strategies](#authentication-strategies)
* [Implementing Authentication](#implementing-authentication)
* [Authorization](#authorization)
* [Protecting Routes with Middleware](#protecting-routes-with-middleware)
* [Setting Up Middleware:](#setting-up-middleware)
* [Defining Protected Routes:](#defining-protected-routes)
* [Middleware Logic:](#middleware-logic)
* [Handling Unauthorized Access:](#handling-unauthorized-access)
* [Protecting Server Actions](#protecting-server-actions)
* [Protecting Route Handlers](#protecting-route-handlers)
* [Authorization Using Server Components](#authorization-using-server-components)
* [Best Practices](#best-practices)
* [Session Management](#session-management)
* [Cookie-Based Sessions](#cookie-based-sessions)
* [Database Sessions](#database-sessions)
* [Selecting Session Management in Next.js](#selecting-session-management-in-nextjs)
* [Examples](#examples)
* [Further Reading](#further-reading)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/09-authentication/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[Building Your Application](/docs/app/building-your-application)Authentication
Authentication
==============
To implement authentication in Next.js, familiarize yourself with three foundational concepts:
* **[Authentication](#authentication)** verifies if the user is who they say they are. It requires the user to prove their identity with something they have, such as a username and password.
* **[Session Management](#session-management)** tracks the user's state (e.g. logged in) across multiple requests.
* **[Authorization](#authorization)** decides what parts of the application the user is allowed to access.
This page demonstrates how to use Next.js features to implement common authentication, authorization, and session management patterns so you can choose the best solutions based on your application's needs.
[Authentication](#authentication)
---------------------------------
Authentication verifies a user's identity. This happens when a user logs in, either with a username and password or through a service like Google. It's all about confirming that users are really who they claim to be, protecting both the user's data and the application from unauthorized access or fraudulent activities.
### [Authentication Strategies](#authentication-strategies)
Modern web applications commonly use several authentication strategies:
1. **OAuth/OpenID Connect (OIDC)**: Enable third-party access without sharing user credentials. Ideal for social media logins and Single Sign-On (SSO) solutions. They add an identity layer with OpenID Connect.
2. **Credentials-based login (Email + Password)**: A standard choice for web applications, where users log in with an email and password. Familiar and easy to implement, it requires robust security measures against threats like phishing.
3. **Passwordless/Token-based authentication**: Use email magic links or SMS one-time codes for secure, password-free access. Popular for its convenience and enhanced security, this method helps reduce password fatigue. Its limitation is the dependency on the user's email or phone availability.
4. **Passkeys/WebAuthn**: Use cryptographic credentials unique to each site, offering high security against phishing. Secure but new, this strategy can be difficult to implement.
Selecting an authentication strategy should align with your application's specific requirements, user interface considerations, and security objectives.
### [Implementing Authentication](#implementing-authentication)
In this section, we'll explore the process of adding basic email-password authentication to a web application. While this method provides a fundamental level of security, it's worth considering more advanced options like OAuth or passwordless logins for enhanced protection against common security threats. The authentication flow we'll discuss is as follows:
1. The user submits their credentials through a login form.
2. The form calls a Server Action.
3. Upon successful verification, the process is completed, indicating the user's successful authentication.
4. If verification is unsuccessful, an error message is shown.
Consider a login form where users can input their credentials:
app/login/page.tsx
TypeScript
JavaScriptTypeScript
import { authenticate } from '@/app/lib/actions'
export default function Page() {
return (
<form action={authenticate}>
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<button type="submit">Login</button>
</form>
)
}
The form above has two input fields for capturing the user's email and password. On submission, it calls the `authenticate` Server Action.
You can then call your Authentication Provider's API in the Server Action to handle authentication:
app/lib/actions.ts
TypeScript
JavaScriptTypeScript
'use server'
import { signIn } from '@/auth'
export async function authenticate(_currentState: unknown, formData: FormData) {
try {
await signIn('credentials', formData)
} catch (error) {
if (error) {
switch (error.type) {
case 'CredentialsSignin':
return 'Invalid credentials.'
default:
return 'Something went wrong.'
}
}
throw error
}
}
In this code, the `signIn` method checks the credentials against stored user data. After the authentication provider processes the credentials, there are two possible outcomes:
* **Successful Authentication**: This outcome implies that the login was successful. Further actions, such as accessing protected routes and fetching user information, can then be initiated.
* **Failed Authentication**: In cases where the credentials are incorrect or an error is encountered, the function returns a corresponding error message to indicate the authentication failure.
Finally, in your `login-form.tsx` component, you can use React's `useFormState` to call the Server Action and handle form errors, and use `useFormStatus` to handle the pending state of the form:
app/login/page.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { authenticate } from '@/app/lib/actions'
import { useFormState, useFormStatus } from 'react-dom'
export default function Page() {
const [errorMessage, dispatch] = useFormState(authenticate, undefined)
return (
<form action={dispatch}>
<input type="email" name="email" placeholder="Email" required />
<input type="password" name="password" placeholder="Password" required />
<div>{errorMessage && <p>{errorMessage}</p>}</div>
<LoginButton />
</form>
)
}
function LoginButton() {
const { pending } = useFormStatus()
const handleClick = (event) => {
if (pending) {
event.preventDefault()
}
}
return (
<button aria-disabled={pending} type="submit" onClick={handleClick}>
Login
</button>
)
}
For a more streamlined authentication setup in Next.js projects, especially when offering multiple login methods, consider using a comprehensive [authentication solution](#examples).
[Authorization](#authorization)
-------------------------------
Once a user is authenticated, you'll need to ensure the user is allowed to visit certain routes, and perform operations such as mutating data with Server Actions and calling Route Handlers.
### [Protecting Routes with Middleware](#protecting-routes-with-middleware)
[Middleware](/docs/app/building-your-application/routing/middleware) in Next.js helps you control who can access different parts of your website. This is important for keeping areas like the user dashboard protected while having other pages like marketing pages be public. It's recommended to apply Middleware across all routes and specify exclusions for public access.
Here's how to implement Middleware for authentication in Next.js:
#### [Setting Up Middleware:](#setting-up-middleware)
* Create a `middleware.ts` or `.js` file in your project's root directory.
* Include logic to authorize user access, such as checking for authentication tokens.
#### [Defining Protected Routes:](#defining-protected-routes)
* Not all routes require authorization. Use the `matcher` option in your Middleware to specify any routes that do not require authorization checks.
#### [Middleware Logic:](#middleware-logic)
* Write logic to verify if a user is authenticated. Check user roles or permissions for route authorization.
#### [Handling Unauthorized Access:](#handling-unauthorized-access)
* Redirect unauthorized users to a login or error page as appropriate.
Example Middleware file:
middleware.ts
TypeScript
JavaScriptTypeScript
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
const currentUser = request.cookies.get('currentUser')?.value
if (currentUser && !request.nextUrl.pathname.startsWith('/dashboard')) {
return Response.redirect(new URL('/dashboard', request.url))
}
if (!currentUser && !request.nextUrl.pathname.startsWith('/login')) {
return Response.redirect(new URL('/login', request.url))
}
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
}
This example uses [`Response.redirect`](https://developer.mozilla.org/en-US/docs/Web/API/Response/redirect_static) for handling redirects early in the request pipeline, making it efficient and centralizing access control.
For specific redirection needs, the `redirect` function can be used in Server Components, Route Handlers, and Server Actions to provide more control. This is useful for role-based navigation or context-sensitive scenarios.
app/page.tsx
TypeScript
JavaScriptTypeScript
import { redirect } from 'next/navigation'
export default function Page() {
// Logic to determine if a redirect is needed
const accessDenied = true
if (accessDenied) {
redirect('/login')
}
// Define other routes and logic
}
After successful authentication, it's important to manage user navigation based on their roles. For example, an admin user might be redirected to an admin dashboard, while a regular user is sent to a different page. This is important for role-specific experiences and conditional navigation, such as prompting users to complete their profile if needed.
When setting up authorization, it's important to ensure that the main security checks happen where your app accesses or changes data. While Middleware can be useful for initial validation, it should not be the sole line of defense in protecting your data. The bulk of security checks should be performed in the Data Access Layer (DAL).
This approach, highlighted in [this security blog](/blog/security-nextjs-server-components-actions), advocates for consolidating all data access within a dedicated DAL. This strategy ensures consistent data access, minimizes authorization bugs, and simplifies maintenance. To ensure comprehensive security, consider the following key areas:
* Server Actions: Implement security checks in server-side processes, especially for sensitive operations.
* Route Handlers: Manage incoming requests with security measures to ensure access is limited to authorized users.
* Data Access Layer (DAL): Directly interacts with the database and is crucial for validating and authorizing data transactions. It's vital to perform critical checks within the DAL to secure data at its most crucial interaction point—access or modification.
For a detailed guide on securing the DAL, including example code snippets and advanced security practices, refer to our [Data Access Layer section](/blog/security-nextjs-server-components-actions#data-access-layer) of the security guide.
### [Protecting Server Actions](#protecting-server-actions)
It is important to treat [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) with the same security considerations as public-facing API endpoints. Verifying user authorization for each action is crucial. Implement checks within Server Actions to determine user permissions, such as restricting certain actions to admin users.
In the example below, we check the user's role before allowing the action to proceed:
app/lib/actions.ts
TypeScript
JavaScriptTypeScript
'use server'
// ...
export async function serverAction() {
const session = await getSession()
const userRole = session?.user?.role
// Check if user is authorized to perform the action
if (userRole !== 'admin') {
throw new Error('Unauthorized access: User does not have admin privileges.')
}
// Proceed with the action for authorized users
// ... implementation of the action
}
### [Protecting Route Handlers](#protecting-route-handlers)
Route Handlers in Next.js play a vital role in managing incoming requests. Just like Server Actions, they should be secured to ensure that only authorized users can access certain functionalities. This often involves verifying the user's authentication status and their permissions.
Here's an example of securing a Route Handler:
app/api/route.ts
TypeScript
JavaScriptTypeScript
export async function GET() {
// User authentication and role verification
const session = await getSession()
// Check if the user is authenticated
if (!session) {
return new Response(null, { status: 401 }) // User is not authenticated
}
// Check if the user has the 'admin' role
if (session.user.role !== 'admin') {
return new Response(null, { status: 403 }) // User is authenticated but does not have the right permissions
}
// Data fetching for authorized users
}
This example demonstrates a Route Handler with a two-tier security check for authentication and authorization. It first checks for an active session, and then verifies if the logged-in user is an 'admin'. This approach ensures secure access, limited to authenticated and authorized users, maintaining robust security for request processing.
### [Authorization Using Server Components](#authorization-using-server-components)
[Server Components](/docs/app/building-your-application/rendering/server-components) in Next.js are designed for server-side execution and offer a secure environment for integrating complex logic like authorization. They enable direct access to back-end resources, optimizing performance for data-heavy tasks and enhancing security for sensitive operations.
In Server Components, a common practice is to conditionally render UI elements based on the user's role. This approach enhances user experience and security by ensuring users only access content they are authorized to view.
**Example:**
app/dashboard/page.tsx
TypeScript
JavaScriptTypeScript
export default async function Dashboard() {
const session = await getSession()
const userRole = session?.user?.role // Assuming 'role' is part of the session object
if (userRole === 'admin') {
return <AdminDashboard /> // Component for admin users
} else if (userRole === 'user') {
return <UserDashboard /> // Component for regular users
} else {
return <AccessDenied /> // Component shown for unauthorized access
}
}
In this example, the Dashboard component renders different UIs for 'admin', 'user', and unauthorized roles. This pattern ensures that each user interacts only with components appropriate to their role, enhancing both security and user experience.
### [Best Practices](#best-practices)
* **Secure Session Management**: Prioritize the security of session data to prevent unauthorized access and data breaches. Use encryption and secure storage practices.
* **Dynamic Role Management**: Use a flexible system for user roles to easily adjust to changes in permissions and roles, avoiding hardcoded roles.
* **Security-First Approach**: In all aspects of authorization logic, prioritize security to safeguard user data and maintain the integrity of your application. This includes thorough testing and considering potential security vulnerabilities.
[Session Management](#session-management)
-----------------------------------------
Session management involves tracking and managing a user's interaction with the application over time, ensuring that their authenticated state is preserved across different parts of the application.
This prevents the need for repeated logins, enhancing both security and user convenience. There are two primary methods used for session management: cookie-based and database sessions.
### [Cookie-Based Sessions](#cookie-based-sessions)
> **🎥 Watch:** Learn more about cookie-based sessions and authentication with Next.js → [YouTube (11 minutes)](https://www.youtube.com/watch?v=DJvM2lSPn6w).
Cookie-based sessions manage user data by storing encrypted session information directly in browser cookies. Upon user login, this encrypted data is stored in the cookie. Each subsequent server request includes this cookie, minimizing the need for repeated server queries and enhancing client-side efficiency.
However, this method requires careful encryption to protect sensitive data, as cookies are susceptible to client-side security risks. Encrypting session data in cookies is key to safeguarding user information from unauthorized access. It ensures that even if a cookie is stolen, the data inside remains unreadable.
Additionally, while individual cookies are limited in size (typically around 4KB), techniques like cookie-chunking can overcome this limitation by dividing large session data into multiple cookies.
Setting a cookie in a Next.js project might look something like this:
**Setting a cookie on the server:**
app/actions.ts
TypeScript
JavaScriptTypeScript
'use server'
import { cookies } from 'next/headers'
export async function handleLogin(sessionData) {
const encryptedSessionData = encrypt(sessionData) // Encrypt your session data
cookies().set('session', encryptedSessionData, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 60 * 60 * 24 * 7, // One week
path: '/',
})
// Redirect or handle the response after setting the cookie
}
**Accessing the session data stored in the cookie in a server component:**
app/page.tsx
TypeScript
JavaScriptTypeScript
import { cookies } from 'next/headers'
export async function getSessionData(req) {
const encryptedSessionData = cookies().get('session')?.value
return encryptedSessionData ? JSON.parse(decrypt(encryptedSessionData)) : null
}
### [Database Sessions](#database-sessions)
Database session management involves storing session data on the server, with the user's browser only receiving a session ID. This ID references the session data stored server-side, without containing the data itself. This method enhances security, as it keeps sensitive session data away from the client-side environment, reducing the risk of exposure to client-side attacks. Database sessions are also more scalable, accommodating larger data storage needs.
However, this approach has its tradeoffs. It can increase performance overhead due to the need for database lookups at each user interaction. Strategies like session data caching can help mitigate this. Additionally, reliance on the database means that session management is as reliable as the database's performance and availability.
Here's a simplified example of implementing database sessions in a Next.js application:
**Creating a Session on the Server**:
import db from './lib/db'
export async function createSession(user) {
const sessionId = generateSessionId() // Generate a unique session ID
await db.insertSession({ sessionId, userId: user.id, createdAt: new Date() })
return sessionId
}
**Retrieving a Session in Middleware or Server-Side Logic**:
import { cookies } from 'next/headers'
import db from './lib/db'
export async function getSession() {
const sessionId = cookies().get('sessionId')?.value
return sessionId ? await db.findSession(sessionId) : null
}
### [Selecting Session Management in Next.js](#selecting-session-management-in-nextjs)
Deciding between cookie-based and database sessions in Next.js depends on your application's needs. Cookie-based sessions are simpler and suit smaller applications with lower server load but may offer less security. Database sessions, while more complex, provide better security and scalability, ideal for larger, data-sensitive applications.
With [authentication solutions](#examples) such as [NextAuth.js](https://authjs.dev/guides/upgrade-to-v5), session management becomes more efficient, using either cookies or database storage. This automation simplifies the development process, but it's important to understand the session management method used by your chosen solution. Ensure it aligns with your application's security and performance requirements.
Regardless of your choice, prioritize security in your session management strategy. For cookie-based sessions, using secure and HTTP-only cookies is crucial to protect session data. For database sessions, regular backups and secure handling of session data are essential. Implementing session expiry and cleanup mechanisms is vital in both approaches to prevent unauthorized access and maintain application performance and reliability.
[Examples](#examples)
---------------------
Here are authentication solutions compatible with Next.js, please refer to the quickstart guides below to learn how to configure them in your Next.js application:
* [Auth0](https://auth0.com/docs/quickstart/webapp/nextjs/01-login)
* [Clerk](https://clerk.com/docs/quickstarts/nextjs)
* [Kinde](https://kinde.com/docs/developer-tools/nextjs-sdk)
* [Lucia](https://lucia-auth.com/getting-started/nextjs-app)
* [NextAuth.js](https://authjs.dev/guides/upgrade-to-v5)
* [Supabase](https://supabase.com/docs/guides/getting-started/quickstarts/nextjs)
* [Stytch](https://stytch.com/docs/guides/quickstarts/nextjs)
* [Iron Session](https://github.com/vvo/iron-session)
[Further Reading](#further-reading)
-----------------------------------
To continue learning about authentication and security, check out the following resources:
* [Understanding XSS Attacks](https://vercel.com/guides/understanding-xss-attacks)
* [Understanding CSRF Attacks](https://vercel.com/guides/understanding-csrf-attacks)
[Previous
Cypress
](/docs/app/building-your-application/testing/cypress)
[Next
Deploying
](/docs/app/building-your-application/deploying)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9b0a5ff6a83f00b74986ea9028d4635d\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/use-router | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [
useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [useRouter()](#userouter)
* [Migrating from next/router](#migrating-from-nextrouter)
* [Examples](#examples)
* [Router events](#router-events)
* [Disabling scroll restoration](#disabling-scroll-restoration)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/use-router.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)useRouter
useRouter
=========
The `useRouter` hook allows you to programmatically change routes inside [Client Components](/docs/app/building-your-application/rendering/client-components).
> **Recommendation:** Use the [`<Link>` component](/docs/app/building-your-application/routing/linking-and-navigating#link-component) for navigation unless you have a specific requirement for using `useRouter`.
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'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()`](#userouter)
---------------------------
* `router.push(href: string, { scroll: boolean })`: Perform a client-side navigation to the provided route. Adds a new entry into the [browser’s history](https://developer.mozilla.org/docs/Web/API/History_API) stack.
* `router.replace(href: string, { scroll: boolean })`: Perform a client-side navigation to the provided route without adding a new entry into the [browser’s history stack](https://developer.mozilla.org/docs/Web/API/History_API).
* `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](/docs/app/building-your-application/routing/linking-and-navigating#2-prefetching) the provided route for faster client-side transitions.
* `router.back()`: Navigate back to the previous route in the browser’s history stack.
* `router.forward()`: Navigate forwards to the next page in the browser’s history stack.
> **Good to know**:
>
> * The `<Link>` component 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 `next/router`](#migrating-from-nextrouter)
* The `useRouter` hook should be imported from `next/navigation` and not `next/router` when using the App Router
* The `pathname` string has been removed and is replaced by [`usePathname()`](/docs/app/api-reference/functions/use-pathname)
* The `query` object has been removed and is replaced by [`useSearchParams()`](/docs/app/api-reference/functions/use-search-params)
* `router.events` has been replaced. [See below.](#router-events)
[View the full migration guide](/docs/app/building-your-application/upgrading/app-router-migration).
[Examples](#examples)
---------------------
### [Router events](#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()
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>
)
}
> **Good to know**: `<NavigationEvents>` is wrapped in a [`Suspense` boundary](/docs/app/building-your-application/routing/loading-ui-and-streaming#example) because[`useSearchParams()`](/docs/app/api-reference/functions/use-search-params) causes client-side rendering up to the closest `Suspense` boundary during [static rendering](/docs/app/building-your-application/rendering/server-components#static-rendering-default). [Learn more](/docs/app/api-reference/functions/use-search-params#behavior).
### [Disabling scroll restoration](#disabling-scroll-restoration)
By default, Next.js will scroll to the top of the page when navigating to a new route. You can disable this behavior by passing `scroll: false` to `router.push()` or `router.replace()`.
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useRouter } from 'next/navigation'
export default function Page() {
const router = useRouter()
return (
<button
type="button"
onClick={() => router.push('/dashboard', { scroll: false })}
>
Dashboard
</button>
)
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`useRouter` from `next/navigation` introduced.
[Previous
useReportWebVitals
](/docs/app/api-reference/functions/use-report-web-vitals)
[Next
useSearchParams
](/docs/app/api-reference/functions/use-search-params)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"525a1d5a965c8ee873f39bf262f588cb\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/template | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [
template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Props](#props)
* [children (required)](#children-required)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/template.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)template.js
template.js
===========
A **template** file is similar to a [layout](/docs/app/building-your-application/routing/pages-and-layouts#layouts) in that it wraps each child layout or page. Unlike layouts that persist across routes and maintain state, templates create a new instance for each of their children on navigation.
app/template.tsx
TypeScript
JavaScriptTypeScript
export default function Template({ children }: { children: React.ReactNode }) {
return <div>{children}</div>
}
![template.js special file](/_next/image?url=%2Fdocs%2Flight%2Ftemplate-special-file.png&w=3840&q=75)![template.js special file](/_next/image?url=%2Fdocs%2Fdark%2Ftemplate-special-file.png&w=3840&q=75)
While less common, you might choose a template over a layout if you want:
* Features that rely on `useEffect` (e.g logging page views) and `useState` (e.g a per-page feedback form).
* To change the default framework behavior. For example, Suspense Boundaries inside layouts only show the fallback the first time the Layout is loaded and not when switching pages. For templates, the fallback is shown on each navigation.
[Props](#props)
---------------
### [`children` (required)](#children-required)
Template components should accept and use a `children` prop. `template` is rendered between a [layout](/docs/app/api-reference/file-conventions/layout) and its children. For example:
Output
<Layout>
{/* Note that the template is given a unique key. */}
<Template key={routeParam}>{children}</Template>
</Layout>
> **Good to know**:
>
> * By default, `template` is a [Server Component](/docs/app/building-your-application/rendering/server-components), but can also be used as a [Client Component](/docs/app/building-your-application/rendering/client-components) through the `"use client"` directive.
> * When a user navigates between routes that share a `template`, a new instance of the component is mounted, DOM elements are recreated, state is **not** preserved, and effects are re-synchronized.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`template` introduced.
[Previous
Route Segment Config
](/docs/app/api-reference/file-conventions/route-segment-config)
[Next
Metadata Files
](/docs/app/api-reference/file-conventions/metadata)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"70ec490fb2d445c464467007bfe7dae2\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/route-segment-config | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [
Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Options](#options)
* [dynamic](#dynamic)
* [dynamicParams](#dynamicparams)
* [revalidate](#revalidate)
* [Revalidation Frequency](#revalidation-frequency)
* [fetchCache](#fetchcache)
* [Cross-route segment behavior](#cross-route-segment-behavior)
* [runtime](#runtime)
* [preferredRegion](#preferredregion)
* [maxDuration](#maxduration)
* [generateStaticParams](#generatestaticparams)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/route-segment-config.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)Route Segment Config
Route Segment Config
====================
The Route Segment options allows you to configure the behavior of a [Page](/docs/app/building-your-application/routing/pages-and-layouts), [Layout](/docs/app/building-your-application/routing/pages-and-layouts), or [Route Handler](/docs/app/building-your-application/routing/route-handlers) by directly exporting the following variables:
Option
Type
Default
[`dynamic`](#dynamic)
`'auto' | 'force-dynamic' | 'error' | 'force-static'`
`'auto'`
[`dynamicParams`](#dynamicparams)
`boolean`
`true`
[`revalidate`](#revalidate)
`false | 0 | number`
`false`
[`fetchCache`](#fetchcache)
`'auto' | 'default-cache' | 'only-cache' | 'force-cache' | 'force-no-store' | 'default-no-store' | 'only-no-store'`
`'auto'`
[`runtime`](#runtime)
`'nodejs' | 'edge'`
`'nodejs'`
[`preferredRegion`](#preferredregion)
`'auto' | 'global' | 'home' | string | string[]`
`'auto'`
[`maxDuration`](#maxduration)
`number`
Set by deployment platform
layout.tsx | page.tsx | route.ts
TypeScript
JavaScriptTypeScript
export const dynamic = 'auto'
export const dynamicParams = true
export const revalidate = false
export const fetchCache = 'auto'
export const runtime = 'nodejs'
export const preferredRegion = 'auto'
export const maxDuration = 5
export default function MyComponent() {}
> **Good to know**:
>
> * The values of the config options currently need be statically analyzable. For example `revalidate = 600` is valid, but `revalidate = 60 * 10` is not.
[Options](#options)
-------------------
### [`dynamic`](#dynamic)
Change the dynamic behavior of a layout or page to fully static or fully dynamic.
layout.tsx | page.tsx | route.ts
TypeScript
JavaScriptTypeScript
export const dynamic = 'auto'
// 'auto' | 'force-dynamic' | 'error' | 'force-static'
> **Good to know**: The new model in the `app` directory favors granular caching control at the `fetch` request level over the binary all-or-nothing model of `getServerSideProps` and `getStaticProps` at the page-level in the `pages` directory. The `dynamic` option is a way to opt back in to the previous model as a convenience and provides a simpler migration path.
* **`'auto'`** (default): The default option to cache as much as possible without preventing any components from opting into dynamic behavior.
* **`'force-dynamic'`**: Force [dynamic rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering), which will result in routes being rendered for each user at request time. This option is equivalent to `getServerSideProps()` in the `pages` directory.
* **`'error'`**: Force static rendering and cache the data of a layout or page by causing an error if any components use [dynamic functions](/docs/app/building-your-application/rendering/server-components#dynamic-functions) or uncached data. This option is equivalent to:
* `getStaticProps()` in the `pages` directory.
* Setting the option of every `fetch()` request in a layout or page to `{ cache: 'force-cache' }`.
* Setting the segment config to `fetchCache = 'only-cache', dynamicParams = false`.
* `dynamic = 'error'` changes the default of `dynamicParams` from `true` to `false`. You can opt back into dynamically rendering pages for dynamic params not generated by `generateStaticParams` by manually setting `dynamicParams = true`.
* **`'force-static'`**: Force static rendering and cache the data of a layout or page by forcing [`cookies()`](/docs/app/api-reference/functions/cookies), [`headers()`](/docs/app/api-reference/functions/headers) and [`useSearchParams()`](/docs/app/api-reference/functions/use-search-params) to return empty values.
> **Good to know**:
>
> * Instructions on [how to migrate](/docs/app/building-your-application/upgrading/app-router-migration#step-6-migrating-data-fetching-methods) from `getServerSideProps` and `getStaticProps` to `dynamic: 'force-dynamic'` and `dynamic: 'error'` can be found in the [upgrade guide](/docs/app/building-your-application/upgrading/app-router-migration#step-6-migrating-data-fetching-methods).
### [`dynamicParams`](#dynamicparams)
Control what happens when a dynamic segment is visited that was not generated with [generateStaticParams](/docs/app/api-reference/functions/generate-static-params).
layout.tsx | page.tsx
TypeScript
JavaScriptTypeScript
export const dynamicParams = true // true | false,
* **`true`** (default): Dynamic segments not included in `generateStaticParams` are generated on demand.
* **`false`**: Dynamic segments not included in `generateStaticParams` will return a 404.
> **Good to know**:
>
> * This option replaces the `fallback: true | false | blocking` option of `getStaticPaths` in the `pages` directory.
> * When `dynamicParams = true`, the segment uses [Streaming Server Rendering](/docs/app/building-your-application/routing/loading-ui-and-streaming#streaming-with-suspense).
> * If the `dynamic = 'error'` and `dynamic = 'force-static'` are used, it'll change the default of `dynamicParams` to `false`.
### [`revalidate`](#revalidate)
Set the default revalidation time for a layout or page. This option does not override the `revalidate` value set by individual `fetch` requests.
layout.tsx | page.tsx | route.ts
TypeScript
JavaScriptTypeScript
export const revalidate = false
// false | 0 | number
* **`false`** (default): The default heuristic to cache any `fetch` requests that set their `cache` option to `'force-cache'` or are discovered before a [dynamic function](/docs/app/building-your-application/rendering/server-components#server-rendering-strategies#dynamic-functions) is used. Semantically equivalent to `revalidate: Infinity` which effectively means the resource should be cached indefinitely. It is still possible for individual `fetch` requests to use `cache: 'no-store'` or `revalidate: 0` to avoid being cached and make the route dynamically rendered. Or set `revalidate` to a positive number lower than the route default to increase the revalidation frequency of a route.
* **`0`**: Ensure a layout or page is always [dynamically rendered](/docs/app/building-your-application/rendering/server-components#dynamic-rendering) even if no dynamic functions or uncached data fetches are discovered. This option changes the default of `fetch` requests that do not set a `cache` option to `'no-store'` but leaves `fetch` requests that opt into `'force-cache'` or use a positive `revalidate` as is.
* **`number`**: (in seconds) Set the default revalidation frequency of a layout or page to `n` seconds.
> **Good to know**: The `revalidate` option is only available when using the [Node.js Runtime](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes#nodejs-runtime). This means using the `revalidate` option with `runtime = 'edge'` will not work.
#### [Revalidation Frequency](#revalidation-frequency)
* The lowest `revalidate` across each layout and page of a single route will determine the revalidation frequency of the _entire_ route. This ensures that child pages are revalidated as frequently as their parent layouts.
* Individual `fetch` requests can set a lower `revalidate` than the route's default `revalidate` to increase the revalidation frequency of the entire route. This allows you to dynamically opt-in to more frequent revalidation for certain routes based on some criteria.
### [`fetchCache`](#fetchcache)
This is an advanced option that should only be used if you specifically need to override the default behavior.
By default, Next.js **will cache** any `fetch()` requests that are reachable **before** any [dynamic functions](/docs/app/building-your-application/rendering/server-components#server-rendering-strategies#dynamic-functions) are used and **will not cache** `fetch` requests that are discovered **after** dynamic functions are used.
`fetchCache` allows you to override the default `cache` option of all `fetch` requests in a layout or page.
layout.tsx | page.tsx | route.ts
TypeScript
JavaScriptTypeScript
export const fetchCache = 'auto'
// 'auto' | 'default-cache' | 'only-cache'
// 'force-cache' | 'force-no-store' | 'default-no-store' | 'only-no-store'
* **`'auto'`** (default): The default option to cache `fetch` requests before dynamic functions with the `cache` option they provide and not cache `fetch` requests after dynamic functions.
* **`'default-cache'`**: Allow any `cache` option to be passed to `fetch` but if no option is provided then set the `cache` option to `'force-cache'`. This means that even `fetch` requests after dynamic functions are considered static.
* **`'only-cache'`**: Ensure all `fetch` requests opt into caching by changing the default to `cache: 'force-cache'` if no option is provided and causing an error if any `fetch` requests use `cache: 'no-store'`.
* **`'force-cache'`**: Ensure all `fetch` requests opt into caching by setting the `cache` option of all `fetch` requests to `'force-cache'`.
* **`'default-no-store'`**: Allow any `cache` option to be passed to `fetch` but if no option is provided then set the `cache` option to `'no-store'`. This means that even `fetch` requests before dynamic functions are considered dynamic.
* **`'only-no-store'`**: Ensure all `fetch` requests opt out of caching by changing the default to `cache: 'no-store'` if no option is provided and causing an error if any `fetch` requests use `cache: 'force-cache'`
* **`'force-no-store'`**: Ensure all `fetch` requests opt out of caching by setting the `cache` option of all `fetch` requests to `'no-store'`. This forces all `fetch` requests to be re-fetched every request even if they provide a `'force-cache'` option.
#### [Cross-route segment behavior](#cross-route-segment-behavior)
* Any options set across each layout and page of a single route need to be compatible with each other.
* If both the `'only-cache'` and `'force-cache'` are provided, then `'force-cache'` wins. If both `'only-no-store'` and `'force-no-store'` are provided, then `'force-no-store'` wins. The force option changes the behavior across the route so a single segment with `'force-*'` would prevent any errors caused by `'only-*'`.
* The intention of the `'only-*'` and `force-*'` options is to guarantee the whole route is either fully static or fully dynamic. This means:
* A combination of `'only-cache'` and `'only-no-store'` in a single route is not allowed.
* A combination of `'force-cache'` and `'force-no-store'` in a single route is not allowed.
* A parent cannot provide `'default-no-store'` if a child provides `'auto'` or `'*-cache'` since that could make the same fetch have different behavior.
* It is generally recommended to leave shared parent layouts as `'auto'` and customize the options where child segments diverge.
### [`runtime`](#runtime)
layout.tsx | page.tsx | route.ts
TypeScript
JavaScriptTypeScript
export const runtime = 'nodejs'
// 'nodejs' | 'edge'
* **`'nodejs'`** (default)
* **`'edge'`**
Learn more about the [Edge and Node.js runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes).
### [`preferredRegion`](#preferredregion)
layout.tsx | page.tsx | route.ts
TypeScript
JavaScriptTypeScript
export const preferredRegion = 'auto'
// 'auto' | 'global' | 'home' | ['iad1', 'sfo1']
Support for `preferredRegion`, and regions supported, is dependent on your deployment platform.
> **Good to know**:
>
> * If a `preferredRegion` is not specified, it will inherit the option of the nearest parent layout.
> * The root layout defaults to `all` regions.
### [`maxDuration`](#maxduration)
By default, Next.js does not limit the execution of server-side logic (rendering a page or handling an API). Deployment platforms can use `maxDuration` from the Next.js build output to add specific execution limits. For example, on [Vercel](https://vercel.com/docs/functions/serverless-functions/runtimes#max-duration).
**Note**: This settings requires Next.js `13.4.10` or higher.
layout.tsx | page.tsx | route.ts
TypeScript
JavaScriptTypeScript
export const maxDuration = 5
> **Good to know**:
>
> * If using [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations), set the `maxDuration` at the page level to change the default timeout of all Server Actions used on the page.
### [`generateStaticParams`](#generatestaticparams)
The `generateStaticParams` function can be used in combination with [dynamic route segments](/docs/app/building-your-application/routing/dynamic-routes) to define the list of route segment parameters that will be statically generated at build time instead of on-demand at request time.
See the [API reference](/docs/app/api-reference/functions/generate-static-params) for more details.
[Previous
route.js
](/docs/app/api-reference/file-conventions/route)
[Next
template.js
](/docs/app/api-reference/file-conventions/template)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"bff433c94c474f1a39356ba3717f420a\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/metadata | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [
Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/01-metadata/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[File Conventions](/docs/app/api-reference/file-conventions)Metadata Files
Metadata Files API Reference
============================
This section of the docs covers **Metadata file conventions**. File-based metadata can be defined by adding special metadata files to route segments.
Each file convention can be defined using a static file (e.g. `opengraph-image.jpg`), or a dynamic variant that uses code to generate the file (e.g. `opengraph-image.js`).
Once a file is defined, Next.js will automatically serve the file (with hashes in production for caching) and update the relevant head elements with the correct metadata, such as the asset's URL, file type, and image size.
[
### favicon, icon, and apple-icon
API Reference for the Favicon, Icon and Apple Icon file conventions.
](/docs/app/api-reference/file-conventions/metadata/app-icons)[
### manifest.json
API Reference for manifest.json file.
](/docs/app/api-reference/file-conventions/metadata/manifest)[
### opengraph-image and twitter-image
API Reference for the Open Graph Image and Twitter Image file conventions.
](/docs/app/api-reference/file-conventions/metadata/opengraph-image)[
### robots.txt
API Reference for robots.txt file.
](/docs/app/api-reference/file-conventions/metadata/robots)[
### sitemap.xml
API Reference for the sitemap.xml file.
](/docs/app/api-reference/file-conventions/metadata/sitemap)
[Previous
template.js
](/docs/app/api-reference/file-conventions/template)
[Next
favicon, icon, and apple-icon
](/docs/app/api-reference/file-conventions/metadata/app-icons)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"1b9adc1ce70a7a44642fa47999a1d349\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/metadata/manifest | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [
manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Static Manifest file](#static-manifest-file)
* [Generate a Manifest file](#generate-a-manifest-file)
* [Manifest Object](#manifest-object)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/01-metadata/manifest.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference/file-conventions)[Metadata Files](/docs/app/api-reference/file-conventions/metadata)manifest.json
manifest.json
=============
Add or generate a `manifest.(json|webmanifest)` file that matches the [Web Manifest Specification](https://developer.mozilla.org/docs/Web/Manifest) in the **root** of `app` directory to provide information about your web application for the browser.
[Static Manifest file](#static-manifest-file)
---------------------------------------------
app/manifest.json | app/manifest.webmanifest
{
"name": "My Next.js Application",
"short_name": "Next.js App",
"description": "An application built with Next.js",
"start_url": "/"
// ...
}
[Generate a Manifest file](#generate-a-manifest-file)
-----------------------------------------------------
Add a `manifest.js` or `manifest.ts` file that returns a [`Manifest` object](#manifest-object).
app/manifest.ts
TypeScript
JavaScriptTypeScript
import { MetadataRoute } from 'next'
export default function manifest(): MetadataRoute.Manifest {
return {
name: 'Next.js App',
short_name: 'Next.js App',
description: 'Next.js App',
start_url: '/',
display: 'standalone',
background_color: '#fff',
theme_color: '#fff',
icons: [
{
src: '/favicon.ico',
sizes: 'any',
type: 'image/x-icon',
},
],
}
}
### [Manifest Object](#manifest-object)
The manifest object contains an extensive list of options that may be updated due to new web standards. For information on all the current options, refer to the `MetadataRoute.Manifest` type in your code editor if using [TypeScript](https://nextjs.org/docs/app/building-your-application/configuring/typescript#typescript-plugin) or see the [MDN](https://developer.mozilla.org/docs/Web/Manifest) docs.
[Previous
favicon, icon, and apple-icon
](/docs/app/api-reference/file-conventions/metadata/app-icons)
[Next
opengraph-image and twitter-image
](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"76d35da00f2a94111c5bc8fd6d95c55d\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/upgrading | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/11-upgrading/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[Building Your Application](/docs/app/building-your-application)Upgrading
Upgrade Guide
=============
Upgrade your application to newer versions of Next.js or migrate from the Pages Router to the App Router.
[
### Codemods
Use codemods to upgrade your Next.js codebase when new features are released.
](/docs/app/building-your-application/upgrading/codemods)[
### App Router Migration
Learn how to upgrade your existing Next.js application from the Pages Router to the App Router.
](/docs/app/building-your-application/upgrading/app-router-migration)[
### Version 14
Upgrade your Next.js Application from Version 13 to 14.
](/docs/app/building-your-application/upgrading/version-14)[
### Migrating from Vite
Learn how to migrate your existing React application from Vite to Next.js.
](/docs/app/building-your-application/upgrading/from-vite)[
### Migrating from Create React App
Learn how to migrate your existing React application from Create React App to Next.js.
](/docs/app/building-your-application/upgrading/from-create-react-app)
[Previous
Static Exports
](/docs/app/building-your-application/deploying/static-exports)
[Next
Codemods
](/docs/app/building-your-application/upgrading/codemods)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"d277165e1eb2b9d43988ba5893c0890c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/deploying | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Production Builds](#production-builds)
* [Managed Next.js with Vercel](#managed-nextjs-with-vercel)
* [Self-Hosting](#self-hosting)
* [Node.js Server](#nodejs-server)
* [Docker Image](#docker-image)
* [Static HTML Export](#static-html-export)
* [Features](#features)
* [Image Optimization](#image-optimization)
* [Middleware](#middleware)
* [Environment Variables](#environment-variables)
* [Caching and ISR](#caching-and-isr)
* [Automatic Caching](#automatic-caching)
* [Static Assets](#static-assets)
* [Configuring Caching](#configuring-caching)
* [Build Cache](#build-cache)
* [Version Skew](#version-skew)
* [Streaming and Suspense](#streaming-and-suspense)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/10-deploying/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[Building Your Application](/docs/app/building-your-application)Deploying
Deploying
=========
Congratulations, it's time to ship to production.
You can deploy [managed Next.js with Vercel](#managed-nextjs-with-vercel), or self-host on a Node.js server, Docker image, or even static HTML files. When deploying using `next start`, all Next.js features are supported.
[Production Builds](#production-builds)
---------------------------------------
Running `next build` generates an optimized version of your application for production. HTML, CSS, and JavaScript files are created based on your pages. JavaScript is **compiled** and browser bundles are **minified** using the [Next.js Compiler](/docs/architecture/nextjs-compiler) to help achieve the best performance and support [all modern browsers](/docs/architecture/supported-browsers).
Next.js produces a standard deployment output used by managed and self-hosted Next.js. This ensures all features are supported across both methods of deployment. In the next major version, we will be transforming this output into our [Build Output API specification](https://vercel.com/docs/build-output-api/v3?utm_source=next-site&utm_medium=docs&utm_campaign=next-website).
[Managed Next.js with Vercel](#managed-nextjs-with-vercel)
----------------------------------------------------------
[Vercel](https://vercel.com/docs/frameworks/nextjs?utm_source=next-site&utm_medium=docs&utm_campaign=next-website), the creators and maintainers of Next.js, provide managed infrastructure and a developer experience platform for your Next.js applications.
Deploying to Vercel is zero-configuration and provides additional enhancements for scalability, availability, and performance globally. However, all Next.js features are still supported when self-hosted.
Learn more about [Next.js on Vercel](https://vercel.com/docs/frameworks/nextjs?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) or [deploy a template for free](https://vercel.com/templates/next.js?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) to try it out.
[Self-Hosting](#self-hosting)
-----------------------------
You can self-host Next.js in three different ways:
* [A Node.js server](#nodejs-server)
* [A Docker container](#docker-image)
* [A static export](#static-html-export)
### [Node.js Server](#nodejs-server)
Next.js can be deployed to any hosting provider that supports Node.js. Ensure your `package.json` has the `"build"` and `"start"` scripts:
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}
Then, run `npm run build` to build your application. Finally, run `npm run start` to start the Node.js server. This server supports all Next.js features.
### [Docker Image](#docker-image)
Next.js can be deployed to any hosting provider that supports [Docker](https://www.docker.com/) containers. You can use this approach when deploying to container orchestrators such as [Kubernetes](https://kubernetes.io/) or when running inside a container in any cloud provider.
1. [Install Docker](https://docs.docker.com/get-docker/) on your machine
2. [Clone our example](https://github.com/vercel/next.js/tree/canary/examples/with-docker) (or the [multi-environment example](https://github.com/vercel/next.js/tree/canary/examples/with-docker-multi-env))
3. Build your container: `docker build -t nextjs-docker .`
4. Run your container: `docker run -p 3000:3000 nextjs-docker`
Next.js through Docker supports all Next.js features.
### [Static HTML Export](#static-html-export)
Next.js enables starting as a static site or Single-Page Application (SPA), then later optionally upgrading to use features that require a server.
Since Next.js supports this [static export](/docs/app/building-your-application/deploying/static-exports), it can be deployed and hosted on any web server that can serve HTML/CSS/JS static assets. This includes tools like AWS S3, Nginx, or Apache.
Running as a [static export](/docs/app/building-your-application/deploying/static-exports) does not support Next.js features that require a server. [Learn more](/docs/app/building-your-application/deploying/static-exports#unsupported-features).
> **Good to know:**
>
> * [Server Components](/docs/app/building-your-application/rendering/server-components) are supported with static exports.
[Features](#features)
---------------------
### [Image Optimization](#image-optimization)
[Image Optimization](/docs/app/building-your-application/optimizing/images) through `next/image` works self-hosted with zero configuration when deploying using `next start`. If you would prefer to have a separate service to optimize images, you can [configure an image loader](/docs/app/building-your-application/optimizing/images#loaders).
Image Optimization can be used with a [static export](/docs/app/building-your-application/deploying/static-exports#image-optimization) by defining a custom image loader in `next.config.js`. Note that images are optimized at runtime, not during the build.
> **Good to know:**
>
> * When self-hosting, consider installing `sharp` for more performant [Image Optimization](/docs/pages/building-your-application/optimizing/images) in your production environment by running `npm install sharp` in your project directory. On Linux platforms, `sharp` may require [additional configuration](https://sharp.pixelplumbing.com/install#linux-memory-allocator) to prevent excessive memory usage.
> * Learn more about the [caching behavior of optimized images](/docs/app/api-reference/components/image#caching-behavior) and how to configure the TTL.
> * You can also [disable Image Optimization](/docs/app/api-reference/components/image#unoptimized) and still retain other benefits of using `next/image` if you prefer. For example, if you are optimizing images yourself separately.
### [Middleware](#middleware)
[Middleware](/docs/app/building-your-application/routing/middleware) works self-hosted with zero configuration when deploying using `next start`. Since it requires access to the incoming request, it is not supported when using a [static export](/docs/app/building-your-application/deploying/static-exports).
Middleware uses a [runtime](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes) that is a subset of all available Node.js APIs to help ensure low latency, since it may run in front of every route or asset in your application. This runtime does not require running “at the edge” and works in a single-region server. Additional configuration and infrastructure are required to run Middleware in multiple regions.
If you are looking to add logic (or use an external package) that requires all Node.js APIs, you might be able to move this logic to a [layout](/docs/app/building-your-application/routing/pages-and-layouts#layouts) as a [Server Component](/docs/app/building-your-application/rendering/server-components). For example, checking [headers](/docs/app/api-reference/functions/headers) and [redirecting](/docs/app/api-reference/functions/redirect). You can also use headers, cookies, or query parameters to [redirect](/docs/app/api-reference/next-config-js/redirects#header-cookie-and-query-matching) or [rewrite](/docs/app/api-reference/next-config-js/rewrites#header-cookie-and-query-matching) through `next.config.js`. If that does not work, you can also use a [custom server](/docs/pages/building-your-application/configuring/custom-server).
### [Environment Variables](#environment-variables)
Next.js can support both build time and runtime environment variables.
**By default, environment variables are only available on the server**. To expose an environment variable to the browser, it must be prefixed with `NEXT_PUBLIC_`. However, these public environment variables will be inlined into the JavaScript bundle during `next build`.
To read runtime environment variables, we recommend using `getServerSideProps` or [incrementally adopting the App Router](/docs/app/building-your-application/upgrading/app-router-migration). With the App Router, we can safely read environment variables on the server during dynamic rendering. This allows you to use a singular Docker image that can be promoted through multiple environments with different values.
import { unstable_noStore as noStore } from 'next/cache';
export default function Component() {
noStore();
// cookies(), headers(), and other dynamic functions
// will also opt into dynamic rendering, making
// this env variable is evaluated at runtime
const value = process.env.MY_VALUE
...
}
> **Good to know:**
>
> * You can run code on server startup using the [`register` function](/docs/app/building-your-application/optimizing/instrumentation).
> * We do not recommend using the [runtimeConfig](/docs/pages/api-reference/next-config-js/runtime-configuration) option, as this does not work with the standalone output mode. Instead, we recommend [incrementally adopting](/docs/app/building-your-application/upgrading/app-router-migration) the App Router.
### [Caching and ISR](#caching-and-isr)
Next.js can cache responses, generated static pages, build outputs, and other static assets like images, fonts, and scripts.
Caching and revalidating pages (using Incremental Static Regeneration (ISR) or newer functions in the App Router) use the **same shared cache**. By default, this cache is stored to the filesystem (on disk) on your Next.js server. **This works automatically when self-hosting** using both the Pages and App Router.
You can configure the Next.js cache location if you want to persist cached pages and data to durable storage, or share the cache across multiple containers or instances of your Next.js application.
#### [Automatic Caching](#automatic-caching)
* Next.js sets the `Cache-Control` header of `public, max-age=31536000, immutable` to truly immutable assets. It cannot be overridden. These immutable files contain a SHA-hash in the file name, so they can be safely cached indefinitely. For example, [Static Image Imports](/docs/app/building-your-application/optimizing/images#local-images). You can [configure the TTL](/docs/app/api-reference/components/image#caching-behavior) for images.
* Incremental Static Regeneration (ISR) sets the `Cache-Control` header of `s-maxage: <revalidate in getStaticProps>, stale-while-revalidate`. This revalidation time is defined in your [`getStaticProps` function](/docs/pages/building-your-application/data-fetching/get-static-props) in seconds. If you set `revalidate: false`, it will default to a one-year cache duration.
* Dynamically rendered pages set a `Cache-Control` header of `private, no-cache, no-store, max-age=0, must-revalidate` to prevent user-specific data from being cached. This applies to both the App Router and Pages Router. This also includes [Draft Mode](/docs/app/building-your-application/configuring/draft-mode).
#### [Static Assets](#static-assets)
If you want to host static assets on a different domain or CDN, you can use the `assetPrefix` [configuration](/docs/app/api-reference/next-config-js/assetPrefix) in `next.config.js`. Next.js will use this asset prefix when retrieving JavaScript or CSS files. Separating your assets to a different domain does come with the downside of extra time spent on DNS and TLS resolution.
[Learn more about `assetPrefix`](/docs/app/api-reference/next-config-js/assetPrefix).
#### [Configuring Caching](#configuring-caching)
By default, generated cache assets will be stored in memory (defaults to 50mb) and on disk. If you are hosting Next.js using a container orchestration platform like Kubernetes, each pod will have a copy of the cache. To prevent stale data from being shown since the cache is not shared between pods by default, you can configure the Next.js cache to provide a cache handler and disable in-memory caching.
To configure the ISR/Data Cache location when self-hosting, you can configure a custom handler in your `next.config.js` file:
next.config.js
module.exports = {
cacheHandler: require.resolve('./cache-handler.js'),
cacheMaxMemorySize: 0, // disable default in-memory caching
}
Then, create `cache-handler.js` in the root of your project, for example:
cache-handler.js
const cache = new Map()
module.exports = class CacheHandler {
constructor(options) {
this.options = options
}
async get(key) {
// This could be stored anywhere, like durable storage
return cache.get(key)
}
async set(key, data, ctx) {
// This could be stored anywhere, like durable storage
cache.set(key, {
value: data,
lastModified: Date.now(),
tags: ctx.tags,
})
}
async revalidateTag(tag) {
// Iterate over all entries in the cache
for (let [key, value] of cache) {
// If the value's tags include the specified tag, delete this entry
if (value.tags.includes(tag)) {
cache.delete(key)
}
}
}
}
Using a custom cache handler will allow you to ensure consistency across all pods hosting your Next.js application. For instance, you can save the cached values anywhere, like [Redis](https://github.com/vercel/next.js/tree/canary/examples/cache-handler-redis) or AWS S3.
> **Good to know:**
>
> * `revalidatePath` is a convenience layer on top of cache tags. Calling `revalidatePath` will call the `revalidateTag` function with a special default tag for the provided page.
### [Build Cache](#build-cache)
Next.js generates an ID during `next build` to identify which version of your application is being served. The same build should be used and boot up multiple containers.
If you are rebuilding for each stage of your environment, you will need to generate a consistent build ID to use between containers. Use the `generateBuildId` command in `next.config.js`:
next.config.js
module.exports = {
generateBuildId: async () => {
// This could be anything, using the latest git hash
return process.env.GIT_HASH
},
}
### [Version Skew](#version-skew)
Next.js will automatically mitigate most instances of [version skew](https://www.industrialempathy.com/posts/version-skew/) and automatically reload the application to retrieve new assets when detected. For example, if there is a mismatch in the `deploymentId`, transitions between pages will perform a hard navigation versus using a prefetched value.
When the application is reloaded, there may be a loss of application state if it's not designed to persist between page navigations. For example, using URL state or local storage would persist state after a page refresh. However, component state like `useState` would be lost in such navigations.
Vercel provides additional [skew protection](https://vercel.com/docs/deployments/skew-protection?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) for Next.js applications to ensure assets and functions from the previous version are still available to older clients, even after the new version is deployed.
You can manually configure the `deploymentId` property in your `next.config.js` file to ensure each request uses either `?dpl` query string or `x-deployment-id` header.
### [Streaming and Suspense](#streaming-and-suspense)
The Next.js App Router supports [streaming responses](/docs/app/building-your-application/routing/loading-ui-and-streaming) when self-hosting. If you are using Nginx or a similar proxy, you will need to configure it to disable buffering to enable streaming.
For example, you can disable buffering in Nginx by setting `X-Accel-Buffering` to `no`:
next.config.js
module.exports = {
async headers() {
return [
{
source: '/:path*{/}?',
headers: [
{
key: 'X-Accel-Buffering',
value: 'no',
},
],
},
]
},
}
[
### Production Checklist
Recommendations to ensure the best performance and user experience before taking your Next.js application to production.
](/docs/app/building-your-application/deploying/production-checklist)[
### Static Exports
Next.js enables starting as a static site or Single-Page Application (SPA), then later optionally upgrading to use features that require a server.
](/docs/app/building-your-application/deploying/static-exports)
[Previous
Authentication
](/docs/app/building-your-application/authentication)
[Next
Production Checklist
](/docs/app/building-your-application/deploying/production-checklist)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"e6e86640e7bd8e17bb6e6b277f770ff9\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/deploying/production-checklist | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [
Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Automatic optimizations](#automatic-optimizations)
* [During development](#during-development)
* [Routing and rendering](#routing-and-rendering)
* [Data fetching and caching](#data-fetching-and-caching)
* [UI and accessibility](#ui-and-accessibility)
* [Security](#security)
* [Metadata and SEO](#metadata-and-seo)
* [Type safety](#type-safety)
* [Before going to production](#before-going-to-production)
* [Core Web Vitals](#core-web-vitals)
* [Analyzing bundles](#analyzing-bundles)
* [After deployment](#after-deployment)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/10-deploying/01-production-checklist.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Deploying](/docs/app/building-your-application/deploying)Production Checklist
Production Checklist
====================
Before taking your Next.js application to production, there are some optimizations and patterns you should consider implementing for the best user experience, performance, and security.
This page provides best practices that you can use as a reference when [building your application](#during-development), [before going to production](#before-going-to-production), and [after deployment](#after-deployment) - as well as the [automatic Next.js optimizations](#automatic-optimizations) you should be aware of.
[Automatic optimizations](#automatic-optimizations)
---------------------------------------------------
These Next.js optimizations are enabled by default and require no configuration:
* **[Server Components](/docs/app/building-your-application/rendering/server-components):** Next.js uses Server Components by default. Server Components run on the server, and don't require JavaScript to render on the client. As such, they have no impact on the size of your client-side JavaScript bundles. You can then use [Client Components](/docs/app/building-your-application/rendering/client-components) as needed for interactivity.
* **[Code-splitting](/docs/app/building-your-application/routing/linking-and-navigating#how-routing-and-navigation-works):** Server Components enable automatic code-splitting by route segments. You may also consider [lazy loading](/docs/app/building-your-application/optimizing/lazy-loading) Client Components and third-party libraries, where appropriate.
* **[Prefetching](/docs/app/building-your-application/routing/linking-and-navigating#2-prefetching):** When a link to a new route enters the user's viewport, Next.js prefetches the route in background. This makes navigation to new routes almost instant. You can opt out of prefetching, where appropriate.
* **[Static Rendering](/docs/app/building-your-application/rendering/server-components#static-rendering-default):** Next.js statically renders Server and Client Components on the server at build time and caches the rendered result to improve your application's performance. You can opt into [Dynamic Rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering) for specific routes, where appropriate.
* **[Caching](/docs/app/building-your-application/caching):** Next.js caches data requests, the rendered result of Server and Client Components, static assets, and more, to reduce the number of network requests to your server, database, and backend services. You may opt out of caching, where appropriate.
These defaults aim to improve your application's performance, and reduce the cost and amount of data transferred on each network request.
[During development](#during-development)
-----------------------------------------
While building your application, we recommend using the following features to ensure the best performance and user experience:
### [Routing and rendering](#routing-and-rendering)
* **[Layouts](/docs/app/building-your-application/routing/pages-and-layouts#layouts):** Use layouts to share UI across pages and enable [partial rendering](/docs/app/building-your-application/routing/linking-and-navigating#4-partial-rendering) on navigation.
* **[`<Link>` component](/docs/app/building-your-application/routing/linking-and-navigating#link-component):** Use the `<Link>` component for [client-side navigation and prefetching](/docs/app/building-your-application/routing/linking-and-navigating#how-routing-and-navigation-works).
* **[Error Handling](/docs/app/building-your-application/routing/error-handling):** Gracefully handle [catch-all errors](/docs/app/building-your-application/routing/error-handling) and [404 errors](/docs/app/api-reference/file-conventions/not-found) in production by creating custom error pages.
* **[Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns):** Follow the recommended composition patterns for Server and Client Components, and check the placement of your [`"use client"` boundaries](/docs/app/building-your-application/rendering/composition-patterns#moving-client-components-down-the-tree) to avoid unnecessarily increasing your client-side JavaScript bundle.
* **[Dynamic Functions](/docs/app/building-your-application/rendering/server-components#dynamic-functions):** Be aware that dynamic functions like [`cookies()`](/docs/app/api-reference/functions/cookies) and the [`searchParams`](/docs/app/api-reference/file-conventions/page#searchparams-optional) prop will opt the entire route into [Dynamic Rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering) (or your whole application if used in the [Root Layout](/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required)). Ensure dynamic function usage is intentional and wrap them in `<Suspense>` boundaries where appropriate.
> **Good to know**: [Partial Prerendering (Experimental)](/blog/next-14#partial-prerendering-preview) will allow parts of a route to be dynamic without opting the whole route into dynamic rendering.
### [Data fetching and caching](#data-fetching-and-caching)
* **[Server Components](/docs/app/building-your-application/data-fetching/patterns#fetching-data-on-the-server):** Leverage the benefits of fetching data on the server using Server Components.
* **[Route Handlers](/docs/app/building-your-application/routing/route-handlers):** Use Route Handlers to access your backend resources from Client Components. But do not call Route Handlers from Server Components to avoid an additional server request.
* **[Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming):** Use Loading UI and React Suspense to progressively send UI from the server to the client, and prevent the whole route from blocking while data is being fetched.
* **[Parallel Data Fetching](/docs/app/building-your-application/data-fetching/patterns#parallel-and-sequential-data-fetching):** Reduce network waterfalls by fetching data in parallel, where appropriate. Also, consider [preloading data](/docs/app/building-your-application/data-fetching/patterns#preloading-data) where appropriate.
* **[Data Caching](/docs/app/building-your-application/caching#data-cache):** Verify whether your data requests are being cached or not, and opt into caching, where appropriate. Ensure requests that don't use `fetch` are [cached](/docs/app/api-reference/functions/unstable_cache).
* **[Static Images](/docs/app/building-your-application/optimizing/static-assets):** Use the `public` directory to automatically cache your application's static assets, e.g. images.
### [UI and accessibility](#ui-and-accessibility)
* **[Forms and Validation](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#forms):** Use Server Actions to handle form submissions, server-side validation, and handle errors.
* **[Font Module](/docs/app/building-your-application/optimizing/fonts):** Optimize fonts by using the Font Module, which automatically hosts your font files with other static assets, removes external network requests, and reduces [layout shift](https://web.dev/articles/cls).
* **[`<Image>` Component](/docs/app/building-your-application/optimizing/images):** Optimize images by using the Image Component, which automatically optimizes images, prevents layout shift, and serves them in modern formats like WebP or AVIF.
* **[`<Script>` Component](/docs/app/building-your-application/optimizing/scripts):** Optimize third-party scripts by using the Script Component, which automatically defers scripts and prevents them from blocking the main thread.
* **[ESLint](/docs/architecture/accessibility#linting):** Use the built-in `eslint-plugin-jsx-a11y` plugin to catch accessibility issues early.
### [Security](#security)
* **[Tainting](/docs/app/building-your-application/data-fetching/patterns#preventing-sensitive-data-from-being-exposed-to-the-client):** Prevent sensitive data from being exposed to the client by tainting data objects and/or specific values.
* **[Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations#authentication-and-authorization):** Ensure users are authorized to call Server Actions. Review the the recommended [security practices](/blog/security-nextjs-server-components-actions).
* **[Environment Variables](/docs/app/building-your-application/configuring/environment-variables):** Ensure your `.env.*` files are added to `.gitignore` and only public variables are prefixed with `NEXT_PUBLIC_`.
* **[Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy):** Consider adding a Content Security Policy to protect your application against various security threats such as cross-site scripting, clickjacking, and other code injection attacks.
### [Metadata and SEO](#metadata-and-seo)
* **[Metadata API](/docs/app/building-your-application/optimizing/metadata):** Use the Metadata API to improve your application's Search Engine Optimization (SEO) by adding page titles, descriptions, and more.
* **[Open Graph (OG) images](/docs/app/api-reference/file-conventions/metadata/opengraph-image):** Create OG images to prepare your application for social sharing.
* **[Sitemaps](/docs/app/api-reference/functions/generate-sitemaps) and [Robots](/docs/app/api-reference/file-conventions/metadata/robots):** Help Search Engines crawl and index your pages by generating sitemaps and robots files.
### [Type safety](#type-safety)
* **TypeScript and [TS Plugin](/docs/app/building-your-application/configuring/typescript):** Use TypeScript and the TypeScript plugin for better type-safety, and to help you catch errors early.
[Before going to production](#before-going-to-production)
---------------------------------------------------------
Before going to production, you can run `next build` to build your application locally and catch any build errors, then run `next start` to measure the performance of your application in a production-like environment.
### [Core Web Vitals](#core-web-vitals)
* **[Lighthouse](https://developers.google.com/web/tools/lighthouse):** Run lighthouse in incognito to gain a better understanding of how your users will experience your site, and to identify areas for improvement. This is a simulated test and should be paired with looking at field data (such as Core Web Vitals).
* **[`useReportWebVitals` hook](/docs/app/api-reference/functions/use-report-web-vitals):** Use this hook to send [Core Web Vitals](https://web.dev/articles/vitals) data to analytics tools.
### [Analyzing bundles](#analyzing-bundles)
Use the [`@next/bundle-analyzer` plugin](/docs/app/building-your-application/optimizing/bundle-analyzer) to analyze the size of your JavaScript bundles and identify large modules and dependencies that might be impacting your application's performance.
Additionally, the following tools can you understand the impact of adding new dependencies to your application:
* [Import Cost](https://marketplace.visualstudio.com/items?itemName=wix.vscode-import-cost)
* [Package Phobia](https://packagephobia.com/)
* [Bundle Phobia](https://bundlephobia.com/)
* [bundlejs](https://bundlejs.com/)
[After deployment](#after-deployment)
-------------------------------------
Depending on where you deploy your application, you might have access to additional tools and integrations to help you monitor and improve your application's performance.
For Vercel deployments, we recommend the following:
* **[Analytics](https://vercel.com/analytics?utm_source=next-site&utm_campaign=nextjs-docs&utm_medium=docs):** A built-in analytics dashboard to help you understand your application's traffic, including the number of unique visitors, page views, and more.
* **[Speed Insights](https://vercel.com/docs/speed-insights?utm_source=next-site&utm_campaign=nextjs-docs&utm_medium=docs):** Real-world performance insights based on visitor data, offering a practical view of how your website is performing in the field.
* **[Logging](https://vercel.com/docs/observability/runtime-logs?utm_source=next-site&utm_campaign=nextjs-docs&utm_medium=docs):** Runtime and Activity logs to help you debug issues and monitor your application in production. Alternatively, see the [integrations page](https://vercel.com/integrations?utm_source=next-site&utm_campaign=nextjs-docs&utm_medium=docs) for a list of third-party tools and services.
> **Good to know:**
>
> To get a comprehensive understanding of the best practices for production deployments on Vercel, including detailed strategies for improving website performance, refer to the [Vercel Production Checklist](https://vercel.com/docs/production-checklist?utm_source=next-site&utm_campaign=nextjs-docs&utm_medium=docs).
Following these recommendations will help you build a faster, more reliable, and secure application for your users.
[Previous
Deploying
](/docs/app/building-your-application/deploying)
[Next
Static Exports
](/docs/app/building-your-application/deploying/static-exports)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a7a64e66a8f5fc67ed5f2ffc5e2f7f74\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/metadata/app-icons | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [
favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Image files (.ico, .jpg, .png)](#image-files-ico-jpg-png)
* [favicon](#favicon)
* [icon](#icon)
* [apple-icon](#apple-icon)
* [Generate icons using code (.js, .ts, .tsx)](#generate-icons-using-code-js-ts-tsx)
* [Props](#props)
* [params (optional)](#params-optional)
* [Returns](#returns)
* [Config exports](#config-exports)
* [size](#size)
* [contentType](#contenttype)
* [Route Segment Config](#route-segment-config)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/01-metadata/app-icons.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference/file-conventions)[Metadata Files](/docs/app/api-reference/file-conventions/metadata)favicon, icon, and apple-icon
favicon, icon, and apple-icon
=============================
The `favicon`, `icon`, or `apple-icon` file conventions allow you to set icons for your application.
They are useful for adding app icons that appear in places like web browser tabs, phone home screens, and search engine results.
There are two ways to set app icons:
* [Using image files (.ico, .jpg, .png)](#image-files-ico-jpg-png)
* [Using code to generate an icon (.js, .ts, .tsx)](#generate-icons-using-code-js-ts-tsx)
[Image files (.ico, .jpg, .png)](#image-files-ico-jpg-png)
----------------------------------------------------------
Use an image file to set an app icon by placing a `favicon`, `icon`, or `apple-icon` image file within your `/app` directory. The `favicon` image can only be located in the top level of `app/`.
Next.js will evaluate the file and automatically add the appropriate tags to your app's `<head>` element.
File convention
Supported file types
Valid locations
[`favicon`](#favicon)
`.ico`
`app/`
[`icon`](#icon)
`.ico`, `.jpg`, `.jpeg`, `.png`, `.svg`
`app/**/*`
[`apple-icon`](#apple-icon)
`.jpg`, `.jpeg`, `.png`
`app/**/*`
### [`favicon`](#favicon)
Add a `favicon.ico` image file to the root `/app` route segment.
<head> output
<link rel="icon" href="/favicon.ico" sizes="any" />
### [`icon`](#icon)
Add an `icon.(ico|jpg|jpeg|png|svg)` image file.
<head> output
<link
rel="icon"
href="/icon?<generated>"
type="image/<generated>"
sizes="<generated>"
/>
### [`apple-icon`](#apple-icon)
Add an `apple-icon.(jpg|jpeg|png)` image file.
<head> output
<link
rel="apple-touch-icon"
href="/apple-icon?<generated>"
type="image/<generated>"
sizes="<generated>"
/>
> **Good to know**
>
> * You can set multiple icons by adding a number suffix to the file name. For example, `icon1.png`, `icon2.png`, etc. Numbered files will sort lexically.
> * Favicons can only be set in the root `/app` segment. If you need more granularity, you can use [`icon`](#icon).
> * The appropriate `<link>` tags and attributes such as `rel`, `href`, `type`, and `sizes` are determined by the icon type and metadata of the evaluated file.
> * For example, a 32 by 32px `.png` file will have `type="image/png"` and `sizes="32x32"` attributes.
> * `sizes="any"` is added to `favicon.ico` output to [avoid a browser bug](https://evilmartians.com/chronicles/how-to-favicon-in-2021-six-files-that-fit-most-needs) where an `.ico` icon is favored over `.svg`.
[Generate icons using code (.js, .ts, .tsx)](#generate-icons-using-code-js-ts-tsx)
----------------------------------------------------------------------------------
In addition to using [literal image files](#image-files-ico-jpg-png), you can programmatically **generate** icons using code.
Generate an app icon by creating an `icon` or `apple-icon` route that default exports a function.
File convention
Supported file types
`icon`
`.js`, `.ts`, `.tsx`
`apple-icon`
`.js`, `.ts`, `.tsx`
The easiest way to generate an icon is to use the [`ImageResponse`](/docs/app/api-reference/functions/image-response) API from `next/og`.
app/icon.tsx
TypeScript
JavaScriptTypeScript
import { ImageResponse } from 'next/og'
// Route segment config
export const runtime = 'edge'
// Image metadata
export const size = {
width: 32,
height: 32,
}
export const contentType = 'image/png'
// Image generation
export default function Icon() {
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 24,
background: 'black',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
}}
>
A
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported icons size metadata
// config to also set the ImageResponse's width and height.
...size,
}
)
}
<head> output
<link rel="icon" href="/icon?<generated>" type="image/png" sizes="32x32" />
> **Good to know**
>
> * By default, generated icons are [**statically optimized**](/docs/app/building-your-application/rendering/server-components#static-rendering-default) (generated at build time and cached) unless they use [dynamic functions](/docs/app/building-your-application/rendering/server-components#server-rendering-strategies#dynamic-functions) or uncached data.
> * You can generate multiple icons in the same file using [`generateImageMetadata`](/docs/app/api-reference/functions/generate-image-metadata).
> * You cannot generate a `favicon` icon. Use [`icon`](#icon) or a [favicon.ico](#favicon) file instead.
### [Props](#props)
The default export function receives the following props:
#### [`params` (optional)](#params-optional)
An object containing the [dynamic route parameters](/docs/app/building-your-application/routing/dynamic-routes) object from the root segment down to the segment `icon` or `apple-icon` is colocated in.
app/shop/\[slug\]/icon.tsx
TypeScript
JavaScriptTypeScript
export default function Icon({ params }: { params: { slug: string } }) {
// ...
}
Route
URL
`params`
`app/shop/icon.js`
`/shop`
`undefined`
`app/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](#returns)
The default export function should return a `Blob` | `ArrayBuffer` | `TypedArray` | `DataView` | `ReadableStream` | `Response`.
> **Good to know**: `ImageResponse` satisfies this return type.
### [Config exports](#config-exports)
You can optionally configure the icon's metadata by exporting `size` and `contentType` variables from the `icon` or `apple-icon` route.
Option
Type
[`size`](#size)
`{ width: number; height: number }`
[`contentType`](#contenttype)
`string` - [image MIME type](https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types#image_types)
#### [`size`](#size)
icon.tsx | apple-icon.tsx
TypeScript
JavaScriptTypeScript
export const size = { width: 32, height: 32 }
export default function Icon() {}
<head> output
<link rel="icon" sizes="32x32" />
#### [`contentType`](#contenttype)
icon.tsx | apple-icon.tsx
TypeScript
JavaScriptTypeScript
export const contentType = 'image/png'
export default function Icon() {}
<head> output
<link rel="icon" type="image/png" />
#### [Route Segment Config](#route-segment-config)
`icon` and `apple-icon` are specialized [Route Handlers](/docs/app/building-your-application/routing/route-handlers) that can use the same [route segment configuration](/docs/app/api-reference/file-conventions/route-segment-config) options as Pages and Layouts.
Option
Type
Default
[`dynamic`](/docs/app/api-reference/file-conventions/route-segment-config#dynamic)
`'auto' | 'force-dynamic' | 'error' | 'force-static'`
`'auto'`
[`revalidate`](/docs/app/api-reference/file-conventions/route-segment-config#revalidate)
`false | 'force-cache' | 0 | number`
`false`
[`runtime`](/docs/app/api-reference/file-conventions/route-segment-config#runtime)
`'nodejs' | 'edge'`
`'nodejs'`
[`preferredRegion`](/docs/app/api-reference/file-conventions/route-segment-config#preferredregion)
`'auto' | 'global' | 'home' | string | string[]`
`'auto'`
app/icon.tsx
TypeScript
JavaScriptTypeScript
export const runtime = 'edge'
export default function Icon() {}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
`favicon` `icon` and `apple-icon` introduced
[Previous
Metadata Files
](/docs/app/api-reference/file-conventions/metadata)
[Next
manifest.json
](/docs/app/api-reference/file-conventions/metadata/manifest)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"b47cd2fc78108d9fcd1f8db4bb84bd42\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/deploying/static-exports | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [
Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Configuration](#configuration)
* [Supported Features](#supported-features)
* [Server Components](#server-components)
* [Client Components](#client-components)
* [Image Optimization](#image-optimization)
* [Route Handlers](#route-handlers)
* [Browser APIs](#browser-apis)
* [Unsupported Features](#unsupported-features)
* [Deploying](#deploying)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/10-deploying/02-static-exports.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Deploying](/docs/app/building-your-application/deploying)Static Exports
Static Exports
==============
Next.js enables starting as a static site or Single-Page Application (SPA), then later optionally upgrading to use features that require a server.
When running `next build`, Next.js generates an HTML file per route. By breaking a strict SPA into individual HTML files, Next.js can avoid loading unnecessary JavaScript code on the client-side, reducing the bundle size and enabling faster page loads.
Since Next.js supports this static export, it can be deployed and hosted on any web server that can serve HTML/CSS/JS static assets.
[Configuration](#configuration)
-------------------------------
To enable a static export, change the output mode inside `next.config.js`:
next.config.js
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
output: 'export',
// Optional: Change links `/me` -> `/me/` and emit `/me.html` -> `/me/index.html`
// trailingSlash: true,
// Optional: Prevent automatic `/me` -> `/me/`, instead preserve `href`
// skipTrailingSlashRedirect: true,
// Optional: Change the output directory `out` -> `dist`
// distDir: 'dist',
}
module.exports = nextConfig
After running `next build`, Next.js will produce an `out` folder which contains the HTML/CSS/JS assets for your application.
[Supported Features](#supported-features)
-----------------------------------------
The core of Next.js has been designed to support static exports.
### [Server Components](#server-components)
When you run `next build` to generate a static export, Server Components consumed inside the `app` directory will run during the build, similar to traditional static-site generation.
The resulting component will be rendered into static HTML for the initial page load and a static payload for client navigation between routes. No changes are required for your Server Components when using the static export, unless they consume [dynamic server functions](#unsupported-features).
app/page.tsx
TypeScript
JavaScriptTypeScript
export default async function Page() {
// This fetch will run on the server during `next build`
const res = await fetch('https://api.example.com/...')
const data = await res.json()
return <main>...</main>
}
### [Client Components](#client-components)
If you want to perform data fetching on the client, you can use a Client Component with [SWR](https://github.com/vercel/swr) to memoize requests.
app/other/page.tsx
TypeScript
JavaScriptTypeScript
'use client'
import useSWR from 'swr'
const fetcher = (url: string) => fetch(url).then((r) => r.json())
export default function Page() {
const { data, error } = useSWR(
`https://jsonplaceholder.typicode.com/posts/1`,
fetcher
)
if (error) return 'Failed to load'
if (!data) return 'Loading...'
return data.title
}
Since route transitions happen client-side, this behaves like a traditional SPA. For example, the following index route allows you to navigate to different posts on the client:
app/page.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
export default function Page() {
return (
<>
<h1>Index Page</h1>
<hr />
<ul>
<li>
<Link href="/post/1">Post 1</Link>
</li>
<li>
<Link href="/post/2">Post 2</Link>
</li>
</ul>
</>
)
}
### [Image Optimization](#image-optimization)
[Image Optimization](/docs/app/building-your-application/optimizing/images) through `next/image` can be used with a static export by defining a custom image loader in `next.config.js`. For example, you can optimize images with a service like Cloudinary:
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export',
images: {
loader: 'custom',
loaderFile: './my-loader.ts',
},
}
module.exports = nextConfig
This custom loader will define how to fetch images from a remote source. For example, the following loader will construct the URL for Cloudinary:
my-loader.ts
TypeScript
JavaScriptTypeScript
export default function cloudinaryLoader({
src,
width,
quality,
}: {
src: string
width: number
quality?: number
}) {
const params = ['f_auto', 'c_limit', `w_${width}`, `q_${quality || 'auto'}`]
return `https://res.cloudinary.com/demo/image/upload/${params.join(
','
)}${src}`
}
You can then use `next/image` in your application, defining relative paths to the image in Cloudinary:
app/page.tsx
TypeScript
JavaScriptTypeScript
import Image from 'next/image'
export default function Page() {
return <Image alt="turtles" src="/turtles.jpg" width={300} height={300} />
}
### [Route Handlers](#route-handlers)
Route Handlers will render a static response when running `next build`. Only the `GET` HTTP verb is supported. This can be used to generate static HTML, JSON, TXT, or other files from cached or uncached data. For example:
app/data.json/route.ts
TypeScript
JavaScriptTypeScript
export async function GET() {
return Response.json({ name: 'Lee' })
}
The above file `app/data.json/route.ts` will render to a static file during `next build`, producing `data.json` containing `{ name: 'Lee' }`.
If you need to read dynamic values from the incoming request, you cannot use a static export.
### [Browser APIs](#browser-apis)
Client Components are pre-rendered to HTML during `next build`. Because [Web APIs](https://developer.mozilla.org/docs/Web/API) like `window`, `localStorage`, and `navigator` are not available on the server, you need to safely access these APIs only when running in the browser. For example:
'use client';
import { useEffect } from 'react';
export default function ClientComponent() {
useEffect(() => {
// You now have access to `window`
console.log(window.innerHeight);
}, [])
return ...;
}
[Unsupported Features](#unsupported-features)
---------------------------------------------
Features that require a Node.js server, or dynamic logic that cannot be computed during the build process, are **not** supported:
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes) with `dynamicParams: true`
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes) without `generateStaticParams()`
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers) that rely on Request
* [Cookies](/docs/app/api-reference/functions/cookies)
* [Rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [Redirects](/docs/app/api-reference/next-config-js/redirects)
* [Headers](/docs/app/api-reference/next-config-js/headers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Incremental Static Regeneration](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Image Optimization](/docs/app/building-your-application/optimizing/images) with the default `loader`
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
Attempting to use any of these features with `next dev` will result in an error, similar to setting the [`dynamic`](/docs/app/api-reference/file-conventions/route-segment-config#dynamic) option to `error` in the root layout.
export const dynamic = 'error'
[Deploying](#deploying)
-----------------------
With a static export, Next.js can be deployed and hosted on any web server that can serve HTML/CSS/JS static assets.
When running `next build`, Next.js generates the static export into the `out` folder. For example, let's say you have the following routes:
* `/`
* `/blog/[id]`
After running `next build`, Next.js will generate the following files:
* `/out/index.html`
* `/out/404.html`
* `/out/blog/post-1.html`
* `/out/blog/post-2.html`
If you are using a static host like Nginx, you can configure rewrites from incoming requests to the correct files:
nginx.conf
server {
listen 80;
server_name acme.com;
root /var/www/out;
location / {
try_files $uri $uri.html $uri/ =404;
}
# This is necessary when `trailingSlash: false`.
# You can omit this when `trailingSlash: true`.
location /blog/ {
rewrite ^/blog/(.*)$ /blog/$1.html break;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v14.0.0`
`next export` has been removed in favor of `"output": "export"`
`v13.4.0`
App Router (Stable) adds enhanced static export support, including using React Server Components and Route Handlers.
`v13.3.0`
`next export` is deprecated and replaced with `"output": "export"`
[Previous
Production Checklist
](/docs/app/building-your-application/deploying/production-checklist)
[Next
Upgrading
](/docs/app/building-your-application/upgrading)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"29d9c03ca68118a881b214b8878b8d64\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/metadata/opengraph-image | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [
opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Image files (.jpg, .png, .gif)](#image-files-jpg-png-gif)
* [opengraph-image](#opengraph-image)
* [twitter-image](#twitter-image)
* [opengraph-image.alt.txt](#opengraph-imagealttxt)
* [twitter-image.alt.txt](#twitter-imagealttxt)
* [Generate images using code (.js, .ts, .tsx)](#generate-images-using-code-js-ts-tsx)
* [Props](#props)
* [params (optional)](#params-optional)
* [Returns](#returns)
* [Config exports](#config-exports)
* [alt](#alt)
* [size](#size)
* [contentType](#contenttype)
* [Route Segment Config](#route-segment-config)
* [Examples](#examples)
* [Using external data](#using-external-data)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/01-metadata/opengraph-image.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference/file-conventions)[Metadata Files](/docs/app/api-reference/file-conventions/metadata)opengraph-image and twitter-image
opengraph-image and twitter-image
=================================
The `opengraph-image` and `twitter-image` file conventions allow you to set Open Graph and Twitter images for a route segment.
They are useful for setting the images that appear on social networks and messaging apps when a user shares a link to your site.
There are two ways to set Open Graph and Twitter images:
* [Using image files (.jpg, .png, .gif)](#image-files-jpg-png-gif)
* [Using code to generate images (.js, .ts, .tsx)](#generate-images-using-code-js-ts-tsx)
[Image files (.jpg, .png, .gif)](#image-files-jpg-png-gif)
----------------------------------------------------------
Use an image file to set a route segment's shared image by placing an `opengraph-image` or `twitter-image` image file in the segment.
Next.js will evaluate the file and automatically add the appropriate tags to your app's `<head>` element.
File convention
Supported file types
[`opengraph-image`](#opengraph-image)
`.jpg`, `.jpeg`, `.png`, `.gif`
[`twitter-image`](#twitter-image)
`.jpg`, `.jpeg`, `.png`, `.gif`
[`opengraph-image.alt`](#opengraph-imagealttxt)
`.txt`
[`twitter-image.alt`](#twitter-imagealttxt)
`.txt`
### [`opengraph-image`](#opengraph-image)
Add an `opengraph-image.(jpg|jpeg|png|gif)` image file to any route segment.
<head> output
<meta property="og:image" content="<generated>" />
<meta property="og:image:type" content="<generated>" />
<meta property="og:image:width" content="<generated>" />
<meta property="og:image:height" content="<generated>" />
### [`twitter-image`](#twitter-image)
Add a `twitter-image.(jpg|jpeg|png|gif)` image file to any route segment.
<head> output
<meta name="twitter:image" content="<generated>" />
<meta name="twitter:image:type" content="<generated>" />
<meta name="twitter:image:width" content="<generated>" />
<meta name="twitter:image:height" content="<generated>" />
### [`opengraph-image.alt.txt`](#opengraph-imagealttxt)
Add an accompanying `opengraph-image.alt.txt` file in the same route segment as the `opengraph-image.(jpg|jpeg|png|gif)` image it's alt text.
opengraph-image.alt.txt
About Acme
<head> output
<meta property="og:image:alt" content="About Acme" />
### [`twitter-image.alt.txt`](#twitter-imagealttxt)
Add an accompanying `twitter-image.alt.txt` file in the same route segment as the `twitter-image.(jpg|jpeg|png|gif)` image it's alt text.
twitter-image.alt.txt
About Acme
<head> output
<meta property="twitter:image:alt" content="About Acme" />
[Generate images using code (.js, .ts, .tsx)](#generate-images-using-code-js-ts-tsx)
------------------------------------------------------------------------------------
In addition to using [literal image files](#image-files-jpg-png-gif), you can programmatically **generate** images using code.
Generate a route segment's shared image by creating an `opengraph-image` or `twitter-image` route that default exports a function.
File convention
Supported file types
`opengraph-image`
`.js`, `.ts`, `.tsx`
`twitter-image`
`.js`, `.ts`, `.tsx`
> **Good to know**
>
> * By default, generated images are [**statically optimized**](/docs/app/building-your-application/rendering/server-components#static-rendering-default) (generated at build time and cached) unless they use [dynamic functions](/docs/app/building-your-application/rendering/server-components#server-rendering-strategies#dynamic-functions) or uncached data.
> * You can generate multiple Images in the same file using [`generateImageMetadata`](/docs/app/api-reference/functions/generate-image-metadata).
The easiest way to generate an image is to use the [ImageResponse](/docs/app/api-reference/functions/image-response) API from `next/og`.
app/about/opengraph-image.tsx
TypeScript
JavaScriptTypeScript
import { ImageResponse } from 'next/og'
// Route segment config
export const runtime = 'edge'
// Image metadata
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
// Image generation
export default async function Image() {
// Font
const interSemiBold = fetch(
new URL('./Inter-SemiBold.ttf', import.meta.url)
).then((res) => res.arrayBuffer())
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 128,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
About Acme
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported opengraph-image
// size config to also set the ImageResponse's width and height.
...size,
fonts: [
{
name: 'Inter',
data: await interSemiBold,
style: 'normal',
weight: 400,
},
],
}
)
}
<head> output
<meta property="og:image" content="<generated>" />
<meta property="og:image:alt" content="About Acme" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
### [Props](#props)
The default export function receives the following props:
#### [`params` (optional)](#params-optional)
An object containing the [dynamic route parameters](/docs/app/building-your-application/routing/dynamic-routes) object from the root segment down to the segment `opengraph-image` or `twitter-image` is colocated in.
app/shop/\[slug\]/opengraph-image.tsx
TypeScript
JavaScriptTypeScript
export default function Image({ params }: { params: { slug: string } }) {
// ...
}
Route
URL
`params`
`app/shop/opengraph-image.js`
`/shop`
`undefined`
`app/shop/[slug]/opengraph-image.js`
`/shop/1`
`{ slug: '1' }`
`app/shop/[tag]/[item]/opengraph-image.js`
`/shop/1/2`
`{ tag: '1', item: '2' }`
`app/shop/[...slug]/opengraph-image.js`
`/shop/1/2`
`{ slug: ['1', '2'] }`
### [Returns](#returns)
The default export function should return a `Blob` | `ArrayBuffer` | `TypedArray` | `DataView` | `ReadableStream` | `Response`.
> **Good to know**: `ImageResponse` satisfies this return type.
### [Config exports](#config-exports)
You can optionally configure the image's metadata by exporting `alt`, `size`, and `contentType` variables from `opengraph-image` or `twitter-image` route.
Option
Type
[`alt`](#alt)
`string`
[`size`](#size)
`{ width: number; height: number }`
[`contentType`](#contenttype)
`string` - [image MIME type](https://developer.mozilla.org/docs/Web/HTTP/Basics_of_HTTP/MIME_types#image_types)
#### [`alt`](#alt)
opengraph-image.tsx | twitter-image.tsx
TypeScript
JavaScriptTypeScript
export const alt = 'My images alt text'
export default function Image() {}
<head> output
<meta property="og:image:alt" content="My images alt text" />
#### [`size`](#size)
opengraph-image.tsx | twitter-image.tsx
TypeScript
JavaScriptTypeScript
export const size = { width: 1200, height: 630 }
export default function Image() {}
<head> output
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
#### [`contentType`](#contenttype)
opengraph-image.tsx | twitter-image.tsx
TypeScript
JavaScriptTypeScript
export const contentType = 'image/png'
export default function Image() {}
<head> output
<meta property="og:image:type" content="image/png" />
#### [Route Segment Config](#route-segment-config)
`opengraph-image` and `twitter-image` are specialized [Route Handlers](/docs/app/building-your-application/routing/route-handlers) that can use the same [route segment configuration](/docs/app/api-reference/file-conventions/route-segment-config) options as Pages and Layouts.
Option
Type
Default
[`dynamic`](/docs/app/api-reference/file-conventions/route-segment-config#dynamic)
`'auto' | 'force-dynamic' | 'error' | 'force-static'`
`'auto'`
[`revalidate`](/docs/app/api-reference/file-conventions/route-segment-config#revalidate)
`false | 'force-cache' | 0 | number`
`false`
[`runtime`](/docs/app/api-reference/file-conventions/route-segment-config#runtime)
`'nodejs' | 'edge'`
`'nodejs'`
[`preferredRegion`](/docs/app/api-reference/file-conventions/route-segment-config#preferredregion)
`'auto' | 'global' | 'home' | string | string[]`
`'auto'`
app/opengraph-image.tsx
TypeScript
JavaScriptTypeScript
export const runtime = 'edge'
export default function Image() {}
### [Examples](#examples)
#### [Using external data](#using-external-data)
This example uses the `params` object and external data to generate the image.
> **Good to know**: By default, this generated image will be [statically optimized](/docs/app/building-your-application/rendering/server-components#static-rendering-default). You can configure the individual `fetch` [`options`](/docs/app/api-reference/functions/fetch) or route segments [options](/docs/app/api-reference/file-conventions/route-segment-config#revalidate) to change this behavior.
app/posts/\[slug\]/opengraph-image.tsx
TypeScript
JavaScriptTypeScript
import { ImageResponse } from 'next/og'
export const runtime = 'edge'
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
export default async function Image({ params }: { params: { slug: string } }) {
const post = await fetch(`https://.../posts/${params.slug}`).then((res) =>
res.json()
)
return new ImageResponse(
(
<div
style={{
fontSize: 48,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
{post.title}
</div>
),
{
...size,
}
)
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
`opengraph-image` and `twitter-image` introduced.
[Previous
manifest.json
](/docs/app/api-reference/file-conventions/metadata/manifest)
[Next
robots.txt
](/docs/app/api-reference/file-conventions/metadata/robots)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"b11b2aad3f39f3b79778436961962369\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/upgrading/codemods | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [
Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Usage](#usage)
* [Next.js Codemods](#nextjs-codemods)
* [14.0](#140)
* [Migrate ImageResponse imports](#migrate-imageresponse-imports)
* [next-og-import](#next-og-import)
* [Use viewport export](#use-viewport-export)
* [metadata-to-viewport-export](#metadata-to-viewport-export)
* [13.2](#132)
* [Use Built-in Font](#use-built-in-font)
* [built-in-next-font](#built-in-next-font)
* [13.0](#130)
* [Rename Next Image Imports](#rename-next-image-imports)
* [next-image-to-legacy-image](#next-image-to-legacy-image)
* [Migrate to the New Image Component](#migrate-to-the-new-image-component)
* [next-image-experimental](#next-image-experimental)
* [Remove <a> Tags From Link Components](#remove-a-tags-from-link-components)
* [new-link](#new-link)
* [11](#11)
* [Migrate from CRA](#migrate-from-cra)
* [cra-to-next](#cra-to-next)
* [10](#10)
* [Add React imports](#add-react-imports)
* [add-missing-react-import](#add-missing-react-import)
* [9](#9)
* [Transform Anonymous Components into Named Components](#transform-anonymous-components-into-named-components)
* [name-default-component](#name-default-component)
* [8](#8)
* [Transform AMP HOC into page config](#transform-amp-hoc-into-page-config)
* [withamp-to-config](#withamp-to-config)
* [6](#6)
* [Use withRouter](#use-withrouter)
* [url-to-withrouter](#url-to-withrouter)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/11-upgrading/01-codemods.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Upgrading](/docs/app/building-your-application/upgrading)Codemods
Codemods
========
Codemods are transformations that run on your codebase programmatically. This allows a large number of changes to be programmatically applied without having to manually go through every file.
Next.js provides Codemod transformations to help upgrade your Next.js codebase when an API is updated or deprecated.
[Usage](#usage)
---------------
In your terminal, navigate (`cd`) into your project's folder, then run:
Terminal
npx @next/codemod <transform> <path>
Replacing `<transform>` and `<path>` with appropriate values.
* `transform` - name of transform
* `path` - files or directory to transform
* `--dry` Do a dry-run, no code will be edited
* `--print` Prints the changed output for comparison
[Next.js Codemods](#nextjs-codemods)
------------------------------------
### [14.0](#140)
#### [Migrate `ImageResponse` imports](#migrate-imageresponse-imports)
##### [`next-og-import`](#next-og-import)
Terminal
npx @next/codemod@latest next-og-import .
This codemod moves transforms imports from `next/server` to `next/og` for usage of [Dynamic OG Image Generation](/docs/app/building-your-application/optimizing/metadata#dynamic-image-generation).
For example:
import { ImageResponse } from 'next/server'
Transforms into:
import { ImageResponse } from 'next/og'
#### [Use `viewport` export](#use-viewport-export)
##### [`metadata-to-viewport-export`](#metadata-to-viewport-export)
Terminal
npx @next/codemod@latest metadata-to-viewport-export .
This codemod migrates certain viewport metadata to `viewport` export.
For example:
export const metadata = {
title: 'My App',
themeColor: 'dark',
viewport: {
width: 1,
},
}
Transforms into:
export const metadata = {
title: 'My App',
}
export const viewport = {
width: 1,
themeColor: 'dark',
}
### [13.2](#132)
#### [Use Built-in Font](#use-built-in-font)
##### [`built-in-next-font`](#built-in-next-font)
Terminal
npx @next/codemod@latest built-in-next-font .
This codemod uninstalls the `@next/font` package and transforms `@next/font` imports into the built-in `next/font`.
For example:
import { Inter } from '@next/font/google'
Transforms into:
import { Inter } from 'next/font/google'
### [13.0](#130)
#### [Rename Next Image Imports](#rename-next-image-imports)
##### [`next-image-to-legacy-image`](#next-image-to-legacy-image)
Terminal
npx @next/codemod@latest next-image-to-legacy-image .
Safely renames `next/image` imports in existing Next.js 10, 11, or 12 applications to `next/legacy/image` in Next.js 13. Also renames `next/future/image` to `next/image`.
For example:
pages/index.js
import Image1 from 'next/image'
import Image2 from 'next/future/image'
export default function Home() {
return (
<div>
<Image1 src="/test.jpg" width="200" height="300" />
<Image2 src="/test.png" width="500" height="400" />
</div>
)
}
Transforms into:
pages/index.js
// 'next/image' becomes 'next/legacy/image'
import Image1 from 'next/legacy/image'
// 'next/future/image' becomes 'next/image'
import Image2 from 'next/image'
export default function Home() {
return (
<div>
<Image1 src="/test.jpg" width="200" height="300" />
<Image2 src="/test.png" width="500" height="400" />
</div>
)
}
#### [Migrate to the New Image Component](#migrate-to-the-new-image-component)
##### [`next-image-experimental`](#next-image-experimental)
Terminal
npx @next/codemod@latest next-image-experimental .
Dangerously migrates from `next/legacy/image` to the new `next/image` by adding inline styles and removing unused props.
* Removes `layout` prop and adds `style`.
* Removes `objectFit` prop and adds `style`.
* Removes `objectPosition` prop and adds `style`.
* Removes `lazyBoundary` prop.
* Removes `lazyRoot` prop.
#### [Remove `<a>` Tags From Link Components](#remove-a-tags-from-link-components)
##### [`new-link`](#new-link)
Terminal
npx @next/codemod@latest new-link .
Remove `<a>` tags inside [Link Components](/docs/app/api-reference/components/link), or add a `legacyBehavior` prop to Links that cannot be auto-fixed.
For example:
<Link href="/about">
<a>About</a>
</Link>
// transforms into
<Link href="/about">
About
</Link>
<Link href="/about">
<a onClick={() => console.log('clicked')}>About</a>
</Link>
// transforms into
<Link href="/about" onClick={() => console.log('clicked')}>
About
</Link>
In cases where auto-fixing can't be applied, the `legacyBehavior` prop is added. This allows your app to keep functioning using the old behavior for that particular link.
const Component = () => <a>About</a>
<Link href="/about">
<Component />
</Link>
// becomes
<Link href="/about" legacyBehavior>
<Component />
</Link>
### [11](#11)
#### [Migrate from CRA](#migrate-from-cra)
##### [`cra-to-next`](#cra-to-next)
Terminal
npx @next/codemod cra-to-next
Migrates a Create React App project to Next.js; creating a Pages Router and necessary config to match behavior. Client-side only rendering is leveraged initially to prevent breaking compatibility due to `window` usage during SSR and can be enabled seamlessly to allow the gradual adoption of Next.js specific features.
Please share any feedback related to this transform [in this discussion](https://github.com/vercel/next.js/discussions/25858).
### [10](#10)
#### [Add React imports](#add-react-imports)
##### [`add-missing-react-import`](#add-missing-react-import)
Terminal
npx @next/codemod add-missing-react-import
Transforms files that do not import `React` to include the import in order for the new [React JSX transform](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html) to work.
For example:
my-component.js
export default class Home extends React.Component {
render() {
return <div>Hello World</div>
}
}
Transforms into:
my-component.js
import React from 'react'
export default class Home extends React.Component {
render() {
return <div>Hello World</div>
}
}
### [9](#9)
#### [Transform Anonymous Components into Named Components](#transform-anonymous-components-into-named-components)
##### [`name-default-component`](#name-default-component)
Terminal
npx @next/codemod name-default-component
**Versions 9 and above.**
Transforms anonymous components into named components to make sure they work with [Fast Refresh](https://nextjs.org/blog/next-9-4#fast-refresh).
For example:
my-component.js
export default function () {
return <div>Hello World</div>
}
Transforms into:
my-component.js
export default function MyComponent() {
return <div>Hello World</div>
}
The component will have a camel-cased name based on the name of the file, and it also works with arrow functions.
### [8](#8)
#### [Transform AMP HOC into page config](#transform-amp-hoc-into-page-config)
##### [`withamp-to-config`](#withamp-to-config)
Terminal
npx @next/codemod withamp-to-config
Transforms the `withAmp` HOC into Next.js 9 page configuration.
For example:
// Before
import { withAmp } from 'next/amp'
function Home() {
return <h1>My AMP Page</h1>
}
export default withAmp(Home)
// After
export default function Home() {
return <h1>My AMP Page</h1>
}
export const config = {
amp: true,
}
### [6](#6)
#### [Use `withRouter`](#use-withrouter)
##### [`url-to-withrouter`](#url-to-withrouter)
Terminal
npx @next/codemod url-to-withrouter
Transforms the deprecated automatically injected `url` property on top level pages to using `withRouter` and the `router` property it injects. Read more here: [https://nextjs.org/docs/messages/url-deprecated](/docs/messages/url-deprecated)
For example:
From
import React from 'react'
export default class extends React.Component {
render() {
const { pathname } = this.props.url
return <div>Current pathname: {pathname}</div>
}
}
To
import React from 'react'
import { withRouter } from 'next/router'
export default withRouter(
class extends React.Component {
render() {
const { pathname } = this.props.router
return <div>Current pathname: {pathname}</div>
}
}
)
This is one case. All the cases that are transformed (and tested) can be found in the [`__testfixtures__` directory](https://github.com/vercel/next.js/tree/canary/packages/next-codemod/transforms/__testfixtures__/url-to-withrouter).
[Previous
Upgrading
](/docs/app/building-your-application/upgrading)
[Next
App Router Migration
](/docs/app/building-your-application/upgrading/app-router-migration)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a9fcaf522c6bff92555faea35f0d42f5\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/upgrading/version-14 | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [
Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Upgrading from 13 to 14](#upgrading-from-13-to-14)
* [v14 Summary](#v14-summary)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/11-upgrading/03-version-14.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Upgrading](/docs/app/building-your-application/upgrading)Version 14
Version 14
==========
[Upgrading from 13 to 14](#upgrading-from-13-to-14)
---------------------------------------------------
To update to Next.js version 14, run the following command using your preferred package manager:
Terminal
npm i next@latest react@latest react-dom@latest eslint-config-next@latest
Terminal
yarn add next@latest react@latest react-dom@latest eslint-config-next@latest
Terminal
pnpm up next react react-dom eslint-config-next --latest
Terminal
bun add next@latest react@latest react-dom@latest eslint-config-next@latest
> **Good to know:** If you are using TypeScript, ensure you also upgrade `@types/react` and `@types/react-dom` to their latest versions.
### [v14 Summary](#v14-summary)
* The minimum Node.js version has been bumped from 16.14 to 18.17, since 16.x has reached end-of-life.
* The `next export` command has been removed in favor of `output: 'export'` config. Please see the [docs](https://nextjs.org/docs/app/building-your-application/deploying/static-exports) for more information.
* The `next/server` import for `ImageResponse` was renamed to `next/og`. A [codemod is available](/docs/app/building-your-application/upgrading/codemods#next-og-import) to safely and automatically rename your imports.
* The `@next/font` package has been fully removed in favor of the built-in `next/font`. A [codemod is available](/docs/app/building-your-application/upgrading/codemods#built-in-next-font) to safely and automatically rename your imports.
* The WASM target for `next-swc` has been removed.
[Previous
App Router Migration
](/docs/app/building-your-application/upgrading/app-router-migration)
[Next
Migrating from Vite
](/docs/app/building-your-application/upgrading/from-vite)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"60f0dcd70dec55879ff376c580c8b6c9\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/userAgent | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [
userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [isBot](#isbot)
* [browser](#browser)
* [device](#device)
* [engine](#engine)
* [os](#os)
* [cpu](#cpu)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/userAgent.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)userAgent
userAgent
=========
The `userAgent` helper extends the [Web Request API](https://developer.mozilla.org/docs/Web/API/Request) with additional properties and methods to interact with the user agent object from the request.
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextRequest, NextResponse, userAgent } from 'next/server'
export function middleware(request: NextRequest) {
const url = request.nextUrl
const { device } = userAgent(request)
const viewport = device.type === 'mobile' ? 'mobile' : 'desktop'
url.searchParams.set('viewport', viewport)
return NextResponse.rewrite(url)
}
[`isBot`](#isbot)
-----------------
A boolean indicating whether the request comes from a known bot.
[`browser`](#browser)
---------------------
An object containing information about the browser used in the request.
* `name`: A string representing the browser's name, or `undefined` if not identifiable.
* `version`: A string representing the browser's version, or `undefined`.
[`device`](#device)
-------------------
An object containing information about the device used in the request.
* `model`: A string representing the model of the device, or `undefined`.
* `type`: A string representing the type of the device, such as `console`, `mobile`, `tablet`, `smarttv`, `wearable`, `embedded`, or `undefined`.
* `vendor`: A string representing the vendor of the device, or `undefined`.
[`engine`](#engine)
-------------------
An object containing information about the browser's engine.
* `name`: A string representing the engine's name. Possible values include: `Amaya`, `Blink`, `EdgeHTML`, `Flow`, `Gecko`, `Goanna`, `iCab`, `KHTML`, `Links`, `Lynx`, `NetFront`, `NetSurf`, `Presto`, `Tasman`, `Trident`, `w3m`, `WebKit` or `undefined`.
* `version`: A string representing the engine's version, or `undefined`.
[`os`](#os)
-----------
An object containing information about the operating system.
* `name`: A string representing the name of the OS, or `undefined`.
* `version`: A string representing the version of the OS, or `undefined`.
[`cpu`](#cpu)
-------------
An object containing information about the CPU architecture.
* `architecture`: A string representing the architecture of the CPU. Possible values include: `68k`, `amd64`, `arm`, `arm64`, `armhf`, `avr`, `ia32`, `ia64`, `irix`, `irix64`, `mips`, `mips64`, `pa-risc`, `ppc`, `sparc`, `sparc64` or `undefined`
[Previous
useSelectedLayoutSegments
](/docs/app/api-reference/functions/use-selected-layout-segments)
[Next
next.config.js Options
](/docs/app/api-reference/next-config-js)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"ee40cc26e60b1e5f22d1c8a319c1b07d\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/use-search-params | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [
useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [Returns](#returns)
* [Static Rendering](#static-rendering)
* [Behavior](#behavior)
* [Dynamic Rendering](#dynamic-rendering)
* [Server Components](#server-components)
* [Pages](#pages)
* [Layouts](#layouts)
* [Examples](#examples)
* [Updating searchParams](#updating-searchparams)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/use-search-params.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)useSearchParams
useSearchParams
===============
`useSearchParams` is a **Client Component** hook that lets you read the current URL's **query string**.
`useSearchParams` returns a **read-only** version of the [`URLSearchParams`](https://developer.mozilla.org/docs/Web/API/URLSearchParams) interface.
app/dashboard/search-bar.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useSearchParams } from 'next/navigation'
export default function SearchBar() {
const searchParams = useSearchParams()
const search = searchParams.get('search')
// URL -> `/dashboard?search=my-project`
// `search` -> 'my-project'
return <>Search: {search}</>
}
[Parameters](#parameters)
-------------------------
const searchParams = useSearchParams()
`useSearchParams` does not take any parameters.
[Returns](#returns)
-------------------
`useSearchParams` returns a **read-only** version of the [`URLSearchParams`](https://developer.mozilla.org/docs/Web/API/URLSearchParams) interface, which includes utility methods for reading the URL's query string:
* [`URLSearchParams.get()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/get): Returns the first value associated with the search parameter. For example:
URL
`searchParams.get("a")`
`/dashboard?a=1`
`'1'`
`/dashboard?a=`
`''`
`/dashboard?b=3`
`null`
`/dashboard?a=1&a=2`
`'1'` _\- use [`getAll()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll) to get all values_
* [`URLSearchParams.has()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/has): Returns a boolean value indicating if the given parameter exists. For example:
URL
`searchParams.has("a")`
`/dashboard?a=1`
`true`
`/dashboard?b=3`
`false`
* Learn more about other **read-only** methods of [`URLSearchParams`](https://developer.mozilla.org/docs/Web/API/URLSearchParams), including the [`getAll()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/getAll), [`keys()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/keys), [`values()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/values), [`entries()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/entries), [`forEach()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/forEach), and [`toString()`](https://developer.mozilla.org/docs/Web/API/URLSearchParams/toString).
> **Good to know**:
>
> * `useSearchParams` is a [Client Component](/docs/app/building-your-application/rendering/client-components) hook and is **not supported** in [Server Components](/docs/app/building-your-application/rendering/server-components) to prevent stale values during [partial rendering](/docs/app/building-your-application/routing/linking-and-navigating#4-partial-rendering).
> * If an application includes the `/pages` directory, `useSearchParams` will return `ReadonlyURLSearchParams | null`. The `null` value is for compatibility during migration since search params cannot be known during pre-rendering of a page that doesn't use `getServerSideProps`
### [Static Rendering](#static-rendering)
If a route is [statically rendered](/docs/app/building-your-application/rendering/server-components#static-rendering-default), calling `useSearchParams` will cause the Client Component tree up to the closest [`Suspense` boundary](/docs/app/building-your-application/routing/loading-ui-and-streaming#example) to be client-side rendered.
This allows a part of the route to be statically rendered while the dynamic part that uses `useSearchParams` is client-side rendered.
We recommend wrapping the Client Component that uses `useSearchParams` in a `<Suspense/>` boundary. This will allow any Client Components above it to be statically rendered and sent as part of initial HTML. [Example](/docs/app/api-reference/functions/use-search-params#static-rendering).
For example:
app/dashboard/search-bar.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useSearchParams } from 'next/navigation'
export default function SearchBar() {
const searchParams = useSearchParams()
const search = searchParams.get('search')
// This will not be logged on the server when using static rendering
console.log(search)
return <>Search: {search}</>
}
app/dashboard/page.tsx
TypeScript
JavaScriptTypeScript
import { Suspense } from 'react'
import SearchBar from './search-bar'
// This component passed as a fallback to the Suspense boundary
// will be rendered in place of the search bar in the initial HTML.
// When the value is available during React hydration the fallback
// will be replaced with the `<SearchBar>` component.
function SearchBarFallback() {
return <>placeholder</>
}
export default function Page() {
return (
<>
<nav>
<Suspense fallback={<SearchBarFallback />}>
<SearchBar />
</Suspense>
</nav>
<h1>Dashboard</h1>
</>
)
}
[Behavior](#behavior)
---------------------
### [Dynamic Rendering](#dynamic-rendering)
If a route is [dynamically rendered](/docs/app/building-your-application/rendering/server-components#dynamic-rendering), `useSearchParams` will be available on the server during the initial server render of the Client Component.
For example:
app/dashboard/search-bar.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useSearchParams } from 'next/navigation'
export default function SearchBar() {
const searchParams = useSearchParams()
const search = searchParams.get('search')
// This will be logged on the server during the initial render
// and on the client on subsequent navigations.
console.log(search)
return <>Search: {search}</>
}
app/dashboard/page.tsx
TypeScript
JavaScriptTypeScript
import SearchBar from './search-bar'
export const dynamic = 'force-dynamic'
export default function Page() {
return (
<>
<nav>
<SearchBar />
</nav>
<h1>Dashboard</h1>
</>
)
}
> **Good to know**: Setting the [`dynamic` route segment config option](/docs/app/api-reference/file-conventions/route-segment-config#dynamic) to `force-dynamic` can be used to force dynamic rendering.
### [Server Components](#server-components)
#### [Pages](#pages)
To access search params in [Pages](/docs/app/api-reference/file-conventions/page) (Server Components), use the [`searchParams`](/docs/app/api-reference/file-conventions/page#searchparams-optional) prop.
#### [Layouts](#layouts)
Unlike Pages, [Layouts](/docs/app/api-reference/file-conventions/layout) (Server Components) **do not** receive the `searchParams` prop. This is because a shared layout is [not re-rendered during navigation](/docs/app/building-your-application/routing/linking-and-navigating#4-partial-rendering) which could lead to stale `searchParams` between navigations. View [detailed explanation](/docs/app/api-reference/file-conventions/layout#layouts-do-not-receive-searchparams).
Instead, use the Page [`searchParams`](/docs/app/api-reference/file-conventions/page) prop or the [`useSearchParams`](/docs/app/api-reference/functions/use-search-params) hook in a Client Component, which is re-rendered on the client with the latest `searchParams`.
[Examples](#examples)
---------------------
### [Updating `searchParams`](#updating-searchparams)
You can use [`useRouter`](/docs/app/api-reference/functions/use-router) or [`Link`](/docs/app/api-reference/components/link) to set new `searchParams`. After a navigation is performed, the current [`page.js`](/docs/app/api-reference/file-conventions/page) will receive an updated [`searchParams` prop](/docs/app/api-reference/file-conventions/page#searchparams-optional).
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
export default function ExampleClientComponent() {
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()
// Get a new searchParams string by merging the current
// searchParams with a provided key/value pair
const createQueryString = useCallback(
(name: string, value: string) => {
const params = new URLSearchParams(searchParams.toString())
params.set(name, value)
return params.toString()
},
[searchParams]
)
return (
<>
<p>Sort By</p>
{/* using useRouter */}
<button
onClick={() => {
// <pathname>?sort=asc
router.push(pathname + '?' + createQueryString('sort', 'asc'))
}}
>
ASC
</button>
{/* using <Link> */}
<Link
href={
// <pathname>?sort=desc
pathname + '?' + createQueryString('sort', 'desc')
}
>
DESC
</Link>
</>
)
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`useSearchParams` introduced.
[Previous
useRouter
](/docs/app/api-reference/functions/use-router)
[Next
useSelectedLayoutSegment
](/docs/app/api-reference/functions/use-selected-layout-segment)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"7066a893f88df3453ca5616284c6ce48\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/file-conventions/metadata/robots | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [
robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Static robots.txt](#static-robotstxt)
* [Generate a Robots file](#generate-a-robots-file)
* [Customizing specific user agents](#customizing-specific-user-agents)
* [Robots object](#robots-object)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/02-file-conventions/01-metadata/robots.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference/file-conventions)[Metadata Files](/docs/app/api-reference/file-conventions/metadata)robots.txt
robots.txt
==========
Add or generate a `robots.txt` file that matches the [Robots Exclusion Standard](https://en.wikipedia.org/wiki/Robots.txt#Standard) in the **root** of `app` directory to tell search engine crawlers which URLs they can access on your site.
[Static `robots.txt`](#static-robotstxt)
----------------------------------------
app/robots.txt
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://acme.com/sitemap.xml
[Generate a Robots file](#generate-a-robots-file)
-------------------------------------------------
Add a `robots.js` or `robots.ts` file that returns a [`Robots` object](#robots-object).
app/robots.ts
TypeScript
JavaScriptTypeScript
import { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: {
userAgent: '*',
allow: '/',
disallow: '/private/',
},
sitemap: 'https://acme.com/sitemap.xml',
}
}
Output:
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://acme.com/sitemap.xml
### [Customizing specific user agents](#customizing-specific-user-agents)
You can customise how individual search engine bots crawl your site by passing an array of user agents to the `rules` property. For example:
app/robots.ts
TypeScript
JavaScriptTypeScript
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: [
{
userAgent: 'Googlebot',
allow: ['/'],
disallow: '/private/',
},
{
userAgent: ['Applebot', 'Bingbot'],
disallow: ['/'],
},
],
sitemap: 'https://acme.com/sitemap.xml',
}
}
Output:
User-Agent: Googlebot
Allow: /
Disallow: /private/
User-Agent: Applebot
Disallow: /
User-Agent: Bingbot
Disallow: /
Sitemap: https://acme.com/sitemap.xml
### [Robots object](#robots-object)
type Robots = {
rules:
| {
userAgent?: string | string[]
allow?: string | string[]
disallow?: string | string[]
crawlDelay?: number
}
| Array<{
userAgent: string | string[]
allow?: string | string[]
disallow?: string | string[]
crawlDelay?: number
}>
sitemap?: string | string[]
host?: string
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
`robots` introduced.
[Previous
opengraph-image and twitter-image
](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
[Next
sitemap.xml
](/docs/app/api-reference/file-conventions/metadata/sitemap)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"65e788e67e9c18f0ffb61e7848ac35e8\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/use-selected-layout-segment | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [
useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [Returns](#returns)
* [Examples](#examples)
* [Creating an active link component](#creating-an-active-link-component)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/use-selected-layout-segment.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)useSelectedLayoutSegment
useSelectedLayoutSegment
========================
`useSelectedLayoutSegment` is a **Client Component** hook that lets you read the active route segment **one level below** the Layout it is called from.
It is useful for navigation UI, such as tabs inside a parent layout that change style depending on the active child segment.
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useSelectedLayoutSegment } from 'next/navigation'
export default function ExampleClientComponent() {
const segment = useSelectedLayoutSegment()
return <p>Active segment: {segment}</p>
}
> **Good to know**:
>
> * Since `useSelectedLayoutSegment` is a [Client Component](/docs/app/building-your-application/rendering/client-components) hook, and Layouts are [Server Components](/docs/app/building-your-application/rendering/server-components) by default, `useSelectedLayoutSegment` is usually called via a Client Component that is imported into a Layout.
> * `useSelectedLayoutSegment` only returns the segment one level down. To return all active segments, see [`useSelectedLayoutSegments`](/docs/app/api-reference/functions/use-selected-layout-segments)
[Parameters](#parameters)
-------------------------
const segment = useSelectedLayoutSegment(parallelRoutesKey?: string)
`useSelectedLayoutSegment` _optionally_ accepts a [`parallelRoutesKey`](/docs/app/building-your-application/routing/parallel-routes#useselectedlayoutsegments), which allows you to read the active route segment within that slot.
[Returns](#returns)
-------------------
`useSelectedLayoutSegment` returns a string of the active segment or `null` if one doesn't exist.
For example, given the Layouts and URLs below, the returned segment would be:
Layout
Visited URL
Returned Segment
`app/layout.js`
`/`
`null`
`app/layout.js`
`/dashboard`
`'dashboard'`
`app/dashboard/layout.js`
`/dashboard`
`null`
`app/dashboard/layout.js`
`/dashboard/settings`
`'settings'`
`app/dashboard/layout.js`
`/dashboard/analytics`
`'analytics'`
`app/dashboard/layout.js`
`/dashboard/analytics/monthly`
`'analytics'`
[Examples](#examples)
---------------------
### [Creating an active link component](#creating-an-active-link-component)
You can use `useSelectedLayoutSegment` to create an active link component that changes style depending on the active segment. For example, a featured posts list in the sidebar of a blog:
app/blog/blog-nav-link.tsx
TypeScript
JavaScriptTypeScript
'use client'
import Link from 'next/link'
import { useSelectedLayoutSegment } from 'next/navigation'
// This *client* component will be imported into a blog layout
export default function BlogNavLink({
slug,
children,
}: {
slug: string
children: React.ReactNode
}) {
// Navigating to `/blog/hello-world` will return 'hello-world'
// for the selected layout segment
const segment = useSelectedLayoutSegment()
const isActive = slug === segment
return (
<Link
href={`/blog/${slug}`}
// Change style depending on whether the link is active
style={{ fontWeight: isActive ? 'bold' : 'normal' }}
>
{children}
</Link>
)
}
app/blog/layout.tsx
TypeScript
JavaScriptTypeScript
// Import the Client Component into a parent Layout (Server Component)
import { BlogNavLink } from './blog-nav-link'
import getFeaturedPosts from './get-featured-posts'
export default async function Layout({
children,
}: {
children: React.ReactNode
}) {
const featuredPosts = await getFeaturedPosts()
return (
<div>
{featuredPosts.map((post) => (
<div key={post.id}>
<BlogNavLink slug={post.slug}>{post.title}</BlogNavLink>
</div>
))}
<div>{children}</div>
</div>
)
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`useSelectedLayoutSegment` introduced.
[Previous
useSearchParams
](/docs/app/api-reference/functions/use-search-params)
[Next
useSelectedLayoutSegments
](/docs/app/api-reference/functions/use-selected-layout-segments)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"5fdac025662cb430a3cdacc3a1cb380c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/building-your-application/upgrading/app-router-migration | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [
App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Upgrading](#upgrading)
* [Node.js Version](#nodejs-version)
* [Next.js Version](#nextjs-version)
* [ESLint Version](#eslint-version)
* [Next Steps](#next-steps)
* [Upgrading New Features](#upgrading-new-features)
* [<Image/> Component](#image-component)
* [<Link> Component](#link-component)
* [<Script> Component](#script-component)
* [Font Optimization](#font-optimization)
* [Migrating from pages to app](#migrating-from-pages-to-app)
* [Step 1: Creating the app directory](#step-1-creating-the-app-directory)
* [Step 2: Creating a Root Layout](#step-2-creating-a-root-layout)
* [Migrating \_document.js and \_app.js](#migrating-_documentjs-and-_appjs)
* [Migrating the getLayout() pattern to Layouts (Optional)](#migrating-the-getlayout-pattern-to-layouts-optional)
* [Step 3: Migrating next/head](#step-3-migrating-nexthead)
* [Step 4: Migrating Pages](#step-4-migrating-pages)
* [Step 5: Migrating Routing Hooks](#step-5-migrating-routing-hooks)
* [Step 6: Migrating Data Fetching Methods](#step-6-migrating-data-fetching-methods)
* [Server-side Rendering (getServerSideProps)](#server-side-rendering-getserversideprops)
* [Accessing Request Object](#accessing-request-object)
* [Static Site Generation (getStaticProps)](#static-site-generation-getstaticprops)
* [Dynamic paths (getStaticPaths)](#dynamic-paths-getstaticpaths)
* [Replacing fallback](#replacing-fallback)
* [Incremental Static Regeneration (getStaticProps with revalidate)](#incremental-static-regeneration-getstaticprops-with-revalidate)
* [API Routes](#api-routes)
* [Step 7: Styling](#step-7-styling)
* [Tailwind CSS](#tailwind-css)
* [Codemods](#codemods)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/01-building-your-application/11-upgrading/02-app-router-migration.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/building-your-application)[Upgrading](/docs/app/building-your-application/upgrading)App Router Migration
App Router Incremental Adoption Guide
=====================================
This guide will help you:
* [Update your Next.js application from version 12 to version 13](#nextjs-version)
* [Upgrade features that work in both the `pages` and the `app` directories](#upgrading-new-features)
* [Incrementally migrate your existing application from `pages` to `app`](#migrating-from-pages-to-app)
[Upgrading](#upgrading)
-----------------------
### [Node.js Version](#nodejs-version)
The minimum Node.js version is now **v18.17**. See the [Node.js documentation](https://nodejs.org/docs/latest-v18.x/api/) for more information.
### [Next.js Version](#nextjs-version)
To update to Next.js version 13, run the following command using your preferred package manager:
Terminal
npm install next@latest react@latest react-dom@latest
### [ESLint Version](#eslint-version)
If you're using ESLint, you need to upgrade your ESLint version:
Terminal
npm install -D eslint-config-next@latest
> **Good to know**: You may need to restart the ESLint server in VS Code for the ESLint changes to take effect. Open the Command Palette (`cmd+shift+p` on Mac; `ctrl+shift+p` on Windows) and search for `ESLint: Restart ESLint Server`.
[Next Steps](#next-steps)
-------------------------
After you've updated, see the following sections for next steps:
* [Upgrade new features](#upgrading-new-features): A guide to help you upgrade to new features such as the improved Image and Link Components.
* [Migrate from the `pages` to `app` directory](#migrating-from-pages-to-app): A step-by-step guide to help you incrementally migrate from the `pages` to the `app` directory.
[Upgrading New Features](#upgrading-new-features)
-------------------------------------------------
Next.js 13 introduced the new [App Router](/docs/app/building-your-application/routing) with new features and conventions. The new Router is available in the `app` directory and co-exists with the `pages` directory.
Upgrading to Next.js 13 does **not** require using the new [App Router](/docs/app/building-your-application/routing#the-app-router). You can continue using `pages` with new features that work in both directories, such as the updated [Image component](#image-component), [Link component](#link-component), [Script component](#script-component), and [Font optimization](#font-optimization).
### [`<Image/>` Component](#image-component)
Next.js 12 introduced new improvements to the Image Component with a temporary import: `next/future/image`. These improvements included less client-side JavaScript, easier ways to extend and style images, better accessibility, and native browser lazy loading.
In version 13, this new behavior is now the default for `next/image`.
There are two codemods to help you migrate to the new Image Component:
* [**`next-image-to-legacy-image` codemod**](/docs/app/building-your-application/upgrading/codemods#next-image-to-legacy-image): Safely and automatically renames `next/image` imports to `next/legacy/image`. Existing components will maintain the same behavior.
* [**`next-image-experimental` codemod**](/docs/app/building-your-application/upgrading/codemods#next-image-experimental): Dangerously adds inline styles and removes unused props. This will change the behavior of existing components to match the new defaults. To use this codemod, you need to run the `next-image-to-legacy-image` codemod first.
### [`<Link>` Component](#link-component)
The [`<Link>` Component](/docs/app/building-your-application/routing/linking-and-navigating#link-component) no longer requires manually adding an `<a>` tag as a child. This behavior was added as an experimental option in [version 12.2](https://nextjs.org/blog/next-12-2) and is now the default. In Next.js 13, `<Link>` always renders `<a>` and allows you to forward props to the underlying tag.
For example:
import Link from 'next/link'
// Next.js 12: `<a>` has to be nested otherwise it's excluded
<Link href="/about">
<a>About</a>
</Link>
// Next.js 13: `<Link>` always renders `<a>` under the hood
<Link href="/about">
About
</Link>
To upgrade your links to Next.js 13, you can use the [`new-link` codemod](/docs/app/building-your-application/upgrading/codemods#new-link).
### [`<Script>` Component](#script-component)
The behavior of [`next/script`](/docs/app/api-reference/components/script) has been updated to support both `pages` and `app`, but some changes need to be made to ensure a smooth migration:
* Move any `beforeInteractive` scripts you previously included in `_document.js` to the root layout file (`app/layout.tsx`).
* The experimental `worker` strategy does not yet work in `app` and scripts denoted with this strategy will either have to be removed or modified to use a different strategy (e.g. `lazyOnload`).
* `onLoad`, `onReady`, and `onError` handlers will not work in Server Components so make sure to move them to a [Client Component](/docs/app/building-your-application/rendering/server-components) or remove them altogether.
### [Font Optimization](#font-optimization)
Previously, Next.js helped you optimize fonts by [inlining font CSS](/docs/app/building-your-application/optimizing/fonts). Version 13 introduces the new [`next/font`](/docs/app/building-your-application/optimizing/fonts) module which gives you the ability to customize your font loading experience while still ensuring great performance and privacy. `next/font` is supported in both the `pages` and `app` directories.
While [inlining CSS](/docs/app/building-your-application/optimizing/fonts) still works in `pages`, it does not work in `app`. You should use [`next/font`](/docs/app/building-your-application/optimizing/fonts) instead.
See the [Font Optimization](/docs/app/building-your-application/optimizing/fonts) page to learn how to use `next/font`.
[Migrating from `pages` to `app`](#migrating-from-pages-to-app)
---------------------------------------------------------------
> **🎥 Watch:** Learn how to incrementally adopt the App Router → [YouTube (16 minutes)](https://www.youtube.com/watch?v=YQMSietiFm0).
Moving to the App Router may be the first time using React features that Next.js builds on top of such as Server Components, Suspense, and more. When combined with new Next.js features such as [special files](/docs/app/building-your-application/routing#file-conventions) and [layouts](/docs/app/building-your-application/routing/pages-and-layouts#layouts), migration means new concepts, mental models, and behavioral changes to learn.
We recommend reducing the combined complexity of these updates by breaking down your migration into smaller steps. The `app` directory is intentionally designed to work simultaneously with the `pages` directory to allow for incremental page-by-page migration.
* The `app` directory supports nested routes _and_ layouts. [Learn more](/docs/app/building-your-application/routing).
* Use nested folders to [define routes](/docs/app/building-your-application/routing/defining-routes) and a special `page.js` file to make a route segment publicly accessible. [Learn more](#step-4-migrating-pages).
* [Special file conventions](/docs/app/building-your-application/routing#file-conventions) are used to create UI for each route segment. The most common special files are `page.js` and `layout.js`.
* Use `page.js` to define UI unique to a route.
* Use `layout.js` to define UI that is shared across multiple routes.
* `.js`, `.jsx`, or `.tsx` file extensions can be used for special files.
* You can colocate other files inside the `app` directory such as components, styles, tests, and more. [Learn more](/docs/app/building-your-application/routing).
* Data fetching functions like `getServerSideProps` and `getStaticProps` have been replaced with [a new API](/docs/app/building-your-application/data-fetching) inside `app`. `getStaticPaths` has been replaced with [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params).
* `pages/_app.js` and `pages/_document.js` have been replaced with a single `app/layout.js` root layout. [Learn more](/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required).
* `pages/_error.js` has been replaced with more granular `error.js` special files. [Learn more](/docs/app/building-your-application/routing/error-handling).
* `pages/404.js` has been replaced with the [`not-found.js`](/docs/app/api-reference/file-conventions/not-found) file.
* `pages/api/*` API Routes have been replaced with the [`route.js`](/docs/app/api-reference/file-conventions/route) (Route Handler) special file.
### [Step 1: Creating the `app` directory](#step-1-creating-the-app-directory)
Update to the latest Next.js version (requires 13.4 or greater):
npm install next@latest
Then, create a new `app` directory at the root of your project (or `src/` directory).
### [Step 2: Creating a Root Layout](#step-2-creating-a-root-layout)
Create a new `app/layout.tsx` file inside the `app` directory. This is a [root layout](/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required) that will apply to all routes inside `app`.
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
// Layouts must accept a children prop.
// This will be populated with nested layouts or pages
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
* The `app` directory **must** include a root layout.
* The root layout must define `<html>`, and `<body>` tags since Next.js does not automatically create them
* The root layout replaces the `pages/_app.tsx` and `pages/_document.tsx` files.
* `.js`, `.jsx`, or `.tsx` extensions can be used for layout files.
To manage `<head>` HTML elements, you can use the [built-in SEO support](/docs/app/building-your-application/optimizing/metadata):
app/layout.tsx
TypeScript
JavaScriptTypeScript
import { Metadata } from 'next'
export const metadata: Metadata = {
title: 'Home',
description: 'Welcome to Next.js',
}
#### [Migrating `_document.js` and `_app.js`](#migrating-_documentjs-and-_appjs)
If you have an existing `_app` or `_document` file, you can copy the contents (e.g. global styles) to the root layout (`app/layout.tsx`). Styles in `app/layout.tsx` will _not_ apply to `pages/*`. You should keep `_app`/`_document` while migrating to prevent your `pages/*` routes from breaking. Once fully migrated, you can then safely delete them.
If you are using any React Context providers, they will need to be moved to a [Client Component](/docs/app/building-your-application/rendering/client-components).
#### [Migrating the `getLayout()` pattern to Layouts (Optional)](#migrating-the-getlayout-pattern-to-layouts-optional)
Next.js recommended adding a [property to Page components](/docs/pages/building-your-application/routing/pages-and-layouts#layout-pattern#per-page-layouts) to achieve per-page layouts in the `pages` directory. This pattern can be replaced with native support for [nested layouts](/docs/app/building-your-application/routing/pages-and-layouts#layouts) in the `app` directory.
See before and after example
**Before**
components/DashboardLayout.js
export default function DashboardLayout({ children }) {
return (
<div>
<h2>My Dashboard</h2>
{children}
</div>
)
}
pages/dashboard/index.js
import DashboardLayout from '../components/DashboardLayout'
export default function Page() {
return <p>My Page</p>
}
Page.getLayout = function getLayout(page) {
return <DashboardLayout>{page}</DashboardLayout>
}
**After**
* Remove the `Page.getLayout` property from `pages/dashboard/index.js` and follow the [steps for migrating pages](#step-4-migrating-pages) to the `app` directory.
app/dashboard/page.js
export default function Page() {
return <p>My Page</p>
}
* Move the contents of `DashboardLayout` into a new [Client Component](/docs/app/building-your-application/rendering/client-components) to retain `pages` directory behavior.
app/dashboard/DashboardLayout.js
'use client' // this directive should be at top of the file, before any imports.
// This is a Client Component
export default function DashboardLayout({ children }) {
return (
<div>
<h2>My Dashboard</h2>
{children}
</div>
)
}
* Import the `DashboardLayout` into a new `layout.js` file inside the `app` directory.
app/dashboard/layout.js
import DashboardLayout from './DashboardLayout'
// This is a Server Component
export default function Layout({ children }) {
return <DashboardLayout>{children}</DashboardLayout>
}
* You can incrementally move non-interactive parts of `DashboardLayout.js` (Client Component) into `layout.js` (Server Component) to reduce the amount of component JavaScript you send to the client.
### [Step 3: Migrating `next/head`](#step-3-migrating-nexthead)
In the `pages` directory, the `next/head` React component is used to manage `<head>` HTML elements such as `title` and `meta` . In the `app` directory, `next/head` is replaced with the new [built-in SEO support](/docs/app/building-your-application/optimizing/metadata).
**Before:**
pages/index.tsx
TypeScript
JavaScriptTypeScript
import Head from 'next/head'
export default function Page() {
return (
<>
<Head>
<title>My page title</title>
</Head>
</>
)
}
**After:**
app/page.tsx
TypeScript
JavaScriptTypeScript
import { Metadata } from 'next'
export const metadata: Metadata = {
title: 'My Page Title',
}
export default function Page() {
return '...'
}
[See all metadata options](/docs/app/api-reference/functions/generate-metadata).
### [Step 4: Migrating Pages](#step-4-migrating-pages)
* Pages in the [`app` directory](/docs/app/building-your-application/routing) are [Server Components](/docs/app/building-your-application/rendering/server-components) by default. This is different from the `pages` directory where pages are [Client Components](/docs/app/building-your-application/rendering/client-components).
* [Data fetching](/docs/app/building-your-application/data-fetching) has changed in `app`. `getServerSideProps`, `getStaticProps` and `getInitialProps` have been replaced with a simpler API.
* The `app` directory uses nested folders to [define routes](/docs/app/building-your-application/routing/defining-routes) and a special `page.js` file to make a route segment publicly accessible.
* `pages` Directory
`app` Directory
Route
`index.js`
`page.js`
`/`
`about.js`
`about/page.js`
`/about`
`blog/[slug].js`
`blog/[slug]/page.js`
`/blog/post-1`
We recommend breaking down the migration of a page into two main steps:
* Step 1: Move the default exported Page Component into a new Client Component.
* Step 2: Import the new Client Component into a new `page.js` file inside the `app` directory.
> **Good to know**: This is the easiest migration path because it has the most comparable behavior to the `pages` directory.
**Step 1: Create a new Client Component**
* Create a new separate file inside the `app` directory (i.e. `app/home-page.tsx` or similar) that exports a Client Component. To define Client Components, add the `'use client'` directive to the top of the file (before any imports).
* Similar to the Pages Router, there is an [optimization step](/docs/app/building-your-application/rendering/client-components#full-page-load) to prerender Client Components to static HTML on the initial page load.
* Move the default exported page component from `pages/index.js` to `app/home-page.tsx`.
app/home-page.tsx
TypeScript
JavaScriptTypeScript
'use client'
// This is a Client Component (same as components in the `pages` directory)
// It receives data as props, has access to state and effects, and is
// prerendered on the server during the initial page load.
export default function HomePage({ recentPosts }) {
return (
<div>
{recentPosts.map((post) => (
<div key={post.id}>{post.title}</div>
))}
</div>
)
}
**Step 2: Create a new page**
* Create a new `app/page.tsx` file inside the `app` directory. This is a Server Component by default.
* Import the `home-page.tsx` Client Component into the page.
* If you were fetching data in `pages/index.js`, move the data fetching logic directly into the Server Component using the new [data fetching APIs](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating). See the [data fetching upgrade guide](#step-6-migrating-data-fetching-methods) for more details.
app/page.tsx
TypeScript
JavaScriptTypeScript
// Import your Client Component
import HomePage from './home-page'
async function getPosts() {
const res = await fetch('https://...')
const posts = await res.json()
return posts
}
export default async function Page() {
// Fetch data directly in a Server Component
const recentPosts = await getPosts()
// Forward fetched data to your Client Component
return <HomePage recentPosts={recentPosts} />
}
* If your previous page used `useRouter`, you'll need to update to the new routing hooks. [Learn more](/docs/app/api-reference/functions/use-router).
* Start your development server and visit [`http://localhost:3000`](http://localhost:3000). You should see your existing index route, now served through the app directory.
### [Step 5: Migrating Routing Hooks](#step-5-migrating-routing-hooks)
A new router has been added to support the new behavior in the `app` directory.
In `app`, you should use the three new hooks imported from `next/navigation`: [`useRouter()`](/docs/app/api-reference/functions/use-router), [`usePathname()`](/docs/app/api-reference/functions/use-pathname), and [`useSearchParams()`](/docs/app/api-reference/functions/use-search-params).
* The new `useRouter` hook is imported from `next/navigation` and has different behavior to the `useRouter` hook in `pages` which is imported from `next/router`.
* The [`useRouter` hook imported from `next/router`](/docs/pages/api-reference/functions/use-router) is not supported in the `app` directory but can continue to be used in the `pages` directory.
* The new `useRouter` does not return the `pathname` string. Use the separate `usePathname` hook instead.
* The new `useRouter` does not return the `query` object. Use the separate `useSearchParams` hook instead.
* You can use `useSearchParams` and `usePathname` together to listen to page changes. See the [Router Events](/docs/app/api-reference/functions/use-router#router-events) section for more details.
* These new hooks are only supported in Client Components. They cannot be used in Server Components.
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useRouter, usePathname, useSearchParams } from 'next/navigation'
export default function ExampleClientComponent() {
const router = useRouter()
const pathname = usePathname()
const searchParams = useSearchParams()
// ...
}
In addition, the new `useRouter` hook has the following changes:
* `isFallback` has been removed because `fallback` has [been replaced](#replacing-fallback).
* The `locale`, `locales`, `defaultLocales`, `domainLocales` values have been removed because built-in i18n Next.js features are no longer necessary in the `app` directory. [Learn more about i18n](/docs/app/building-your-application/routing/internationalization).
* `basePath` has been removed. The alternative will not be part of `useRouter`. It has not yet been implemented.
* `asPath` has been removed because the concept of `as` has been removed from the new router.
* `isReady` has been removed because it is no longer necessary. During [static rendering](/docs/app/building-your-application/rendering/server-components#static-rendering-default), any component that uses the [`useSearchParams()`](/docs/app/api-reference/functions/use-search-params) hook will skip the prerendering step and instead be rendered on the client at runtime.
[View the `useRouter()` API reference](/docs/app/api-reference/functions/use-router).
### [Step 6: Migrating Data Fetching Methods](#step-6-migrating-data-fetching-methods)
The `pages` directory uses `getServerSideProps` and `getStaticProps` to fetch data for pages. Inside the `app` directory, these previous data fetching functions are replaced with a [simpler API](/docs/app/building-your-application/data-fetching) built on top of `fetch()` and `async` React Server Components.
app/page.tsx
TypeScript
JavaScriptTypeScript
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' })
// 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>
}
#### [Server-side Rendering (`getServerSideProps`)](#server-side-rendering-getserversideprops)
In the `pages` directory, `getServerSideProps` is used to fetch data on the server and forward props to the default exported React component in the file. The initial HTML for the page is prerendered from the server, followed by "hydrating" the page in the browser (making it interactive).
pages/dashboard.js
// `pages` directory
export async function getServerSideProps() {
const res = await fetch(`https://...`)
const projects = await res.json()
return { props: { projects } }
}
export default function Dashboard({ projects }) {
return (
<ul>
{projects.map((project) => (
<li key={project.id}>{project.name}</li>
))}
</ul>
)
}
In the `app` directory, we can colocate our data fetching inside our React components using [Server Components](/docs/app/building-your-application/rendering/server-components). This allows us to send less JavaScript to the client, while maintaining the rendered HTML from the server.
By setting the `cache` option to `no-store`, we can indicate that the fetched data should [never be cached](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating). This is similar to `getServerSideProps` in the `pages` directory.
app/dashboard/page.tsx
TypeScript
JavaScriptTypeScript
// `app` directory
// This function can be named anything
async function getProjects() {
const res = await fetch(`https://...`, { cache: 'no-store' })
const projects = await res.json()
return projects
}
export default async function Dashboard() {
const projects = await getProjects()
return (
<ul>
{projects.map((project) => (
<li key={project.id}>{project.name}</li>
))}
</ul>
)
}
#### [Accessing Request Object](#accessing-request-object)
In the `pages` directory, you can retrieve request-based data based on the Node.js HTTP API.
For example, you can retrieve the `req` object from `getServerSideProps` and use it to retrieve the request's cookies and headers.
pages/index.js
// `pages` directory
export async function getServerSideProps({ req, query }) {
const authHeader = req.getHeaders()['authorization'];
const theme = req.cookies['theme'];
return { props: { ... }}
}
export default function Page(props) {
return ...
}
The `app` directory exposes new read-only functions to retrieve request data:
* [`headers()`](/docs/app/api-reference/functions/headers): Based on the Web Headers API, and can be used inside [Server Components](/docs/app/building-your-application/rendering/server-components) to retrieve request headers.
* [`cookies()`](/docs/app/api-reference/functions/cookies): Based on the Web Cookies API, and can be used inside [Server Components](/docs/app/building-your-application/rendering/server-components) to retrieve cookies.
app/page.tsx
TypeScript
JavaScriptTypeScript
// `app` directory
import { cookies, headers } from 'next/headers'
async function getData() {
const authHeader = headers().get('authorization')
return '...'
}
export default async function Page() {
// You can use `cookies()` or `headers()` inside Server Components
// directly or in your data fetching function
const theme = cookies().get('theme')
const data = await getData()
return '...'
}
#### [Static Site Generation (`getStaticProps`)](#static-site-generation-getstaticprops)
In the `pages` directory, the `getStaticProps` function is used to pre-render a page at build time. This function can be used to fetch data from an external API or directly from a database, and pass this data down to the entire page as it's being generated during the build.
pages/index.js
// `pages` directory
export async function getStaticProps() {
const res = await fetch(`https://...`)
const projects = await res.json()
return { props: { projects } }
}
export default function Index({ projects }) {
return projects.map((project) => <div>{project.name}</div>)
}
In the `app` directory, data fetching with [`fetch()`](/docs/app/api-reference/functions/fetch) will default to `cache: 'force-cache'`, which will cache the request data until manually invalidated. This is similar to `getStaticProps` in the `pages` directory.
app/page.js
// `app` directory
// This function can be named anything
async function getProjects() {
const res = await fetch(`https://...`)
const projects = await res.json()
return projects
}
export default async function Index() {
const projects = await getProjects()
return projects.map((project) => <div>{project.name}</div>)
}
#### [Dynamic paths (`getStaticPaths`)](#dynamic-paths-getstaticpaths)
In the `pages` directory, the `getStaticPaths` function is used to define the dynamic paths that should be pre-rendered at build time.
pages/posts/\[id\].js
// `pages` directory
import PostLayout from '@/components/post-layout'
export async function getStaticPaths() {
return {
paths: [{ params: { id: '1' } }, { params: { id: '2' } }],
}
}
export async function getStaticProps({ params }) {
const res = await fetch(`https://.../posts/${params.id}`)
const post = await res.json()
return { props: { post } }
}
export default function Post({ post }) {
return <PostLayout post={post} />
}
In the `app` directory, `getStaticPaths` is replaced with [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params).
[`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params) behaves similarly to `getStaticPaths`, but has a simplified API for returning route parameters and can be used inside [layouts](/docs/app/building-your-application/routing/pages-and-layouts). The return shape of `generateStaticParams` is an array of segments instead of an array of nested `param` objects or a string of resolved paths.
app/posts/\[id\]/page.js
// `app` directory
import PostLayout from '@/components/post-layout'
export async function generateStaticParams() {
return [{ id: '1' }, { id: '2' }]
}
async function getPost(params) {
const res = await fetch(`https://.../posts/${params.id}`)
const post = await res.json()
return post
}
export default async function Post({ params }) {
const post = await getPost(params)
return <PostLayout post={post} />
}
Using the name `generateStaticParams` is more appropriate than `getStaticPaths` for the new model in the `app` directory. The `get` prefix is replaced with a more descriptive `generate`, which sits better alone now that `getStaticProps` and `getServerSideProps` are no longer necessary. The `Paths` suffix is replaced by `Params`, which is more appropriate for nested routing with multiple dynamic segments.
* * *
#### [Replacing `fallback`](#replacing-fallback)
In the `pages` directory, the `fallback` property returned from `getStaticPaths` is used to define the behavior of a page that isn't pre-rendered at build time. This property can be set to `true` to show a fallback page while the page is being generated, `false` to show a 404 page, or `blocking` to generate the page at request time.
pages/posts/\[id\].js
// `pages` directory
export async function getStaticPaths() {
return {
paths: [],
fallback: 'blocking'
};
}
export async function getStaticProps({ params }) {
...
}
export default function Post({ post }) {
return ...
}
In the `app` directory the [`config.dynamicParams` property](/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams) controls how params outside of [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params) are handled:
* **`true`**: (default) Dynamic segments not included in `generateStaticParams` are generated on demand.
* **`false`**: Dynamic segments not included in `generateStaticParams` will return a 404.
This replaces the `fallback: true | false | 'blocking'` option of `getStaticPaths` in the `pages` directory. The `fallback: 'blocking'` option is not included in `dynamicParams` because the difference between `'blocking'` and `true` is negligible with streaming.
app/posts/\[id\]/page.js
// `app` directory
export const dynamicParams = true;
export async function generateStaticParams() {
return [...]
}
async function getPost(params) {
...
}
export default async function Post({ params }) {
const post = await getPost(params);
return ...
}
With [`dynamicParams`](/docs/app/api-reference/file-conventions/route-segment-config#dynamicparams) set to `true` (the default), when a route segment is requested that hasn't been generated, it will be server-rendered and cached.
#### [Incremental Static Regeneration (`getStaticProps` with `revalidate`)](#incremental-static-regeneration-getstaticprops-with-revalidate)
In the `pages` directory, the `getStaticProps` function allows you to add a `revalidate` field to automatically regenerate a page after a certain amount of time.
pages/index.js
// `pages` directory
export async function getStaticProps() {
const res = await fetch(`https://.../posts`)
const posts = await res.json()
return {
props: { posts },
revalidate: 60,
}
}
export default function Index({ posts }) {
return (
<Layout>
<PostList posts={posts} />
</Layout>
)
}
In the `app` directory, data fetching with [`fetch()`](/docs/app/api-reference/functions/fetch) can use `revalidate`, which will cache the request for the specified amount of seconds.
app/page.js
// `app` directory
async function getPosts() {
const res = await fetch(`https://.../posts`, { next: { revalidate: 60 } })
const data = await res.json()
return data.posts
}
export default async function PostList() {
const posts = await getPosts()
return posts.map((post) => <div>{post.name}</div>)
}
#### [API Routes](#api-routes)
API Routes continue to work in the `pages/api` directory without any changes. However, they have been replaced by [Route Handlers](/docs/app/building-your-application/routing/route-handlers) in the `app` directory.
Route Handlers allow you to create custom request handlers for a given route using the Web [Request](https://developer.mozilla.org/docs/Web/API/Request) and [Response](https://developer.mozilla.org/docs/Web/API/Response) APIs.
app/api/route.ts
TypeScript
JavaScriptTypeScript
export async function GET(request: Request) {}
> **Good to know**: If you previously used API routes to call an external API from the client, you can now use [Server Components](/docs/app/building-your-application/rendering/server-components) instead to securely fetch data. Learn more about [data fetching](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating).
### [Step 7: Styling](#step-7-styling)
In the `pages` directory, global stylesheets are restricted to only `pages/_app.js`. With the `app` directory, this restriction has been lifted. Global styles can be added to any layout, page, or component.
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [Global Styles](/docs/app/building-your-application/styling/css-modules#global-styles)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [External Stylesheets](/docs/app/building-your-application/styling/css-modules#external-stylesheets)
* [Sass](/docs/app/building-your-application/styling/sass)
#### [Tailwind CSS](#tailwind-css)
If you're using Tailwind CSS, you'll need to add the `app` directory to your `tailwind.config.js` file:
tailwind.config.js
module.exports = {
content: [
'./app/**/*.{js,ts,jsx,tsx,mdx}', // <-- Add this line
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
'./components/**/*.{js,ts,jsx,tsx,mdx}',
],
}
You'll also need to import your global styles in your `app/layout.js` file:
app/layout.js
import '../styles/globals.css'
export default function RootLayout({ children }) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
Learn more about [styling with Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
[Codemods](#codemods)
---------------------
Next.js provides Codemod transformations to help upgrade your codebase when a feature is deprecated. See [Codemods](/docs/app/building-your-application/upgrading/codemods) for more information.
[Previous
Codemods
](/docs/app/building-your-application/upgrading/codemods)
[Next
Version 14
](/docs/app/building-your-application/upgrading/version-14)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"d79a145e1b137e154dc6056461adea6c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[API Reference](/docs/app/api-reference)next.config.js Options
next.config.js Options
======================
Next.js can be configured through a `next.config.js` file in the root of your project directory (for example, by `package.json`) with a default export.
next.config.js
// @ts-check
/** @type {import('next').NextConfig} */
const nextConfig = {
/* config options here */
}
module.exports = nextConfig
`next.config.js` is a regular Node.js module, not a JSON file. It gets used by the Next.js server and build phases, and it's not included in the browser build.
If you need [ECMAScript modules](https://nodejs.org/api/esm.html), you can use `next.config.mjs`:
next.config.mjs
// @ts-check
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
export default nextConfig
You can also use a function:
next.config.mjs
// @ts-check
export default (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
Since Next.js 12.1.0, you can use an async function:
next.config.js
// @ts-check
module.exports = async (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
`phase` is the current context in which the configuration is loaded. You can see the [available phases](https://github.com/vercel/next.js/blob/5e6b008b561caf2710ab7be63320a3d549474a5b/packages/next/shared/lib/constants.ts#L19-L23). Phases can be imported from `next/constants`:
// @ts-check
const { PHASE_DEVELOPMENT_SERVER } = require('next/constants')
module.exports = (phase, { defaultConfig }) => {
if (phase === PHASE_DEVELOPMENT_SERVER) {
return {
/* development only config options here */
}
}
return {
/* config options for all phases except development here */
}
}
The commented lines are the place where you can put the configs allowed by `next.config.js`, which are [defined in this file](https://github.com/vercel/next.js/blob/canary/packages/next/src/server/config-shared.ts).
However, none of the configs are required, and it's not necessary to understand what each config does. Instead, search for the features you need to enable or modify in this section and they will show you what to do.
> Avoid using new JavaScript features not available in your target Node.js version. `next.config.js` will not be parsed by Webpack, Babel or TypeScript.
This page documents all the available configuration options:
[
### appDir
Enable the App Router to use layouts, streaming, and more.
](/docs/app/api-reference/next-config-js/appDir)[
### assetPrefix
Learn how to use the assetPrefix config option to configure your CDN.
](/docs/app/api-reference/next-config-js/assetPrefix)[
### basePath
Use \`basePath\` to deploy a Next.js application under a sub-path of a domain.
](/docs/app/api-reference/next-config-js/basePath)[
### compress
Next.js provides gzip compression to compress rendered content and static files, it only works with the server target. Learn more about it here.
](/docs/app/api-reference/next-config-js/compress)[
### crossOrigin
Use the \`crossOrigin\` option to add a crossOrigin tag on the \`script\` tags generated by \`next/script\`.
](/docs/app/api-reference/next-config-js/crossOrigin)[
### devIndicators
Optimized pages include an indicator to let you know if it's being statically optimized. You can opt-out of it here.
](/docs/app/api-reference/next-config-js/devIndicators)[
### distDir
Set a custom build directory to use instead of the default .next directory.
](/docs/app/api-reference/next-config-js/distDir)[
### env
Learn to add and access environment variables in your Next.js application at build time.
](/docs/app/api-reference/next-config-js/env)[
### eslint
Next.js reports ESLint errors and warnings during builds by default. Learn how to opt-out of this behavior here.
](/docs/app/api-reference/next-config-js/eslint)[
### exportPathMap
Customize the pages that will be exported as HTML files when using \`next export\`.
](/docs/app/api-reference/next-config-js/exportPathMap)[
### generateBuildId
Configure the build id, which is used to identify the current build in which your application is being served.
](/docs/app/api-reference/next-config-js/generateBuildId)[
### generateEtags
Next.js will generate etags for every page by default. Learn more about how to disable etag generation here.
](/docs/app/api-reference/next-config-js/generateEtags)[
### headers
Add custom HTTP headers to your Next.js app.
](/docs/app/api-reference/next-config-js/headers)[
### httpAgentOptions
Next.js will automatically use HTTP Keep-Alive by default. Learn more about how to disable HTTP Keep-Alive here.
](/docs/app/api-reference/next-config-js/httpAgentOptions)[
### images
Custom configuration for the next/image loader
](/docs/app/api-reference/next-config-js/images)[
### cacheHandler
Configure the Next.js cache used for storing and revalidating data to use any external service like Redis, Memcached, or others.
](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)[
### instrumentationHook
Use the instrumentationHook option to set up instrumentation in your Next.js App.
](/docs/app/api-reference/next-config-js/instrumentationHook)[
### logging
Configure how data fetches are logged to the console when running Next.js in development mode.
](/docs/app/api-reference/next-config-js/logging)[
### mdxRs
Use the new Rust compiler to compile MDX files in the App Router.
](/docs/app/api-reference/next-config-js/mdxRs)[
### onDemandEntries
Configure how Next.js will dispose and keep in memory pages created in development.
](/docs/app/api-reference/next-config-js/onDemandEntries)[
### optimizePackageImports
API Reference for optmizedPackageImports Next.js Config Option
](/docs/app/api-reference/next-config-js/optimizePackageImports)[
### output
Next.js automatically traces which files are needed by each page to allow for easy deployment of your application. Learn how it works here.
](/docs/app/api-reference/next-config-js/output)[
### pageExtensions
Extend the default page extensions used by Next.js when resolving pages in the Pages Router.
](/docs/app/api-reference/next-config-js/pageExtensions)[
### Partial Prerendering (experimental)
Learn how to enable Partial Prerendering (experimental) in Next.js 14.
](/docs/app/api-reference/next-config-js/partial-prerendering)[
### poweredByHeader
Next.js will add the \`x-powered-by\` header by default. Learn to opt-out of it here.
](/docs/app/api-reference/next-config-js/poweredByHeader)[
### productionBrowserSourceMaps
Enables browser source map generation during the production build.
](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)[
### reactStrictMode
The complete Next.js runtime is now Strict Mode-compliant, learn how to opt-in
](/docs/app/api-reference/next-config-js/reactStrictMode)[
### redirects
Add redirects to your Next.js app.
](/docs/app/api-reference/next-config-js/redirects)[
### rewrites
Add rewrites to your Next.js app.
](/docs/app/api-reference/next-config-js/rewrites)[
### serverActions
Configure Server Actions behavior in your Next.js application.
](/docs/app/api-reference/next-config-js/serverActions)[
### serverComponentsExternalPackages
Opt-out specific dependencies from the Server Components bundling and use native Node.js \`require\`.
](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)[
### StaleTimes (experimental)
Learn how to override the invalidation time of the Client Router Cache.
](/docs/app/api-reference/next-config-js/staleTimes)[
### trailingSlash
Configure Next.js pages to resolve with or without a trailing slash.
](/docs/app/api-reference/next-config-js/trailingSlash)[
### transpilePackages
Automatically transpile and bundle dependencies from local packages (like monorepos) or from external dependencies (\`node\_modules\`).
](/docs/app/api-reference/next-config-js/transpilePackages)[
### turbo
Configure Next.js with Turbopack-specific options
](/docs/app/api-reference/next-config-js/turbo)[
### typedRoutes
Enable experimental support for statically typed links.
](/docs/app/api-reference/next-config-js/typedRoutes)[
### typescript
Next.js reports TypeScript errors by default. Learn to opt-out of this behavior here.
](/docs/app/api-reference/next-config-js/typescript)[
### urlImports
Configure Next.js to allow importing modules from external URLs (experimental).
](/docs/app/api-reference/next-config-js/urlImports)[
### webpack
Learn how to customize the webpack config used by Next.js
](/docs/app/api-reference/next-config-js/webpack)[
### webVitalsAttribution
Learn how to use the webVitalsAttribution option to pinpoint the source of Web Vitals issues.
](/docs/app/api-reference/next-config-js/webVitalsAttribution)
[Previous
userAgent
](/docs/app/api-reference/functions/userAgent)
[Next
appDir
](/docs/app/api-reference/next-config-js/appDir)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"06f7d734ba2b3da567fc586a8e23ae5b\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/functions/use-selected-layout-segments | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [
useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [Returns](#returns)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/04-functions/use-selected-layout-segments.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[Functions](/docs/app/api-reference/functions)useSelectedLayoutSegments
useSelectedLayoutSegments
=========================
`useSelectedLayoutSegments` is a **Client Component** hook that lets you read the active route segments **below** the Layout it is called from.
It is useful for creating UI in parent Layouts that need knowledge of active child segments such as breadcrumbs.
app/example-client-component.tsx
TypeScript
JavaScriptTypeScript
'use client'
import { useSelectedLayoutSegments } from 'next/navigation'
export default function ExampleClientComponent() {
const segments = useSelectedLayoutSegments()
return (
<ul>
{segments.map((segment, index) => (
<li key={index}>{segment}</li>
))}
</ul>
)
}
> **Good to know**:
>
> * Since `useSelectedLayoutSegments` is a [Client Component](/docs/app/building-your-application/rendering/client-components) hook, and Layouts are [Server Components](/docs/app/building-your-application/rendering/server-components) by default, `useSelectedLayoutSegments` is usually called via a Client Component that is imported into a Layout.
> * The returned segments include [Route Groups](/docs/app/building-your-application/routing/route-groups), which you might not want to be included in your UI. You can use the `filter()` array method to remove items that start with a bracket.
[Parameters](#parameters)
-------------------------
const segments = useSelectedLayoutSegments(parallelRoutesKey?: string)
`useSelectedLayoutSegments` _optionally_ accepts a [`parallelRoutesKey`](/docs/app/building-your-application/routing/parallel-routes#useselectedlayoutsegments), which allows you to read the active route segment within that slot.
[Returns](#returns)
-------------------
`useSelectedLayoutSegments` returns an array of strings containing the active segments one level down from the layout the hook was called from. Or an empty array if none exist.
For example, given the Layouts and URLs below, the returned segments would be:
Layout
Visited URL
Returned Segments
`app/layout.js`
`/`
`[]`
`app/layout.js`
`/dashboard`
`['dashboard']`
`app/layout.js`
`/dashboard/settings`
`['dashboard', 'settings']`
`app/dashboard/layout.js`
`/dashboard`
`[]`
`app/dashboard/layout.js`
`/dashboard/settings`
`['settings']`
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`useSelectedLayoutSegments` introduced.
[Previous
useSelectedLayoutSegment
](/docs/app/api-reference/functions/use-selected-layout-segment)
[Next
userAgent
](/docs/app/api-reference/functions/userAgent)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"61b084630547be6bfd54e3f51c0b9c96\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/compress | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [
compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Disabling compression](#disabling-compression)
* [Changing the compression algorithm](#changing-the-compression-algorithm)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/compress.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)compress
compress
========
By default, Next.js uses `gzip` to compress rendered content and static files when using `next start` or a custom server. This is an optimization for applications that do not have compression configured. If compression is _already_ configured in your application via a custom server, Next.js will not add compression.
> **Good to know:**
>
> * When hosting your application on [Vercel](https://vercel.com/docs/edge-network/compression), compression uses `brotli` first, then `gzip`.
> * You can check if compression is enabled and which algorithm is used by looking at the [`Accept-Encoding`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Encoding) (browser accepted options) and [`Content-Encoding`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding) (currently used) headers in the response.
[Disabling compression](#disabling-compression)
-----------------------------------------------
To disable **compression**, set the `compress` config option to `false`:
next.config.js
module.exports = {
compress: false,
}
We do not recommend disabling compression unless you have compression configured on your server, as compression reduces bandwidth usage and improves the performance of your application.
[Changing the compression algorithm](#changing-the-compression-algorithm)
-------------------------------------------------------------------------
To change your compression algorithm, you will need to configure your custom server and set the `compress` option to `false` in your `next.config.js` file.
For example, you're using [nginx](https://www.nginx.com/) and want to switch to `brotli`, set the `compress` option to `false` to allow nginx to handle compression.
> **Good to know:**
>
> * For Next.js applications on Vercel, compression is handled by the Vercel's Edge Network and not Next.js. See the [Vercel documentation](https://vercel.com/docs/edge-network/compression) for more information.
[Previous
basePath
](/docs/app/api-reference/next-config-js/basePath)
[Next
crossOrigin
](/docs/app/api-reference/next-config-js/crossOrigin)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"e111db2193f563f628a0d17b195017cb\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/basePath | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [
basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Links](#links)
* [Images](#images)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/basePath.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)basePath
basePath
========
To deploy a Next.js application under a sub-path of a domain you can use the `basePath` config option.
`basePath` allows you to set a path prefix for the application. For example, to use `/docs` instead of `''` (an empty string, the default), open `next.config.js` and add the `basePath` config:
next.config.js
module.exports = {
basePath: '/docs',
}
> **Good to know**: This value must be set at build time and cannot be changed without re-building as the value is inlined in the client-side bundles.
### [Links](#links)
When linking to other pages using `next/link` and `next/router` the `basePath` will be automatically applied.
For example, using `/about` will automatically become `/docs/about` when `basePath` is set to `/docs`.
export default function HomePage() {
return (
<>
<Link href="/about">About Page</Link>
</>
)
}
Output html:
<a href="/docs/about">About Page</a>
This makes sure that you don't have to change all links in your application when changing the `basePath` value.
### [Images](#images)
When using the [`next/image`](/docs/app/api-reference/components/image) component, you will need to add the `basePath` in front of `src`.
For example, using `/docs/me.png` will properly serve your image when `basePath` is set to `/docs`.
import Image from 'next/image'
function Home() {
return (
<>
<h1>My Homepage</h1>
<Image
src="/docs/me.png"
alt="Picture of the author"
width={500}
height={500}
/>
<p>Welcome to my homepage!</p>
</>
)
}
export default Home
[Previous
assetPrefix
](/docs/app/api-reference/next-config-js/assetPrefix)
[Next
compress
](/docs/app/api-reference/next-config-js/compress)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a719b4f194fd4e8b13d28a20cc499762\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/crossOrigin | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [
crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Options](#options)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/crossOrigin.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)crossOrigin
crossOrigin
===========
Use the `crossOrigin` option to add a [`crossOrigin` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin) in all `<script>` tags generated by the [`next/script`](/docs/app/building-your-application/optimizing/scripts) component , and define how cross-origin requests should be handled.
next.config.js
module.exports = {
crossOrigin: 'anonymous',
}
[Options](#options)
-------------------
* `'anonymous'`: Adds [`crossOrigin="anonymous"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin#anonymous) attribute.
* `'use-credentials'`: Adds [`crossOrigin="use-credentials"`](https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes/crossorigin#use-credentials).
[Previous
compress
](/docs/app/api-reference/next-config-js/compress)
[Next
devIndicators
](/docs/app/api-reference/next-config-js/devIndicators)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"40ba0de003e143a86e595e6f05a3760b\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/assetPrefix | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [
assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/assetPrefix.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)assetPrefix
assetPrefix
===========
> **Attention**: [Deploying to Vercel](/docs/app/building-your-application/deploying) automatically configures a global CDN for your Next.js project. You do not need to manually setup an Asset Prefix.
> **Good to know**: Next.js 9.5+ added support for a customizable [Base Path](/docs/app/api-reference/next-config-js/basePath), which is better suited for hosting your application on a sub-path like `/docs`. We do not suggest you use a custom Asset Prefix for this use case.
To set up a [CDN](https://en.wikipedia.org/wiki/Content_delivery_network), you can set up an asset prefix and configure your CDN's origin to resolve to the domain that Next.js is hosted on.
Open `next.config.js` and add the `assetPrefix` config:
next.config.js
const isProd = process.env.NODE_ENV === 'production'
module.exports = {
// Use the CDN in production and localhost for development.
assetPrefix: isProd ? 'https://cdn.mydomain.com' : undefined,
}
Next.js will automatically use your asset prefix for the JavaScript and CSS files it loads from the `/_next/` path (`.next/static/` folder). For example, with the above configuration, the following request for a JS chunk:
/_next/static/chunks/4b9b41aaa062cbbfeff4add70f256968c51ece5d.4d708494b3aed70c04f0.js
Would instead become:
https://cdn.mydomain.com/_next/static/chunks/4b9b41aaa062cbbfeff4add70f256968c51ece5d.4d708494b3aed70c04f0.js
The exact configuration for uploading your files to a given CDN will depend on your CDN of choice. The only folder you need to host on your CDN is the contents of `.next/static/`, which should be uploaded as `_next/static/` as the above URL request indicates. **Do not upload the rest of your `.next/` folder**, as you should not expose your server code and other configuration to the public.
While `assetPrefix` covers requests to `_next/static`, it does not influence the following paths:
* Files in the [public](/docs/app/building-your-application/optimizing/static-assets) folder; if you want to serve those assets over a CDN, you'll have to introduce the prefix yourself
[Previous
appDir
](/docs/app/api-reference/next-config-js/appDir)
[Next
basePath
](/docs/app/api-reference/next-config-js/basePath)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9a8a31baa32e09fe963bbca9228ee10c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/appDir | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [
appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/appDir.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)appDir
appDir
======
> **Good to know**: This option is **no longer** needed as of Next.js 13.4. The App Router is now stable.
The App Router ([`app` directory](/docs/app/building-your-application/routing)) enables support for [layouts](/docs/app/building-your-application/routing/pages-and-layouts), [Server Components](/docs/app/building-your-application/rendering/server-components), [streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming), and [colocated data fetching](/docs/app/building-your-application/data-fetching).
Using the `app` directory will automatically enable [React Strict Mode](https://react.dev/reference/react/StrictMode). Learn how to [incrementally adopt `app`](/docs/app/building-your-application/upgrading/app-router-migration#migrating-from-pages-to-app).
[Previous
next.config.js Options
](/docs/app/api-reference/next-config-js)
[Next
assetPrefix
](/docs/app/api-reference/next-config-js/assetPrefix)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"ee17b8a5bb618b4b40361b38d94be126\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/devIndicators | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [
devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/devIndicators.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)devIndicators
devIndicators
=============
When you edit your code, and Next.js is compiling the application, a compilation indicator appears in the bottom right corner of the page.
> **Good to know**: This indicator is only present in development mode and will not appear when building and running the app in production mode.
In some cases this indicator can be misplaced on your page, for example, when conflicting with a chat launcher. To change its position, open `next.config.js` and set the `buildActivityPosition` in the `devIndicators` object to `bottom-right` (default), `bottom-left`, `top-right` or `top-left`:
next.config.js
module.exports = {
devIndicators: {
buildActivityPosition: 'bottom-right',
},
}
In some cases this indicator might not be useful for you. To remove it, open `next.config.js` and disable the `buildActivity` config in `devIndicators` object:
next.config.js
module.exports = {
devIndicators: {
buildActivity: false,
},
}
[Previous
crossOrigin
](/docs/app/api-reference/next-config-js/crossOrigin)
[Next
distDir
](/docs/app/api-reference/next-config-js/distDir)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"4759fb9fdeb23a7a99b0ebcad112abdc\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/distDir | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [
distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/distDir.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)distDir
distDir
=======
You can specify a name to use for a custom build directory to use instead of `.next`.
Open `next.config.js` and add the `distDir` config:
next.config.js
module.exports = {
distDir: 'build',
}
Now if you run `next build` Next.js will use `build` instead of the default `.next` folder.
> `distDir` **should not** leave your project directory. For example, `../build` is an **invalid** directory.
[Previous
devIndicators
](/docs/app/api-reference/next-config-js/devIndicators)
[Next
env
](/docs/app/api-reference/next-config-js/env)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"c11eab35cab0223f872df68019d59760\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/env | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [
env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/env.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)env
env
===
> Since the release of [Next.js 9.4](https://nextjs.org/blog/next-9-4) we now have a more intuitive and ergonomic experience for [adding environment variables](/docs/app/building-your-application/configuring/environment-variables). Give it a try!
> **Good to know**: environment variables specified in this way will **always** be included in the JavaScript bundle, prefixing the environment variable name with `NEXT_PUBLIC_` only has an effect when specifying them [through the environment or .env files](/docs/app/building-your-application/configuring/environment-variables).
To add environment variables to the JavaScript bundle, open `next.config.js` and add the `env` config:
next.config.js
module.exports = {
env: {
customKey: 'my-value',
},
}
Now you can access `process.env.customKey` in your code. For example:
function Page() {
return <h1>The value of customKey is: {process.env.customKey}</h1>
}
export default Page
Next.js will replace `process.env.customKey` with `'my-value'` at build time. Trying to destructure `process.env` variables won't work due to the nature of webpack [DefinePlugin](https://webpack.js.org/plugins/define-plugin/).
For example, the following line:
return <h1>The value of customKey is: {process.env.customKey}</h1>
Will end up being:
return <h1>The value of customKey is: {'my-value'}</h1>
[Previous
distDir
](/docs/app/api-reference/next-config-js/distDir)
[Next
eslint
](/docs/app/api-reference/next-config-js/eslint)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"76eca3b17e89f9bdcb1afe7741d8c777\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/output | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [
output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [How it Works](#how-it-works)
* [Automatically Copying Traced Files](#automatically-copying-traced-files)
* [Caveats](#caveats)
* [Experimental turbotrace](#experimental-turbotrace)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/output.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)output
output
======
During a build, Next.js will automatically trace each page and its dependencies to determine all of the files that are needed for deploying a production version of your application.
This feature helps reduce the size of deployments drastically. Previously, when deploying with Docker you would need to have all files from your package's `dependencies` installed to run `next start`. Starting with Next.js 12, you can leverage Output File Tracing in the `.next/` directory to only include the necessary files.
Furthermore, this removes the need for the deprecated `serverless` target which can cause various issues and also creates unnecessary duplication.
[How it Works](#how-it-works)
-----------------------------
During `next build`, Next.js will use [`@vercel/nft`](https://github.com/vercel/nft) to statically analyze `import`, `require`, and `fs` usage to determine all files that a page might load.
Next.js' production server is also traced for its needed files and output at `.next/next-server.js.nft.json` which can be leveraged in production.
To leverage the `.nft.json` files emitted to the `.next` output directory, you can read the list of files in each trace that are relative to the `.nft.json` file and then copy them to your deployment location.
[Automatically Copying Traced Files](#automatically-copying-traced-files)
-------------------------------------------------------------------------
Next.js can automatically create a `standalone` folder that copies only the necessary files for a production deployment including select files in `node_modules`.
To leverage this automatic copying you can enable it in your `next.config.js`:
next.config.js
module.exports = {
output: 'standalone',
}
This will create a folder at `.next/standalone` which can then be deployed on its own without installing `node_modules`.
Additionally, a minimal `server.js` file is also output which can be used instead of `next start`. This minimal server does not copy the `public` or `.next/static` folders by default as these should ideally be handled by a CDN instead, although these folders can be copied to the `standalone/public` and `standalone/.next/static` folders manually, after which `server.js` file will serve these automatically.
> **Good to know**:
>
> * If your project needs to listen to a specific port or hostname, you can define `PORT` or `HOSTNAME` environment variables before running `server.js`. For example, run `PORT=8080 HOSTNAME=0.0.0.0 node server.js` to start the server on `http://0.0.0.0:8080`.
> * If your project uses [Image Optimization](/docs/app/building-your-application/optimizing/images) with the default `loader`, you must install `sharp` as a dependency:
Terminal
npm i sharp
Terminal
yarn add sharp
Terminal
pnpm add sharp
Terminal
bun add sharp
[Caveats](#caveats)
-------------------
* While tracing in monorepo setups, the project directory is used for tracing by default. For `next build packages/web-app`, `packages/web-app` would be the tracing root and any files outside of that folder will not be included. To include files outside of this folder you can set `experimental.outputFileTracingRoot` in your `next.config.js`.
packages/web-app/next.config.js
module.exports = {
experimental: {
// this includes files from the monorepo base two directories up
outputFileTracingRoot: path.join(__dirname, '../../'),
},
}
* There are some cases in which Next.js might fail to include required files, or might incorrectly include unused files. In those cases, you can leverage `experimental.outputFileTracingExcludes` and `experimental.outputFileTracingIncludes` respectively in `next.config.js`. Each config accepts an object with [minimatch globs](https://www.npmjs.com/package/minimatch) for the key to match specific pages and a value of an array with globs relative to the project's root to either include or exclude in the trace.
next.config.js
module.exports = {
experimental: {
outputFileTracingExcludes: {
'/api/hello': ['./un-necessary-folder/**/*'],
},
outputFileTracingIncludes: {
'/api/another': ['./necessary-folder/**/*'],
},
},
}
* Currently, Next.js does not do anything with the emitted `.nft.json` files. The files must be read by your deployment platform, for example [Vercel](https://vercel.com), to create a minimal deployment. In a future release, a new command is planned to utilize these `.nft.json` files.
[Experimental `turbotrace`](#experimental-turbotrace)
-----------------------------------------------------
Tracing dependencies can be slow because it requires very complex computations and analysis. We created `turbotrace` in Rust as a faster and smarter alternative to the JavaScript implementation.
To enable it, you can add the following configuration to your `next.config.js`:
next.config.js
module.exports = {
experimental: {
turbotrace: {
// control the log level of the turbotrace, default is `error`
logLevel?:
| 'bug'
| 'fatal'
| 'error'
| 'warning'
| 'hint'
| 'note'
| 'suggestions'
| 'info',
// control if the log of turbotrace should contain the details of the analysis, default is `false`
logDetail?: boolean
// show all log messages without limit
// turbotrace only show 1 log message for each categories by default
logAll?: boolean
// control the context directory of the turbotrace
// files outside of the context directory will not be traced
// set the `experimental.outputFileTracingRoot` has the same effect
// if the `experimental.outputFileTracingRoot` and this option are both set, the `experimental.turbotrace.contextDirectory` will be used
contextDirectory?: string
// if there is `process.cwd()` expression in your code, you can set this option to tell `turbotrace` the value of `process.cwd()` while tracing.
// for example the require(process.cwd() + '/package.json') will be traced as require('/path/to/cwd/package.json')
processCwd?: string
// control the maximum memory usage of the `turbotrace`, in `MB`, default is `6000`.
memoryLimit?: number
},
},
}
[Previous
optimizePackageImports
](/docs/app/api-reference/next-config-js/optimizePackageImports)
[Next
pageExtensions
](/docs/app/api-reference/next-config-js/pageExtensions)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"e7214f130c81fb4dd4848aa6301f4607\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/optimizePackageImports | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [
optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/optimizePackageImports.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)optimizePackageImports
optimizePackageImports
======================
Some packages can export hundreds or thousands of modules, which can cause performance issues in development and production.
Adding a package to `experimental.optimizePackageImports` will only load the modules you are actually using, while still giving you the convenience of writing import statements with many named exports.
next.config.js
module.exports = {
experimental: {
optimizePackageImports: ['package-name'],
},
}
The following libraries are optimized by default:
* `lucide-react`
* `date-fns`
* `lodash-es`
* `ramda`
* `antd`
* `react-bootstrap`
* `ahooks`
* `@ant-design/icons`
* `@headlessui/react`
* `@headlessui-float/react`
* `@heroicons/react/20/solid`
* `@heroicons/react/24/solid`
* `@heroicons/react/24/outline`
* `@visx/visx`
* `@tremor/react`
* `rxjs`
* `@mui/material`
* `@mui/icons-material`
* `recharts`
* `react-use`
* `@material-ui/core`
* `@material-ui/icons`
* `@tabler/icons-react`
* `mui-core`
* `react-icons/*`
[Previous
onDemandEntries
](/docs/app/api-reference/next-config-js/onDemandEntries)
[Next
output
](/docs/app/api-reference/next-config-js/output)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f9af9d92d1809f0c86b4fbafc658cc2e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/eslint | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [
eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/eslint.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)eslint
eslint
======
When ESLint is detected in your project, Next.js fails your **production build** (`next build`) when errors are present.
If you'd like Next.js to produce production code even when your application has ESLint errors, you can disable the built-in linting step completely. This is not recommended unless you already have ESLint configured to run in a separate part of your workflow (for example, in CI or a pre-commit hook).
Open `next.config.js` and enable the `ignoreDuringBuilds` option in the `eslint` config:
next.config.js
module.exports = {
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
}
[Previous
env
](/docs/app/api-reference/next-config-js/env)
[Next
exportPathMap
](/docs/app/api-reference/next-config-js/exportPathMap)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"e2b91665f8c0bf0b7072fda81b03cb93\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/pages-and-layouts | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [
Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Index routes](#index-routes)
* [Nested routes](#nested-routes)
* [Pages with Dynamic Routes](#pages-with-dynamic-routes)
* [Layout Pattern](#layout-pattern)
* [Examples](#examples)
* [Single Shared Layout with Custom App](#single-shared-layout-with-custom-app)
* [Per-Page Layouts](#per-page-layouts)
* [With TypeScript](#with-typescript)
* [Data Fetching](#data-fetching)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/01-pages-and-layouts.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Pages and Layouts
Pages and Layouts
=================
The Pages Router has a file-system based router built on the concept of pages.
When a file is added to the `pages` directory, it's automatically available as a route.
In Next.js, a **page** is a [React Component](https://react.dev/learn/your-first-component) exported from a `.js`, `.jsx`, `.ts`, or `.tsx` file in the `pages` directory. Each page is associated with a route based on its file name.
**Example**: If you create `pages/about.js` that exports a React component like below, it will be accessible at `/about`.
export default function About() {
return <div>About</div>
}
[Index routes](#index-routes)
-----------------------------
The router will automatically route files named `index` to the root of the directory.
* `pages/index.js` → `/`
* `pages/blog/index.js` → `/blog`
[Nested routes](#nested-routes)
-------------------------------
The router supports nested files. If you create a nested folder structure, files will automatically be routed in the same way still.
* `pages/blog/first-post.js` → `/blog/first-post`
* `pages/dashboard/settings/username.js` → `/dashboard/settings/username`
[Pages with Dynamic Routes](#pages-with-dynamic-routes)
-------------------------------------------------------
Next.js supports pages with dynamic routes. For example, if you create a file called `pages/posts/[id].js`, then it will be accessible at `posts/1`, `posts/2`, etc.
> To learn more about dynamic routing, check the [Dynamic Routing documentation](/docs/pages/building-your-application/routing/dynamic-routes).
[Layout Pattern](#layout-pattern)
---------------------------------
The React model allows us to deconstruct a [page](/docs/pages/building-your-application/routing/pages-and-layouts) into a series of components. Many of these components are often reused between pages. For example, you might have the same navigation bar and footer on every page.
components/layout.js
import Navbar from './navbar'
import Footer from './footer'
export default function Layout({ children }) {
return (
<>
<Navbar />
<main>{children}</main>
<Footer />
</>
)
}
[Examples](#examples)
---------------------
### [Single Shared Layout with Custom App](#single-shared-layout-with-custom-app)
If you only have one layout for your entire application, you can create a [Custom App](/docs/pages/building-your-application/routing/custom-app) and wrap your application with the layout. Since the `<Layout />` component is re-used when changing pages, its component state will be preserved (e.g. input values).
pages/\_app.js
import Layout from '../components/layout'
export default function MyApp({ Component, pageProps }) {
return (
<Layout>
<Component {...pageProps} />
</Layout>
)
}
### [Per-Page Layouts](#per-page-layouts)
If you need multiple layouts, you can add a property `getLayout` to your page, allowing you to return a React component for the layout. This allows you to define the layout on a _per-page basis_. Since we're returning a function, we can have complex nested layouts if desired.
pages/index.js
import Layout from '../components/layout'
import NestedLayout from '../components/nested-layout'
export default function Page() {
return (
/** Your content */
)
}
Page.getLayout = function getLayout(page) {
return (
<Layout>
<NestedLayout>{page}</NestedLayout>
</Layout>
)
}
pages/\_app.js
export default function MyApp({ Component, pageProps }) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout ?? ((page) => page)
return getLayout(<Component {...pageProps} />)
}
When navigating between pages, we want to _persist_ page state (input values, scroll position, etc.) for a Single-Page Application (SPA) experience.
This layout pattern enables state persistence because the React component tree is maintained between page transitions. With the component tree, React can understand which elements have changed to preserve state.
> **Good to know**: This process is called [reconciliation](https://react.dev/learn/preserving-and-resetting-state), which is how React understands which elements have changed.
### [With TypeScript](#with-typescript)
When using TypeScript, you must first create a new type for your pages which includes a `getLayout` function. Then, you must create a new type for your `AppProps` which overrides the `Component` property to use the previously created type.
pages/index.tsx
TypeScript
JavaScriptTypeScript
import type { ReactElement } from 'react'
import Layout from '../components/layout'
import NestedLayout from '../components/nested-layout'
import type { NextPageWithLayout } from './_app'
const Page: NextPageWithLayout = () => {
return <p>hello world</p>
}
Page.getLayout = function getLayout(page: ReactElement) {
return (
<Layout>
<NestedLayout>{page}</NestedLayout>
</Layout>
)
}
export default Page
pages/\_app.tsx
TypeScript
JavaScriptTypeScript
import type { ReactElement, ReactNode } from 'react'
import type { NextPage } from 'next'
import type { AppProps } from 'next/app'
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (page: ReactElement) => ReactNode
}
type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout
}
export default function MyApp({ Component, pageProps }: AppPropsWithLayout) {
// Use the layout defined at the page level, if available
const getLayout = Component.getLayout ?? ((page) => page)
return getLayout(<Component {...pageProps} />)
}
### [Data Fetching](#data-fetching)
Inside your layout, you can fetch data on the client-side using `useEffect` or a library like [SWR](https://swr.vercel.app/). Because this file is not a [Page](/docs/pages/building-your-application/routing/pages-and-layouts), you cannot use `getStaticProps` or `getServerSideProps` currently.
components/layout.js
import useSWR from 'swr'
import Navbar from './navbar'
import Footer from './footer'
export default function Layout({ children }) {
const { data, error } = useSWR('/api/navigation', fetcher)
if (error) return <div>Failed to load</div>
if (!data) return <div>Loading...</div>
return (
<>
<Navbar links={data.links} />
<main>{children}</main>
<Footer />
</>
)
}
[Previous
Routing
](/docs/pages/building-your-application/routing)
[Next
Dynamic Routes
](/docs/pages/building-your-application/routing/dynamic-routes)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"cc7fddf8f40b46fc65c9776714e1fe74\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/exportPathMap | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [
exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Adding a trailing slash](#adding-a-trailing-slash)
* [Customizing the output directory](#customizing-the-output-directory)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/exportPathMap.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)exportPathMap
exportPathMap (Deprecated)
==========================
> This feature is exclusive to `next export` and currently **deprecated** in favor of `getStaticPaths` with `pages` or `generateStaticParams` with `app`.
Examples
* [Static Export](https://github.com/vercel/next.js/tree/canary/examples/with-static-export)
`exportPathMap` allows you to specify a mapping of request paths to page destinations, to be used during export. Paths defined in `exportPathMap` will also be available when using [`next dev`](/docs/app/api-reference/next-cli#development).
Let's start with an example, to create a custom `exportPathMap` for an app with the following pages:
* `pages/index.js`
* `pages/about.js`
* `pages/post.js`
Open `next.config.js` and add the following `exportPathMap` config:
next.config.js
module.exports = {
exportPathMap: async function (
defaultPathMap,
{ dev, dir, outDir, distDir, buildId }
) {
return {
'/': { page: '/' },
'/about': { page: '/about' },
'/p/hello-nextjs': { page: '/post', query: { title: 'hello-nextjs' } },
'/p/learn-nextjs': { page: '/post', query: { title: 'learn-nextjs' } },
'/p/deploy-nextjs': { page: '/post', query: { title: 'deploy-nextjs' } },
}
},
}
> **Good to know**: the `query` field in `exportPathMap` cannot be used with [automatically statically optimized pages](/docs/pages/building-your-application/rendering/automatic-static-optimization) or [`getStaticProps` pages](/docs/pages/building-your-application/data-fetching/get-static-props) as they are rendered to HTML files at build-time and additional query information cannot be provided during `next export`.
The pages will then be exported as HTML files, for example, `/about` will become `/about.html`.
`exportPathMap` is an `async` function that receives 2 arguments: the first one is `defaultPathMap`, which is the default map used by Next.js. The second argument is an object with:
* `dev` - `true` when `exportPathMap` is being called in development. `false` when running `next export`. In development `exportPathMap` is used to define routes.
* `dir` - Absolute path to the project directory
* `outDir` - Absolute path to the `out/` directory ([configurable with `-o`](#customizing-the-output-directory)). When `dev` is `true` the value of `outDir` will be `null`.
* `distDir` - Absolute path to the `.next/` directory (configurable with the [`distDir`](/docs/pages/api-reference/next-config-js/distDir) config)
* `buildId` - The generated build id
The returned object is a map of pages where the `key` is the `pathname` and the `value` is an object that accepts the following fields:
* `page`: `String` - the page inside the `pages` directory to render
* `query`: `Object` - the `query` object passed to `getInitialProps` when prerendering. Defaults to `{}`
> The exported `pathname` can also be a filename (for example, `/readme.md`), but you may need to set the `Content-Type` header to `text/html` when serving its content if it is different than `.html`.
[Adding a trailing slash](#adding-a-trailing-slash)
---------------------------------------------------
It is possible to configure Next.js to export pages as `index.html` files and require trailing slashes, `/about` becomes `/about/index.html` and is routable via `/about/`. This was the default behavior prior to Next.js 9.
To switch back and add a trailing slash, open `next.config.js` and enable the `trailingSlash` config:
next.config.js
module.exports = {
trailingSlash: true,
}
[Customizing the output directory](#customizing-the-output-directory)
---------------------------------------------------------------------
[`next export`](/docs/app/building-your-application/deploying/static-exports) will use `out` as the default output directory, you can customize this using the `-o` argument, like so:
Terminal
next export -o outdir
> **Warning**: Using `exportPathMap` is deprecated and is overridden by `getStaticPaths` inside `pages`. We don't recommend using them together.
[Previous
eslint
](/docs/app/api-reference/next-config-js/eslint)
[Next
generateBuildId
](/docs/app/api-reference/next-config-js/generateBuildId)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a99bb6628ccff989906a1b424f3ab9b8\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/pageExtensions | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [
pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/pageExtensions.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)pageExtensions
pageExtensions
==============
By default, Next.js accepts files with the following extensions: `.tsx`, `.ts`, `.jsx`, `.js`. This can be modified to allow other extensions like markdown (`.md`, `.mdx`).
next.config.js
const withMDX = require('@next/mdx')()
/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['ts', 'tsx', 'mdx'],
experimental: {
mdxRs: true,
},
}
module.exports = withMDX(nextConfig)
[Previous
output
](/docs/app/api-reference/next-config-js/output)
[Next
Partial Prerendering (experimental)
](/docs/app/api-reference/next-config-js/partial-prerendering)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9771db116dfbe9c0dfbadec5979a3da1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/redirecting | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [
Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [useRouter() hook](#userouter-hook)
* [redirects in next.config.js](#redirects-in-nextconfigjs)
* [NextResponse.redirect in Middleware](#nextresponseredirect-in-middleware)
* [Managing redirects at scale (advanced)](#managing-redirects-at-scale-advanced)
* [1\. Creating and storing a redirect map](#1-creating-and-storing-a-redirect-map)
* [2\. Optimizing data lookup performance](#2-optimizing-data-lookup-performance)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/04-redirecting.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Redirecting
Redirecting
===========
There are a few ways you can handle redirects in Next.js. This page will go through each available option, use cases, and how to manage large numbers of redirects.
API
Purpose
Where
Status Code
[`useRouter`](#userouter-hook)
Perform a client-side navigation
Components
N/A
[`redirects` in `next.config.js`](#redirects-in-nextconfigjs)
Redirect an incoming request based on a path
`next.config.js` file
307 (Temporary) or 308 (Permanent)
[`NextResponse.redirect`](#nextresponseredirect-in-middleware)
Redirect an incoming request based on a condition
Middleware
Any
[`useRouter()` hook](#userouter-hook)
-------------------------------------
If you need to redirect inside a component, you can use the `push` method from the `useRouter` hook. For example:
app/page.tsx
TypeScript
JavaScriptTypeScript
import { useRouter } from 'next/router'
export default function Page() {
const router = useRouter()
return (
<button type="button" onClick={() => router.push('/dashboard')}>
Dashboard
</button>
)
}
> **Good to know**:
>
> * If you don't need to programmatically navigate a user, you should use a [`<Link>`](/docs/app/api-reference/components/link) component.
See the [`useRouter` API reference](/docs/pages/api-reference/functions/use-router) for more information.
[`redirects` in `next.config.js`](#redirects-in-nextconfigjs)
-------------------------------------------------------------
The `redirects` option in the `next.config.js` file allows you to redirect an incoming request path to a different destination path. This is useful when you change the URL structure of pages or have a list of redirects that are known ahead of time.
`redirects` supports [path](/docs/app/api-reference/next-config-js/redirects#path-matching), [header, cookie, and query matching](/docs/app/api-reference/next-config-js/redirects#header-cookie-and-query-matching), giving you the flexibility to redirect users based on an incoming request.
To use `redirects`, add the option to your `next.config.js` file:
next.config.js
module.exports = {
async redirects() {
return [
// Basic redirect
{
source: '/about',
destination: '/',
permanent: true,
},
// Wildcard path matching
{
source: '/blog/:slug',
destination: '/news/:slug',
permanent: true,
},
]
},
}
See the [`redirects` API reference](/docs/app/api-reference/next-config-js/redirects) for more information.
> **Good to know**:
>
> * `redirects` can return a 307 (Temporary Redirect) or 308 (Permanent Redirect) status code with the `permanent` option.
> * `redirects` may have a limit on platforms. For example, on Vercel, there's a limit of 1,024 redirects. To manage a large number of redirects (1000+), consider creating a custom solution using [Middleware](/docs/app/building-your-application/routing/middleware). See [managing redirects at scale](#managing-redirects-at-scale-advanced) for more.
> * `redirects` runs **before** Middleware.
[`NextResponse.redirect` in Middleware](#nextresponseredirect-in-middleware)
----------------------------------------------------------------------------
Middleware allows you to run code before a request is completed. Then, based on the incoming request, redirect to a different URL using `NextResponse.redirect`. This is useful if you want to redirect users based on a condition (e.g. authentication, session management, etc) or have [a large number of redirects](#managing-redirects-at-scale-advanced).
For example, to redirect the user to a `/login` page if they are not authenticated:
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse, NextRequest } from 'next/server'
import { authenticate } from 'auth-provider'
export function middleware(request: NextRequest) {
const isAuthenticated = authenticate(request)
// If the user is authenticated, continue as normal
if (isAuthenticated) {
return NextResponse.next()
}
// Redirect to login page if not authenticated
return NextResponse.redirect(new URL('/login', request.url))
}
export const config = {
matcher: '/dashboard/:path*',
}
> **Good to know**:
>
> * Middleware runs **after** `redirects` in `next.config.js` and **before** rendering.
See the [Middleware](/docs/app/building-your-application/routing/middleware) documentation for more information.
[Managing redirects at scale (advanced)](#managing-redirects-at-scale-advanced)
-------------------------------------------------------------------------------
To manage a large number of redirects (1000+), you may consider creating a custom solution using Middleware. This allows you to handle redirects programmatically without having to redeploy your application.
To do this, you'll need to consider:
1. Creating and storing a redirect map.
2. Optimizing data lookup performance.
> **Next.js Example**: See our [Middleware with Bloom filter](https://redirects-bloom-filter.vercel.app/) example for an implementation of the recommendations below.
### [1\. Creating and storing a redirect map](#1-creating-and-storing-a-redirect-map)
A redirect map is a list of redirects that you can store in a database (usually a key-value store) or JSON file.
Consider the following data structure:
{
"/old": {
"destination": "/new",
"permanent": true
},
"/blog/post-old": {
"destination": "/blog/post-new",
"permanent": true
}
}
In [Middleware](/docs/app/building-your-application/routing/middleware), you can read from a database such as Vercel's [Edge Config](https://vercel.com/docs/storage/edge-config/get-started?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) or [Redis](https://vercel.com/docs/storage/vercel-kv?utm_source=next-site&utm_medium=docs&utm_campaign=next-website), and redirect the user based on the incoming request:
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse, NextRequest } from 'next/server'
import { get } from '@vercel/edge-config'
type RedirectEntry = {
destination: string
permanent: boolean
}
export async function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname
const redirectData = await get(pathname)
if (redirectData && typeof redirectData === 'string') {
const redirectEntry: RedirectEntry = JSON.parse(redirectData)
const statusCode = redirectEntry.permanent ? 308 : 307
return NextResponse.redirect(redirectEntry.destination, statusCode)
}
// No redirect found, continue without redirecting
return NextResponse.next()
}
### [2\. Optimizing data lookup performance](#2-optimizing-data-lookup-performance)
Reading a large dataset for every incoming request can be slow and expensive. There are two ways you can optimize data lookup performance:
* Use a database that is optimized for fast reads, such as [Vercel Edge Config](https://vercel.com/docs/storage/edge-config/get-started?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) or [Redis](https://vercel.com/docs/storage/vercel-kv?utm_source=next-site&utm_medium=docs&utm_campaign=next-website).
* Use a data lookup strategy such as a [Bloom filter](https://en.wikipedia.org/wiki/Bloom_filter) to efficiently check if a redirect exists **before** reading the larger redirects file or database.
Considering the previous example, you can import a generated bloom filter file into Middleware, then, check if the incoming request pathname exists in the bloom filter.
If it does, forward the request to a [API Routes](/docs/pages/building-your-application/routing/api-routes) which will check the actual file and redirect the user to the appropriate URL. This avoids importing a large redirects file into Middleware, which can slow down every incoming request.
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse, NextRequest } from 'next/server'
import { ScalableBloomFilter } from 'bloom-filters'
import GeneratedBloomFilter from './redirects/bloom-filter.json'
type RedirectEntry = {
destination: string
permanent: boolean
}
// Initialize bloom filter from a generated JSON file
const bloomFilter = ScalableBloomFilter.fromJSON(GeneratedBloomFilter as any)
export async function middleware(request: NextRequest) {
// Get the path for the incoming request
const pathname = request.nextUrl.pathname
// Check if the path is in the bloom filter
if (bloomFilter.has(pathname)) {
// Forward the pathname to the Route Handler
const api = new URL(
`/api/redirects?pathname=${encodeURIComponent(request.nextUrl.pathname)}`,
request.nextUrl.origin
)
try {
// Fetch redirect data from the Route Handler
const redirectData = await fetch(api)
if (redirectData.ok) {
const redirectEntry: RedirectEntry | undefined =
await redirectData.json()
if (redirectEntry) {
// Determine the status code
const statusCode = redirectEntry.permanent ? 308 : 307
// Redirect to the destination
return NextResponse.redirect(redirectEntry.destination, statusCode)
}
}
} catch (error) {
console.error(error)
}
}
// No redirect found, continue the request without redirecting
return NextResponse.next()
}
Then, in the API Route:
pages/api/redirects.ts
TypeScript
JavaScriptTypeScript
import { NextApiRequest, NextApiResponse } from 'next'
import redirects from '@/app/redirects/redirects.json'
type RedirectEntry = {
destination: string
permanent: boolean
}
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const pathname = req.query.pathname
if (!pathname) {
return res.status(400).json({ message: 'Bad Request' })
}
// Get the redirect entry from the redirects.json file
const redirect = (redirects as Record<string, RedirectEntry>)[pathname]
// Account for bloom filter false positives
if (!redirect) {
return res.status(400).json({ message: 'No redirect' })
}
// Return the redirect entry
return res.json(redirect)
}
> **Good to know:**
>
> * To generate a bloom filter, you can use a library like [`bloom-filters`](https://www.npmjs.com/package/bloom-filters).
> * You should validate requests made to your Route Handler to prevent malicious requests.
[Previous
Linking and Navigating
](/docs/pages/building-your-application/routing/linking-and-navigating)
[Next
Custom App
](/docs/pages/building-your-application/routing/custom-app)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"3a348e8be8311783f9f99a0c35808281\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/partial-prerendering | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [
Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/partial-prerendering.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)Partial Prerendering (experimental)
Partial Prerendering (experimental)
===================================
> **Warning**: Partial Prerendering is an experimental feature and is currently **not suitable for production environments**.
Partial Prerendering is an experimental feature that allows static portions of a route to be prerendered and served from the cache with dynamic holes streamed in, all in a single HTTP request.
Partial Prerendering is available in `next@canary`:
Terminal
npm install next@canary
You can enable Partial Prerendering by setting the experimental `ppr` flag:
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
ppr: true,
},
}
module.exports = nextConfig
> **Good to know:**
>
> * Partial Prerendering does not yet apply to client-side navigations. We are actively working on this.
> * Partial Prerendering is designed for the [Node.js runtime](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes) only. Using the subset of the Node.js runtime is not needed when you can instantly serve the static shell.
Learn more about Partial Prerendering in the [Next.js Learn course](/learn/dashboard-app/partial-prerendering).
[Previous
pageExtensions
](/docs/app/api-reference/next-config-js/pageExtensions)
[Next
poweredByHeader
](/docs/app/api-reference/next-config-js/poweredByHeader)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"5faa6e3aa9775118fcdd217c4e117c41\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/linking-and-navigating | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [
Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Linking to dynamic paths](#linking-to-dynamic-paths)
* [Injecting the router](#injecting-the-router)
* [Imperative Routing](#imperative-routing)
* [Shallow Routing](#shallow-routing)
* [Caveats](#caveats)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/03-linking-and-navigating.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Linking and Navigating
Linking and Navigating
======================
The Next.js router allows you to do client-side route transitions between pages, similar to a single-page application.
A React component called `Link` is provided to do this client-side route transition.
import Link from 'next/link'
function Home() {
return (
<ul>
<li>
<Link href="/">Home</Link>
</li>
<li>
<Link href="/about">About Us</Link>
</li>
<li>
<Link href="/blog/hello-world">Blog Post</Link>
</li>
</ul>
)
}
export default Home
The example above uses multiple links. Each one maps a path (`href`) to a known page:
* `/` → `pages/index.js`
* `/about` → `pages/about.js`
* `/blog/hello-world` → `pages/blog/[slug].js`
Any `<Link />` in the viewport (initially or through scroll) will be prefetched by default (including the corresponding data) for pages using [Static Generation](/docs/pages/building-your-application/data-fetching/get-static-props). The corresponding data for [server-rendered](/docs/pages/building-your-application/data-fetching/get-server-side-props) routes is fetched _only when_ the `<Link />` is clicked.
[Linking to dynamic paths](#linking-to-dynamic-paths)
-----------------------------------------------------
You can also use interpolation to create the path, which comes in handy for [dynamic route segments](/docs/pages/building-your-application/routing/dynamic-routes). For example, to show a list of posts which have been passed to the component as a prop:
import Link from 'next/link'
function Posts({ posts }) {
return (
<ul>
{posts.map((post) => (
<li key={post.id}>
<Link href={`/blog/${encodeURIComponent(post.slug)}`}>
{post.title}
</Link>
</li>
))}
</ul>
)
}
export default Posts
> [`encodeURIComponent`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent) is used in the example to keep the path utf-8 compatible.
Alternatively, using a URL Object:
import Link from 'next/link'
function Posts({ posts }) {
return (
<ul>
{posts.map((post) => (
<li key={post.id}>
<Link
href={{
pathname: '/blog/[slug]',
query: { slug: post.slug },
}}
>
{post.title}
</Link>
</li>
))}
</ul>
)
}
export default Posts
Now, instead of using interpolation to create the path, we use a URL object in `href` where:
* `pathname` is the name of the page in the `pages` directory. `/blog/[slug]` in this case.
* `query` is an object with the dynamic segment. `slug` in this case.
[Injecting the router](#injecting-the-router)
---------------------------------------------
Examples
* [Dynamic Routing](https://github.com/vercel/next.js/tree/canary/examples/dynamic-routing)
To access the [`router` object](/docs/pages/api-reference/functions/use-router#router-object) in a React component you can use [`useRouter`](/docs/pages/api-reference/functions/use-router) or [`withRouter`](/docs/pages/api-reference/functions/use-router#withrouter).
In general we recommend using [`useRouter`](/docs/pages/api-reference/functions/use-router).
[Imperative Routing](#imperative-routing)
-----------------------------------------
[`next/link`](/docs/pages/api-reference/components/link) should be able to cover most of your routing needs, but you can also do client-side navigations without it, take a look at the [documentation for `next/router`](/docs/pages/api-reference/functions/use-router).
The following example shows how to do basic page navigations with [`useRouter`](/docs/pages/api-reference/functions/use-router):
import { useRouter } from 'next/router'
export default function ReadMore() {
const router = useRouter()
return (
<button onClick={() => router.push('/about')}>
Click here to read more
</button>
)
}
[Shallow Routing](#shallow-routing)
-----------------------------------
Examples
* [Shallow Routing](https://github.com/vercel/next.js/tree/canary/examples/with-shallow-routing)
Shallow routing allows you to change the URL without running data fetching methods again, that includes [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props), [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props), and [`getInitialProps`](/docs/pages/api-reference/functions/get-initial-props).
You'll receive the updated `pathname` and the `query` via the [`router` object](/docs/pages/api-reference/functions/use-router#router-object) (added by [`useRouter`](/docs/pages/api-reference/functions/use-router) or [`withRouter`](/docs/pages/api-reference/functions/use-router#withrouter)), without losing state.
To enable shallow routing, set the `shallow` option to `true`. Consider the following example:
import { useEffect } from 'react'
import { useRouter } from 'next/router'
// Current URL is '/'
function Page() {
const router = useRouter()
useEffect(() => {
// Always do navigations after the first render
router.push('/?counter=10', undefined, { shallow: true })
}, [])
useEffect(() => {
// The counter changed!
}, [router.query.counter])
}
export default Page
The URL will get updated to `/?counter=10` and the page won't get replaced, only the state of the route is changed.
You can also watch for URL changes via [`componentDidUpdate`](https://react.dev/reference/react/Component#componentdidupdate) as shown below:
componentDidUpdate(prevProps) {
const { pathname, query } = this.props.router
// verify props have changed to avoid an infinite loop
if (query.counter !== prevProps.router.query.counter) {
// fetch data based on the new query
}
}
### [Caveats](#caveats)
Shallow routing **only** works for URL changes in the current page. For example, let's assume we have another page called `pages/about.js`, and you run this:
router.push('/?counter=10', '/about?counter=10', { shallow: true })
Since that's a new page, it'll unload the current page, load the new one and wait for data fetching even though we asked to do shallow routing.
When shallow routing is used with middleware it will not ensure the new page matches the current page like previously done without middleware. This is due to middleware being able to rewrite dynamically and can't be verified client-side without a data fetch which is skipped with shallow, so a shallow route change must always be treated as shallow.
[Previous
Dynamic Routes
](/docs/pages/building-your-application/routing/dynamic-routes)
[Next
Redirecting
](/docs/pages/building-your-application/routing/redirecting)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"17b3810af8f51acbe2f74784091105f4\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/dynamic-routes | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [
Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Convention](#convention)
* [Example](#example)
* [Catch-all Segments](#catch-all-segments)
* [Optional Catch-all Segments](#optional-catch-all-segments)
* [Next Steps](#next-steps)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/02-dynamic-routes.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Dynamic Routes
Dynamic Routes
==============
When you don't know the exact segment names ahead of time and want to create routes from dynamic data, you can use Dynamic Segments that are filled in at request time or [prerendered](/docs/pages/building-your-application/data-fetching/get-static-paths) at build time.
[Convention](#convention)
-------------------------
A Dynamic Segment can be created by wrapping a file or folder name in square brackets: `[segmentName]`. For example, `[id]` or `[slug]`.
Dynamic Segments can be accessed from [`useRouter`](/docs/pages/api-reference/functions/use-router).
[Example](#example)
-------------------
For example, a blog could include the following route `pages/blog/[slug].js` where `[slug]` is the Dynamic Segment for blog posts.
import { useRouter } from 'next/router'
export default function Page() {
const router = useRouter()
return <p>Post: {router.query.slug}</p>
}
Route
Example URL
`params`
`pages/blog/[slug].js`
`/blog/a`
`{ slug: 'a' }`
`pages/blog/[slug].js`
`/blog/b`
`{ slug: 'b' }`
`pages/blog/[slug].js`
`/blog/c`
`{ slug: 'c' }`
[Catch-all Segments](#catch-all-segments)
-----------------------------------------
Dynamic Segments can be extended to **catch-all** subsequent segments by adding an ellipsis inside the brackets `[...segmentName]`.
For example, `pages/shop/[...slug].js` will match `/shop/clothes`, but also `/shop/clothes/tops`, `/shop/clothes/tops/t-shirts`, and so on.
Route
Example URL
`params`
`pages/shop/[...slug].js`
`/shop/a`
`{ slug: ['a'] }`
`pages/shop/[...slug].js`
`/shop/a/b`
`{ slug: ['a', 'b'] }`
`pages/shop/[...slug].js`
`/shop/a/b/c`
`{ slug: ['a', 'b', 'c'] }`
[Optional Catch-all Segments](#optional-catch-all-segments)
-----------------------------------------------------------
Catch-all Segments can be made **optional** by including the parameter in double square brackets: `[[...segmentName]]`.
For example, `pages/shop/[[...slug]].js` will **also** match `/shop`, in addition to `/shop/clothes`, `/shop/clothes/tops`, `/shop/clothes/tops/t-shirts`.
The difference between **catch-all** and **optional catch-all** segments is that with optional, the route without the parameter is also matched (`/shop` in the example above).
Route
Example URL
`params`
`pages/shop/[[...slug]].js`
`/shop`
`{ slug: [] }`
`pages/shop/[[...slug]].js`
`/shop/a`
`{ slug: ['a'] }`
`pages/shop/[[...slug]].js`
`/shop/a/b`
`{ slug: ['a', 'b'] }`
`pages/shop/[[...slug]].js`
`/shop/a/b/c`
`{ slug: ['a', 'b', 'c'] }`
Next Steps
----------
For more information on what to do next, we recommend the following sections
[
Pages Router
...
Routing
### Linking and Navigating
Learn how navigation works in Next.js, and how to use the Link Component and \`useRouter\` hook.
](/docs/pages/building-your-application/routing/linking-and-navigating)[
Pages Router
...
Functions
### useRouter
Learn more about the API of the Next.js Router, and access the router instance in your page with the useRouter hook.
](/docs/pages/api-reference/functions/use-router)
[Previous
Pages and Layouts
](/docs/pages/building-your-application/routing/pages-and-layouts)
[Next
Linking and Navigating
](/docs/pages/building-your-application/routing/linking-and-navigating)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"74f86bafe012270e64075abf3a5a260e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/generateBuildId | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [
generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/generateBuildId.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)generateBuildId
generateBuildId
===============
Next.js generates an ID during `next build` to identify which version of your application is being served. The same build should be used and boot up multiple containers.
If you are rebuilding for each stage of your environment, you will need to generate a consistent build ID to use between containers. Use the `generateBuildId` command in `next.config.js`:
next.config.js
module.exports = {
generateBuildId: async () => {
// This could be anything, using the latest git hash
return process.env.GIT_HASH
},
}
[Previous
exportPathMap
](/docs/app/api-reference/next-config-js/exportPathMap)
[Next
generateEtags
](/docs/app/api-reference/next-config-js/generateEtags)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"7122bf4ddf69b5bee8154a69dc3f8e21\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/generateEtags | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [
generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/generateEtags.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)generateEtags
generateEtags
=============
Next.js will generate [etags](https://en.wikipedia.org/wiki/HTTP_ETag) for every page by default. You may want to disable etag generation for HTML pages depending on your cache strategy.
Open `next.config.js` and disable the `generateEtags` option:
next.config.js
module.exports = {
generateEtags: false,
}
[Previous
generateBuildId
](/docs/app/api-reference/next-config-js/generateBuildId)
[Next
headers
](/docs/app/api-reference/next-config-js/headers)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"1cabaf7f795cdb3402cda8e83a1e50fb\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/productionBrowserSourceMaps | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [
productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/productionBrowserSourceMaps.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)productionBrowserSourceMaps
productionBrowserSourceMaps
===========================
Source Maps are enabled by default during development. During production builds, they are disabled to prevent you leaking your source on the client, unless you specifically opt-in with the configuration flag.
Next.js provides a configuration flag you can use to enable browser source map generation during the production build:
next.config.js
module.exports = {
productionBrowserSourceMaps: true,
}
When the `productionBrowserSourceMaps` option is enabled, the source maps will be output in the same directory as the JavaScript files. Next.js will automatically serve these files when requested.
* Adding source maps can increase `next build` time
* Increases memory usage during `next build`
[Previous
poweredByHeader
](/docs/app/api-reference/next-config-js/poweredByHeader)
[Next
reactStrictMode
](/docs/app/api-reference/next-config-js/reactStrictMode)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"c30f543d6907044e6919051f7d4a1bdd\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/custom-app | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [
Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Usage](#usage)
* [getInitialProps with App](#getinitialprops-with-app)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/05-custom-app.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Custom App
Custom App
==========
Next.js uses the `App` component to initialize pages. You can override it and control the page initialization and:
* Create a shared layout between page changes
* Inject additional data into pages
* [Add global CSS](/docs/pages/building-your-application/styling)
[Usage](#usage)
---------------
To override the default `App`, create the file `pages/_app` as shown below:
pages/\_app.tsx
TypeScript
JavaScriptTypeScript
import type { AppProps } from 'next/app'
export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
The `Component` prop is the active `page`, so whenever you navigate between routes, `Component` will change to the new `page`. Therefore, any props you send to `Component` will be received by the `page`.
`pageProps` is an object with the initial props that were preloaded for your page by one of our [data fetching methods](/docs/pages/building-your-application/data-fetching), otherwise it's an empty object.
> **Good to know**
>
> * If your app is running and you added a custom `App`, you'll need to restart the development server. Only required if `pages/_app.js` didn't exist before.
> * `App` does not support Next.js [Data Fetching methods](/docs/pages/building-your-application/data-fetching) like [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) or [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props).
[`getInitialProps` with `App`](#getinitialprops-with-app)
---------------------------------------------------------
Using [`getInitialProps`](/docs/pages/api-reference/functions/get-initial-props) in `App` will disable [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization) for pages without [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props).
**We do not recommend using this pattern.** Instead, consider [incrementally adopting](/docs/app/building-your-application/upgrading/app-router-migration) the App Router, which allows you to more easily fetch data for [pages and layouts](/docs/app/building-your-application/routing/pages-and-layouts).
pages/\_app.tsx
TypeScript
JavaScriptTypeScript
import App, { AppContext, AppInitialProps, AppProps } from 'next/app'
type AppOwnProps = { example: string }
export default function MyApp({
Component,
pageProps,
example,
}: AppProps & AppOwnProps) {
return (
<>
<p>Data: {example}</p>
<Component {...pageProps} />
</>
)
}
MyApp.getInitialProps = async (
context: AppContext
): Promise<AppOwnProps & AppInitialProps> => {
const ctx = await App.getInitialProps(context)
return { ...ctx, example: 'data' }
}
[Previous
Redirecting
](/docs/pages/building-your-application/routing/redirecting)
[Next
Custom Document
](/docs/pages/building-your-application/routing/custom-document)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"13558b1ab5d2eb6f64efb90b3407d25f\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/headers | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [
headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Header Overriding Behavior](#header-overriding-behavior)
* [Path Matching](#path-matching)
* [Wildcard Path Matching](#wildcard-path-matching)
* [Regex Path Matching](#regex-path-matching)
* [Header, Cookie, and Query Matching](#header-cookie-and-query-matching)
* [Headers with basePath support](#headers-with-basepath-support)
* [Headers with i18n support](#headers-with-i18n-support)
* [Cache-Control](#cache-control)
* [Options](#options)
* [CORS](#cors)
* [X-DNS-Prefetch-Control](#x-dns-prefetch-control)
* [Strict-Transport-Security](#strict-transport-security)
* [X-Frame-Options](#x-frame-options)
* [Permissions-Policy](#permissions-policy)
* [X-Content-Type-Options](#x-content-type-options)
* [Referrer-Policy](#referrer-policy)
* [Content-Security-Policy](#content-security-policy)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/headers.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)headers
headers
=======
Headers allow you to set custom HTTP headers on the response to an incoming request on a given path.
To set custom HTTP headers you can use the `headers` key in `next.config.js`:
next.config.js
module.exports = {
async headers() {
return [
{
source: '/about',
headers: [
{
key: 'x-custom-header',
value: 'my custom header value',
},
{
key: 'x-another-custom-header',
value: 'my other custom header value',
},
],
},
]
},
}
`headers` is an async function that expects an array to be returned holding objects with `source` and `headers` properties:
* `source` is the incoming request path pattern.
* `headers` is an array of response header objects, with `key` and `value` properties.
* `basePath`: `false` or `undefined` - if false the basePath won't be included when matching, can be used for external rewrites only.
* `locale`: `false` or `undefined` - whether the locale should not be included when matching.
* `has` is an array of [has objects](#header-cookie-and-query-matching) with the `type`, `key` and `value` properties.
* `missing` is an array of [missing objects](#header-cookie-and-query-matching) with the `type`, `key` and `value` properties.
Headers are checked before the filesystem which includes pages and `/public` files.
[Header Overriding Behavior](#header-overriding-behavior)
---------------------------------------------------------
If two headers match the same path and set the same header key, the last header key will override the first. Using the below headers, the path `/hello` will result in the header `x-hello` being `world` due to the last header value set being `world`.
next.config.js
module.exports = {
async headers() {
return [
{
source: '/:path*',
headers: [
{
key: 'x-hello',
value: 'there',
},
],
},
{
source: '/hello',
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
]
},
}
[Path Matching](#path-matching)
-------------------------------
Path matches are allowed, for example `/blog/:slug` will match `/blog/hello-world` (no nested paths):
next.config.js
module.exports = {
async headers() {
return [
{
source: '/blog/:slug',
headers: [
{
key: 'x-slug',
value: ':slug', // Matched parameters can be used in the value
},
{
key: 'x-slug-:slug', // Matched parameters can be used in the key
value: 'my other custom header value',
},
],
},
]
},
}
### [Wildcard Path Matching](#wildcard-path-matching)
To match a wildcard path you can use `*` after a parameter, for example `/blog/:slug*` will match `/blog/a/b/c/d/hello-world`:
next.config.js
module.exports = {
async headers() {
return [
{
source: '/blog/:slug*',
headers: [
{
key: 'x-slug',
value: ':slug*', // Matched parameters can be used in the value
},
{
key: 'x-slug-:slug*', // Matched parameters can be used in the key
value: 'my other custom header value',
},
],
},
]
},
}
### [Regex Path Matching](#regex-path-matching)
To match a regex path you can wrap the regex in parenthesis after a parameter, for example `/blog/:slug(\\d{1,})` will match `/blog/123` but not `/blog/abc`:
next.config.js
module.exports = {
async headers() {
return [
{
source: '/blog/:post(\\d{1,})',
headers: [
{
key: 'x-post',
value: ':post',
},
],
},
]
},
}
The following characters `(`, `)`, `{`, `}`, `:`, `*`, `+`, `?` are used for regex path matching, so when used in the `source` as non-special values they must be escaped by adding `\\` before them:
next.config.js
module.exports = {
async headers() {
return [
{
// this will match `/english(default)/something` being requested
source: '/english\\(default\\)/:slug',
headers: [
{
key: 'x-header',
value: 'value',
},
],
},
]
},
}
[Header, Cookie, and Query Matching](#header-cookie-and-query-matching)
-----------------------------------------------------------------------
To only apply a header when header, cookie, or query values also match the `has` field or don't match the `missing` field can be used. Both the `source` and all `has` items must match and all `missing` items must not match for the header to be applied.
`has` and `missing` items can have the following fields:
* `type`: `String` - must be either `header`, `cookie`, `host`, or `query`.
* `key`: `String` - the key from the selected type to match against.
* `value`: `String` or `undefined` - the value to check for, if undefined any value will match. A regex like string can be used to capture a specific part of the value, e.g. if the value `first-(?<paramName>.*)` is used for `first-second` then `second` will be usable in the destination with `:paramName`.
next.config.js
module.exports = {
async headers() {
return [
// if the header `x-add-header` is present,
// the `x-another-header` header will be applied
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-add-header',
},
],
headers: [
{
key: 'x-another-header',
value: 'hello',
},
],
},
// if the header `x-no-header` is not present,
// the `x-another-header` header will be applied
{
source: '/:path*',
missing: [
{
type: 'header',
key: 'x-no-header',
},
],
headers: [
{
key: 'x-another-header',
value: 'hello',
},
],
},
// if the source, query, and cookie are matched,
// the `x-authorized` header will be applied
{
source: '/specific/:path*',
has: [
{
type: 'query',
key: 'page',
// the page value will not be available in the
// header key/values since value is provided and
// doesn't use a named capture group e.g. (?<page>home)
value: 'home',
},
{
type: 'cookie',
key: 'authorized',
value: 'true',
},
],
headers: [
{
key: 'x-authorized',
value: ':authorized',
},
],
},
// if the header `x-authorized` is present and
// contains a matching value, the `x-another-header` will be applied
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-authorized',
value: '(?<authorized>yes|true)',
},
],
headers: [
{
key: 'x-another-header',
value: ':authorized',
},
],
},
// if the host is `example.com`,
// this header will be applied
{
source: '/:path*',
has: [
{
type: 'host',
value: 'example.com',
},
],
headers: [
{
key: 'x-another-header',
value: ':authorized',
},
],
},
]
},
}
[Headers with basePath support](#headers-with-basepath-support)
---------------------------------------------------------------
When leveraging [`basePath` support](/docs/app/api-reference/next-config-js/basePath) with headers each `source` is automatically prefixed with the `basePath` unless you add `basePath: false` to the header:
next.config.js
module.exports = {
basePath: '/docs',
async headers() {
return [
{
source: '/with-basePath', // becomes /docs/with-basePath
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
{
source: '/without-basePath', // is not modified since basePath: false is set
headers: [
{
key: 'x-hello',
value: 'world',
},
],
basePath: false,
},
]
},
}
[Headers with i18n support](#headers-with-i18n-support)
-------------------------------------------------------
When leveraging [`i18n` support](/docs/app/building-your-application/routing/internationalization) with headers each `source` is automatically prefixed to handle the configured `locales` unless you add `locale: false` to the header. If `locale: false` is used you must prefix the `source` with a locale for it to be matched correctly.
next.config.js
module.exports = {
i18n: {
locales: ['en', 'fr', 'de'],
defaultLocale: 'en',
},
async headers() {
return [
{
source: '/with-locale', // automatically handles all locales
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
{
// does not handle locales automatically since locale: false is set
source: '/nl/with-locale-manual',
locale: false,
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
{
// this matches '/' since `en` is the defaultLocale
source: '/en',
locale: false,
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
{
// this gets converted to /(en|fr|de)/(.*) so will not match the top-level
// `/` or `/fr` routes like /:path* would
source: '/(.*)',
headers: [
{
key: 'x-hello',
value: 'world',
},
],
},
]
},
}
[Cache-Control](#cache-control)
-------------------------------
You cannot set `Cache-Control` headers in `next.config.js` for pages or assets, as these headers will be overwritten in production to ensure that responses and static assets are cached effectively.
Learn more about [caching](/docs/app/building-your-application/caching) with the App Router.
[Options](#options)
-------------------
### [CORS](#cors)
[Cross-Origin Resource Sharing (CORS)](https://developer.mozilla.org/docs/Web/HTTP/CORS) is a security feature that allows you to control which sites can access your resources. You can set the `Access-Control-Allow-Origin` header to allow a specific origin to access your Route Handlers.
async headers() {
return [
{
source: "/api/:path*",
headers: [
{
key: "Access-Control-Allow-Origin",
value: "*", // Set your origin
},
{
key: "Access-Control-Allow-Methods",
value: "GET, POST, PUT, DELETE, OPTIONS",
},
{
key: "Access-Control-Allow-Headers",
value: "Content-Type, Authorization",
},
],
},
];
},
### [X-DNS-Prefetch-Control](#x-dns-prefetch-control)
[This header](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control) controls DNS prefetching, allowing browsers to proactively perform domain name resolution on external links, images, CSS, JavaScript, and more. This prefetching is performed in the background, so the [DNS](https://developer.mozilla.org/docs/Glossary/DNS) is more likely to be resolved by the time the referenced items are needed. This reduces latency when the user clicks a link.
{
key: 'X-DNS-Prefetch-Control',
value: 'on'
}
### [Strict-Transport-Security](#strict-transport-security)
[This header](https://developer.mozilla.org/docs/Web/HTTP/Headers/Strict-Transport-Security) informs browsers it should only be accessed using HTTPS, instead of using HTTP. Using the configuration below, all present and future subdomains will use HTTPS for a `max-age` of 2 years. This blocks access to pages or subdomains that can only be served over HTTP.
If you're deploying to [Vercel](https://vercel.com/docs/concepts/edge-network/headers#strict-transport-security?utm_source=next-site&utm_medium=docs&utm_campaign=next-website), this header is not necessary as it's automatically added to all deployments unless you declare `headers` in your `next.config.js`.
{
key: 'Strict-Transport-Security',
value: 'max-age=63072000; includeSubDomains; preload'
}
### [X-Frame-Options](#x-frame-options)
[This header](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Frame-Options) indicates whether the site should be allowed to be displayed within an `iframe`. This can prevent against clickjacking attacks.
**This header has been superseded by CSP's `frame-ancestors` option**, which has better support in modern browsers (see [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy) for configuration details).
{
key: 'X-Frame-Options',
value: 'SAMEORIGIN'
}
### [Permissions-Policy](#permissions-policy)
[This header](https://developer.mozilla.org/docs/Web/HTTP/Headers/Permissions-Policy) allows you to control which features and APIs can be used in the browser. It was previously named `Feature-Policy`.
{
key: 'Permissions-Policy',
value: 'camera=(), microphone=(), geolocation=(), browsing-topics=()'
}
### [X-Content-Type-Options](#x-content-type-options)
[This header](https://developer.mozilla.org/docs/Web/HTTP/Headers/X-Content-Type-Options) prevents the browser from attempting to guess the type of content if the `Content-Type` header is not explicitly set. This can prevent XSS exploits for websites that allow users to upload and share files.
For example, a user trying to download an image, but having it treated as a different `Content-Type` like an executable, which could be malicious. This header also applies to downloading browser extensions. The only valid value for this header is `nosniff`.
{
key: 'X-Content-Type-Options',
value: 'nosniff'
}
### [Referrer-Policy](#referrer-policy)
[This header](https://developer.mozilla.org/docs/Web/HTTP/Headers/Referrer-Policy) controls how much information the browser includes when navigating from the current website (origin) to another.
{
key: 'Referrer-Policy',
value: 'origin-when-cross-origin'
}
### [Content-Security-Policy](#content-security-policy)
Learn more about adding a [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy) to your application.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
`missing` added.
`v10.2.0`
`has` added.
`v9.5.0`
Headers added.
[Previous
generateEtags
](/docs/app/api-reference/next-config-js/generateEtags)
[Next
httpAgentOptions
](/docs/app/api-reference/next-config-js/httpAgentOptions)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"84e90f9675402bbe340ddc55b58fc292\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/httpAgentOptions | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [
httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/httpAgentOptions.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)httpAgentOptions
httpAgentOptions
================
In Node.js versions prior to 18, Next.js automatically polyfills `fetch()` with [undici](/docs/architecture/supported-browsers#polyfills) and enables [HTTP Keep-Alive](https://developer.mozilla.org/docs/Web/HTTP/Headers/Keep-Alive) by default.
To disable HTTP Keep-Alive for all `fetch()` calls on the server-side, open `next.config.js` and add the `httpAgentOptions` config:
next.config.js
module.exports = {
httpAgentOptions: {
keepAlive: false,
},
}
[Previous
headers
](/docs/app/api-reference/next-config-js/headers)
[Next
images
](/docs/app/api-reference/next-config-js/images)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a7a00f09e8b9744596d3f80ab26abd85\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/serverActions | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [
serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [allowedOrigins](#allowedorigins)
* [bodySizeLimit](#bodysizelimit)
* [Enabling Server Actions (v13)](#enabling-server-actions-v13)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/serverActions.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)serverActions
serverActions
=============
Options for configuring Server Actions behavior in your Next.js application.
[`allowedOrigins`](#allowedorigins)
-----------------------------------
A list of extra safe origin domains from which Server Actions can be invoked. Next.js compares the origin of a Server Action request with the host domain, ensuring they match to prevent CSRF attacks. If not provided, only the same origin is allowed.
next.config.js
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
serverActions: {
allowedOrigins: ['my-proxy.com', '*.my-proxy.com'],
},
},
}
[`bodySizeLimit`](#bodysizelimit)
---------------------------------
By default, the maximum size of the request body sent to a Server Action is 1MB, to prevent the consumption of excessive server resources in parsing large amounts of data, as well as potential DDoS attacks.
However, you can configure this limit using the `serverActions.bodySizeLimit` option. It can take the number of bytes or any string format supported by bytes, for example `1000`, `'500kb'` or `'3mb'`.
next.config.js
/** @type {import('next').NextConfig} */
module.exports = {
experimental: {
serverActions: {
bodySizeLimit: '2mb',
},
},
}
[Enabling Server Actions (v13)](#enabling-server-actions-v13)
-------------------------------------------------------------
Server Actions became a stable feature in Next.js 14, and are enabled by default. However, if you are using an earlier version of Next.js, you can enable them by setting `experimental.serverActions` to `true`.
next.config.js
/** @type {import('next').NextConfig} */
const config = {
experimental: {
serverActions: true,
},
}
module.exports = config
[Previous
rewrites
](/docs/app/api-reference/next-config-js/rewrites)
[Next
serverComponentsExternalPackages
](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"685211e11a47c92d9459d18117483f4e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/rewrites | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [
rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Rewrite parameters](#rewrite-parameters)
* [Path Matching](#path-matching)
* [Wildcard Path Matching](#wildcard-path-matching)
* [Regex Path Matching](#regex-path-matching)
* [Header, Cookie, and Query Matching](#header-cookie-and-query-matching)
* [Rewriting to an external URL](#rewriting-to-an-external-url)
* [Incremental adoption of Next.js](#incremental-adoption-of-nextjs)
* [Rewrites with basePath support](#rewrites-with-basepath-support)
* [Rewrites with i18n support](#rewrites-with-i18n-support)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/rewrites.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)rewrites
rewrites
========
Rewrites allow you to map an incoming request path to a different destination path.
Rewrites act as a URL proxy and mask the destination path, making it appear the user hasn't changed their location on the site. In contrast, [redirects](/docs/app/api-reference/next-config-js/redirects) will reroute to a new page and show the URL changes.
To use rewrites you can use the `rewrites` key in `next.config.js`:
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/about',
destination: '/',
},
]
},
}
Rewrites are applied to client-side routing, a `<Link href="/about">` will have the rewrite applied in the above example.
`rewrites` is an async function that expects to return either an array or an object of arrays (see below) holding objects with `source` and `destination` properties:
* `source`: `String` - is the incoming request path pattern.
* `destination`: `String` is the path you want to route to.
* `basePath`: `false` or `undefined` - if false the basePath won't be included when matching, can be used for external rewrites only.
* `locale`: `false` or `undefined` - whether the locale should not be included when matching.
* `has` is an array of [has objects](#header-cookie-and-query-matching) with the `type`, `key` and `value` properties.
* `missing` is an array of [missing objects](#header-cookie-and-query-matching) with the `type`, `key` and `value` properties.
When the `rewrites` function returns an array, rewrites are applied after checking the filesystem (pages and `/public` files) and before dynamic routes. When the `rewrites` function returns an object of arrays with a specific shape, this behavior can be changed and more finely controlled, as of `v10.1` of Next.js:
next.config.js
module.exports = {
async rewrites() {
return {
beforeFiles: [
// These rewrites are checked after headers/redirects
// and before all files including _next/public files which
// allows overriding page files
{
source: '/some-page',
destination: '/somewhere-else',
has: [{ type: 'query', key: 'overrideMe' }],
},
],
afterFiles: [
// These rewrites are checked after pages/public files
// are checked but before dynamic routes
{
source: '/non-existent',
destination: '/somewhere-else',
},
],
fallback: [
// These rewrites are checked after both pages/public files
// and dynamic routes are checked
{
source: '/:path*',
destination: `https://my-old-site.com/:path*`,
},
],
}
},
}
> **Good to know**: rewrites in `beforeFiles` do not check the filesystem/dynamic routes immediately after matching a source, they continue until all `beforeFiles` have been checked.
The order Next.js routes are checked is:
1. [headers](/docs/app/api-reference/next-config-js/headers) are checked/applied
2. [redirects](/docs/app/api-reference/next-config-js/redirects) are checked/applied
3. `beforeFiles` rewrites are checked/applied
4. static files from the [public directory](/docs/app/building-your-application/optimizing/static-assets), `_next/static` files, and non-dynamic pages are checked/served
5. `afterFiles` rewrites are checked/applied, if one of these rewrites is matched we check dynamic routes/static files after each match
6. `fallback` rewrites are checked/applied, these are applied before rendering the 404 page and after dynamic routes/all static assets have been checked. If you use [fallback: true/'blocking'](/docs/pages/api-reference/functions/get-static-paths#fallback-true) in `getStaticPaths`, the fallback `rewrites` defined in your `next.config.js` will _not_ be run.
[Rewrite parameters](#rewrite-parameters)
-----------------------------------------
When using parameters in a rewrite the parameters will be passed in the query by default when none of the parameters are used in the `destination`.
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/old-about/:path*',
destination: '/about', // The :path parameter isn't used here so will be automatically passed in the query
},
]
},
}
If a parameter is used in the destination none of the parameters will be automatically passed in the query.
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/docs/:path*',
destination: '/:path*', // The :path parameter is used here so will not be automatically passed in the query
},
]
},
}
You can still pass the parameters manually in the query if one is already used in the destination by specifying the query in the `destination`.
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/:first/:second',
destination: '/:first?second=:second',
// Since the :first parameter is used in the destination the :second parameter
// will not automatically be added in the query although we can manually add it
// as shown above
},
]
},
}
> **Good to know**: Static pages from [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization) or [prerendering](/docs/pages/building-your-application/data-fetching/get-static-props) params from rewrites will be parsed on the client after hydration and provided in the query.
[Path Matching](#path-matching)
-------------------------------
Path matches are allowed, for example `/blog/:slug` will match `/blog/hello-world` (no nested paths):
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/blog/:slug',
destination: '/news/:slug', // Matched parameters can be used in the destination
},
]
},
}
### [Wildcard Path Matching](#wildcard-path-matching)
To match a wildcard path you can use `*` after a parameter, for example `/blog/:slug*` will match `/blog/a/b/c/d/hello-world`:
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/blog/:slug*',
destination: '/news/:slug*', // Matched parameters can be used in the destination
},
]
},
}
### [Regex Path Matching](#regex-path-matching)
To match a regex path you can wrap the regex in parenthesis after a parameter, for example `/blog/:slug(\\d{1,})` will match `/blog/123` but not `/blog/abc`:
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/old-blog/:post(\\d{1,})',
destination: '/blog/:post', // Matched parameters can be used in the destination
},
]
},
}
The following characters `(`, `)`, `{`, `}`, `[`, `]`, `|`, `\`, `^`, `.`, `:`, `*`, `+`, `-`, `?`, `$` are used for regex path matching, so when used in the `source` as non-special values they must be escaped by adding `\\` before them:
next.config.js
module.exports = {
async rewrites() {
return [
{
// this will match `/english(default)/something` being requested
source: '/english\\(default\\)/:slug',
destination: '/en-us/:slug',
},
]
},
}
[Header, Cookie, and Query Matching](#header-cookie-and-query-matching)
-----------------------------------------------------------------------
To only match a rewrite when header, cookie, or query values also match the `has` field or don't match the `missing` field can be used. Both the `source` and all `has` items must match and all `missing` items must not match for the rewrite to be applied.
`has` and `missing` items can have the following fields:
* `type`: `String` - must be either `header`, `cookie`, `host`, or `query`.
* `key`: `String` - the key from the selected type to match against.
* `value`: `String` or `undefined` - the value to check for, if undefined any value will match. A regex like string can be used to capture a specific part of the value, e.g. if the value `first-(?<paramName>.*)` is used for `first-second` then `second` will be usable in the destination with `:paramName`.
next.config.js
module.exports = {
async rewrites() {
return [
// if the header `x-rewrite-me` is present,
// this rewrite will be applied
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-rewrite-me',
},
],
destination: '/another-page',
},
// if the header `x-rewrite-me` is not present,
// this rewrite will be applied
{
source: '/:path*',
missing: [
{
type: 'header',
key: 'x-rewrite-me',
},
],
destination: '/another-page',
},
// if the source, query, and cookie are matched,
// this rewrite will be applied
{
source: '/specific/:path*',
has: [
{
type: 'query',
key: 'page',
// the page value will not be available in the
// destination since value is provided and doesn't
// use a named capture group e.g. (?<page>home)
value: 'home',
},
{
type: 'cookie',
key: 'authorized',
value: 'true',
},
],
destination: '/:path*/home',
},
// if the header `x-authorized` is present and
// contains a matching value, this rewrite will be applied
{
source: '/:path*',
has: [
{
type: 'header',
key: 'x-authorized',
value: '(?<authorized>yes|true)',
},
],
destination: '/home?authorized=:authorized',
},
// if the host is `example.com`,
// this rewrite will be applied
{
source: '/:path*',
has: [
{
type: 'host',
value: 'example.com',
},
],
destination: '/another-page',
},
]
},
}
[Rewriting to an external URL](#rewriting-to-an-external-url)
-------------------------------------------------------------
Examples
* [Incremental adoption of Next.js](https://github.com/vercel/next.js/tree/canary/examples/custom-routes-proxying)
* [Using Multiple Zones](https://github.com/vercel/next.js/tree/canary/examples/with-zones)
Rewrites allow you to rewrite to an external url. This is especially useful for incrementally adopting Next.js. The following is an example rewrite for redirecting the `/blog` route of your main app to an external site.
next.config.js
module.exports = {
async rewrites() {
return [
{
source: '/blog',
destination: 'https://example.com/blog',
},
{
source: '/blog/:slug',
destination: 'https://example.com/blog/:slug', // Matched parameters can be used in the destination
},
]
},
}
If you're using `trailingSlash: true`, you also need to insert a trailing slash in the `source` parameter. If the destination server is also expecting a trailing slash it should be included in the `destination` parameter as well.
next.config.js
module.exports = {
trailingSlash: true,
async rewrites() {
return [
{
source: '/blog/',
destination: 'https://example.com/blog/',
},
{
source: '/blog/:path*/',
destination: 'https://example.com/blog/:path*/',
},
]
},
}
### [Incremental adoption of Next.js](#incremental-adoption-of-nextjs)
You can also have Next.js fall back to proxying to an existing website after checking all Next.js routes.
This way you don't have to change the rewrites configuration when migrating more pages to Next.js
next.config.js
module.exports = {
async rewrites() {
return {
fallback: [
{
source: '/:path*',
destination: `https://custom-routes-proxying-endpoint.vercel.app/:path*`,
},
],
}
},
}
### [Rewrites with basePath support](#rewrites-with-basepath-support)
When leveraging [`basePath` support](/docs/app/api-reference/next-config-js/basePath) with rewrites each `source` and `destination` is automatically prefixed with the `basePath` unless you add `basePath: false` to the rewrite:
next.config.js
module.exports = {
basePath: '/docs',
async rewrites() {
return [
{
source: '/with-basePath', // automatically becomes /docs/with-basePath
destination: '/another', // automatically becomes /docs/another
},
{
// does not add /docs to /without-basePath since basePath: false is set
// Note: this can not be used for internal rewrites e.g. `destination: '/another'`
source: '/without-basePath',
destination: 'https://example.com',
basePath: false,
},
]
},
}
### [Rewrites with i18n support](#rewrites-with-i18n-support)
When leveraging [`i18n` support](/docs/app/building-your-application/routing/internationalization) with rewrites each `source` and `destination` is automatically prefixed to handle the configured `locales` unless you add `locale: false` to the rewrite. If `locale: false` is used you must prefix the `source` and `destination` with a locale for it to be matched correctly.
next.config.js
module.exports = {
i18n: {
locales: ['en', 'fr', 'de'],
defaultLocale: 'en',
},
async rewrites() {
return [
{
source: '/with-locale', // automatically handles all locales
destination: '/another', // automatically passes the locale on
},
{
// does not handle locales automatically since locale: false is set
source: '/nl/with-locale-manual',
destination: '/nl/another',
locale: false,
},
{
// this matches '/' since `en` is the defaultLocale
source: '/en',
destination: '/en/another',
locale: false,
},
{
// it's possible to match all locales even when locale: false is set
source: '/:locale/api-alias/:path*',
destination: '/api/:path*',
locale: false,
},
{
// this gets converted to /(en|fr|de)/(.*) so will not match the top-level
// `/` or `/fr` routes like /:path* would
source: '/(.*)',
destination: '/another',
},
]
},
}
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
`missing` added.
`v10.2.0`
`has` added.
`v9.5.0`
Headers added.
[Previous
redirects
](/docs/app/api-reference/next-config-js/redirects)
[Next
serverActions
](/docs/app/api-reference/next-config-js/serverActions)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"7e064a6e0804534c6b63ebf0112119c1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/reactStrictMode | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [
reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/reactStrictMode.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)reactStrictMode
reactStrictMode
===============
> **Good to know**: Since Next.js 13.4, Strict Mode is `true` by default with `app` router, so the above configuration is only necessary for `pages`. You can still disable Strict Mode by setting `reactStrictMode: false`.
> **Suggested**: We strongly suggest you enable Strict Mode in your Next.js application to better prepare your application for the future of React.
React's [Strict Mode](https://react.dev/reference/react/StrictMode) is a development mode only feature for highlighting potential problems in an application. It helps to identify unsafe lifecycles, legacy API usage, and a number of other features.
The Next.js runtime is Strict Mode-compliant. To opt-in to Strict Mode, configure the following option in your `next.config.js`:
next.config.js
module.exports = {
reactStrictMode: true,
}
If you or your team are not ready to use Strict Mode in your entire application, that's OK! You can incrementally migrate on a page-by-page basis using `<React.StrictMode>`.
[Previous
productionBrowserSourceMaps
](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
[Next
redirects
](/docs/app/api-reference/next-config-js/redirects)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a046bc0c5b1af9f96413ee1915212749\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/redirects | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [
redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Path Matching](#path-matching)
* [Wildcard Path Matching](#wildcard-path-matching)
* [Regex Path Matching](#regex-path-matching)
* [Header, Cookie, and Query Matching](#header-cookie-and-query-matching)
* [Redirects with basePath support](#redirects-with-basepath-support)
* [Redirects with i18n support](#redirects-with-i18n-support)
* [Other Redirects](#other-redirects)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/redirects.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)redirects
redirects
=========
Redirects allow you to redirect an incoming request path to a different destination path.
To use redirects you can use the `redirects` key in `next.config.js`:
next.config.js
module.exports = {
async redirects() {
return [
{
source: '/about',
destination: '/',
permanent: true,
},
]
},
}
`redirects` is an async function that expects an array to be returned holding objects with `source`, `destination`, and `permanent` properties:
* `source` is the incoming request path pattern.
* `destination` is the path you want to route to.
* `permanent` `true` or `false` - if `true` will use the 308 status code which instructs clients/search engines to cache the redirect forever, if `false` will use the 307 status code which is temporary and is not cached.
> **Why does Next.js use 307 and 308?** Traditionally a 302 was used for a temporary redirect, and a 301 for a permanent redirect, but many browsers changed the request method of the redirect to `GET`, regardless of the original method. For example, if the browser made a request to `POST /v1/users` which returned status code `302` with location `/v2/users`, the subsequent request might be `GET /v2/users` instead of the expected `POST /v2/users`. Next.js uses the 307 temporary redirect, and 308 permanent redirect status codes to explicitly preserve the request method used.
* `basePath`: `false` or `undefined` - if false the `basePath` won't be included when matching, can be used for external redirects only.
* `locale`: `false` or `undefined` - whether the locale should not be included when matching.
* `has` is an array of [has objects](#header-cookie-and-query-matching) with the `type`, `key` and `value` properties.
* `missing` is an array of [missing objects](#header-cookie-and-query-matching) with the `type`, `key` and `value` properties.
Redirects are checked before the filesystem which includes pages and `/public` files.
When using the Pages Router, redirects are not applied to client-side routing (`Link`, `router.push`) unless [Middleware](/docs/app/building-your-application/routing/middleware) is present and matches the path.
When a redirect is applied, any query values provided in the request will be passed through to the redirect destination. For example, see the following redirect configuration:
{
source: '/old-blog/:path*',
destination: '/blog/:path*',
permanent: false
}
When `/old-blog/post-1?hello=world` is requested, the client will be redirected to `/blog/post-1?hello=world`.
[Path Matching](#path-matching)
-------------------------------
Path matches are allowed, for example `/old-blog/:slug` will match `/old-blog/hello-world` (no nested paths):
next.config.js
module.exports = {
async redirects() {
return [
{
source: '/old-blog/:slug',
destination: '/news/:slug', // Matched parameters can be used in the destination
permanent: true,
},
]
},
}
### [Wildcard Path Matching](#wildcard-path-matching)
To match a wildcard path you can use `*` after a parameter, for example `/blog/:slug*` will match `/blog/a/b/c/d/hello-world`:
next.config.js
module.exports = {
async redirects() {
return [
{
source: '/blog/:slug*',
destination: '/news/:slug*', // Matched parameters can be used in the destination
permanent: true,
},
]
},
}
### [Regex Path Matching](#regex-path-matching)
To match a regex path you can wrap the regex in parentheses after a parameter, for example `/post/:slug(\\d{1,})` will match `/post/123` but not `/post/abc`:
next.config.js
module.exports = {
async redirects() {
return [
{
source: '/post/:slug(\\d{1,})',
destination: '/news/:slug', // Matched parameters can be used in the destination
permanent: false,
},
]
},
}
The following characters `(`, `)`, `{`, `}`, `:`, `*`, `+`, `?` are used for regex path matching, so when used in the `source` as non-special values they must be escaped by adding `\\` before them:
next.config.js
module.exports = {
async redirects() {
return [
{
// this will match `/english(default)/something` being requested
source: '/english\\(default\\)/:slug',
destination: '/en-us/:slug',
permanent: false,
},
]
},
}
[Header, Cookie, and Query Matching](#header-cookie-and-query-matching)
-----------------------------------------------------------------------
To only match a redirect when header, cookie, or query values also match the `has` field or don't match the `missing` field can be used. Both the `source` and all `has` items must match and all `missing` items must not match for the redirect to be applied.
`has` and `missing` items can have the following fields:
* `type`: `String` - must be either `header`, `cookie`, `host`, or `query`.
* `key`: `String` - the key from the selected type to match against.
* `value`: `String` or `undefined` - the value to check for, if undefined any value will match. A regex like string can be used to capture a specific part of the value, e.g. if the value `first-(?<paramName>.*)` is used for `first-second` then `second` will be usable in the destination with `:paramName`.
next.config.js
module.exports = {
async redirects() {
return [
// if the header `x-redirect-me` is present,
// this redirect will be applied
{
source: '/:path((?!another-page$).*)',
has: [
{
type: 'header',
key: 'x-redirect-me',
},
],
permanent: false,
destination: '/another-page',
},
// if the header `x-dont-redirect` is present,
// this redirect will NOT be applied
{
source: '/:path((?!another-page$).*)',
missing: [
{
type: 'header',
key: 'x-do-not-redirect',
},
],
permanent: false,
destination: '/another-page',
},
// if the source, query, and cookie are matched,
// this redirect will be applied
{
source: '/specific/:path*',
has: [
{
type: 'query',
key: 'page',
// the page value will not be available in the
// destination since value is provided and doesn't
// use a named capture group e.g. (?<page>home)
value: 'home',
},
{
type: 'cookie',
key: 'authorized',
value: 'true',
},
],
permanent: false,
destination: '/another/:path*',
},
// if the header `x-authorized` is present and
// contains a matching value, this redirect will be applied
{
source: '/',
has: [
{
type: 'header',
key: 'x-authorized',
value: '(?<authorized>yes|true)',
},
],
permanent: false,
destination: '/home?authorized=:authorized',
},
// if the host is `example.com`,
// this redirect will be applied
{
source: '/:path((?!another-page$).*)',
has: [
{
type: 'host',
value: 'example.com',
},
],
permanent: false,
destination: '/another-page',
},
]
},
}
### [Redirects with basePath support](#redirects-with-basepath-support)
When leveraging [`basePath` support](/docs/app/api-reference/next-config-js/basePath) with redirects each `source` and `destination` is automatically prefixed with the `basePath` unless you add `basePath: false` to the redirect:
next.config.js
module.exports = {
basePath: '/docs',
async redirects() {
return [
{
source: '/with-basePath', // automatically becomes /docs/with-basePath
destination: '/another', // automatically becomes /docs/another
permanent: false,
},
{
// does not add /docs since basePath: false is set
source: '/without-basePath',
destination: 'https://example.com',
basePath: false,
permanent: false,
},
]
},
}
### [Redirects with i18n support](#redirects-with-i18n-support)
When leveraging [`i18n` support](/docs/app/building-your-application/routing/internationalization) with redirects each `source` and `destination` is automatically prefixed to handle the configured `locales` unless you add `locale: false` to the redirect. If `locale: false` is used you must prefix the `source` and `destination` with a locale for it to be matched correctly.
next.config.js
module.exports = {
i18n: {
locales: ['en', 'fr', 'de'],
defaultLocale: 'en',
},
async redirects() {
return [
{
source: '/with-locale', // automatically handles all locales
destination: '/another', // automatically passes the locale on
permanent: false,
},
{
// does not handle locales automatically since locale: false is set
source: '/nl/with-locale-manual',
destination: '/nl/another',
locale: false,
permanent: false,
},
{
// this matches '/' since `en` is the defaultLocale
source: '/en',
destination: '/en/another',
locale: false,
permanent: false,
},
// it's possible to match all locales even when locale: false is set
{
source: '/:locale/page',
destination: '/en/newpage',
permanent: false,
locale: false,
},
{
// this gets converted to /(en|fr|de)/(.*) so will not match the top-level
// `/` or `/fr` routes like /:path* would
source: '/(.*)',
destination: '/another',
permanent: false,
},
]
},
}
In some rare cases, you might need to assign a custom status code for older HTTP Clients to properly redirect. In these cases, you can use the `statusCode` property instead of the `permanent` property, but not both. To to ensure IE11 compatibility, a `Refresh` header is automatically added for the 308 status code.
[Other Redirects](#other-redirects)
-----------------------------------
* Inside [API Routes](/docs/pages/building-your-application/routing/api-routes) and [Route Handlers](/docs/app/building-your-application/routing/route-handlers), you can redirect based on the incoming request.
* Inside [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) and [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props), you can redirect specific pages at request-time.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.3.0`
`missing` added.
`v10.2.0`
`has` added.
`v9.5.0`
`redirects` added.
[Previous
reactStrictMode
](/docs/app/api-reference/next-config-js/reactStrictMode)
[Next
rewrites
](/docs/app/api-reference/next-config-js/rewrites)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a5994715e76abfaa3e0d1ca606656179\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/custom-document | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [
Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Caveats](#caveats)
* [Customizing renderPage](#customizing-renderpage)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/06-custom-document.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Custom Document
Custom Document
===============
A custom `Document` can update the `<html>` and `<body>` tags used to render a [Page](/docs/pages/building-your-application/routing/pages-and-layouts).
To override the default `Document`, create the file `pages/_document` as shown below:
pages/\_document.tsx
TypeScript
JavaScriptTypeScript
import { Html, Head, Main, NextScript } from 'next/document'
export default function Document() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
> **Good to know**
>
> * `_document` is only rendered on the server, so event handlers like `onClick` cannot be used in this file.
> * `<Html>`, `<Head />`, `<Main />` and `<NextScript />` are required for the page to be properly rendered.
[Caveats](#caveats)
-------------------
* The `<Head />` component used in `_document` is not the same as [`next/head`](/docs/pages/api-reference/components/head). The `<Head />` component used here should only be used for any `<head>` code that is common for all pages. For all other cases, such as `<title>` tags, we recommend using [`next/head`](/docs/pages/api-reference/components/head) in your pages or components.
* React components outside of `<Main />` will not be initialized by the browser. Do _not_ add application logic here or custom CSS (like `styled-jsx`). If you need shared components in all your pages (like a menu or a toolbar), read [Layouts](/docs/pages/building-your-application/routing/pages-and-layouts#layout-pattern) instead.
* `Document` currently does not support Next.js [Data Fetching methods](/docs/pages/building-your-application/data-fetching) like [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) or [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props).
[Customizing `renderPage`](#customizing-renderpage)
---------------------------------------------------
Customizing `renderPage` is advanced and only needed for libraries like CSS-in-JS to support server-side rendering. This is not needed for built-in `styled-jsx` support.
**We do not recommend using this pattern.** Instead, consider [incrementally adopting](/docs/app/building-your-application/upgrading/app-router-migration) the App Router, which allows you to more easily fetch data for [pages and layouts](/docs/app/building-your-application/routing/pages-and-layouts).
pages/\_document.tsx
TypeScript
JavaScriptTypeScript
import Document, {
Html,
Head,
Main,
NextScript,
DocumentContext,
DocumentInitialProps,
} from 'next/document'
class MyDocument extends Document {
static async getInitialProps(
ctx: DocumentContext
): Promise<DocumentInitialProps> {
const originalRenderPage = ctx.renderPage
// Run the React rendering logic synchronously
ctx.renderPage = () =>
originalRenderPage({
// Useful for wrapping the whole react tree
enhanceApp: (App) => App,
// Useful for wrapping in a per-page basis
enhanceComponent: (Component) => Component,
})
// Run the parent `getInitialProps`, it now includes the custom `renderPage`
const initialProps = await Document.getInitialProps(ctx)
return initialProps
}
render() {
return (
<Html lang="en">
<Head />
<body>
<Main />
<NextScript />
</body>
</Html>
)
}
}
export default MyDocument
> **Good to know**
>
> * `getInitialProps` in `_document` is not called during client-side transitions.
> * The `ctx` object for `_document` is equivalent to the one received in [`getInitialProps`](/docs/pages/api-reference/functions/get-initial-props#context-object), with the addition of `renderPage`.
[Previous
Custom App
](/docs/pages/building-your-application/routing/custom-app)
[Next
API Routes
](/docs/pages/building-your-application/routing/api-routes)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2a664bdcf74b07b916c04381f7346367\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [
serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/serverComponentsExternalPackages.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)serverComponentsExternalPackages
serverComponentsExternalPackages
================================
Dependencies used inside [Server Components](/docs/app/building-your-application/rendering/server-components) and [Route Handlers](/docs/app/building-your-application/routing/route-handlers) will automatically be bundled by Next.js.
If a dependency is using Node.js specific features, you can choose to opt-out specific dependencies from the Server Components bundling and use native Node.js `require`.
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
serverComponentsExternalPackages: ['@acme/ui'],
},
}
module.exports = nextConfig
Next.js includes a [short list of popular packages](https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json) that currently are working on compatibility and automatically opt-ed out:
* `@aws-sdk/client-s3`
* `@aws-sdk/s3-presigned-post`
* `@blockfrost/blockfrost-js`
* `@highlight-run/node`
* `@jpg-store/lucid-cardano`
* `@libsql/client`
* `@mikro-orm/core`
* `@mikro-orm/knex`
* `@prisma/client`
* `@react-pdf/renderer`
* `@sentry/profiling-node`
* `@swc/core`
* `argon2`
* `autoprefixer`
* `aws-crt`
* `bcrypt`
* `better-sqlite3`
* `canvas`
* `cpu-features`
* `cypress`
* `eslint`
* `express`
* `firebase-admin`
* `jest`
* `jsdom`
* `libsql`
* `mdx-bundler`
* `mongodb`
* `mongoose`
* `next-mdx-remote`
* `next-seo`
* `node-pty`
* `node-web-audio-api`
* `pg`
* `playwright`
* `postcss`
* `prettier`
* `prisma`
* `puppeteer-core`
* `puppeteer`
* `rimraf`
* `sharp`
* `shiki`
* `sqlite3`
* `tailwindcss`
* `ts-node`
* `typescript`
* `vscode-oniguruma`
* `undici`
* `webpack`
* `websocket`
* `zeromq`
[Previous
serverActions
](/docs/app/api-reference/next-config-js/serverActions)
[Next
StaleTimes (experimental)
](/docs/app/api-reference/next-config-js/staleTimes)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"564fb5b6889ded6bd8b58ed1e9cd90d3\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [
cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [API Reference](#api-reference)
* [get()](#get)
* [set()](#set)
* [revalidateTag()](#revalidatetag)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/incrementalCacheHandlerPath.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)cacheHandler
Custom Next.js Cache Handler
============================
In Next.js, the [default cache handler](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) for the Pages and App Router uses the filesystem cache. This requires no configuration, however, you can customize the cache handler by using the `cacheHandler` field in `next.config.js`.
next.config.js
module.exports = {
cacheHandler: require.resolve('./cache-handler.js'),
cacheMaxMemorySize: 0, // disable default in-memory caching
}
View an example of a [custom cache handler](/docs/app/building-your-application/deploying#configuring-caching) and learn more about implementation.
[API Reference](#api-reference)
-------------------------------
The cache handler can implement the following methods: `get`, `set`, and `revalidateTag`.
### [`get()`](#get)
Parameter
Type
Description
`key`
`string`
The key to the cached value.
Returns the cached value or `null` if not found.
### [`set()`](#set)
Parameter
Type
Description
`key`
`string`
The key to store the data under.
`data`
Data or `null`
The data to be cached.
`ctx`
`{ tags: [] }`
The cache tags provided.
Returns `Promise<void>`.
### [`revalidateTag()`](#revalidatetag)
Parameter
Type
Description
`tag`
`string`
The cache tag to revalidate.
Returns `Promise<void>`. Learn more about [revalidating data](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) or the [`revalidateTag()`](/docs/app/api-reference/functions/revalidateTag) function.
**Good to know:**
* `revalidatePath` is a convenience layer on top of cache tags. Calling `revalidatePath` will call your `revalidateTag` function, which you can then choose if you want to tag cache keys based on the path.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v14.1.0`
Renamed `cacheHandler` is stable.
`v13.4.0`
`incrementalCacheHandlerPath` (experimental) supports `revalidateTag`.
`v13.4.0`
`incrementalCacheHandlerPath` (experimental) supports standalone output.
`v12.2.0`
`incrementalCacheHandlerPath` (experimental) is added.
[Previous
images
](/docs/app/api-reference/next-config-js/images)
[Next
instrumentationHook
](/docs/app/api-reference/next-config-js/instrumentationHook)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"7f74cc4deaca7e6671f94be8e51d297f\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/api-routes | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [
API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Parameters](#parameters)
* [HTTP Methods](#http-methods)
* [Request Helpers](#request-helpers)
* [Custom config](#custom-config)
* [Response Helpers](#response-helpers)
* [Setting the status code of a response](#setting-the-status-code-of-a-response)
* [Sending a JSON response](#sending-a-json-response)
* [Sending a HTTP response](#sending-a-http-response)
* [Redirects to a specified path or URL](#redirects-to-a-specified-path-or-url)
* [Adding TypeScript types](#adding-typescript-types)
* [Dynamic API Routes](#dynamic-api-routes)
* [Catch all API routes](#catch-all-api-routes)
* [Optional catch all API routes](#optional-catch-all-api-routes)
* [Caveats](#caveats)
* [Edge API Routes](#edge-api-routes)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/07-api-routes.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)API Routes
API Routes
==========
Examples
* [Basic API Routes](https://github.com/vercel/next.js/tree/canary/examples/api-routes)
* [API Routes Request Helpers](https://github.com/vercel/next.js/tree/canary/examples/api-routes-middleware)
* [API Routes with GraphQL](https://github.com/vercel/next.js/tree/canary/examples/api-routes-graphql)
* [API Routes with REST](https://github.com/vercel/next.js/tree/canary/examples/api-routes-rest)
* [API Routes with CORS](https://github.com/vercel/next.js/tree/canary/examples/api-routes-cors)
> **Good to know**: If you are using the App Router, you can use [Server Components](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating) or [Route Handlers](/docs/app/building-your-application/routing/route-handlers) instead of API Routes.
API routes provide a solution to build a **public API** with Next.js.
Any file inside the folder `pages/api` is mapped to `/api/*` and will be treated as an API endpoint instead of a `page`. They are server-side only bundles and won't increase your client-side bundle size.
For example, the following API route returns a JSON response with a status code of `200`:
pages/api/hello.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
> **Good to know**:
>
> * API Routes [do not specify CORS headers](https://developer.mozilla.org/docs/Web/HTTP/CORS), meaning they are **same-origin only** by default. You can customize such behavior by wrapping the request handler with the [CORS request helpers](https://github.com/vercel/next.js/tree/canary/examples/api-routes-cors).
* API Routes can't be used with [static exports](/docs/pages/building-your-application/deploying/static-exports). However, [Route Handlers](/docs/app/building-your-application/routing/route-handlers) in the App Router can.
> * API Routes will be affected by [`pageExtensions` configuration](/docs/pages/api-reference/next-config-js/pageExtensions) in `next.config.js`.
[Parameters](#parameters)
-------------------------
export default function handler(req: NextApiRequest, res: NextApiResponse) {
// ...
}
* `req`: An instance of [http.IncomingMessage](https://nodejs.org/api/http.html#class-httpincomingmessage)
* `res`: An instance of [http.ServerResponse](https://nodejs.org/api/http.html#class-httpserverresponse)
[HTTP Methods](#http-methods)
-----------------------------
To handle different HTTP methods in an API route, you can use `req.method` in your request handler, like so:
pages/api/hello.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
if (req.method === 'POST') {
// Process a POST request
} else {
// Handle any other HTTP method
}
}
[Request Helpers](#request-helpers)
-----------------------------------
API Routes provide built-in request helpers which parse the incoming request (`req`):
* `req.cookies` - An object containing the cookies sent by the request. Defaults to `{}`
* `req.query` - An object containing the [query string](https://en.wikipedia.org/wiki/Query_string). Defaults to `{}`
* `req.body` - An object containing the body parsed by `content-type`, or `null` if no body was sent
### [Custom config](#custom-config)
Every API Route can export a `config` object to change the default configuration, which is the following:
export const config = {
api: {
bodyParser: {
sizeLimit: '1mb',
},
},
// Specifies the maximum allowed duration for this function to execute (in seconds)
maxDuration: 5,
}
`bodyParser` is automatically enabled. If you want to consume the body as a `Stream` or with [`raw-body`](https://www.npmjs.com/package/raw-body), you can set this to `false`.
One use case for disabling the automatic `bodyParsing` is to allow you to verify the raw body of a **webhook** request, for example [from GitHub](https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks#validating-payloads-from-github).
export const config = {
api: {
bodyParser: false,
},
}
`bodyParser.sizeLimit` is the maximum size allowed for the parsed body, in any format supported by [bytes](https://github.com/visionmedia/bytes.js), like so:
export const config = {
api: {
bodyParser: {
sizeLimit: '500kb',
},
},
}
`externalResolver` is an explicit flag that tells the server that this route is being handled by an external resolver like _express_ or _connect_. Enabling this option disables warnings for unresolved requests.
export const config = {
api: {
externalResolver: true,
},
}
`responseLimit` is automatically enabled, warning when an API Routes' response body is over 4MB.
If you are not using Next.js in a serverless environment, and understand the performance implications of not using a CDN or dedicated media host, you can set this limit to `false`.
export const config = {
api: {
responseLimit: false,
},
}
`responseLimit` can also take the number of bytes or any string format supported by `bytes`, for example `1000`, `'500kb'` or `'3mb'`. This value will be the maximum response size before a warning is displayed. Default is 4MB. (see above)
export const config = {
api: {
responseLimit: '8mb',
},
}
[Response Helpers](#response-helpers)
-------------------------------------
The [Server Response object](https://nodejs.org/api/http.html#http_class_http_serverresponse), (often abbreviated as `res`) includes a set of Express.js-like helper methods to improve the developer experience and increase the speed of creating new API endpoints.
The included helpers are:
* `res.status(code)` - A function to set the status code. `code` must be a valid [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)
* `res.json(body)` - Sends a JSON response. `body` must be a [serializable object](https://developer.mozilla.org/docs/Glossary/Serialization)
* `res.send(body)` - Sends the HTTP response. `body` can be a `string`, an `object` or a `Buffer`
* `res.redirect([status,] path)` - Redirects to a specified path or URL. `status` must be a valid [HTTP status code](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). If not specified, `status` defaults to "307" "Temporary redirect".
* `res.revalidate(urlPath)` - [Revalidate a page on demand](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation) using `getStaticProps`. `urlPath` must be a `string`.
### [Setting the status code of a response](#setting-the-status-code-of-a-response)
When sending a response back to the client, you can set the status code of the response.
The following example sets the status code of the response to `200` (`OK`) and returns a `message` property with the value of `Hello from Next.js!` as a JSON response:
pages/api/hello.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
### [Sending a JSON response](#sending-a-json-response)
When sending a response back to the client you can send a JSON response, this must be a [serializable object](https://developer.mozilla.org/docs/Glossary/Serialization). In a real world application you might want to let the client know the status of the request depending on the result of the requested endpoint.
The following example sends a JSON response with the status code `200` (`OK`) and the result of the async operation. It's contained in a try catch block to handle any errors that may occur, with the appropriate status code and error message caught and sent back to the client:
pages/api/hello.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const result = await someAsyncOperation()
res.status(200).json({ result })
} catch (err) {
res.status(500).json({ error: 'failed to load data' })
}
}
### [Sending a HTTP response](#sending-a-http-response)
Sending an HTTP response works the same way as when sending a JSON response. The only difference is that the response body can be a `string`, an `object` or a `Buffer`.
The following example sends a HTTP response with the status code `200` (`OK`) and the result of the async operation.
pages/api/hello.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const result = await someAsyncOperation()
res.status(200).send({ result })
} catch (err) {
res.status(500).send({ error: 'failed to fetch data' })
}
}
### [Redirects to a specified path or URL](#redirects-to-a-specified-path-or-url)
Taking a form as an example, you may want to redirect your client to a specified path or URL once they have submitted the form.
The following example redirects the client to the `/` path if the form is successfully submitted:
pages/api/hello.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const { name, message } = req.body
try {
await handleFormInputAsync({ name, message })
res.redirect(307, '/')
} catch (err) {
res.status(500).send({ error: 'Failed to fetch data' })
}
}
### [Adding TypeScript types](#adding-typescript-types)
You can make your API Routes more type-safe by importing the `NextApiRequest` and `NextApiResponse` types from `next`, in addition to those, you can also type your response data:
import type { NextApiRequest, NextApiResponse } from 'next'
type ResponseData = {
message: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<ResponseData>
) {
res.status(200).json({ message: 'Hello from Next.js!' })
}
> **Good to know**: The body of `NextApiRequest` is `any` because the client may include any payload. You should validate the type/shape of the body at runtime before using it.
[Dynamic API Routes](#dynamic-api-routes)
-----------------------------------------
API Routes support [dynamic routes](/docs/pages/building-your-application/routing/dynamic-routes), and follow the same file naming rules used for `pages/`.
pages/api/post/\[pid\].ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { pid } = req.query
res.end(`Post: ${pid}`)
}
Now, a request to `/api/post/abc` will respond with the text: `Post: abc`.
### [Catch all API routes](#catch-all-api-routes)
API Routes can be extended to catch all paths by adding three dots (`...`) inside the brackets. For example:
* `pages/api/post/[...slug].js` matches `/api/post/a`, but also `/api/post/a/b`, `/api/post/a/b/c` and so on.
> **Good to know**: You can use names other than `slug`, such as: `[...param]`
Matched parameters will be sent as a query parameter (`slug` in the example) to the page, and it will always be an array, so, the path `/api/post/a` will have the following `query` object:
{ "slug": ["a"] }
And in the case of `/api/post/a/b`, and any other matching path, new parameters will be added to the array, like so:
{ "slug": ["a", "b"] }
For example:
pages/api/post/\[...slug\].ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const { slug } = req.query
res.end(`Post: ${slug.join(', ')}`)
}
Now, a request to `/api/post/a/b/c` will respond with the text: `Post: a, b, c`.
### [Optional catch all API routes](#optional-catch-all-api-routes)
Catch all routes can be made optional by including the parameter in double brackets (`[[...slug]]`).
For example, `pages/api/post/[[...slug]].js` will match `/api/post`, `/api/post/a`, `/api/post/a/b`, and so on.
The main difference between catch all and optional catch all routes is that with optional, the route without the parameter is also matched (`/api/post` in the example above).
The `query` objects are as follows:
{ } // GET `/api/post` (empty object)
{ "slug": ["a"] } // `GET /api/post/a` (single-element array)
{ "slug": ["a", "b"] } // `GET /api/post/a/b` (multi-element array)
### [Caveats](#caveats)
* Predefined API routes take precedence over dynamic API routes, and dynamic API routes over catch all API routes. Take a look at the following examples:
* `pages/api/post/create.js` - Will match `/api/post/create`
* `pages/api/post/[pid].js` - Will match `/api/post/1`, `/api/post/abc`, etc. But not `/api/post/create`
* `pages/api/post/[...slug].js` - Will match `/api/post/1/2`, `/api/post/a/b/c`, etc. But not `/api/post/create`, `/api/post/abc`
[Edge API Routes](#edge-api-routes)
-----------------------------------
If you would like to use API Routes with the Edge Runtime, we recommend incrementally adopting the App Router and using [Route Handlers](/docs/app/building-your-application/routing/route-handlers) instead.
The Route Handlers function signature is isomorphic, meaning you can use the same function for both Edge and Node.js runtimes.
[Previous
Custom Document
](/docs/pages/building-your-application/routing/custom-document)
[Next
Custom Errors
](/docs/pages/building-your-application/routing/custom-error)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"e42810cd32cd210a68cf43a338b7593d\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/images | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [
images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Example Loader Configuration](#example-loader-configuration)
* [Akamai](#akamai)
* [AWS CloudFront](#aws-cloudfront)
* [Cloudinary](#cloudinary)
* [Cloudflare](#cloudflare)
* [Contentful](#contentful)
* [Fastly](#fastly)
* [Gumlet](#gumlet)
* [ImageEngine](#imageengine)
* [Imgix](#imgix)
* [Thumbor](#thumbor)
* [Sanity](#sanity)
* [Sirv](#sirv)
* [Supabase](#supabase)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/images.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)images
images
======
If you want to use a cloud provider to optimize images instead of using the Next.js built-in Image Optimization API, you can configure `next.config.js` with the following:
next.config.js
module.exports = {
images: {
loader: 'custom',
loaderFile: './my/image/loader.js',
},
}
This `loaderFile` must point to a file relative to the root of your Next.js application. The file must export a default function that returns a string, for example:
my/image/loader.js
'use client'
export default function myImageLoader({ src, width, quality }) {
return `https://example.com/${src}?w=${width}&q=${quality || 75}`
}
Alternatively, you can use the [`loader` prop](/docs/app/api-reference/components/image#loader) to pass the function to each instance of `next/image`.
> **Good to know**: Customizing the image loader file, which accepts a function, requires using [Client Components](/docs/app/building-your-application/rendering/client-components) to serialize the provided function.
To learn more about configuring the behavior of the built-in [Image Optimization API](/docs/app/building-your-application/optimizing/images) and the [Image Component](/docs/app/api-reference/components/image), see [Image Configuration Options](/docs/app/api-reference/components/image#configuration-options) for available options.
[Example Loader Configuration](#example-loader-configuration)
-------------------------------------------------------------
* [Akamai](#akamai)
* [AWS CloudFront](#aws-cloudfront)
* [Cloudinary](#cloudinary)
* [Cloudflare](#cloudflare)
* [Contentful](#contentful)
* [Fastly](#fastly)
* [Gumlet](#gumlet)
* [ImageEngine](#imageengine)
* [Imgix](#imgix)
* [Thumbor](#thumbor)
* [Sanity](#sanity)
* [Sirv](#sirv)
* [Supabase](#supabase)
### [Akamai](#akamai)
// Docs: https://techdocs.akamai.com/ivm/reference/test-images-on-demand
export default function akamaiLoader({ src, width, quality }) {
return `https://example.com/${src}?imwidth=${width}`
}
### [AWS CloudFront](#aws-cloudfront)
// Docs: https://aws.amazon.com/developer/application-security-performance/articles/image-optimization
export default function cloudfrontLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
url.searchParams.set('format', 'auto')
url.searchParams.set('width', width.toString())
url.searchParams.set('quality', (quality || 75).toString())
return url.href
### [Cloudinary](#cloudinary)
// Demo: https://res.cloudinary.com/demo/image/upload/w_300,c_limit,q_auto/turtles.jpg
export default function cloudinaryLoader({ src, width, quality }) {
const params = ['f_auto', 'c_limit', `w_${width}`, `q_${quality || 'auto'}`]
return `https://example.com/${params.join(',')}${src}`
}
### [Cloudflare](#cloudflare)
// Docs: https://developers.cloudflare.com/images/url-format
export default function cloudflareLoader({ src, width, quality }) {
const params = [`width=${width}`, `quality=${quality || 75}`, 'format=auto']
return `https://example.com/cdn-cgi/image/${params.join(',')}/${src}`
}
### [Contentful](#contentful)
// Docs: https://www.contentful.com/developers/docs/references/images-api/
export default function contentfulLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
url.searchParams.set('fm', 'webp')
url.searchParams.set('w', width.toString())
url.searchParams.set('q', (quality || 75).toString())
return url.href
}
### [Fastly](#fastly)
// Docs: https://developer.fastly.com/reference/io/
export default function fastlyLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
url.searchParams.set('auto', 'webp')
url.searchParams.set('width', width.toString())
url.searchParams.set('quality', (quality || 75).toString())
return url.href
}
### [Gumlet](#gumlet)
// Docs: https://docs.gumlet.com/reference/image-transform-size
export default function gumletLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
url.searchParams.set('format', 'auto')
url.searchParams.set('w', width.toString())
url.searchParams.set('q', (quality || 75).toString())
return url.href
}
### [ImageEngine](#imageengine)
// Docs: https://support.imageengine.io/hc/en-us/articles/360058880672-Directives
export default function imageengineLoader({ src, width, quality }) {
const compression = 100 - (quality || 50)
const params = [`w_${width}`, `cmpr_${compression}`)]
return `https://example.com${src}?imgeng=/${params.join('/')`
}
### [Imgix](#imgix)
// Demo: https://static.imgix.net/daisy.png?format=auto&fit=max&w=300
export default function imgixLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
const params = url.searchParams
params.set('auto', params.getAll('auto').join(',') || 'format')
params.set('fit', params.get('fit') || 'max')
params.set('w', params.get('w') || width.toString())
params.set('q', (quality || 50).toString())
return url.href
}
### [Thumbor](#thumbor)
// Docs: https://thumbor.readthedocs.io/en/latest/
export default function thumborLoader({ src, width, quality }) {
const params = [`${width}x0`, `filters:quality(${quality || 75})`]
return `https://example.com${params.join('/')}${src}`
}
### [Sanity](#sanity)
// Docs: https://www.sanity.io/docs/image-urls
export default function sanityLoader({ src, width, quality }) {
const prj = 'zp7mbokg'
const dataset = 'production'
const url = new URL(`https://cdn.sanity.io/images/${prj}/${dataset}${src}`)
url.searchParams.set('auto', 'format')
url.searchParams.set('fit', 'max')
url.searchParams.set('w', width.toString())
if (quality) {
url.searchParams.set('q', quality.toString())
}
return url.href
}
### [Sirv](#sirv)
// Docs: https://sirv.com/help/articles/dynamic-imaging/
export default function sirvLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
const params = url.searchParams
params.set('format', params.getAll('format').join(',') || 'optimal')
params.set('w', params.get('w') || width.toString())
params.set('q', (quality || 85).toString())
return url.href
}
### [Supabase](#supabase)
// Docs: https://supabase.com/docs/guides/storage/image-transformations#nextjs-loader
export default function supabaseLoader({ src, width, quality }) {
const url = new URL(`https://example.com${src}`)
url.searchParams.set('width', width.toString())
url.searchParams.set('quality', (quality || 75).toString())
return url.href
}
[Previous
httpAgentOptions
](/docs/app/api-reference/next-config-js/httpAgentOptions)
[Next
cacheHandler
](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"6998a3fb0d289c7e9e675a2d48637c24\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/custom-error | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [
Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [404 Page](#404-page)
* [Customizing The 404 Page](#customizing-the-404-page)
* [500 Page](#500-page)
* [Customizing The 500 Page](#customizing-the-500-page)
* [More Advanced Error Page Customizing](#more-advanced-error-page-customizing)
* [Reusing the built-in error page](#reusing-the-built-in-error-page)
* [Caveats](#caveats)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/08-custom-error.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Custom Errors
Custom Errors
=============
[404 Page](#404-page)
---------------------
A 404 page may be accessed very often. Server-rendering an error page for every visit increases the load of the Next.js server. This can result in increased costs and slow experiences.
To avoid the above pitfalls, Next.js provides a static 404 page by default without having to add any additional files.
### [Customizing The 404 Page](#customizing-the-404-page)
To create a custom 404 page you can create a `pages/404.js` file. This file is statically generated at build time.
pages/404.js
export default function Custom404() {
return <h1>404 - Page Not Found</h1>
}
> **Good to know**: You can use [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) inside this page if you need to fetch data at build time.
[500 Page](#500-page)
---------------------
Server-rendering an error page for every visit adds complexity to responding to errors. To help users get responses to errors as fast as possible, Next.js provides a static 500 page by default without having to add any additional files.
### [Customizing The 500 Page](#customizing-the-500-page)
To customize the 500 page you can create a `pages/500.js` file. This file is statically generated at build time.
pages/500.js
export default function Custom500() {
return <h1>500 - Server-side error occurred</h1>
}
> **Good to know**: You can use [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) inside this page if you need to fetch data at build time.
### [More Advanced Error Page Customizing](#more-advanced-error-page-customizing)
500 errors are handled both client-side and server-side by the `Error` component. If you wish to override it, define the file `pages/_error.js` and add the following code:
function Error({ statusCode }) {
return (
<p>
{statusCode
? `An error ${statusCode} occurred on server`
: 'An error occurred on client'}
</p>
)
}
Error.getInitialProps = ({ res, err }) => {
const statusCode = res ? res.statusCode : err ? err.statusCode : 404
return { statusCode }
}
export default Error
> `pages/_error.js` is only used in production. In development you’ll get an error with the call stack to know where the error originated from.
### [Reusing the built-in error page](#reusing-the-built-in-error-page)
If you want to render the built-in error page you can by importing the `Error` component:
import Error from 'next/error'
export async function getServerSideProps() {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const errorCode = res.ok ? false : res.status
const json = await res.json()
return {
props: { errorCode, stars: json.stargazers_count },
}
}
export default function Page({ errorCode, stars }) {
if (errorCode) {
return <Error statusCode={errorCode} />
}
return <div>Next stars: {stars}</div>
}
The `Error` component also takes `title` as a property if you want to pass in a text message along with a `statusCode`.
If you have a custom `Error` component be sure to import that one instead. `next/error` exports the default component used by Next.js.
### [Caveats](#caveats)
* `Error` does not currently support Next.js [Data Fetching methods](/docs/pages/building-your-application/data-fetching) like [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) or [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props).
* `_error`, like `_app`, is a reserved pathname. `_error` is used to define the customized layouts and behaviors of the error pages. `/_error` will render 404 when accessed directly via [routing](/docs/pages/building-your-application/routing) or rendering in a [custom server](/docs/pages/building-your-application/configuring/custom-server).
[Previous
API Routes
](/docs/pages/building-your-application/routing/api-routes)
[Next
Internationalization
](/docs/pages/building-your-application/routing/internationalization)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2c81c6d860946e14f4c725c52685d1c6\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/middleware | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [
Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Use Cases](#use-cases)
* [Convention](#convention)
* [Example](#example)
* [Matching Paths](#matching-paths)
* [Matcher](#matcher)
* [Conditional Statements](#conditional-statements)
* [NextResponse](#nextresponse)
* [Using Cookies](#using-cookies)
* [Setting Headers](#setting-headers)
* [CORS](#cors)
* [Producing a Response](#producing-a-response)
* [waitUntil and NextFetchEvent](#waituntil-and-nextfetchevent)
* [Advanced Middleware Flags](#advanced-middleware-flags)
* [Runtime](#runtime)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/11-middleware.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Middleware
Middleware
==========
Middleware allows you to run code before a request is completed. Then, based on the incoming request, you can modify the response by rewriting, redirecting, modifying the request or response headers, or responding directly.
Middleware runs before cached content and routes are matched. See [Matching Paths](#matching-paths) for more details.
[Use Cases](#use-cases)
-----------------------
Integrating Middleware into your application can lead to significant improvements in performance, security, and user experience. Some common scenarios where Middleware is particularly effective include:
* Authentication and Authorization: Ensure user identity and check session cookies before granting access to specific pages or API routes.
* Server-Side Redirects: Redirect users at the server level based on certain conditions (e.g., locale, user role).
* Path Rewriting: Support A/B testing, feature rollouts, or legacy paths by dynamically rewriting paths to API routes or pages based on request properties.
* Bot Detection: Protect your resources by detecting and blocking bot traffic.
* Logging and Analytics: Capture and analyze request data for insights before processing by the page or API.
* Feature Flagging: Enable or disable features dynamically for seamless feature rollouts or testing.
Recognizing situations where middleware may not be the optimal approach is just as crucial. Here are some scenarios to be mindful of:
* Complex Data Fetching and Manipulation: Middleware is not designed for direct data fetching or manipulation, this should be done within Route Handlers or server-side utilities instead.
* Heavy Computational Tasks: Middleware should be lightweight and respond quickly or it can cause delays in page load. Heavy computational tasks or long-running processes should be done within dedicated Route Handlers.
* Extensive Session Management: While Middleware can manage basic session tasks, extensive session management should be managed by dedicated authentication services or within Route Handlers.
* Direct Database Operations: Performing direct database operations within Middleware is not recommended. Database interactions should done within Route Handlers or server-side utilities.
[Convention](#convention)
-------------------------
Use the file `middleware.ts` (or `.js`) in the root of your project to define Middleware. For example, at the same level as `pages` or `app`, or inside `src` if applicable.
> **Note**: While only one `middleware.ts` file is supported per project, you can still organize your middleware logic modularly. Break out middleware functionalities into separate `.ts` or `.js` files and import them into your main `middleware.ts` file. This allows for cleaner management of route-specific middleware, aggregated in the `middleware.ts` for centralized control. By enforcing a single middleware file, it simplifies configuration, prevents potential conflicts, and optimizes performance by avoiding multiple middleware layers.
[Example](#example)
-------------------
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
return NextResponse.redirect(new URL('/home', request.url))
}
// See "Matching Paths" below to learn more
export const config = {
matcher: '/about/:path*',
}
[Matching Paths](#matching-paths)
---------------------------------
Middleware will be invoked for **every route in your project**. Given this, it's crucial to use matchers to precisely target or exclude specific routes. The following is the execution order:
1. `headers` from `next.config.js`
2. `redirects` from `next.config.js`
3. Middleware (`rewrites`, `redirects`, etc.)
4. `beforeFiles` (`rewrites`) from `next.config.js`
5. Filesystem routes (`public/`, `_next/static/`, `pages/`, `app/`, etc.)
6. `afterFiles` (`rewrites`) from `next.config.js`
7. Dynamic Routes (`/blog/[slug]`)
8. `fallback` (`rewrites`) from `next.config.js`
There are two ways to define which paths Middleware will run on:
1. [Custom matcher config](#matcher)
2. [Conditional statements](#conditional-statements)
### [Matcher](#matcher)
`matcher` allows you to filter Middleware to run on specific paths.
middleware.js
export const config = {
matcher: '/about/:path*',
}
You can match a single path or multiple paths with an array syntax:
middleware.js
export const config = {
matcher: ['/about/:path*', '/dashboard/:path*'],
}
The `matcher` config allows full regex so matching like negative lookaheads or character matching is supported. An example of a negative lookahead to match all except specific paths can be seen here:
middleware.js
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!api|_next/static|_next/image|favicon.ico).*)',
],
}
You can also bypass Middleware for certain requests by using the `missing` or `has` arrays, or a combination of both:
middleware.js
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
has: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
{
source: '/((?!api|_next/static|_next/image|favicon.ico).*)',
has: [{ type: 'header', key: 'x-present' }],
missing: [{ type: 'header', key: 'x-missing', value: 'prefetch' }],
},
],
}
> **Good to know**: The `matcher` values need to be constants so they can be statically analyzed at build-time. Dynamic values such as variables will be ignored.
Configured matchers:
1. MUST start with `/`
2. Can include named parameters: `/about/:path` matches `/about/a` and `/about/b` but not `/about/a/c`
3. Can have modifiers on named parameters (starting with `:`): `/about/:path*` matches `/about/a/b/c` because `*` is _zero or more_. `?` is _zero or one_ and `+` _one or more_
4. Can use regular expression enclosed in parenthesis: `/about/(.*)` is the same as `/about/:path*`
Read more details on [path-to-regexp](https://github.com/pillarjs/path-to-regexp#path-to-regexp-1) documentation.
> **Good to know**: For backward compatibility, Next.js always considers `/public` as `/public/index`. Therefore, a matcher of `/public/:path` will match.
### [Conditional Statements](#conditional-statements)
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.startsWith('/about')) {
return NextResponse.rewrite(new URL('/about-2', request.url))
}
if (request.nextUrl.pathname.startsWith('/dashboard')) {
return NextResponse.rewrite(new URL('/dashboard/user', request.url))
}
}
[NextResponse](#nextresponse)
-----------------------------
The `NextResponse` API allows you to:
* `redirect` the incoming request to a different URL
* `rewrite` the response by displaying a given URL
* Set request headers for API Routes, `getServerSideProps`, and `rewrite` destinations
* Set response cookies
* Set response headers
To produce a response from Middleware, you can:
1. `rewrite` to a route ([Page](/docs/pages/building-your-application/routing/pages-and-layouts) or [Edge API Route](/docs/pages/building-your-application/routing/api-routes)) that produces a response
2. return a `NextResponse` directly. See [Producing a Response](#producing-a-response)
[Using Cookies](#using-cookies)
-------------------------------
Cookies are regular headers. On a `Request`, they are stored in the `Cookie` header. On a `Response` they are in the `Set-Cookie` header. Next.js provides a convenient way to access and manipulate these cookies through the `cookies` extension on `NextRequest` and `NextResponse`.
1. For incoming requests, `cookies` comes with the following methods: `get`, `getAll`, `set`, and `delete` cookies. You can check for the existence of a cookie with `has` or remove all cookies with `clear`.
2. For outgoing responses, `cookies` have the following methods `get`, `getAll`, `set`, and `delete`.
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
// Assume a "Cookie:nextjs=fast" header to be present on the incoming request
// Getting cookies from the request using the `RequestCookies` API
let cookie = request.cookies.get('nextjs')
console.log(cookie) // => { name: 'nextjs', value: 'fast', Path: '/' }
const allCookies = request.cookies.getAll()
console.log(allCookies) // => [{ name: 'nextjs', value: 'fast' }]
request.cookies.has('nextjs') // => true
request.cookies.delete('nextjs')
request.cookies.has('nextjs') // => false
// Setting cookies on the response using the `ResponseCookies` API
const response = NextResponse.next()
response.cookies.set('vercel', 'fast')
response.cookies.set({
name: 'vercel',
value: 'fast',
path: '/',
})
cookie = response.cookies.get('vercel')
console.log(cookie) // => { name: 'vercel', value: 'fast', Path: '/' }
// The outgoing response will have a `Set-Cookie:vercel=fast;path=/` header.
return response
}
[Setting Headers](#setting-headers)
-----------------------------------
You can set request and response headers using the `NextResponse` API (setting _request_ headers is available since Next.js v13.0.0).
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export function middleware(request: NextRequest) {
// Clone the request headers and set a new header `x-hello-from-middleware1`
const requestHeaders = new Headers(request.headers)
requestHeaders.set('x-hello-from-middleware1', 'hello')
// You can also set request headers in NextResponse.rewrite
const response = NextResponse.next({
request: {
// New request headers
headers: requestHeaders,
},
})
// Set a new response header `x-hello-from-middleware2`
response.headers.set('x-hello-from-middleware2', 'hello')
return response
}
> **Good to know**: Avoid setting large headers as it might cause [431 Request Header Fields Too Large](https://developer.mozilla.org/docs/Web/HTTP/Status/431) error depending on your backend web server configuration.
### [CORS](#cors)
You can set CORS headers in Middleware to allow cross-origin requests, including [simple](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests) and [preflighted](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests) requests.
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextRequest, NextResponse } from 'next/server'
const allowedOrigins = ['https://acme.com', 'https://my-app.org']
const corsOptions = {
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
}
export function middleware(request: NextRequest) {
// Check the origin from the request
const origin = request.headers.get('origin') ?? ''
const isAllowedOrigin = allowedOrigins.includes(origin)
// Handle preflighted requests
const isPreflight = request.method === 'OPTIONS'
if (isPreflight) {
const preflightHeaders = {
...(isAllowedOrigin && { 'Access-Control-Allow-Origin': origin }),
...corsOptions,
}
return NextResponse.json({}, { headers: preflightHeaders })
}
// Handle simple requests
const response = NextResponse.next()
if (isAllowedOrigin) {
response.headers.set('Access-Control-Allow-Origin', origin)
}
Object.entries(corsOptions).forEach(([key, value]) => {
response.headers.set(key, value)
})
return response
}
export const config = {
matcher: '/api/:path*',
}
[Producing a Response](#producing-a-response)
---------------------------------------------
You can respond from Middleware directly by returning a `Response` or `NextResponse` instance. (This is available since [Next.js v13.1.0](https://nextjs.org/blog/next-13-1#nextjs-advanced-middleware))
middleware.ts
TypeScript
JavaScriptTypeScript
import { NextRequest } from 'next/server'
import { isAuthenticated } from '@lib/auth'
// Limit the middleware to paths starting with `/api/`
export const config = {
matcher: '/api/:function*',
}
export function middleware(request: NextRequest) {
// Call our authentication function to check the request
if (!isAuthenticated(request)) {
// Respond with JSON indicating an error message
return Response.json(
{ success: false, message: 'authentication failed' },
{ status: 401 }
)
}
}
### [`waitUntil` and `NextFetchEvent`](#waituntil-and-nextfetchevent)
The `NextFetchEvent` object extends the native [`FetchEvent`](https://developer.mozilla.org/docs/Web/API/FetchEvent) object, and includes the [`waitUntil()`](https://developer.mozilla.org/docs/Web/API/ExtendableEvent/waitUntil) method.
The `waitUntil()` method takes a promise as an argument, and extends the lifetime of the Middleware until the promise settles. This is useful for performing work in the background.
middleware.ts
import { NextResponse } from 'next/server'
import type { NextFetchEvent, NextRequest } from 'next/server'
export function middleware(req: NextRequest, event: NextFetchEvent) {
event.waitUntil(
fetch('https://my-analytics-platform.com', {
method: 'POST',
body: JSON.stringify({ pathname: req.nextUrl.pathname }),
})
)
return NextResponse.next()
}
[Advanced Middleware Flags](#advanced-middleware-flags)
-------------------------------------------------------
In `v13.1` of Next.js two additional flags were introduced for middleware, `skipMiddlewareUrlNormalize` and `skipTrailingSlashRedirect` to handle advanced use cases.
`skipTrailingSlashRedirect` disables Next.js redirects for adding or removing trailing slashes. This allows custom handling inside middleware to maintain the trailing slash for some paths but not others, which can make incremental migrations easier.
next.config.js
module.exports = {
skipTrailingSlashRedirect: true,
}
middleware.js
const legacyPrefixes = ['/docs', '/blog']
export default async function middleware(req) {
const { pathname } = req.nextUrl
if (legacyPrefixes.some((prefix) => pathname.startsWith(prefix))) {
return NextResponse.next()
}
// apply trailing slash handling
if (
!pathname.endsWith('/') &&
!pathname.match(/((?!\.well-known(?:\/.*)?)(?:[^/]+\/)*[^/]+\.\w+)/)
) {
req.nextUrl.pathname += '/'
return NextResponse.redirect(req.nextUrl)
}
}
`skipMiddlewareUrlNormalize` allows for disabling the URL normalization in Next.js to make handling direct visits and client-transitions the same. In some advanced cases, this option provides full control by using the original URL.
next.config.js
module.exports = {
skipMiddlewareUrlNormalize: true,
}
middleware.js
export default async function middleware(req) {
const { pathname } = req.nextUrl
// GET /_next/data/build-id/hello.json
console.log(pathname)
// with the flag this now /_next/data/build-id/hello.json
// without the flag this would be normalized to /hello
}
[Runtime](#runtime)
-------------------
Middleware currently only supports the [Edge runtime](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes). The Node.js runtime can not be used.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.1.0`
Advanced Middleware flags added
`v13.0.0`
Middleware can modify request headers, response headers, and send responses
`v12.2.0`
Middleware is stable, please see the [upgrade guide](/docs/messages/middleware-upgrade-guide)
`v12.0.9`
Enforce absolute URLs in Edge Runtime ([PR](https://github.com/vercel/next.js/pull/33410))
`v12.0.0`
Middleware (Beta) added
[Previous
Internationalization
](/docs/pages/building-your-application/routing/internationalization)
[Next
Rendering
](/docs/pages/building-your-application/rendering)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"ab04e6dac9cc957d52581ad1b8fd6bb1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/instrumentationHook | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [
instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Learn more about Instrumentation](#learn-more-about-instrumentation)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/instrumentationHook.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)instrumentationHook
instrumentationHook
===================
The experimental `instrumentationHook` option allows you to set up instrumentation via the [`instrumentation` file](/docs/app/api-reference/file-conventions/instrumentation) in your Next.js App.
next.config.js
module.exports = {
experimental: {
instrumentationHook: true,
},
}
Learn more about Instrumentation
--------------------------------
[
App Router
...
File Conventions
### instrumentation.js
API reference for the instrumentation.js file.
](/docs/app/api-reference/file-conventions/instrumentation)[
App Router
...
Optimizing
### Instrumentation
Learn how to use instrumentation to run code at server startup in your Next.js app
](/docs/app/building-your-application/optimizing/instrumentation)
[Previous
cacheHandler
](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
[Next
logging
](/docs/app/api-reference/next-config-js/logging)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"262a3725c82074258c071619006c497e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/trailingSlash | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [
trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/trailingSlash.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)trailingSlash
trailingSlash
=============
By default Next.js will redirect urls with trailing slashes to their counterpart without a trailing slash. For example `/about/` will redirect to `/about`. You can configure this behavior to act the opposite way, where urls without trailing slashes are redirected to their counterparts with trailing slashes.
Open `next.config.js` and add the `trailingSlash` config:
next.config.js
module.exports = {
trailingSlash: true,
}
With this option set, urls like `/about` will redirect to `/about/`.
When used with [`output: "export"`](/docs/app/building-your-application/deploying/static-exports) configuration, the `/about` page will output `/about/index.html` (instead of the default `/about.html`).
[Version History](#version-history)
-----------------------------------
Version
Changes
`v9.5.0`
`trailingSlash` added.
[Previous
StaleTimes (experimental)
](/docs/app/api-reference/next-config-js/staleTimes)
[Next
transpilePackages
](/docs/app/api-reference/next-config-js/transpilePackages)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"801f08879be99b436e95e02845747156\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing/internationalization | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [
Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Getting started](#getting-started)
* [Locale Strategies](#locale-strategies)
* [Sub-path Routing](#sub-path-routing)
* [Domain Routing](#domain-routing)
* [Automatic Locale Detection](#automatic-locale-detection)
* [Prefixing the Default Locale](#prefixing-the-default-locale)
* [Disabling Automatic Locale Detection](#disabling-automatic-locale-detection)
* [Accessing the locale information](#accessing-the-locale-information)
* [Transition between locales](#transition-between-locales)
* [Leveraging the NEXT\_LOCALE cookie](#leveraging-the-next_locale-cookie)
* [Search Engine Optimization](#search-engine-optimization)
* [How does this work with Static Generation?](#how-does-this-work-with-static-generation)
* [Dynamic Routes and getStaticProps Pages](#dynamic-routes-and-getstaticprops-pages)
* [Automatically Statically Optimized Pages](#automatically-statically-optimized-pages)
* [Non-dynamic getStaticProps Pages](#non-dynamic-getstaticprops-pages)
* [Limits for the i18n config](#limits-for-the-i18n-config)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/01-routing/10-internationalization.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Routing](/docs/pages/building-your-application/routing)Internationalization
Internationalization (i18n) Routing
===================================
Examples
* [i18n routing](https://github.com/vercel/next.js/tree/canary/examples/i18n-routing)
Next.js has built-in support for internationalized ([i18n](https://en.wikipedia.org/wiki/Internationalization_and_localization#Naming)) routing since `v10.0.0`. You can provide a list of locales, the default locale, and domain-specific locales and Next.js will automatically handle the routing.
The i18n routing support is currently meant to complement existing i18n library solutions like [`react-intl`](https://formatjs.io/docs/getting-started/installation), [`react-i18next`](https://react.i18next.com/), [`lingui`](https://lingui.dev/), [`rosetta`](https://github.com/lukeed/rosetta), [`next-intl`](https://github.com/amannn/next-intl), [`next-translate`](https://github.com/aralroca/next-translate), [`next-multilingual`](https://github.com/Avansai/next-multilingual), [`tolgee`](https://tolgee.io/integrations/next), and others by streamlining the routes and locale parsing.
[Getting started](#getting-started)
-----------------------------------
To get started, add the `i18n` config to your `next.config.js` file.
Locales are [UTS Locale Identifiers](https://www.unicode.org/reports/tr35/tr35-59/tr35.html#Identifiers), a standardized format for defining locales.
Generally a Locale Identifier is made up of a language, region, and script separated by a dash: `language-region-script`. The region and script are optional. An example:
* `en-US` - English as spoken in the United States
* `nl-NL` - Dutch as spoken in the Netherlands
* `nl` - Dutch, no specific region
If user locale is `nl-BE` and it is not listed in your configuration, they will be redirected to `nl` if available, or to the default locale otherwise. If you don't plan to support all regions of a country, it is therefore a good practice to include country locales that will act as fallbacks.
next.config.js
module.exports = {
i18n: {
// These are all the locales you want to support in
// your application
locales: ['en-US', 'fr', 'nl-NL'],
// This is the default locale you want to be used when visiting
// a non-locale prefixed path e.g. `/hello`
defaultLocale: 'en-US',
// This is a list of locale domains and the default locale they
// should handle (these are only required when setting up domain routing)
// Note: subdomains must be included in the domain value to be matched e.g. "fr.example.com".
domains: [
{
domain: 'example.com',
defaultLocale: 'en-US',
},
{
domain: 'example.nl',
defaultLocale: 'nl-NL',
},
{
domain: 'example.fr',
defaultLocale: 'fr',
// an optional http field can also be used to test
// locale domains locally with http instead of https
http: true,
},
],
},
}
[Locale Strategies](#locale-strategies)
---------------------------------------
There are two locale handling strategies: Sub-path Routing and Domain Routing.
### [Sub-path Routing](#sub-path-routing)
Sub-path Routing puts the locale in the url path.
next.config.js
module.exports = {
i18n: {
locales: ['en-US', 'fr', 'nl-NL'],
defaultLocale: 'en-US',
},
}
With the above configuration `en-US`, `fr`, and `nl-NL` will be available to be routed to, and `en-US` is the default locale. If you have a `pages/blog.js` the following urls would be available:
* `/blog`
* `/fr/blog`
* `/nl-nl/blog`
The default locale does not have a prefix.
### [Domain Routing](#domain-routing)
By using domain routing you can configure locales to be served from different domains:
next.config.js
module.exports = {
i18n: {
locales: ['en-US', 'fr', 'nl-NL', 'nl-BE'],
defaultLocale: 'en-US',
domains: [
{
// Note: subdomains must be included in the domain value to be matched
// e.g. www.example.com should be used if that is the expected hostname
domain: 'example.com',
defaultLocale: 'en-US',
},
{
domain: 'example.fr',
defaultLocale: 'fr',
},
{
domain: 'example.nl',
defaultLocale: 'nl-NL',
// specify other locales that should be redirected
// to this domain
locales: ['nl-BE'],
},
],
},
}
For example if you have `pages/blog.js` the following urls will be available:
* `example.com/blog`
* `www.example.com/blog`
* `example.fr/blog`
* `example.nl/blog`
* `example.nl/nl-BE/blog`
[Automatic Locale Detection](#automatic-locale-detection)
---------------------------------------------------------
When a user visits the application root (generally `/`), Next.js will try to automatically detect which locale the user prefers based on the [`Accept-Language`](https://developer.mozilla.org/docs/Web/HTTP/Headers/Accept-Language) header and the current domain.
If a locale other than the default locale is detected, the user will be redirected to either:
* **When using Sub-path Routing:** The locale prefixed path
* **When using Domain Routing:** The domain with that locale specified as the default
When using Domain Routing, if a user with the `Accept-Language` header `fr;q=0.9` visits `example.com`, they will be redirected to `example.fr` since that domain handles the `fr` locale by default.
When using Sub-path Routing, the user would be redirected to `/fr`.
### [Prefixing the Default Locale](#prefixing-the-default-locale)
With Next.js 12 and [Middleware](/docs/pages/building-your-application/routing/middleware), we can add a prefix to the default locale with a [workaround](https://github.com/vercel/next.js/discussions/18419).
For example, here's a `next.config.js` file with support for a few languages. Note the `"default"` locale has been added intentionally.
next.config.js
module.exports = {
i18n: {
locales: ['default', 'en', 'de', 'fr'],
defaultLocale: 'default',
localeDetection: false,
},
trailingSlash: true,
}
Next, we can use [Middleware](/docs/pages/building-your-application/routing/middleware) to add custom routing rules:
middleware.ts
import { NextRequest, NextResponse } from 'next/server'
const PUBLIC_FILE = /\.(.*)$/
export async function middleware(req: NextRequest) {
if (
req.nextUrl.pathname.startsWith('/_next') ||
req.nextUrl.pathname.includes('/api/') ||
PUBLIC_FILE.test(req.nextUrl.pathname)
) {
return
}
if (req.nextUrl.locale === 'default') {
const locale = req.cookies.get('NEXT_LOCALE')?.value || 'en'
return NextResponse.redirect(
new URL(`/${locale}${req.nextUrl.pathname}${req.nextUrl.search}`, req.url)
)
}
}
This [Middleware](/docs/pages/building-your-application/routing/middleware) skips adding the default prefix to [API Routes](/docs/pages/building-your-application/routing/api-routes) and [public](/docs/pages/building-your-application/optimizing/static-assets) files like fonts or images. If a request is made to the default locale, we redirect to our prefix `/en`.
### [Disabling Automatic Locale Detection](#disabling-automatic-locale-detection)
The automatic locale detection can be disabled with:
next.config.js
module.exports = {
i18n: {
localeDetection: false,
},
}
When `localeDetection` is set to `false` Next.js will no longer automatically redirect based on the user's preferred locale and will only provide locale information detected from either the locale based domain or locale path as described above.
[Accessing the locale information](#accessing-the-locale-information)
---------------------------------------------------------------------
You can access the locale information via the Next.js router. For example, using the [`useRouter()`](/docs/pages/api-reference/functions/use-router) hook the following properties are available:
* `locale` contains the currently active locale.
* `locales` contains all configured locales.
* `defaultLocale` contains the configured default locale.
When [pre-rendering](/docs/pages/building-your-application/rendering/static-site-generation) pages with `getStaticProps` or `getServerSideProps`, the locale information is provided in [the context](/docs/pages/building-your-application/data-fetching/get-static-props) provided to the function.
When leveraging `getStaticPaths`, the configured locales are provided in the context parameter of the function under `locales` and the configured defaultLocale under `defaultLocale`.
[Transition between locales](#transition-between-locales)
---------------------------------------------------------
You can use `next/link` or `next/router` to transition between locales.
For `next/link`, a `locale` prop can be provided to transition to a different locale from the currently active one. If no `locale` prop is provided, the currently active `locale` is used during client-transitions. For example:
import Link from 'next/link'
export default function IndexPage(props) {
return (
<Link href="/another" locale="fr">
To /fr/another
</Link>
)
}
When using the `next/router` methods directly, you can specify the `locale` that should be used via the transition options. For example:
import { useRouter } from 'next/router'
export default function IndexPage(props) {
const router = useRouter()
return (
<div
onClick={() => {
router.push('/another', '/another', { locale: 'fr' })
}}
>
to /fr/another
</div>
)
}
Note that to handle switching only the `locale` while preserving all routing information such as [dynamic route](/docs/pages/building-your-application/routing/dynamic-routes) query values or hidden href query values, you can provide the `href` parameter as an object:
import { useRouter } from 'next/router'
const router = useRouter()
const { pathname, asPath, query } = router
// change just the locale and maintain all other route information including href's query
router.push({ pathname, query }, asPath, { locale: nextLocale })
See [here](/docs/pages/api-reference/functions/use-router#with-url-object) for more information on the object structure for `router.push`.
If you have a `href` that already includes the locale you can opt-out of automatically handling the locale prefixing:
import Link from 'next/link'
export default function IndexPage(props) {
return (
<Link href="/fr/another" locale={false}>
To /fr/another
</Link>
)
}
[Leveraging the `NEXT_LOCALE` cookie](#leveraging-the-next_locale-cookie)
-------------------------------------------------------------------------
Next.js supports overriding the accept-language header with a `NEXT_LOCALE=the-locale` cookie. This cookie can be set using a language switcher and then when a user comes back to the site it will leverage the locale specified in the cookie when redirecting from `/` to the correct locale location.
For example, if a user prefers the locale `fr` in their accept-language header but a `NEXT_LOCALE=en` cookie is set the `en` locale when visiting `/` the user will be redirected to the `en` locale location until the cookie is removed or expired.
[Search Engine Optimization](#search-engine-optimization)
---------------------------------------------------------
Since Next.js knows what language the user is visiting it will automatically add the `lang` attribute to the `<html>` tag.
Next.js doesn't know about variants of a page so it's up to you to add the `hreflang` meta tags using [`next/head`](/docs/pages/api-reference/components/head). You can learn more about `hreflang` in the [Google Webmasters documentation](https://support.google.com/webmasters/answer/189077).
[How does this work with Static Generation?](#how-does-this-work-with-static-generation)
----------------------------------------------------------------------------------------
> Note that Internationalized Routing does not integrate with [`output: 'export'`](/docs/pages/building-your-application/deploying/static-exports) as it does not leverage the Next.js routing layer. Hybrid Next.js applications that do not use `output: 'export'` are fully supported.
### [Dynamic Routes and `getStaticProps` Pages](#dynamic-routes-and-getstaticprops-pages)
For pages using `getStaticProps` with [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes), all locale variants of the page desired to be prerendered need to be returned from [`getStaticPaths`](/docs/pages/building-your-application/data-fetching/get-static-paths). Along with the `params` object returned for `paths`, you can also return a `locale` field specifying which locale you want to render. For example:
pages/blog/\[slug\].js
export const getStaticPaths = ({ locales }) => {
return {
paths: [
// if no `locale` is provided only the defaultLocale will be generated
{ params: { slug: 'post-1' }, locale: 'en-US' },
{ params: { slug: 'post-1' }, locale: 'fr' },
],
fallback: true,
}
}
For [Automatically Statically Optimized](/docs/pages/building-your-application/rendering/automatic-static-optimization) and non-dynamic `getStaticProps` pages, **a version of the page will be generated for each locale**. This is important to consider because it can increase build times depending on how many locales are configured inside `getStaticProps`.
For example, if you have 50 locales configured with 10 non-dynamic pages using `getStaticProps`, this means `getStaticProps` will be called 500 times. 50 versions of the 10 pages will be generated during each build.
To decrease the build time of dynamic pages with `getStaticProps`, use a [`fallback` mode](/docs/pages/api-reference/functions/get-static-paths#fallback-true). This allows you to return only the most popular paths and locales from `getStaticPaths` for prerendering during the build. Then, Next.js will build the remaining pages at runtime as they are requested.
### [Automatically Statically Optimized Pages](#automatically-statically-optimized-pages)
For pages that are [automatically statically optimized](/docs/pages/building-your-application/rendering/automatic-static-optimization), a version of the page will be generated for each locale.
### [Non-dynamic getStaticProps Pages](#non-dynamic-getstaticprops-pages)
For non-dynamic `getStaticProps` pages, a version is generated for each locale like above. `getStaticProps` is called with each `locale` that is being rendered. If you would like to opt-out of a certain locale from being pre-rendered, you can return `notFound: true` from `getStaticProps` and this variant of the page will not be generated.
export async function getStaticProps({ locale }) {
// Call an external API endpoint to get posts.
// You can use any data fetching library
const res = await fetch(`https://.../posts?locale=${locale}`)
const posts = await res.json()
if (posts.length === 0) {
return {
notFound: true,
}
}
// By returning { props: posts }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts,
},
}
}
[Limits for the i18n config](#limits-for-the-i18n-config)
---------------------------------------------------------
* `locales`: 100 total locales
* `domains`: 100 total locale domain items
> **Good to know**: These limits have been added initially to prevent potential [performance issues at build time](#dynamic-routes-and-getstaticprops-pages). You can workaround these limits with custom routing using [Middleware](/docs/pages/building-your-application/routing/middleware) in Next.js 12.
[Previous
Custom Errors
](/docs/pages/building-your-application/routing/custom-error)
[Next
Middleware
](/docs/pages/building-your-application/routing/middleware)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"39f3013b37bf7986dee62197d2bea65e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/logging | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [
logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/logging.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)logging
logging
=======
You can configure the logging level and whether the full URL is logged to the console when running Next.js in development mode.
Currently, `logging` only applies to data fetching using the `fetch` API. It does not yet apply to other logs inside of Next.js.
next.config.js
module.exports = {
logging: {
fetches: {
fullUrl: true,
},
},
}
[Previous
instrumentationHook
](/docs/app/api-reference/next-config-js/instrumentationHook)
[Next
mdxRs
](/docs/app/api-reference/next-config-js/mdxRs)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"5c8168de820be142f5f11a57db644bea\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/staleTimes | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [
StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/staleTimes.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)StaleTimes (experimental)
StaleTimes (experimental)
=========================
> **Warning**: The `staleTimes` configuration is an experimental feature. This configuration strategy will likely change in the future.
`staleTimes` is an experimental feature that allows configuring the [invalidation period](/docs/app/building-your-application/caching#duration-3) of the client router cache.
This configuration option is available as of v14.2.0-canary.53.
You can enable this experimental feature & provide custom revalidation times by setting the experimental `staleTimes` flag:
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
staleTimes: {
dynamic: 30,
static: 180,
},
},
}
module.exports = nextConfig
The `static` and `dynamic` properties correspond with the time period (in seconds) based on different types of [link prefetching](/docs/app/api-reference/components/link#prefetch).
* The `dynamic` property is used when the `prefetch` prop on `Link` is left unspecified.
* Default: 30 seconds
* The `static` property is used when the `prefetch` prop on `Link` is set to `true`, or when calling [`router.prefetch`](/docs/app/building-your-application/caching#routerprefetch).
* Default: 5 minutes
> **Good to know:**
>
> * [Loading boundaries](/docs/app/api-reference/file-conventions/loading) are considered reusable for the `static` period defined in this configuration.
> * This doesn't disable [partial rendering support](/docs/app/building-your-application/routing/linking-and-navigating#4-partial-rendering), **meaning shared layouts won't automatically be refetched every navigation, only the new segment data.**
> * This doesn't change [back/forward caching](/docs/app/building-your-application/caching#router-cache) behavior to prevent layout shift & to prevent losing the browser scroll position.
> * The different properties of this config refer to variable levels of "liveness" and are unrelated to whether the segment itself is opting into static or dynamic rendering. In other words, the current `static` default of 5 minutes suggests that data feels static by virtue of it being revalidated infrequently.
You can learn more about the Client Router Cache [here](/docs/app/building-your-application/caching#router-cache).
[Previous
serverComponentsExternalPackages
](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
[Next
trailingSlash
](/docs/app/api-reference/next-config-js/trailingSlash)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"8aa9a5dc8c5054b1f868e5873af459ee\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/styling/tailwind-css | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [
Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Installing Tailwind](#installing-tailwind)
* [Configuring Tailwind](#configuring-tailwind)
* [Importing Styles](#importing-styles-1)
* [Using Classes](#using-classes-1)
* [Usage with Turbopack](#usage-with-turbopack)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/04-styling/02-tailwind-css.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Styling](/docs/pages/building-your-application/styling)Tailwind CSS
Tailwind CSS
============
Examples
* [With Tailwind CSS](https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss)
[Tailwind CSS](https://tailwindcss.com/) is a utility-first CSS framework that works exceptionally well with Next.js.
[Installing Tailwind](#installing-tailwind)
-------------------------------------------
Install the Tailwind CSS packages and run the `init` command to generate both the `tailwind.config.js` and `postcss.config.js` files:
Terminal
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
[Configuring Tailwind](#configuring-tailwind)
---------------------------------------------
Inside `tailwind.config.js`, add paths to the files that will use Tailwind CSS class names:
tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./app/**/*.{js,ts,jsx,tsx,mdx}', // Note the addition of the `app` directory.
'./pages/**/*.{js,ts,jsx,tsx,mdx}',
'./components/**/*.{js,ts,jsx,tsx,mdx}',
// Or if using `src` directory:
'./src/**/*.{js,ts,jsx,tsx,mdx}',
],
theme: {
extend: {},
},
plugins: [],
}
You do not need to modify `postcss.config.js`.
[Importing Styles](#importing-styles-1)
---------------------------------------
Add the [Tailwind CSS directives](https://tailwindcss.com/docs/functions-and-directives#directives) that Tailwind will use to inject its generated styles to a [Global Stylesheet](/docs/pages/building-your-application/styling/css-modules#global-styles) in your application, for example:
styles/globals.css
@tailwind base;
@tailwind components;
@tailwind utilities;
Inside the [custom app file](/docs/pages/building-your-application/routing/custom-app) (`pages/_app.js`), import the `globals.css` stylesheet to apply the styles to every route in your application.
pages/\_app.tsx
TypeScript
JavaScriptTypeScript
// These styles apply to every route in the application
import '@/styles/globals.css'
import type { AppProps } from 'next/app'
export default function App({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
[Using Classes](#using-classes-1)
---------------------------------
After installing Tailwind CSS and adding the global styles, you can use Tailwind's utility classes in your application.
pages/index.tsx
TypeScript
JavaScriptTypeScript
export default function Page() {
return <h1 className="text-3xl font-bold underline">Hello, Next.js!</h1>
}
[Usage with Turbopack](#usage-with-turbopack)
---------------------------------------------
As of Next.js 13.1, Tailwind CSS and PostCSS are supported with [Turbopack](https://turbo.build/pack/docs/features/css#tailwind-css).
[Previous
CSS Modules
](/docs/pages/building-your-application/styling/css-modules)
[Next
CSS-in-JS
](/docs/pages/building-your-application/styling/css-in-js)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f5368013b26055bae7e03a3958bb26c2\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/mdxRs | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [
mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/mdxRs.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)mdxRs
mdxRs
=====
For use with `@next/mdx`. Compile MDX files using the new Rust compiler.
next.config.js
const withMDX = require('@next/mdx')()
/** @type {import('next').NextConfig} */
const nextConfig = {
pageExtensions: ['ts', 'tsx', 'mdx'],
experimental: {
mdxRs: true,
},
}
module.exports = withMDX(nextConfig)
[Previous
logging
](/docs/app/api-reference/next-config-js/logging)
[Next
onDemandEntries
](/docs/app/api-reference/next-config-js/onDemandEntries)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"66e9a66a2b9be93f2c4136c1eba21074\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/rendering/server-side-rendering | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [
Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/02-rendering/01-server-side-rendering.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Rendering](/docs/pages/building-your-application/rendering)Server-side Rendering (SSR)
Server-side Rendering (SSR)
===========================
> Also referred to as "SSR" or "Dynamic Rendering".
If a page uses **Server-side Rendering**, the page HTML is generated on **each request**.
To use Server-side Rendering for a page, you need to `export` an `async` function called `getServerSideProps`. This function will be called by the server on every request.
For example, suppose that your page needs to pre-render frequently updated data (fetched from an external API). You can write `getServerSideProps` which fetches this data and passes it to `Page` like below:
export default function Page({ data }) {
// Render data...
}
// This gets called on every request
export async function getServerSideProps() {
// Fetch data from external API
const res = await fetch(`https://.../data`)
const data = await res.json()
// Pass data to the page via props
return { props: { data } }
}
As you can see, `getServerSideProps` is similar to `getStaticProps`, but the difference is that `getServerSideProps` is run on every request instead of on build time.
To learn more about how `getServerSideProps` works, check out our [Data Fetching documentation](/docs/pages/building-your-application/data-fetching/get-server-side-props).
[Previous
Rendering
](/docs/pages/building-your-application/rendering)
[Next
Static Site Generation (SSG)
](/docs/pages/building-your-application/rendering/static-site-generation)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"3050d75d9d11b9887dc20962f8e24d06\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/transpilePackages | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [
transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/transpilePackages.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)transpilePackages
transpilePackages
=================
Next.js can automatically transpile and bundle dependencies from local packages (like monorepos) or from external dependencies (`node_modules`). This replaces the `next-transpile-modules` package.
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: ['@acme/ui', 'lodash-es'],
}
module.exports = nextConfig
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
`transpilePackages` added.
[Previous
trailingSlash
](/docs/app/api-reference/next-config-js/trailingSlash)
[Next
turbo
](/docs/app/api-reference/next-config-js/turbo)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"d5b27276e7a26e660a8ab8ad46af0602\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/rendering | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Pre-rendering](#pre-rendering)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/02-rendering/index.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[Building Your Application](/docs/pages/building-your-application)Rendering
Rendering
=========
By default, Next.js **pre-renders** every page. This means that Next.js generates HTML for each page in advance, instead of having it all done by client-side JavaScript. Pre-rendering can result in better performance and SEO.
Each generated HTML is associated with minimal JavaScript code necessary for that page. When a page is loaded by the browser, its JavaScript code runs and makes the page fully interactive (this process is called [hydration](https://react.dev/reference/react-dom/client/hydrateRoot) in React).
### [Pre-rendering](#pre-rendering)
Next.js has two forms of pre-rendering: **Static Generation** and **Server-side Rendering**. The difference is in **when** it generates the HTML for a page.
* Static Generation: The HTML is generated at **build time** and will be reused on each request.
* Server-side Rendering: The HTML is generated on **each request**.
Importantly, Next.js lets you choose which pre-rendering form you'd like to use for each page. You can create a "hybrid" Next.js app by using Static Generation for most pages and using Server-side Rendering for others.
We recommend using Static Generation over Server-side Rendering for performance reasons. Statically generated pages can be cached by CDN with no extra configuration to boost performance. However, in some cases, Server-side Rendering might be the only option.
You can also use client-side data fetching along with Static Generation or Server-side Rendering. That means some parts of a page can be rendered entirely by clientside JavaScript. To learn more, take a look at the [Data Fetching](/docs/pages/building-your-application/data-fetching/client-side) documentation.
[
### Server-side Rendering (SSR)
Use Server-side Rendering to render pages on each request.
](/docs/pages/building-your-application/rendering/server-side-rendering)[
### Static Site Generation (SSG)
Use Static Site Generation (SSG) to pre-render pages at build time.
](/docs/pages/building-your-application/rendering/static-site-generation)[
### Automatic Static Optimization
Next.js automatically optimizes your app to be static HTML whenever possible. Learn how it works here.
](/docs/pages/building-your-application/rendering/automatic-static-optimization)[
### Client-side Rendering (CSR)
Learn how to implement client-side rendering in the Pages Router.
](/docs/pages/building-your-application/rendering/client-side-rendering)[
### Edge and Node.js Runtimes
Learn more about the switchable runtimes (Edge and Node.js) in Next.js.
](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
[Previous
Middleware
](/docs/pages/building-your-application/routing/middleware)
[Next
Server-side Rendering (SSR)
](/docs/pages/building-your-application/rendering/server-side-rendering)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"28bef53b759304c48b86be3670559f9e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/styling/css-in-js | Menu
Using Pages Router
Features available in /pages
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [
CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [Disabling JavaScript](#disabling-javascript)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/04-styling/03-css-in-js.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Styling](/docs/pages/building-your-application/styling)CSS-in-JS
CSS-in-JS
=========
Examples
* [Styled JSX](https://github.com/vercel/next.js/tree/canary/examples/with-styled-jsx)
* [Styled Components](https://github.com/vercel/next.js/tree/canary/examples/with-styled-components)
* [Emotion](https://github.com/vercel/next.js/tree/canary/examples/with-emotion)
* [Linaria](https://github.com/vercel/next.js/tree/canary/examples/with-linaria)
* [Styletron](https://github.com/vercel/next.js/tree/canary/examples/with-styletron)
* [Cxs](https://github.com/vercel/next.js/tree/canary/examples/with-cxs)
* [Fela](https://github.com/vercel/next.js/tree/canary/examples/with-fela)
* [Stitches](https://github.com/vercel/next.js/tree/canary/examples/with-stitches)
It's possible to use any existing CSS-in-JS solution. The simplest one is inline styles:
function HiThere() {
return <p style={{ color: 'red' }}>hi there</p>
}
export default HiThere
We bundle [styled-jsx](https://github.com/vercel/styled-jsx) to provide support for isolated scoped CSS. The aim is to support "shadow CSS" similar to Web Components, which unfortunately [do not support server-rendering and are JS-only](https://github.com/w3c/webcomponents/issues/71).
See the above examples for other popular CSS-in-JS solutions (like Styled Components).
A component using `styled-jsx` looks like this:
function HelloWorld() {
return (
<div>
Hello world
<p>scoped!</p>
<style jsx>{`
p {
color: blue;
}
div {
background: red;
}
@media (max-width: 600px) {
div {
background: blue;
}
}
`}</style>
<style global jsx>{`
body {
background: black;
}
`}</style>
</div>
)
}
export default HelloWorld
Please see the [styled-jsx documentation](https://github.com/vercel/styled-jsx) for more examples.
### [Disabling JavaScript](#disabling-javascript)
Yes, if you disable JavaScript the CSS will still be loaded in the production build (`next start`). During development, we require JavaScript to be enabled to provide the best developer experience with [Fast Refresh](https://nextjs.org/blog/next-9-4#fast-refresh).
[Previous
Tailwind CSS
](/docs/pages/building-your-application/styling/tailwind-css)
[Next
Sass
](/docs/pages/building-your-application/styling/sass)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"25c9683196061309f6f1aa64a052ac65\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/turbo | Menu
Using App Router
Features available in /app
* [Getting Started](/docs/getting-started)
* [Installation](/docs/getting-started/installation)
* [Project Structure](/docs/getting-started/project-structure)
* [Building Your Application](/docs/app/building-your-application)
* [Routing](/docs/app/building-your-application/routing)
* [Defining Routes](/docs/app/building-your-application/routing/defining-routes)
* [Pages and Layouts](/docs/app/building-your-application/routing/pages-and-layouts)
* [Linking and Navigating](/docs/app/building-your-application/routing/linking-and-navigating)
* [Loading UI and Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [Error Handling](/docs/app/building-your-application/routing/error-handling)
* [Redirecting](/docs/app/building-your-application/routing/redirecting)
* [Route Groups](/docs/app/building-your-application/routing/route-groups)
* [Project Organization](/docs/app/building-your-application/routing/colocation)
* [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes)
* [Parallel Routes](/docs/app/building-your-application/routing/parallel-routes)
* [Intercepting Routes](/docs/app/building-your-application/routing/intercepting-routes)
* [Route Handlers](/docs/app/building-your-application/routing/route-handlers)
* [Middleware](/docs/app/building-your-application/routing/middleware)
* [Internationalization](/docs/app/building-your-application/routing/internationalization)
* [Data Fetching](/docs/app/building-your-application/data-fetching)
* [Fetching, Caching, and Revalidating](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating)
* [Server Actions and Mutations](/docs/app/building-your-application/data-fetching/server-actions-and-mutations)
* [Data Fetching Patterns and Best Practices](/docs/app/building-your-application/data-fetching/patterns)
* [Rendering](/docs/app/building-your-application/rendering)
* [Server Components](/docs/app/building-your-application/rendering/server-components)
* [Client Components](/docs/app/building-your-application/rendering/client-components)
* [Composition Patterns](/docs/app/building-your-application/rendering/composition-patterns)
* [Edge and Node.js Runtimes](/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Caching](/docs/app/building-your-application/caching)
* [Styling](/docs/app/building-your-application/styling)
* [CSS Modules](/docs/app/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/app/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/app/building-your-application/styling/css-in-js)
* [Sass](/docs/app/building-your-application/styling/sass)
* [Optimizing](/docs/app/building-your-application/optimizing)
* [Images](/docs/app/building-your-application/optimizing/images)
* [Videos](/docs/app/building-your-application/optimizing/videos)
* [Fonts](/docs/app/building-your-application/optimizing/fonts)
* [Metadata](/docs/app/building-your-application/optimizing/metadata)
* [Scripts](/docs/app/building-your-application/optimizing/scripts)
* [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer)
* [Lazy Loading](/docs/app/building-your-application/optimizing/lazy-loading)
* [Analytics](/docs/app/building-your-application/optimizing/analytics)
* [Instrumentation](/docs/app/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/app/building-your-application/optimizing/open-telemetry)
* [Static Assets](/docs/app/building-your-application/optimizing/static-assets)
* [Third Party Libraries](/docs/app/building-your-application/optimizing/third-party-libraries)
* [Memory Usage](/docs/app/building-your-application/optimizing/memory-usage)
* [Configuring](/docs/app/building-your-application/configuring)
* [TypeScript](/docs/app/building-your-application/configuring/typescript)
* [ESLint](/docs/app/building-your-application/configuring/eslint)
* [Environment Variables](/docs/app/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/app/building-your-application/configuring/absolute-imports-and-module-aliases)
* [MDX](/docs/app/building-your-application/configuring/mdx)
* [src Directory](/docs/app/building-your-application/configuring/src-directory)
* [Draft Mode](/docs/app/building-your-application/configuring/draft-mode)
* [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)
* [Testing](/docs/app/building-your-application/testing)
* [Vitest](/docs/app/building-your-application/testing/vitest)
* [Jest](/docs/app/building-your-application/testing/jest)
* [Playwright](/docs/app/building-your-application/testing/playwright)
* [Cypress](/docs/app/building-your-application/testing/cypress)
* [Authentication](/docs/app/building-your-application/authentication)
* [Deploying](/docs/app/building-your-application/deploying)
* [Production Checklist](/docs/app/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/app/building-your-application/deploying/static-exports)
* [Upgrading](/docs/app/building-your-application/upgrading)
* [Codemods](/docs/app/building-your-application/upgrading/codemods)
* [App Router Migration](/docs/app/building-your-application/upgrading/app-router-migration)
* [Version 14](/docs/app/building-your-application/upgrading/version-14)
* [Migrating from Vite](/docs/app/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/app/building-your-application/upgrading/from-create-react-app)
* [API Reference](/docs/app/api-reference)
* [Components](/docs/app/api-reference/components)
* [Font](/docs/app/api-reference/components/font)
* [<Image>](/docs/app/api-reference/components/image)
* [<Link>](/docs/app/api-reference/components/link)
* [<Script>](/docs/app/api-reference/components/script)
* [File Conventions](/docs/app/api-reference/file-conventions)
* [default.js](/docs/app/api-reference/file-conventions/default)
* [error.js](/docs/app/api-reference/file-conventions/error)
* [instrumentation.js](/docs/app/api-reference/file-conventions/instrumentation)
* [layout.js](/docs/app/api-reference/file-conventions/layout)
* [loading.js](/docs/app/api-reference/file-conventions/loading)
* [middleware.js](/docs/app/api-reference/file-conventions/middleware)
* [not-found.js](/docs/app/api-reference/file-conventions/not-found)
* [page.js](/docs/app/api-reference/file-conventions/page)
* [route.js](/docs/app/api-reference/file-conventions/route)
* [Route Segment Config](/docs/app/api-reference/file-conventions/route-segment-config)
* [template.js](/docs/app/api-reference/file-conventions/template)
* [Metadata Files](/docs/app/api-reference/file-conventions/metadata)
* [favicon, icon, and apple-icon](/docs/app/api-reference/file-conventions/metadata/app-icons)
* [manifest.json](/docs/app/api-reference/file-conventions/metadata/manifest)
* [opengraph-image and twitter-image](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
* [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
* [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
* [Functions](/docs/app/api-reference/functions)
* [cookies](/docs/app/api-reference/functions/cookies)
* [draftMode](/docs/app/api-reference/functions/draft-mode)
* [fetch](/docs/app/api-reference/functions/fetch)
* [generateImageMetadata](/docs/app/api-reference/functions/generate-image-metadata)
* [generateMetadata](/docs/app/api-reference/functions/generate-metadata)
* [generateSitemaps](/docs/app/api-reference/functions/generate-sitemaps)
* [generateStaticParams](/docs/app/api-reference/functions/generate-static-params)
* [generateViewport](/docs/app/api-reference/functions/generate-viewport)
* [headers](/docs/app/api-reference/functions/headers)
* [ImageResponse](/docs/app/api-reference/functions/image-response)
* [NextRequest](/docs/app/api-reference/functions/next-request)
* [NextResponse](/docs/app/api-reference/functions/next-response)
* [notFound](/docs/app/api-reference/functions/not-found)
* [permanentRedirect](/docs/app/api-reference/functions/permanentRedirect)
* [redirect](/docs/app/api-reference/functions/redirect)
* [revalidatePath](/docs/app/api-reference/functions/revalidatePath)
* [revalidateTag](/docs/app/api-reference/functions/revalidateTag)
* [unstable\_cache](/docs/app/api-reference/functions/unstable_cache)
* [unstable\_noStore](/docs/app/api-reference/functions/unstable_noStore)
* [useParams](/docs/app/api-reference/functions/use-params)
* [usePathname](/docs/app/api-reference/functions/use-pathname)
* [useReportWebVitals](/docs/app/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/app/api-reference/functions/use-router)
* [useSearchParams](/docs/app/api-reference/functions/use-search-params)
* [useSelectedLayoutSegment](/docs/app/api-reference/functions/use-selected-layout-segment)
* [useSelectedLayoutSegments](/docs/app/api-reference/functions/use-selected-layout-segments)
* [userAgent](/docs/app/api-reference/functions/userAgent)
* [next.config.js Options](/docs/app/api-reference/next-config-js)
* [appDir](/docs/app/api-reference/next-config-js/appDir)
* [assetPrefix](/docs/app/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/app/api-reference/next-config-js/basePath)
* [compress](/docs/app/api-reference/next-config-js/compress)
* [crossOrigin](/docs/app/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/app/api-reference/next-config-js/devIndicators)
* [distDir](/docs/app/api-reference/next-config-js/distDir)
* [env](/docs/app/api-reference/next-config-js/env)
* [eslint](/docs/app/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/app/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/app/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/app/api-reference/next-config-js/generateEtags)
* [headers](/docs/app/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/app/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/app/api-reference/next-config-js/images)
* [cacheHandler](/docs/app/api-reference/next-config-js/incrementalCacheHandlerPath)
* [instrumentationHook](/docs/app/api-reference/next-config-js/instrumentationHook)
* [logging](/docs/app/api-reference/next-config-js/logging)
* [mdxRs](/docs/app/api-reference/next-config-js/mdxRs)
* [onDemandEntries](/docs/app/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/app/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/app/api-reference/next-config-js/output)
* [pageExtensions](/docs/app/api-reference/next-config-js/pageExtensions)
* [Partial Prerendering (experimental)](/docs/app/api-reference/next-config-js/partial-prerendering)
* [poweredByHeader](/docs/app/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/app/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/app/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/app/api-reference/next-config-js/redirects)
* [rewrites](/docs/app/api-reference/next-config-js/rewrites)
* [serverActions](/docs/app/api-reference/next-config-js/serverActions)
* [serverComponentsExternalPackages](/docs/app/api-reference/next-config-js/serverComponentsExternalPackages)
* [StaleTimes (experimental)](/docs/app/api-reference/next-config-js/staleTimes)
* [trailingSlash](/docs/app/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/app/api-reference/next-config-js/transpilePackages)
* [
turbo](/docs/app/api-reference/next-config-js/turbo)
* [typedRoutes](/docs/app/api-reference/next-config-js/typedRoutes)
* [typescript](/docs/app/api-reference/next-config-js/typescript)
* [urlImports](/docs/app/api-reference/next-config-js/urlImports)
* [webpack](/docs/app/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/app/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/app/api-reference/create-next-app)
* [Edge Runtime](/docs/app/api-reference/edge)
* [Next.js CLI](/docs/app/api-reference/next-cli)
* [Building Your Application](/docs/pages/building-your-application)
* [Routing](/docs/pages/building-your-application/routing)
* [Pages and Layouts](/docs/pages/building-your-application/routing/pages-and-layouts)
* [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes)
* [Linking and Navigating](/docs/pages/building-your-application/routing/linking-and-navigating)
* [Redirecting](/docs/pages/building-your-application/routing/redirecting)
* [Custom App](/docs/pages/building-your-application/routing/custom-app)
* [Custom Document](/docs/pages/building-your-application/routing/custom-document)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Custom Errors](/docs/pages/building-your-application/routing/custom-error)
* [Internationalization](/docs/pages/building-your-application/routing/internationalization)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Rendering](/docs/pages/building-your-application/rendering)
* [Server-side Rendering (SSR)](/docs/pages/building-your-application/rendering/server-side-rendering)
* [Static Site Generation (SSG)](/docs/pages/building-your-application/rendering/static-site-generation)
* [Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization)
* [Client-side Rendering (CSR)](/docs/pages/building-your-application/rendering/client-side-rendering)
* [Edge and Node.js Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
* [Data Fetching](/docs/pages/building-your-application/data-fetching)
* [getStaticProps](/docs/pages/building-your-application/data-fetching/get-static-props)
* [getStaticPaths](/docs/pages/building-your-application/data-fetching/get-static-paths)
* [Forms and Mutations](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
* [getServerSideProps](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* [Incremental Static Regeneration (ISR)](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Client-side Fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [Styling](/docs/pages/building-your-application/styling)
* [CSS Modules](/docs/pages/building-your-application/styling/css-modules)
* [Tailwind CSS](/docs/pages/building-your-application/styling/tailwind-css)
* [CSS-in-JS](/docs/pages/building-your-application/styling/css-in-js)
* [Sass](/docs/pages/building-your-application/styling/sass)
* [Optimizing](/docs/pages/building-your-application/optimizing)
* [Images](/docs/pages/building-your-application/optimizing/images)
* [Fonts](/docs/pages/building-your-application/optimizing/fonts)
* [Scripts](/docs/pages/building-your-application/optimizing/scripts)
* [Static Assets](/docs/pages/building-your-application/optimizing/static-assets)
* [Bundle Analyzer](/docs/pages/building-your-application/optimizing/bundle-analyzer)
* [Analytics](/docs/pages/building-your-application/optimizing/analytics)
* [Lazy Loading](/docs/pages/building-your-application/optimizing/lazy-loading)
* [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation)
* [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry)
* [Third Party Libraries](/docs/pages/building-your-application/optimizing/third-party-libraries)
* [Configuring](/docs/pages/building-your-application/configuring)
* [TypeScript](/docs/pages/building-your-application/configuring/typescript)
* [ESLint](/docs/pages/building-your-application/configuring/eslint)
* [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables)
* [Absolute Imports and Module Path Aliases](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
* [src Directory](/docs/pages/building-your-application/configuring/src-directory)
* [MDX](/docs/pages/building-your-application/configuring/mdx)
* [AMP](/docs/pages/building-your-application/configuring/amp)
* [Babel](/docs/pages/building-your-application/configuring/babel)
* [PostCSS](/docs/pages/building-your-application/configuring/post-css)
* [Custom Server](/docs/pages/building-your-application/configuring/custom-server)
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [Error Handling](/docs/pages/building-your-application/configuring/error-handling)
* [Debugging](/docs/pages/building-your-application/configuring/debugging)
* [Preview Mode](/docs/pages/building-your-application/configuring/preview-mode)
* [Content Security Policy](/docs/pages/building-your-application/configuring/content-security-policy)
* [Testing](/docs/pages/building-your-application/testing)
* [Vitest](/docs/pages/building-your-application/testing/vitest)
* [Jest](/docs/pages/building-your-application/testing/jest)
* [Playwright](/docs/pages/building-your-application/testing/playwright)
* [Cypress](/docs/pages/building-your-application/testing/cypress)
* [Authentication](/docs/pages/building-your-application/authentication)
* [Deploying](/docs/pages/building-your-application/deploying)
* [Production Checklist](/docs/pages/building-your-application/deploying/production-checklist)
* [Static Exports](/docs/pages/building-your-application/deploying/static-exports)
* [Multi Zones](/docs/pages/building-your-application/deploying/multi-zones)
* [Continuous Integration (CI) Build Caching](/docs/pages/building-your-application/deploying/ci-build-caching)
* [Upgrading](/docs/pages/building-your-application/upgrading)
* [Codemods](/docs/pages/building-your-application/upgrading/codemods)
* [From Pages to App](/docs/pages/building-your-application/upgrading/app-router-migration)
* [Migrating from Vite](/docs/pages/building-your-application/upgrading/from-vite)
* [Migrating from Create React App](/docs/pages/building-your-application/upgrading/from-create-react-app)
* [Version 14](/docs/pages/building-your-application/upgrading/version-14)
* [Version 13](/docs/pages/building-your-application/upgrading/version-13)
* [Version 12](/docs/pages/building-your-application/upgrading/version-12)
* [Version 11](/docs/pages/building-your-application/upgrading/version-11)
* [Version 10](/docs/pages/building-your-application/upgrading/version-10)
* [Version 9](/docs/pages/building-your-application/upgrading/version-9)
* [API Reference](/docs/pages/api-reference)
* [Components](/docs/pages/api-reference/components)
* [Font](/docs/pages/api-reference/components/font)
* [<Head>](/docs/pages/api-reference/components/head)
* [<Image>](/docs/pages/api-reference/components/image)
* [<Image> (Legacy)](/docs/pages/api-reference/components/image-legacy)
* [<Link>](/docs/pages/api-reference/components/link)
* [<Script>](/docs/pages/api-reference/components/script)
* [Functions](/docs/pages/api-reference/functions)
* [getInitialProps](/docs/pages/api-reference/functions/get-initial-props)
* [getServerSideProps](/docs/pages/api-reference/functions/get-server-side-props)
* [getStaticPaths](/docs/pages/api-reference/functions/get-static-paths)
* [getStaticProps](/docs/pages/api-reference/functions/get-static-props)
* [NextRequest](/docs/pages/api-reference/functions/next-request)
* [NextResponse](/docs/pages/api-reference/functions/next-response)
* [useAmp](/docs/pages/api-reference/functions/use-amp)
* [useReportWebVitals](/docs/pages/api-reference/functions/use-report-web-vitals)
* [useRouter](/docs/pages/api-reference/functions/use-router)
* [userAgent](/docs/pages/api-reference/functions/userAgent)
* [next.config.js Options](/docs/pages/api-reference/next-config-js)
* [assetPrefix](/docs/pages/api-reference/next-config-js/assetPrefix)
* [basePath](/docs/pages/api-reference/next-config-js/basePath)
* [compress](/docs/pages/api-reference/next-config-js/compress)
* [crossOrigin](/docs/pages/api-reference/next-config-js/crossOrigin)
* [devIndicators](/docs/pages/api-reference/next-config-js/devIndicators)
* [distDir](/docs/pages/api-reference/next-config-js/distDir)
* [env](/docs/pages/api-reference/next-config-js/env)
* [eslint](/docs/pages/api-reference/next-config-js/eslint)
* [exportPathMap](/docs/pages/api-reference/next-config-js/exportPathMap)
* [generateBuildId](/docs/pages/api-reference/next-config-js/generateBuildId)
* [generateEtags](/docs/pages/api-reference/next-config-js/generateEtags)
* [headers](/docs/pages/api-reference/next-config-js/headers)
* [httpAgentOptions](/docs/pages/api-reference/next-config-js/httpAgentOptions)
* [images](/docs/pages/api-reference/next-config-js/images)
* [instrumentationHook](/docs/pages/api-reference/next-config-js/instrumentationHook)
* [onDemandEntries](/docs/pages/api-reference/next-config-js/onDemandEntries)
* [optimizePackageImports](/docs/pages/api-reference/next-config-js/optimizePackageImports)
* [output](/docs/pages/api-reference/next-config-js/output)
* [pageExtensions](/docs/pages/api-reference/next-config-js/pageExtensions)
* [poweredByHeader](/docs/pages/api-reference/next-config-js/poweredByHeader)
* [productionBrowserSourceMaps](/docs/pages/api-reference/next-config-js/productionBrowserSourceMaps)
* [reactStrictMode](/docs/pages/api-reference/next-config-js/reactStrictMode)
* [redirects](/docs/pages/api-reference/next-config-js/redirects)
* [rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Runtime Config](/docs/pages/api-reference/next-config-js/runtime-configuration)
* [trailingSlash](/docs/pages/api-reference/next-config-js/trailingSlash)
* [transpilePackages](/docs/pages/api-reference/next-config-js/transpilePackages)
* [turbo](/docs/pages/api-reference/next-config-js/turbo)
* [typescript](/docs/pages/api-reference/next-config-js/typescript)
* [urlImports](/docs/pages/api-reference/next-config-js/urlImports)
* [webpack](/docs/pages/api-reference/next-config-js/webpack)
* [webVitalsAttribution](/docs/pages/api-reference/next-config-js/webVitalsAttribution)
* [create-next-app](/docs/pages/api-reference/create-next-app)
* [Next.js CLI](/docs/pages/api-reference/next-cli)
* [Edge Runtime](/docs/pages/api-reference/edge)
* [Architecture](/docs/architecture)
* [Accessibility](/docs/architecture/accessibility)
* [Fast Refresh](/docs/architecture/fast-refresh)
* [Next.js Compiler](/docs/architecture/nextjs-compiler)
* [Supported Browsers](/docs/architecture/supported-browsers)
* [Turbopack](/docs/architecture/turbopack)
* [Community](/docs/community)
* [Contribution Guide](/docs/community/contribution-guide)
On this page
* [webpack loaders](#webpack-loaders)
* [Supported loaders](#supported-loaders)
* [Resolve aliases](#resolve-aliases)
* [Resolve extensions](#resolve-extensions)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/turbo.mdx) [Managed Next.js (Vercel)](https://vercel.com/solutions/nextjs?utm_source=next_site&utm_medium=web&utm_campaign=managed_nextjs_solutions_page) Scroll to top
[App Router](/docs/app)[...](/docs/app/api-reference)[next.config.js Options](/docs/app/api-reference/next-config-js)turbo
turbo (Experimental)
====================
Turbopack can be customized to transform different files and change how modules are resolved.
> **Good to know**:
>
> * These features are experimental and will only work with `next --turbo`.
> * Turbopack for Next.js does not require loaders nor loader configuration for built-in functionality. Turbopack has built-in support for css and compiling modern JavaScript, so there's no need for `css-loader`, `postcss-loader`, or `babel-loader` if you're using `@babel/preset-env`.
[webpack loaders](#webpack-loaders)
-----------------------------------
If you need loader support beyond what's built in, many webpack loaders already work with Turbopack. There are currently some limitations:
* Only a core subset of the webpack loader API is implemented. Currently, there is enough coverage for some popular loaders, and we'll expand our API support in the future.
* Only loaders that return JavaScript code are supported. Loaders that transform files like stylesheets or images are not currently supported.
* Options passed to webpack loaders must be plain JavaScript primitives, objects, and arrays. For example, it's not possible to pass `require()`d plugin modules as option values.
To configure loaders, add the names of the loaders you've installed and any options in `next.config.js`, mapping file extensions to a list of loaders:
next.config.js
module.exports = {
experimental: {
turbo: {
rules: {
'*.svg': {
loaders: ['@svgr/webpack'],
as: '*.js',
},
},
},
},
}
> **Good to know**: Prior to Next.js version 13.4.4, `experimental.turbo.rules` was named `experimental.turbo.loaders` and only accepted file extensions like `.mdx` instead of `*.mdx`.
### [Supported loaders](#supported-loaders)
The following loaders have been tested to work with Turbopack's webpack loader implementation:
* [`babel-loader`](https://www.npmjs.com/package/babel-loader)
* [`@svgr/webpack`](https://www.npmjs.com/package/@svgr/webpack)
* [`svg-inline-loader`](https://www.npmjs.com/package/svg-inline-loader)
* [`yaml-loader`](https://www.npmjs.com/package/yaml-loader)
* [`string-replace-loader`](https://www.npmjs.com/package/string-replace-loader)
* [`raw-loader`](https://www.npmjs.com/package/raw-loader)
* ['sass-loader'](https://www.npmjs.com/package/sass-loader)
[Resolve aliases](#resolve-aliases)
-----------------------------------
Through `next.config.js`, Turbopack can be configured to modify module resolution through aliases, similar to webpack's [`resolve.alias`](https://webpack.js.org/configuration/resolve/#resolvealias) configuration.
To configure resolve aliases, map imported patterns to their new destination in `next.config.js`:
next.config.js
module.exports = {
experimental: {
turbo: {
resolveAlias: {
underscore: 'lodash',
mocha: { browser: 'mocha/browser-entry.js' },
},
},
},
}
This aliases imports of the `underscore` package to the `lodash` package. In other words, `import underscore from 'underscore'` will load the `lodash` module instead of `underscore`.
Turbopack also supports conditional aliasing through this field, similar to Node.js's [conditional exports](https://nodejs.org/docs/latest-v18.x/api/packages.html#conditional-exports). At the moment only the `browser` condition is supported. In the case above, imports of the `mocha` module will be aliased to `mocha/browser-entry.js` when Turbopack targets browser environments.
[Resolve extensions](#resolve-extensions)
-----------------------------------------
Through `next.config.js`, Turbopack can be configured to resolve modules with custom extensions, similar to webpack's [`resolve.extensions`](https://webpack.js.org/configuration/resolve/#resolveextensions) configuration.
To configure resolve extensions, use the `resolveExtensions` field in `next.config.js`:
next.config.js
module.exports = {
experimental: {
turbo: {
resolveExtensions: [
'.mdx',
'.tsx',
'.ts',
'.jsx',
'.js',
'.mjs',
'.json',
],
},
},
}
This overwrites the original resolve extensions with the provided list. Make sure to include the default extensions.
For more information and guidance for how to migrate your app to Turbopack from webpack, see [Turbopack's documentation on webpack compatibility](https://turbo.build/pack/docs/migrating-from-webpack).
[Previous
transpilePackages
](/docs/app/api-reference/next-config-js/transpilePackages)
[Next
typedRoutes
](/docs/app/api-reference/next-config-js/typedRoutes)
Was this helpful?
supported.
Send |