metadata
dict | url
stringlengths 19
103
| markdown
stringlengths 199
68.6k
|
---|---|---|
{
"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/\"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/\"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/\"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/\"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/\"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/\"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/\"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/\"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/\"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/\"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/\"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/\"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/\"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/\"578433aa2846ed0d2e4a95e392a9dabd\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/styling/sass | 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
* [Customizing Sass Options](#customizing-sass-options)
* [Sass Variables](#sass-variables)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/04-styling/04-sass.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)Sass
Sass
====
Next.js has built-in support for integrating with Sass after the package is installed using both the `.scss` and `.sass` extensions. You can use component-level Sass via CSS Modules and the `.module.scss`or `.module.sass` extension.
First, install [`sass`](https://github.com/sass/sass):
Terminal
npm install --save-dev sass
> **Good to know**:
>
> Sass supports [two different syntaxes](https://sass-lang.com/documentation/syntax), each with their own extension. The `.scss` extension requires you use the [SCSS syntax](https://sass-lang.com/documentation/syntax#scss), while the `.sass` extension requires you use the [Indented Syntax ("Sass")](https://sass-lang.com/documentation/syntax#the-indented-syntax).
>
> If you're not sure which to choose, start with the `.scss` extension which is a superset of CSS, and doesn't require you learn the Indented Syntax ("Sass").
### [Customizing Sass Options](#customizing-sass-options)
If you want to configure the Sass compiler, use `sassOptions` in `next.config.js`.
next.config.js
const path = require('path')
module.exports = {
sassOptions: {
includePaths: [path.join(__dirname, 'styles')],
},
}
### [Sass Variables](#sass-variables)
Next.js supports Sass variables exported from CSS Module files.
For example, using the exported `primaryColor` Sass variable:
app/variables.module.scss
$primary-color: #64ff00;
:export {
primaryColor: $primary-color;
}
pages/\_app.js
import variables from '../styles/variables.module.scss'
export default function MyApp({ Component, pageProps }) {
return (
<Layout color={variables.primaryColor}>
<Component {...pageProps} />
</Layout>
)
}
[Previous
CSS-in-JS
](/docs/pages/building-your-application/styling/css-in-js)
[Next
Optimizing
](/docs/pages/building-your-application/optimizing)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"d6d6b27283ddb9875618b022a7772d73\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing | 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
* [Built-in Components](#built-in-components)
* [Metadata](#metadata)
* [Static Assets](#static-assets)
* [Analytics and Monitoring](#analytics-and-monitoring)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/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)Optimizing
Optimizations
=============
Next.js comes with a variety of built-in optimizations designed to improve your application's speed and [Core Web Vitals](https://web.dev/vitals/). This guide will cover the optimizations you can leverage to enhance your user experience.
[Built-in Components](#built-in-components)
-------------------------------------------
Built-in components abstract away the complexity of implementing common UI optimizations. These components are:
* **Images**: Built on the native `<img>` element. The Image Component optimizes images for performance by lazy loading and automatically resizing images based on device size.
* **Link**: Built on the native `<a>` tags. The Link Component prefetches pages in the background, for faster and smoother page transitions.
* **Scripts**: Built on the native `<script>` tags. The Script Component gives you control over loading and execution of third-party scripts.
[Metadata](#metadata)
---------------------
Metadata helps search engines understand your content better (which can result in better SEO), and allows you to customize how your content is presented on social media, helping you create a more engaging and consistent user experience across various platforms.
The Head Component in Next.js allows you to modify the `<head>` of a page. Learn more in the [Head Component](/docs/pages/api-reference/components/head) documentation.
[Static Assets](#static-assets)
-------------------------------
Next.js `/public` folder can be used to serve static assets like images, fonts, and other files. Files inside `/public` can also be cached by CDN providers so that they are delivered efficiently.
[Analytics and Monitoring](#analytics-and-monitoring)
-----------------------------------------------------
For large applications, Next.js integrates with popular analytics and monitoring tools to help you understand how your application is performing. Learn more in the [Analytics](/docs/app/building-your-application/optimizing/analytics), [OpenTelemetry](/docs/pages/building-your-application/optimizing/open-telemetry), and [Instrumentation](/docs/pages/building-your-application/optimizing/instrumentation) guides.
[
### Images
Optimize your images with the built-in \`next/image\` component.
](/docs/pages/building-your-application/optimizing/images)[
### Fonts
Optimize your application's web fonts with the built-in \`next/font\` loaders.
](/docs/pages/building-your-application/optimizing/fonts)[
### Scripts
Optimize 3rd party scripts with the built-in Script component.
](/docs/pages/building-your-application/optimizing/scripts)[
### Static Assets
Next.js allows you to serve static files, like images, in the public directory. You can learn how it works here.
](/docs/pages/building-your-application/optimizing/static-assets)[
### Bundle Analyzer
Analyze the size of your JavaScript bundles using the @next/bundle-analyzer plugin.
](/docs/pages/building-your-application/optimizing/bundle-analyzer)[
### Analytics
Measure and track page performance using Next.js Speed Insights
](/docs/pages/building-your-application/optimizing/analytics)[
### Lazy Loading
Lazy load imported libraries and React Components to improve your application's loading performance.
](/docs/pages/building-your-application/optimizing/lazy-loading)[
### Instrumentation
Learn how to use instrumentation to run code at server startup in your Next.js app
](/docs/pages/building-your-application/optimizing/instrumentation)[
### OpenTelemetry
Learn how to instrument your Next.js app with OpenTelemetry.
](/docs/pages/building-your-application/optimizing/open-telemetry)[
### Third Party Libraries
Optimize the performance of third-party libraries in your application with the \`@next/third-parties\` package.
](/docs/pages/building-your-application/optimizing/third-party-libraries)
[Previous
Sass
](/docs/pages/building-your-application/styling/sass)
[Next
Images
](/docs/pages/building-your-application/optimizing/images)
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/\"78bc13b209f1e233fc3f522f96512a0c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/onDemandEntries | 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/onDemandEntries.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)onDemandEntries
onDemandEntries
===============
Next.js exposes some options that give you some control over how the server will dispose or keep in memory built pages in development.
To change the defaults, open `next.config.js` and add the `onDemandEntries` config:
next.config.js
module.exports = {
onDemandEntries: {
// period (in ms) where the server will keep pages in the buffer
maxInactiveAge: 25 * 1000,
// number of pages that should be kept simultaneously without being disposed
pagesBufferLength: 2,
},
}
[Previous
mdxRs
](/docs/app/api-reference/next-config-js/mdxRs)
[Next
optimizePackageImports
](/docs/app/api-reference/next-config-js/optimizePackageImports)
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 |
{
"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/\"f136e69b21048fb16aeafb07952f8df4\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/rendering/client-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)
On this page
* [Next Steps](#next-steps)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/02-rendering/05-client-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)Client-side Rendering (CSR)
Client-side Rendering (CSR)
===========================
In Client-Side Rendering (CSR) with React, the browser downloads a minimal HTML page and the JavaScript needed for the page. The JavaScript is then used to update the DOM and render the page. When the application is first loaded, the user may notice a slight delay before they can see the full page, this is because the page isn't fully rendered until all the JavaScript is downloaded, parsed, and executed.
After the page has been loaded for the first time, navigating to other pages on the same website is typically faster, as only necessary data needs to be fetched, and JavaScript can re-render parts of the page without requiring a full page refresh.
In Next.js, there are two ways you can implement client-side rendering:
1. Using React's `useEffect()` hook inside your pages instead of the server-side rendering methods ([`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) and [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props)).
2. Using a data fetching library like [SWR](https://swr.vercel.app/) or [TanStack Query](https://tanstack.com/query/latest/) to fetch data on the client (recommended).
Here's an example of using `useEffect()` inside a Next.js page:
pages/index.js
import React, { useState, useEffect } from 'react'
export function Page() {
const [data, setData] = useState(null)
useEffect(() => {
const fetchData = async () => {
const response = await fetch('https://api.example.com/data')
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`)
}
const result = await response.json()
setData(result)
}
fetchData().catch((e) => {
// handle the error as needed
console.error('An error occurred while fetching the data: ', e)
})
}, [])
return <p>{data ? `Your data: ${data}` : 'Loading...'}</p>
}
In the example above, the component starts by rendering `Loading...`. Then, once the data is fetched, it re-renders and displays the data.
Although fetching data in a `useEffect` is a pattern you may see in older React Applications, we recommend using a data-fetching library for better performance, caching, optimistic updates, and more. Here's a minimum example using [SWR](https://swr.vercel.app/) to fetch data on the client:
pages/index.js
import useSWR from 'swr'
export function Page() {
const { data, error, isLoading } = useSWR(
'https://api.example.com/data',
fetcher
)
if (error) return <p>Failed to load.</p>
if (isLoading) return <p>Loading...</p>
return <p>Your Data: {data}</p>
}
> **Good to know**:
>
> Keep in mind that CSR can impact SEO. Some search engine crawlers might not execute JavaScript and therefore only see the initial empty or loading state of your application. It can also lead to performance issues for users with slower internet connections or devices, as they need to wait for all the JavaScript to load and run before they can see the full page. Next.js promotes a hybrid approach that allows you to use a combination of [server-side rendering](/docs/pages/building-your-application/rendering/server-side-rendering), [static site generation](/docs/pages/building-your-application/rendering/static-site-generation), and client-side rendering, **depending on the needs of each page** in your application. In the App Router, you can also use [Loading UI with Suspense](/docs/app/building-your-application/routing/loading-ui-and-streaming) to show a loading indicator while the page is being rendered.
Next Steps
----------
Learn about the alternative rendering methods in Next.js.
[
Pages Router
...
Rendering
### Server-side Rendering (SSR)
Use Server-side Rendering to render pages on each request.
](/docs/pages/building-your-application/rendering/server-side-rendering)[
Pages Router
...
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)[
Pages Router
...
Data Fetching
### Incremental Static Regeneration (ISR)
Learn how to create or update static pages at runtime with Incremental Static Regeneration.
](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)[
App Router
...
Routing
### Loading UI and Streaming
Built on top of Suspense, Loading UI allows you to create a fallback for specific route segments, and automatically stream content as it becomes ready.
](/docs/app/building-your-application/routing/loading-ui-and-streaming)
[Previous
Automatic Static Optimization
](/docs/pages/building-your-application/rendering/automatic-static-optimization)
[Next
Edge and Node.js Runtimes
](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"883b54e29688c1dc9df3cfca6a2146b7\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes | 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
* [Runtime Differences](#runtime-differences)
* [Edge Runtime](#edge-runtime)
* [Node.js Runtime](#nodejs-runtime)
* [Serverless Node.js](#serverless-nodejs)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/02-rendering/06-edge-and-nodejs-runtimes.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)Edge and Node.js Runtimes
Edge and Node.js Runtimes
=========================
In the context of Next.js, runtime refers to the set of libraries, APIs, and general functionality available to your code during execution.
On the server, there are two runtimes where parts of your application code can be rendered:
* The **Node.js Runtime** (default) has access to all Node.js APIs and compatible packages from the ecosystem.
* The **Edge Runtime** is based on [Web APIs](/docs/app/api-reference/edge).
[Runtime Differences](#runtime-differences)
-------------------------------------------
There are many considerations to make when choosing a runtime. This table shows the major differences at a glance. If you want a more in-depth analysis of the differences, check out the sections below.
Node
Serverless
Edge
Cold Boot
/
Normal
Low
[HTTP Streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming)
Yes
Yes
Yes
IO
All
All
`fetch`
Scalability
/
High
Highest
Security
Normal
High
High
Latency
Normal
Low
Lowest
npm Packages
All
All
A smaller subset
[Static Rendering](/docs/app/building-your-application/rendering/server-components#static-rendering-default)
Yes
Yes
No
[Dynamic Rendering](/docs/app/building-your-application/rendering/server-components#dynamic-rendering)
Yes
Yes
Yes
[Data Revalidation w/ `fetch`](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data)
Yes
Yes
Yes
### [Edge Runtime](#edge-runtime)
In Next.js, the lightweight Edge Runtime is a subset of available Node.js APIs.
The Edge Runtime is ideal if you need to deliver dynamic, personalized content at low latency with small, simple functions. The Edge Runtime's speed comes from its minimal use of resources, but that can be limiting in many scenarios.
For example, code executed in the Edge Runtime [on Vercel cannot exceed between 1 MB and 4 MB](https://vercel.com/docs/concepts/limits/overview#edge-middleware-and-edge-functions-size), this limit includes imported packages, fonts and files, and will vary depending on your deployment infrastructure. In addition, the Edge Runtime does not support all Node.js APIs meaning some `npm` packages may not work. For example, "Module not found: Can't resolve 'fs'" or similar errors. We recommend using the Node.js runtime if you need to use these APIs or packages.
### [Node.js Runtime](#nodejs-runtime)
Using the Node.js runtime gives you access to all Node.js APIs, and all npm packages that rely on them. However, it's not as fast to start up as routes using the Edge runtime.
Deploying your Next.js application to a Node.js server will require managing, scaling, and configuring your infrastructure. Alternatively, you can consider deploying your Next.js application to a serverless platform like Vercel, which will handle this for you.
### [Serverless Node.js](#serverless-nodejs)
Serverless is ideal if you need a scalable solution that can handle more complex computational loads than the Edge Runtime. With Serverless Functions on Vercel, for example, your overall code size is [50MB](https://vercel.com/docs/concepts/limits/overview#serverless-function-size) including imported packages, fonts, and files.
The downside compared to routes using the [Edge](https://vercel.com/docs/concepts/functions/edge-functions) is that it can take hundreds of milliseconds for Serverless Functions to boot up before they begin processing requests. Depending on the amount of traffic your site receives, this could be a frequent occurrence as the functions are not frequently "warm".
> Please refer to the [Node.js Docs](https://nodejs.org/docs/latest/api/) and [Edge Docs](/docs/app/api-reference/edge) for the full list of available APIs. Both runtimes can also support [streaming](/docs/app/building-your-application/routing/loading-ui-and-streaming) depending on your deployment infrastructure.
[Previous
Client-side Rendering (CSR)
](/docs/pages/building-your-application/rendering/client-side-rendering)
[Next
Data Fetching
](/docs/pages/building-your-application/data-fetching)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"7e0aaa7768633b138113c304381eccdc\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/typedRoutes | 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/typedRoutes.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)typedRoutes
typedRoutes (experimental)
==========================
Experimental support for [statically typed links](/docs/app/building-your-application/configuring/typescript#statically-typed-links). This feature requires using the App Router as well as TypeScript in your project.
next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
typedRoutes: true,
},
}
module.exports = nextConfig
[Previous
turbo
](/docs/app/api-reference/next-config-js/turbo)
[Next
typescript
](/docs/app/api-reference/next-config-js/typescript)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"e18698cec431d7378807b13f3ae54f50\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/typescript | 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/typescript.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)typescript
typescript
==========
Next.js fails your **production build** (`next build`) when TypeScript errors are present in your project.
If you'd like Next.js to dangerously produce production code even when your application has errors, you can disable the built-in type checking step.
If disabled, be sure you are running type checks as part of your build or deploy process, otherwise this can be very dangerous.
Open `next.config.js` and enable the `ignoreBuildErrors` option in the `typescript` config:
next.config.js
module.exports = {
typescript: {
// !! WARN !!
// Dangerously allow production builds to successfully complete even if
// your project has type errors.
// !! WARN !!
ignoreBuildErrors: true,
},
}
[Previous
typedRoutes
](/docs/app/api-reference/next-config-js/typedRoutes)
[Next
urlImports
](/docs/app/api-reference/next-config-js/urlImports)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"44f9a9d873c6fbb07670ae073b1778a2\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/rendering/automatic-static-optimization | 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
* [How it works](#how-it-works)
* [Caveats](#caveats)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/02-rendering/04-automatic-static-optimization.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)Automatic Static Optimization
Automatic Static Optimization
=============================
Next.js automatically determines that a page is static (can be prerendered) if it has no blocking data requirements. This determination is made by the absence of `getServerSideProps` and `getInitialProps` in the page.
This feature allows Next.js to emit hybrid applications that contain **both server-rendered and statically generated pages**.
> Statically generated pages are still reactive: Next.js will hydrate your application client-side to give it full interactivity.
One of the main benefits of this feature is that optimized pages require no server-side computation, and can be instantly streamed to the end-user from multiple CDN locations. The result is an _ultra fast_ loading experience for your users.
[How it works](#how-it-works)
-----------------------------
If `getServerSideProps` or `getInitialProps` is present in a page, Next.js will switch to render the page on-demand, per-request (meaning [Server-Side Rendering](/docs/pages/building-your-application/rendering/server-side-rendering)).
If the above is not the case, Next.js will **statically optimize** your page automatically by prerendering the page to static HTML.
During prerendering, the router's `query` object will be empty since we do not have `query` information to provide during this phase. After hydration, Next.js will trigger an update to your application to provide the route parameters in the `query` object.
The cases where the query will be updated after hydration triggering another render are:
* The page is a [dynamic-route](/docs/pages/building-your-application/routing/dynamic-routes).
* The page has query values in the URL.
* [Rewrites](/docs/pages/api-reference/next-config-js/rewrites) are configured in your `next.config.js` since these can have parameters that may need to be parsed and provided in the `query`.
To be able to distinguish if the query is fully updated and ready for use, you can leverage the `isReady` field on [`next/router`](/docs/pages/api-reference/functions/use-router#router-object).
> **Good to know**: Parameters added with [dynamic routes](/docs/pages/building-your-application/routing/dynamic-routes) to a page that's using [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) will always be available inside the `query` object.
`next build` will emit `.html` files for statically optimized pages. For example, the result for the page `pages/about.js` would be:
Terminal
.next/server/pages/about.html
And if you add `getServerSideProps` to the page, it will then be JavaScript, like so:
Terminal
.next/server/pages/about.js
[Caveats](#caveats)
-------------------
* If you have a [custom `App`](/docs/pages/building-your-application/routing/custom-app) with `getInitialProps` then this optimization will be turned off in pages without [Static Generation](/docs/pages/building-your-application/data-fetching/get-static-props).
* If you have a [custom `Document`](/docs/pages/building-your-application/routing/custom-document) with `getInitialProps` be sure you check if `ctx.req` is defined before assuming the page is server-side rendered. `ctx.req` will be `undefined` for pages that are prerendered.
* Avoid using the `asPath` value on [`next/router`](/docs/pages/api-reference/functions/use-router#router-object) in the rendering tree until the router's `isReady` field is `true`. Statically optimized pages only know `asPath` on the client and not the server, so using it as a prop may lead to mismatch errors. The [`active-class-name` example](https://github.com/vercel/next.js/tree/canary/examples/active-class-name) demonstrates one way to use `asPath` as a prop.
[Previous
Static Site Generation (SSG)
](/docs/pages/building-your-application/rendering/static-site-generation)
[Next
Client-side Rendering (CSR)
](/docs/pages/building-your-application/rendering/client-side-rendering)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"89fe1e96bd724ad2ffd8b5241a5be18c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/rendering/static-site-generation | 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
* [Static Generation without data](#static-generation-without-data)
* [Static Generation with data](#static-generation-with-data)
* [Scenario 1: Your page content depends on external data](#scenario-1-your-page-content-depends-on-external-data)
* [Scenario 2: Your page paths depend on external data](#scenario-2-your-page-paths-depend-on-external-data)
* [When should I use Static Generation?](#when-should-i-use-static-generation)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/02-rendering/02-static-site-generation.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)Static Site Generation (SSG)
Static Site Generation (SSG)
============================
Examples
* [WordPress Example](https://github.com/vercel/next.js/tree/canary/examples/cms-wordpress)([Demo](https://next-blog-wordpress.vercel.app))
* [Blog Starter using markdown files](https://github.com/vercel/next.js/tree/canary/examples/blog-starter) ([Demo](https://next-blog-starter.vercel.app/))
* [DatoCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-datocms) ([Demo](https://next-blog-datocms.vercel.app/))
* [TakeShape Example](https://github.com/vercel/next.js/tree/canary/examples/cms-takeshape) ([Demo](https://next-blog-takeshape.vercel.app/))
* [Sanity Example](https://github.com/vercel/next.js/tree/canary/examples/cms-sanity) ([Demo](https://next-blog-sanity.vercel.app/))
* [Prismic Example](https://github.com/vercel/next.js/tree/canary/examples/cms-prismic) ([Demo](https://next-blog-prismic.vercel.app/))
* [Contentful Example](https://github.com/vercel/next.js/tree/canary/examples/cms-contentful) ([Demo](https://next-blog-contentful.vercel.app/))
* [Strapi Example](https://github.com/vercel/next.js/tree/canary/examples/cms-strapi) ([Demo](https://next-blog-strapi.vercel.app/))
* [Prepr Example](https://github.com/vercel/next.js/tree/canary/examples/cms-prepr) ([Demo](https://next-blog-prepr.vercel.app/))
* [Agility CMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-agilitycms) ([Demo](https://next-blog-agilitycms.vercel.app/))
* [Cosmic Example](https://github.com/vercel/next.js/tree/canary/examples/cms-cosmic) ([Demo](https://next-blog-cosmic.vercel.app/))
* [ButterCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-buttercms) ([Demo](https://next-blog-buttercms.vercel.app/))
* [Storyblok Example](https://github.com/vercel/next.js/tree/canary/examples/cms-storyblok) ([Demo](https://next-blog-storyblok.vercel.app/))
* [GraphCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-graphcms) ([Demo](https://next-blog-graphcms.vercel.app/))
* [Kontent Example](https://github.com/vercel/next.js/tree/canary/examples/cms-kontent-ai) ([Demo](https://next-blog-kontent.vercel.app/))
* [Builder.io Example](https://github.com/vercel/next.js/tree/canary/examples/cms-builder-io) ([Demo](https://cms-builder-io.vercel.app/))
* [TinaCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-tina) ([Demo](https://cms-tina-example.vercel.app/))
* [Static Tweet (Demo)](https://static-tweet.vercel.app/)
* [Enterspeed Example](https://github.com/vercel/next.js/tree/canary/examples/cms-enterspeed) ([Demo](https://next-blog-demo.enterspeed.com/))
If a page uses **Static Generation**, the page HTML is generated at **build time**. That means in production, the page HTML is generated when you run `next build`. This HTML will then be reused on each request. It can be cached by a CDN.
In Next.js, you can statically generate pages **with or without data**. Let's take a look at each case.
### [Static Generation without data](#static-generation-without-data)
By default, Next.js pre-renders pages using Static Generation without fetching data. Here's an example:
function About() {
return <div>About</div>
}
export default About
Note that this page does not need to fetch any external data to be pre-rendered. In cases like this, Next.js generates a single HTML file per page during build time.
### [Static Generation with data](#static-generation-with-data)
Some pages require fetching external data for pre-rendering. There are two scenarios, and one or both might apply. In each case, you can use these functions that Next.js provides:
1. Your page **content** depends on external data: Use `getStaticProps`.
2. Your page **paths** depend on external data: Use `getStaticPaths` (usually in addition to `getStaticProps`).
#### [Scenario 1: Your page content depends on external data](#scenario-1-your-page-content-depends-on-external-data)
**Example**: Your blog page might need to fetch the list of blog posts from a CMS (content management system).
// TODO: Need to fetch `posts` (by calling some API endpoint)
// before this page can be pre-rendered.
export default function Blog({ posts }) {
return (
<ul>
{posts.map((post) => (
<li>{post.title}</li>
))}
</ul>
)
}
To fetch this data on pre-render, Next.js allows you to `export` an `async` function called `getStaticProps` from the same file. This function gets called at build time and lets you pass fetched data to the page's `props` on pre-render.
export default function Blog({ posts }) {
// Render posts...
}
// This function gets called at build time
export async function getStaticProps() {
// Call an external API endpoint to get posts
const res = await fetch('https://.../posts')
const posts = await res.json()
// By returning { props: { posts } }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts,
},
}
}
To learn more about how `getStaticProps` works, check out the [Data Fetching documentation](/docs/pages/building-your-application/data-fetching/get-static-props).
#### [Scenario 2: Your page paths depend on external data](#scenario-2-your-page-paths-depend-on-external-data)
Next.js allows you to create pages with **dynamic routes**. For example, you can create a file called `pages/posts/[id].js` to show a single blog post based on `id`. This will allow you to show a blog post with `id: 1` when you access `posts/1`.
> To learn more about dynamic routing, check the [Dynamic Routing documentation](/docs/pages/building-your-application/routing/dynamic-routes).
However, which `id` you want to pre-render at build time might depend on external data.
**Example**: suppose that you've only added one blog post (with `id: 1`) to the database. In this case, you'd only want to pre-render `posts/1` at build time.
Later, you might add the second post with `id: 2`. Then you'd want to pre-render `posts/2` as well.
So your page **paths** that are pre-rendered depend on external data. To handle this, Next.js lets you `export` an `async` function called `getStaticPaths` from a dynamic page (`pages/posts/[id].js` in this case). This function gets called at build time and lets you specify which paths you want to pre-render.
// This function gets called at build time
export async function getStaticPaths() {
// Call an external API endpoint to get posts
const res = await fetch('https://.../posts')
const posts = await res.json()
// Get the paths we want to pre-render based on posts
const paths = posts.map((post) => ({
params: { id: post.id },
}))
// We'll pre-render only these paths at build time.
// { fallback: false } means other routes should 404.
return { paths, fallback: false }
}
Also in `pages/posts/[id].js`, you need to export `getStaticProps` so that you can fetch the data about the post with this `id` and use it to pre-render the page:
export default function Post({ post }) {
// Render post...
}
export async function getStaticPaths() {
// ...
}
// This also gets called at build time
export async function getStaticProps({ params }) {
// params contains the post `id`.
// If the route is like /posts/1, then params.id is 1
const res = await fetch(`https://.../posts/${params.id}`)
const post = await res.json()
// Pass post data to the page via props
return { props: { post } }
}
To learn more about how `getStaticPaths` works, check out the [Data Fetching documentation](/docs/pages/building-your-application/data-fetching/get-static-paths).
### [When should I use Static Generation?](#when-should-i-use-static-generation)
We recommend using **Static Generation** (with and without data) whenever possible because your page can be built once and served by CDN, which makes it much faster than having a server render the page on every request.
You can use Static Generation for many types of pages, including:
* Marketing pages
* Blog posts and portfolios
* E-commerce product listings
* Help and documentation
You should ask yourself: "Can I pre-render this page **ahead** of a user's request?" If the answer is yes, then you should choose Static Generation.
On the other hand, Static Generation is **not** a good idea if you cannot pre-render a page ahead of a user's request. Maybe your page shows frequently updated data, and the page content changes on every request.
In cases like this, you can do one of the following:
* Use Static Generation with **Client-side data fetching:** You can skip pre-rendering some parts of a page and then use client-side JavaScript to populate them. To learn more about this approach, check out the [Data Fetching documentation](/docs/pages/building-your-application/data-fetching/client-side).
* Use **Server-Side Rendering:** Next.js pre-renders a page on each request. It will be slower because the page cannot be cached by a CDN, but the pre-rendered page will always be up-to-date. We'll talk about this approach below.
[Previous
Server-side Rendering (SSR)
](/docs/pages/building-your-application/rendering/server-side-rendering)
[Next
Automatic Static Optimization
](/docs/pages/building-your-application/rendering/automatic-static-optimization)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"0d33896cda3411c198b25bce891320b3\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/images | 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)
* [Local Images](#local-images)
* [Remote Images](#remote-images)
* [Domains](#domains)
* [Loaders](#loaders)
* [Priority](#priority)
* [Image Sizing](#image-sizing)
* [Styling](#styling)
* [Examples](#examples)
* [Responsive](#responsive)
* [Fill Container](#fill-container)
* [Background Image](#background-image)
* [Other Properties](#other-properties)
* [Configuration](#configuration)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/01-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/building-your-application/optimizing)Images
Image Optimization
==================
Examples
* [Image Component](https://github.com/vercel/next.js/tree/canary/examples/image-component)
According to [Web Almanac](https://almanac.httparchive.org), images account for a huge portion of the typical website’s [page weight](https://almanac.httparchive.org/en/2022/page-weight#content-type-and-file-formats) and can have a sizable impact on your website's [LCP performance](https://almanac.httparchive.org/en/2022/performance#lcp-image-optimization).
The Next.js Image component extends the HTML `<img>` element with features for automatic image optimization:
* **Size Optimization:** Automatically serve correctly sized images for each device, using modern image formats like WebP and AVIF.
* **Visual Stability:** Prevent [layout shift](/learn/seo/web-performance/cls) automatically when images are loading.
* **Faster Page Loads:** Images are only loaded when they enter the viewport using native browser lazy loading, with optional blur-up placeholders.
* **Asset Flexibility:** On-demand image resizing, even for images stored on remote servers
> **🎥 Watch:** Learn more about how to use `next/image` → [YouTube (9 minutes)](https://youtu.be/IU_qq_c_lKA).
[Usage](#usage)
---------------
import Image from 'next/image'
You can then define the `src` for your image (either local or remote).
### [Local Images](#local-images)
To use a local image, `import` your `.jpg`, `.png`, or `.webp` image files.
Next.js will [automatically determine](#image-sizing) the `width` and `height` of your image based on the imported file. These values are used to prevent [Cumulative Layout Shift](https://nextjs.org/learn/seo/web-performance/cls) while your image is loading.
pages/index.js
import Image from 'next/image'
import profilePic from '../public/me.png'
export default function Page() {
return (
<Image
src={profilePic}
alt="Picture of the author"
// width={500} automatically provided
// height={500} automatically provided
// blurDataURL="data:..." automatically provided
// placeholder="blur" // Optional blur-up while loading
/>
)
}
> **Warning:** Dynamic `await import()` or `require()` are _not_ supported. The `import` must be static so it can be analyzed at build time.
### [Remote Images](#remote-images)
To use a remote image, the `src` property should be a URL string.
Since Next.js does not have access to remote files during the build process, you'll need to provide the [`width`](/docs/app/api-reference/components/image#width), [`height`](/docs/app/api-reference/components/image#height) and optional [`blurDataURL`](/docs/app/api-reference/components/image#blurdataurl) props manually.
The `width` and `height` attributes are used to infer the correct aspect ratio of image and avoid layout shift from the image loading in. The `width` and `height` do _not_ determine the rendered size of the image file. Learn more about [Image Sizing](#image-sizing).
app/page.js
import Image from 'next/image'
export default function Page() {
return (
<Image
src="https://s3.amazonaws.com/my-bucket/profile.png"
alt="Picture of the author"
width={500}
height={500}
/>
)
}
To safely allow optimizing images, define a list of supported URL patterns in `next.config.js`. Be as specific as possible to prevent malicious usage. For example, the following configuration will only allow images from a specific AWS S3 bucket:
next.config.js
module.exports = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 's3.amazonaws.com',
port: '',
pathname: '/my-bucket/**',
},
],
},
}
Learn more about [`remotePatterns`](/docs/app/api-reference/components/image#remotepatterns) configuration. If you want to use relative URLs for the image `src`, use a [`loader`](/docs/app/api-reference/components/image#loader).
### [Domains](#domains)
Sometimes you may want to optimize a remote image, but still use the built-in Next.js Image Optimization API. To do this, leave the `loader` at its default setting and enter an absolute URL for the Image `src` prop.
To protect your application from malicious users, you must define a list of remote hostnames you intend to use with the `next/image` component.
> Learn more about [`remotePatterns`](/docs/app/api-reference/components/image#remotepatterns) configuration.
### [Loaders](#loaders)
Note that in the [example earlier](#local-images), a partial URL (`"/me.png"`) is provided for a local image. This is possible because of the loader architecture.
A loader is a function that generates the URLs for your image. It modifies the provided `src`, and generates multiple URLs to request the image at different sizes. These multiple URLs are used in the automatic [srcset](https://developer.mozilla.org/docs/Web/API/HTMLImageElement/srcset) generation, so that visitors to your site will be served an image that is the right size for their viewport.
The default loader for Next.js applications uses the built-in Image Optimization API, which optimizes images from anywhere on the web, and then serves them directly from the Next.js web server. If you would like to serve your images directly from a CDN or image server, you can write your own loader function with a few lines of JavaScript.
You can define a loader per-image with the [`loader` prop](/docs/app/api-reference/components/image#loader), or at the application level with the [`loaderFile` configuration](/docs/app/api-reference/components/image#loaderfile).
[Priority](#priority)
---------------------
You should add the `priority` property to the image that will be the [Largest Contentful Paint (LCP) element](https://web.dev/lcp/#what-elements-are-considered) for each page. Doing so allows Next.js to specially prioritize the image for loading (e.g. through preload tags or priority hints), leading to a meaningful boost in LCP.
The LCP element is typically the largest image or text block visible within the viewport of the page. When you run `next dev`, you'll see a console warning if the LCP element is an `<Image>` without the `priority` property.
Once you've identified the LCP image, you can add the property like this:
app/page.js
import Image from 'next/image'
export default function Home() {
return (
<>
<h1>My Homepage</h1>
<Image
src="/me.png"
alt="Picture of the author"
width={500}
height={500}
priority
/>
<p>Welcome to my homepage!</p>
</>
)
}
See more about priority in the [`next/image` component documentation](/docs/app/api-reference/components/image#priority).
[Image Sizing](#image-sizing)
-----------------------------
One of the ways that images most commonly hurt performance is through _layout shift_, where the image pushes other elements around on the page as it loads in. This performance problem is so annoying to users that it has its own Core Web Vital, called [Cumulative Layout Shift](https://web.dev/cls/). The way to avoid image-based layout shifts is to [always size your images](https://web.dev/optimize-cls/#images-without-dimensions). This allows the browser to reserve precisely enough space for the image before it loads.
Because `next/image` is designed to guarantee good performance results, it cannot be used in a way that will contribute to layout shift, and **must** be sized in one of three ways:
1. Automatically, using a [static import](#local-images)
2. Explicitly, by including a [`width`](/docs/app/api-reference/components/image#width) and [`height`](/docs/app/api-reference/components/image#height) property
3. Implicitly, by using [fill](/docs/app/api-reference/components/image#fill) which causes the image to expand to fill its parent element.
> **What if I don't know the size of my images?**
>
> If you are accessing images from a source without knowledge of the images' sizes, there are several things you can do:
>
> **Use `fill`**
>
> The [`fill`](/docs/app/api-reference/components/image#fill) prop allows your image to be sized by its parent element. Consider using CSS to give the image's parent element space on the page along [`sizes`](/docs/app/api-reference/components/image#sizes) prop to match any media query break points. You can also use [`object-fit`](https://developer.mozilla.org/docs/Web/CSS/object-fit) with `fill`, `contain`, or `cover`, and [`object-position`](https://developer.mozilla.org/docs/Web/CSS/object-position) to define how the image should occupy that space.
>
> **Normalize your images**
>
> If you're serving images from a source that you control, consider modifying your image pipeline to normalize the images to a specific size.
>
> **Modify your API calls**
>
> If your application is retrieving image URLs using an API call (such as to a CMS), you may be able to modify the API call to return the image dimensions along with the URL.
If none of the suggested methods works for sizing your images, the `next/image` component is designed to work well on a page alongside standard `<img>` elements.
[Styling](#styling)
-------------------
Styling the Image component is similar to styling a normal `<img>` element, but there are a few guidelines to keep in mind:
* Use `className` or `style`, not `styled-jsx`.
* In most cases, we recommend using the `className` prop. This can be an imported [CSS Module](/docs/app/building-your-application/styling/css-modules), a [global stylesheet](/docs/app/building-your-application/styling/css-modules#global-styles), etc.
* You can also use the `style` prop to assign inline styles.
* You cannot use [styled-jsx](/docs/app/building-your-application/styling/css-in-js) because it's scoped to the current component (unless you mark the style as `global`).
* When using `fill`, the parent element must have `position: relative`
* This is necessary for the proper rendering of the image element in that layout mode.
* When using `fill`, the parent element must have `display: block`
* This is the default for `<div>` elements but should be specified otherwise.
[Examples](#examples)
---------------------
### [Responsive](#responsive)
![Responsive image filling the width and height of its parent container](/_next/image?url=%2Fdocs%2Flight%2Fresponsive-image.png&w=3840&q=75)![Responsive image filling the width and height of its parent container](/_next/image?url=%2Fdocs%2Fdark%2Fresponsive-image.png&w=3840&q=75)
import Image from 'next/image'
import mountains from '../public/mountains.jpg'
export default function Responsive() {
return (
<div style={{ display: 'flex', flexDirection: 'column' }}>
<Image
alt="Mountains"
// Importing an image will
// automatically set the width and height
src={mountains}
sizes="100vw"
// Make the image display full width
style={{
width: '100%',
height: 'auto',
}}
/>
</div>
)
}
### [Fill Container](#fill-container)
![Grid of images filling parent container width](/_next/image?url=%2Fdocs%2Flight%2Ffill-container.png&w=3840&q=75)![Grid of images filling parent container width](/_next/image?url=%2Fdocs%2Fdark%2Ffill-container.png&w=3840&q=75)
import Image from 'next/image'
import mountains from '../public/mountains.jpg'
export default function Fill() {
return (
<div
style={{
display: 'grid',
gridGap: '8px',
gridTemplateColumns: 'repeat(auto-fit, minmax(400px, auto))',
}}
>
<div style={{ position: 'relative', height: '400px' }}>
<Image
alt="Mountains"
src={mountains}
fill
sizes="(min-width: 808px) 50vw, 100vw"
style={{
objectFit: 'cover', // cover, contain, none
}}
/>
</div>
{/* And more images in the grid... */}
</div>
)
}
### [Background Image](#background-image)
![Background image taking full width and height of page](/_next/image?url=%2Fdocs%2Flight%2Fbackground-image.png&w=3840&q=75)![Background image taking full width and height of page](/_next/image?url=%2Fdocs%2Fdark%2Fbackground-image.png&w=3840&q=75)
import Image from 'next/image'
import mountains from '../public/mountains.jpg'
export default function Background() {
return (
<Image
alt="Mountains"
src={mountains}
placeholder="blur"
quality={100}
fill
sizes="100vw"
style={{
objectFit: 'cover',
}}
/>
)
}
For examples of the Image component used with the various styles, see the [Image Component Demo](https://image-component.nextjs.gallery).
[Other Properties](#other-properties)
-------------------------------------
[**View all properties available to the `next/image` component.**](/docs/app/api-reference/components/image)
[Configuration](#configuration)
-------------------------------
The `next/image` component and Next.js Image Optimization API can be configured in the [`next.config.js` file](/docs/app/api-reference/next-config-js). These configurations allow you to [enable remote images](/docs/app/api-reference/components/image#remotepatterns), [define custom image breakpoints](/docs/app/api-reference/components/image#devicesizes), [change caching behavior](/docs/app/api-reference/components/image#caching-behavior) and more.
[**Read the full image configuration documentation for more information.**](/docs/app/api-reference/components/image#configuration-options)
[Previous
Optimizing
](/docs/pages/building-your-application/optimizing)
[Next
Fonts
](/docs/pages/building-your-application/optimizing/fonts)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2143c90ee1feb79e8a34daa6cf52d370\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/fonts | 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
* [Google Fonts](#google-fonts)
* [Apply the font in <head>](#apply-the-font-in-head)
* [Single page usage](#single-page-usage)
* [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)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/02-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/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.
To use the font in all your pages, add it to [`_app.js` file](/docs/pages/building-your-application/routing/custom-app) under `/pages` as shown below:
pages/\_app.js
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'] })
export default function MyApp({ Component, pageProps }) {
return (
<main className={inter.className}>
<Component {...pageProps} />
</main>
)
}
If you can't use a variable font, you will **need to specify a weight**:
pages/\_app.js
import { Roboto } from 'next/font/google'
const roboto = Roboto({
weight: '400',
subsets: ['latin'],
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={roboto.className}>
<Component {...pageProps} />
</main>
)
}
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`.
### [Apply the font in `<head>`](#apply-the-font-in-head)
You can also use the font without a wrapper and `className` by injecting it inside the `<head>` as follows:
pages/\_app.js
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) {
return (
<>
<style jsx global>{`
html {
font-family: ${inter.style.fontFamily};
}
`}</style>
<Component {...pageProps} />
</>
)
}
### [Single page usage](#single-page-usage)
To use the font on a single page, add it to the specific page as shown below:
pages/index.js
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
export default function Home() {
return (
<div className={inter.className}>
<p>Hello World</p>
</div>
)
}
### [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:
pages/\_app.js
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',
})
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/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.
pages/\_app.js
import localFont from 'next/font/local'
// Font files can be colocated inside of `pages`
const myFont = localFont({ src: './my-font.woff2' })
export default function MyApp({ Component, pageProps }) {
return (
<main className={myFont.className}>
<Component {...pageProps} />
</main>
)
}
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.
pages/\_app.js
import { Inter } from 'next/font/google'
const inter = Inter({
subsets: ['latin'],
variable: '--font-inter',
})
export default function MyApp({ Component, pageProps }) {
return (
<main className={`${inter.variable} font-sans`}>
<Component {...pageProps} />
</main>
)
}
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 route/s based on the type of file where it is used:
* if it's a [unique page](/docs/pages/building-your-application/routing/pages-and-layouts), it is preloaded on the unique route for that page
* if it's in the [custom App](/docs/pages/building-your-application/routing/custom-app), it is preloaded on all the routes of the site under `/pages`
[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
[Previous
Images
](/docs/pages/building-your-application/optimizing/images)
[Next
Scripts
](/docs/pages/building-your-application/optimizing/scripts)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"d5353d3b9a6af3d34fa89f91fd60d4a6\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/urlImports | 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
* [Security Model](#security-model)
* [Lockfile](#lockfile)
* [Examples](#examples)
* [Skypack](#skypack)
* [Static Image Imports](#static-image-imports)
* [URLs in CSS](#urls-in-css)
* [Asset Imports](#asset-imports)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/urlImports.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)urlImports
urlImports
==========
URL imports are an experimental feature that allows you to import modules directly from external servers (instead of from the local disk).
> **Warning**: This feature is experimental. Only use domains that you trust to download and execute on your machine. Please exercise discretion, and caution until the feature is flagged as stable.
To opt-in, add the allowed URL prefixes inside `next.config.js`:
next.config.js
module.exports = {
experimental: {
urlImports: ['https://example.com/assets/', 'https://cdn.skypack.dev'],
},
}
Then, you can import modules directly from URLs:
import { a, b, c } from 'https://example.com/assets/some/module.js'
URL Imports can be used everywhere normal package imports can be used.
[Security Model](#security-model)
---------------------------------
This feature is being designed with **security as the top priority**. To start, we added an experimental flag forcing you to explicitly allow the domains you accept URL imports from. We're working to take this further by limiting URL imports to execute in the browser sandbox using the [Edge Runtime](/docs/app/api-reference/edge).
[Lockfile](#lockfile)
---------------------
When using URL imports, Next.js will create a `next.lock` directory containing a lockfile and fetched assets. This directory **must be committed to Git**, not ignored by `.gitignore`.
* When running `next dev`, Next.js will download and add all newly discovered URL Imports to your lockfile
* When running `next build`, Next.js will use only the lockfile to build the application for production
Typically, no network requests are needed and any outdated lockfile will cause the build to fail. One exception is resources that respond with `Cache-Control: no-cache`. These resources will have a `no-cache` entry in the lockfile and will always be fetched from the network on each build.
[Examples](#examples)
---------------------
### [Skypack](#skypack)
import confetti from 'https://cdn.skypack.dev/canvas-confetti'
import { useEffect } from 'react'
export default () => {
useEffect(() => {
confetti()
})
return <p>Hello</p>
}
### [Static Image Imports](#static-image-imports)
import Image from 'next/image'
import logo from 'https://example.com/assets/logo.png'
export default () => (
<div>
<Image src={logo} placeholder="blur" />
</div>
)
### [URLs in CSS](#urls-in-css)
.className {
background: url('https://example.com/assets/hero.jpg');
}
### [Asset Imports](#asset-imports)
const logo = new URL('https://example.com/assets/file.txt', import.meta.url)
console.log(logo.pathname)
// prints "/_next/static/media/file.a9727b5d.txt"
[Previous
typescript
](/docs/app/api-reference/next-config-js/typescript)
[Next
webpack
](/docs/app/api-reference/next-config-js/webpack)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"4b70e5c6c60c75758449e9743c16ff45\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/scripts | 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
* [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)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/03-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/building-your-application/optimizing)Scripts
Script Optimization
===================
### [Application Scripts](#application-scripts)
To load a third-party script for all routes, import `next/script` and include the script directly in your custom `_app`:
pages/\_app.js
import Script from 'next/script'
export default function MyApp({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<Script src="https://example.com/script.js" />
</>
)
}
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:
pages/index.tsx
TypeScript
JavaScriptTypeScript
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/pages/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.
pages/index.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"
/>
</>
)
}
[Previous
Fonts
](/docs/pages/building-your-application/optimizing/fonts)
[Next
Static Assets
](/docs/pages/building-your-application/optimizing/static-assets)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"506fdfc8184968575828ca159c2c75d3\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/webVitalsAttribution | 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/webVitalsAttribution.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)webVitalsAttribution
webVitalsAttribution
====================
When debugging issues related to Web Vitals, it is often helpful if we can pinpoint the source of the problem. For example, in the case of Cumulative Layout Shift (CLS), we might want to know the first element that shifted when the single largest layout shift occurred. Or, in the case of Largest Contentful Paint (LCP), we might want to identify the element corresponding to the LCP for the page. If the LCP element is an image, knowing the URL of the image resource can help us locate the asset we need to optimize.
Pinpointing the biggest contributor to the Web Vitals score, aka [attribution](https://github.com/GoogleChrome/web-vitals/blob/4ca38ae64b8d1e899028c692f94d4c56acfc996c/README.md#attribution), allows us to obtain more in-depth information like entries for [PerformanceEventTiming](https://developer.mozilla.org/docs/Web/API/PerformanceEventTiming), [PerformanceNavigationTiming](https://developer.mozilla.org/docs/Web/API/PerformanceNavigationTiming) and [PerformanceResourceTiming](https://developer.mozilla.org/docs/Web/API/PerformanceResourceTiming).
Attribution is disabled by default in Next.js but can be enabled **per metric** by specifying the following in `next.config.js`.
next.config.js
experimental: {
webVitalsAttribution: ['CLS', 'LCP']
}
Valid attribution values are all `web-vitals` metrics specified in the [`NextWebVitalsMetric`](https://github.com/vercel/next.js/blob/442378d21dd56d6e769863eb8c2cb521a463a2e0/packages/next/shared/lib/utils.ts#L43) type.
[Previous
webpack
](/docs/app/api-reference/next-config-js/webpack)
[Next
create-next-app
](/docs/app/api-reference/create-next-app)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9e0cca143879f9c53ea369ffb8ec3852\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/data-fetching | 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
* [Examples](#examples)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/03-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
[Pages Router](/docs/pages)[Building Your Application](/docs/pages/building-your-application)Data Fetching
Data Fetching
=============
Data fetching in Next.js allows you to render your content in different ways, depending on your application's use case. These include pre-rendering with **Server-side Rendering** or **Static Generation**, and updating or creating content at runtime with **Incremental Static Regeneration**.
[Examples](#examples)
---------------------
* [WordPress Example](https://github.com/vercel/next.js/tree/canary/examples/cms-wordpress)([Demo](https://next-blog-wordpress.vercel.app))
* [Blog Starter using markdown files](https://github.com/vercel/next.js/tree/canary/examples/blog-starter) ([Demo](https://next-blog-starter.vercel.app/))
* [DatoCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-datocms) ([Demo](https://next-blog-datocms.vercel.app/))
* [TakeShape Example](https://github.com/vercel/next.js/tree/canary/examples/cms-takeshape) ([Demo](https://next-blog-takeshape.vercel.app/))
* [Sanity Example](https://github.com/vercel/next.js/tree/canary/examples/cms-sanity) ([Demo](https://next-blog-sanity.vercel.app/))
* [Prismic Example](https://github.com/vercel/next.js/tree/canary/examples/cms-prismic) ([Demo](https://next-blog-prismic.vercel.app/))
* [Contentful Example](https://github.com/vercel/next.js/tree/canary/examples/cms-contentful) ([Demo](https://next-blog-contentful.vercel.app/))
* [Strapi Example](https://github.com/vercel/next.js/tree/canary/examples/cms-strapi) ([Demo](https://next-blog-strapi.vercel.app/))
* [Prepr Example](https://github.com/vercel/next.js/tree/canary/examples/cms-prepr) ([Demo](https://next-blog-prepr.vercel.app/))
* [Agility CMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-agilitycms) ([Demo](https://next-blog-agilitycms.vercel.app/))
* [Cosmic Example](https://github.com/vercel/next.js/tree/canary/examples/cms-cosmic) ([Demo](https://next-blog-cosmic.vercel.app/))
* [ButterCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-buttercms) ([Demo](https://next-blog-buttercms.vercel.app/))
* [Storyblok Example](https://github.com/vercel/next.js/tree/canary/examples/cms-storyblok) ([Demo](https://next-blog-storyblok.vercel.app/))
* [GraphCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-graphcms) ([Demo](https://next-blog-graphcms.vercel.app/))
* [Kontent Example](https://github.com/vercel/next.js/tree/canary/examples/cms-kontent-ai) ([Demo](https://next-blog-kontent.vercel.app/))
* [Static Tweet Demo](https://static-tweet.vercel.app/)
* [Enterspeed Example](https://github.com/vercel/next.js/tree/canary/examples/cms-enterspeed) ([Demo](https://next-blog-demo.enterspeed.com/))
[
### getStaticProps
Fetch data and generate static pages with \`getStaticProps\`. Learn more about this API for data fetching in Next.js.
](/docs/pages/building-your-application/data-fetching/get-static-props)[
### getStaticPaths
Fetch data and generate static pages with \`getStaticPaths\`. Learn more about this API for data fetching in Next.js.
](/docs/pages/building-your-application/data-fetching/get-static-paths)[
### Forms and Mutations
Learn how to handle form submissions and data mutations with Next.js.
](/docs/pages/building-your-application/data-fetching/forms-and-mutations)[
### getServerSideProps
Fetch data on each request with \`getServerSideProps\`.
](/docs/pages/building-your-application/data-fetching/get-server-side-props)[
### Incremental Static Regeneration (ISR)
Learn how to create or update static pages at runtime with Incremental Static Regeneration.
](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)[
### Client-side Fetching
Learn about client-side data fetching, and how to use SWR, a data fetching React hook library that handles caching, revalidation, focus tracking, refetching on interval and more.
](/docs/pages/building-your-application/data-fetching/client-side)
[Previous
Edge and Node.js Runtimes
](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes)
[Next
getStaticProps
](/docs/pages/building-your-application/data-fetching/get-static-props)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"adb2bb474ee384a7f2416106d1c1b2f5\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/data-fetching/get-static-props | 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
* [When should I use getStaticProps?](#when-should-i-use-getstaticprops)
* [When does getStaticProps run](#when-does-getstaticprops-run)
* [Using getStaticProps to fetch data from a CMS](#using-getstaticprops-to-fetch-data-from-a-cms)
* [Write server-side code directly](#write-server-side-code-directly)
* [Statically generates both HTML and JSON](#statically-generates-both-html-and-json)
* [Where can I use getStaticProps](#where-can-i-use-getstaticprops)
* [Runs on every request in development](#runs-on-every-request-in-development)
* [Preview Mode](#preview-mode)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/03-data-fetching/01-get-static-props.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)[Data Fetching](/docs/pages/building-your-application/data-fetching)getStaticProps
getStaticProps
==============
If you export a function called `getStaticProps` (Static Site Generation) from a page, Next.js will pre-render this page at build time using the props returned by `getStaticProps`.
pages/index.tsx
TypeScript
JavaScriptTypeScript
import type { InferGetStaticPropsType, GetStaticProps } from 'next'
type Repo = {
name: string
stargazers_count: number
}
export const getStaticProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}) satisfies GetStaticProps<{
repo: Repo
}>
export default function Page({
repo,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return repo.stargazers_count
}
> Note that irrespective of rendering type, any `props` will be passed to the page component and can be viewed on the client-side in the initial HTML. This is to allow the page to be [hydrated](https://react.dev/reference/react-dom/hydrate) correctly. Make sure that you don't pass any sensitive information that shouldn't be available on the client in `props`.
The [`getStaticProps` API reference](/docs/pages/api-reference/functions/get-static-props) covers all parameters and props that can be used with `getStaticProps`.
[When should I use getStaticProps?](#when-should-i-use-getstaticprops)
----------------------------------------------------------------------
You should use `getStaticProps` if:
* The data required to render the page is available at build time ahead of a user’s request
* The data comes from a headless CMS
* The page must be pre-rendered (for SEO) and be very fast — `getStaticProps` generates `HTML` and `JSON` files, both of which can be cached by a CDN for performance
* The data can be publicly cached (not user-specific). This condition can be bypassed in certain specific situation by using a Middleware to rewrite the path.
[When does getStaticProps run](#when-does-getstaticprops-run)
-------------------------------------------------------------
`getStaticProps` always runs on the server and never on the client. You can validate code written inside `getStaticProps` is removed from the client-side bundle [with this tool](https://next-code-elimination.vercel.app/).
* `getStaticProps` always runs during `next build`
* `getStaticProps` runs in the background when using [`fallback: true`](/docs/pages/api-reference/functions/get-static-paths#fallback-true)
* `getStaticProps` is called before initial render when using [`fallback: blocking`](/docs/pages/api-reference/functions/get-static-paths#fallback-blocking)
* `getStaticProps` runs in the background when using `revalidate`
* `getStaticProps` runs on-demand in the background when using [`revalidate()`](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation)
When combined with [Incremental Static Regeneration](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration), `getStaticProps` will run in the background while the stale page is being revalidated, and the fresh page served to the browser.
`getStaticProps` does not have access to the incoming request (such as query parameters or HTTP headers) as it generates static HTML. If you need access to the request for your page, consider using [Middleware](/docs/pages/building-your-application/routing/middleware) in addition to `getStaticProps`.
[Using getStaticProps to fetch data from a CMS](#using-getstaticprops-to-fetch-data-from-a-cms)
-----------------------------------------------------------------------------------------------
The following example shows how you can fetch a list of blog posts from a CMS.
pages/blog.tsx
TypeScript
JavaScriptTypeScript
// posts will be populated at build time by getStaticProps()
export default function Blog({ posts }) {
return (
<ul>
{posts.map((post) => (
<li>{post.title}</li>
))}
</ul>
)
}
// This function gets called at build time on server-side.
// It won't be called on client-side, so you can even do
// direct database queries.
export async function getStaticProps() {
// Call an external API endpoint to get posts.
// You can use any data fetching library
const res = await fetch('https://.../posts')
const posts = await res.json()
// By returning { props: { posts } }, the Blog component
// will receive `posts` as a prop at build time
return {
props: {
posts,
},
}
}
The [`getStaticProps` API reference](/docs/pages/api-reference/functions/get-static-props) covers all parameters and props that can be used with `getStaticProps`.
[Write server-side code directly](#write-server-side-code-directly)
-------------------------------------------------------------------
As `getStaticProps` runs only on the server-side, it will never run on the client-side. It won’t even be included in the JS bundle for the browser, so you can write direct database queries without them being sent to browsers.
This means that instead of fetching an **API route** from `getStaticProps` (that itself fetches data from an external source), you can write the server-side code directly in `getStaticProps`.
Take the following example. An API route is used to fetch some data from a CMS. That API route is then called directly from `getStaticProps`. This produces an additional call, reducing performance. Instead, the logic for fetching the data from the CMS can be shared by using a `lib/` directory. Then it can be shared with `getStaticProps`.
lib/load-posts.js
// The following function is shared
// with getStaticProps and API routes
// from a `lib/` directory
export async function loadPosts() {
// Call an external API endpoint to get posts
const res = await fetch('https://.../posts/')
const data = await res.json()
return data
}
pages/blog.js
// pages/blog.js
import { loadPosts } from '../lib/load-posts'
// This function runs only on the server side
export async function getStaticProps() {
// Instead of fetching your `/api` route you can call the same
// function directly in `getStaticProps`
const posts = await loadPosts()
// Props returned will be passed to the page component
return { props: { posts } }
}
Alternatively, if you are **not** using API routes to fetch data, then the [`fetch()`](https://developer.mozilla.org/docs/Web/API/Fetch_API) API _can_ be used directly in `getStaticProps` to fetch data.
To verify what Next.js eliminates from the client-side bundle, you can use the [next-code-elimination tool](https://next-code-elimination.vercel.app/).
[Statically generates both HTML and JSON](#statically-generates-both-html-and-json)
-----------------------------------------------------------------------------------
When a page with `getStaticProps` is pre-rendered at build time, in addition to the page HTML file, Next.js generates a JSON file holding the result of running `getStaticProps`.
This JSON file will be used in client-side routing through [`next/link`](/docs/pages/api-reference/components/link) or [`next/router`](/docs/pages/api-reference/functions/use-router). When you navigate to a page that’s pre-rendered using `getStaticProps`, Next.js fetches this JSON file (pre-computed at build time) and uses it as the props for the page component. This means that client-side page transitions will **not** call `getStaticProps` as only the exported JSON is used.
When using Incremental Static Generation, `getStaticProps` will be executed in the background to generate the JSON needed for client-side navigation. You may see this in the form of multiple requests being made for the same page, however, this is intended and has no impact on end-user performance.
[Where can I use getStaticProps](#where-can-i-use-getstaticprops)
-----------------------------------------------------------------
`getStaticProps` can only be exported from a **page**. You **cannot** export it from non-page files, `_app`, `_document`, or `_error`.
One of the reasons for this restriction is that React needs to have all the required data before the page is rendered.
Also, you must use export `getStaticProps` as a standalone function — it will **not** work if you add `getStaticProps` as a property of the page component.
> **Good to know**: if you have created a [custom app](/docs/pages/building-your-application/routing/custom-app), ensure you are passing the `pageProps` to the page component as shown in the linked document, otherwise the props will be empty.
[Runs on every request in development](#runs-on-every-request-in-development)
-----------------------------------------------------------------------------
In development (`next dev`), `getStaticProps` will be called on every request.
[Preview Mode](#preview-mode)
-----------------------------
You can temporarily bypass static generation and render the page at **request time** instead of build time using [**Preview Mode**](/docs/pages/building-your-application/configuring/preview-mode). For example, you might be using a headless CMS and want to preview drafts before they're published.
[Previous
Data Fetching
](/docs/pages/building-your-application/data-fetching)
[Next
getStaticPaths
](/docs/pages/building-your-application/data-fetching/get-static-paths)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"cf549d5c8b6a1e6f451ac587d8754338\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-config-js/webpack | 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
* [nextRuntime](#nextruntime)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/05-next-config-js/webpack.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)webpack
Custom Webpack Config
=====================
> **Good to know**: changes to webpack config are not covered by semver so proceed at your own risk
Before continuing to add custom webpack configuration to your application make sure Next.js doesn't already support your use-case:
* [CSS imports](/docs/app/building-your-application/styling)
* [CSS modules](/docs/app/building-your-application/styling/css-modules)
* [Sass/SCSS imports](/docs/app/building-your-application/styling/sass)
* [Sass/SCSS modules](/docs/app/building-your-application/styling/sass)
Some commonly asked for features are available as plugins:
* [@next/mdx](https://github.com/vercel/next.js/tree/canary/packages/next-mdx)
* [@next/bundle-analyzer](https://github.com/vercel/next.js/tree/canary/packages/next-bundle-analyzer)
In order to extend our usage of `webpack`, you can define a function that extends its config inside `next.config.js`, like so:
next.config.js
module.exports = {
webpack: (
config,
{ buildId, dev, isServer, defaultLoaders, nextRuntime, webpack }
) => {
// Important: return the modified config
return config
},
}
> The `webpack` function is executed three times, twice for the server (nodejs / edge runtime) and once for the client. This allows you to distinguish between client and server configuration using the `isServer` property.
The second argument to the `webpack` function is an object with the following properties:
* `buildId`: `String` - The build id, used as a unique identifier between builds
* `dev`: `Boolean` - Indicates if the compilation will be done in development
* `isServer`: `Boolean` - It's `true` for server-side compilation, and `false` for client-side compilation
* `nextRuntime`: `String | undefined` - The target runtime for server-side compilation; either `"edge"` or `"nodejs"`, it's `undefined` for client-side compilation.
* `defaultLoaders`: `Object` - Default loaders used internally by Next.js:
* `babel`: `Object` - Default `babel-loader` configuration
Example usage of `defaultLoaders.babel`:
// Example config for adding a loader that depends on babel-loader
// This source was taken from the @next/mdx plugin source:
// https://github.com/vercel/next.js/tree/canary/packages/next-mdx
module.exports = {
webpack: (config, options) => {
config.module.rules.push({
test: /\.mdx/,
use: [
options.defaultLoaders.babel,
{
loader: '@mdx-js/loader',
options: pluginOptions.options,
},
],
})
return config
},
}
#### [`nextRuntime`](#nextruntime)
Notice that `isServer` is `true` when `nextRuntime` is `"edge"` or `"nodejs"`, nextRuntime "`edge`" is currently for middleware and Server Components in edge runtime only.
[Previous
urlImports
](/docs/app/api-reference/next-config-js/urlImports)
[Next
webVitalsAttribution
](/docs/app/api-reference/next-config-js/webVitalsAttribution)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f57562e20904604c6710bd19cbc77f41\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/data-fetching/get-static-paths | 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
* [When should I use getStaticPaths?](#when-should-i-use-getstaticpaths)
* [When does getStaticPaths run](#when-does-getstaticpaths-run)
* [How does getStaticProps run with regards to getStaticPaths](#how-does-getstaticprops-run-with-regards-to-getstaticpaths)
* [Where can I use getStaticPaths](#where-can-i-use-getstaticpaths)
* [Runs on every request in development](#runs-on-every-request-in-development)
* [Generating paths on-demand](#generating-paths-on-demand)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/03-data-fetching/02-get-static-paths.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)[Data Fetching](/docs/pages/building-your-application/data-fetching)getStaticPaths
getStaticPaths
==============
If a page has [Dynamic Routes](/docs/pages/building-your-application/routing/dynamic-routes) and uses `getStaticProps`, it needs to define a list of paths to be statically generated.
When you export a function called `getStaticPaths` (Static Site Generation) from a page that uses dynamic routes, Next.js will statically pre-render all the paths specified by `getStaticPaths`.
pages/repo/\[name\].tsx
TypeScript
JavaScriptTypeScript
import type {
InferGetStaticPropsType,
GetStaticProps,
GetStaticPaths,
} from 'next'
type Repo = {
name: string
stargazers_count: number
}
export const getStaticPaths = (async () => {
return {
paths: [
{
params: {
name: 'next.js',
},
}, // See the "paths" section below
],
fallback: true, // false or "blocking"
}
}) satisfies GetStaticPaths
export const getStaticProps = (async (context) => {
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo = await res.json()
return { props: { repo } }
}) satisfies GetStaticProps<{
repo: Repo
}>
export default function Page({
repo,
}: InferGetStaticPropsType<typeof getStaticProps>) {
return repo.stargazers_count
}
The [`getStaticPaths` API reference](/docs/pages/api-reference/functions/get-static-paths) covers all parameters and props that can be used with `getStaticPaths`.
[When should I use getStaticPaths?](#when-should-i-use-getstaticpaths)
----------------------------------------------------------------------
You should use `getStaticPaths` if you’re statically pre-rendering pages that use dynamic routes and:
* The data comes from a headless CMS
* The data comes from a database
* The data comes from the filesystem
* The data can be publicly cached (not user-specific)
* The page must be pre-rendered (for SEO) and be very fast — `getStaticProps` generates `HTML` and `JSON` files, both of which can be cached by a CDN for performance
[When does getStaticPaths run](#when-does-getstaticpaths-run)
-------------------------------------------------------------
`getStaticPaths` will only run during build in production, it will not be called during runtime. You can validate code written inside `getStaticPaths` is removed from the client-side bundle [with this tool](https://next-code-elimination.vercel.app/).
### [How does getStaticProps run with regards to getStaticPaths](#how-does-getstaticprops-run-with-regards-to-getstaticpaths)
* `getStaticProps` runs during `next build` for any `paths` returned during build
* `getStaticProps` runs in the background when using `fallback: true`
* `getStaticProps` is called before initial render when using `fallback: blocking`
[Where can I use getStaticPaths](#where-can-i-use-getstaticpaths)
-----------------------------------------------------------------
* `getStaticPaths` **must** be used with `getStaticProps`
* You **cannot** use `getStaticPaths` with [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props)
* You can export `getStaticPaths` from a [Dynamic Route](/docs/pages/building-your-application/routing/dynamic-routes) that also uses `getStaticProps`
* You **cannot** export `getStaticPaths` from non-page file (e.g. your `components` folder)
* You must export `getStaticPaths` as a standalone function, and not a property of the page component
[Runs on every request in development](#runs-on-every-request-in-development)
-----------------------------------------------------------------------------
In development (`next dev`), `getStaticPaths` will be called on every request.
[Generating paths on-demand](#generating-paths-on-demand)
---------------------------------------------------------
`getStaticPaths` allows you to control which pages are generated during the build instead of on-demand with [`fallback`](/docs/pages/api-reference/functions/get-static-paths#fallback-blocking). Generating more pages during a build will cause slower builds.
You can defer generating all pages on-demand by returning an empty array for `paths`. This can be especially helpful when deploying your Next.js application to multiple environments. For example, you can have faster builds by generating all pages on-demand for previews (but not production builds). This is helpful for sites with hundreds/thousands of static pages.
pages/posts/\[id\].js
export async function getStaticPaths() {
// When this is true (in preview environments) don't
// prerender any static pages
// (faster builds, but slower initial page load)
if (process.env.SKIP_BUILD_STATIC_GENERATION) {
return {
paths: [],
fallback: 'blocking',
}
}
// Call an external API endpoint to get posts
const res = await fetch('https://.../posts')
const posts = await res.json()
// Get the paths we want to prerender based on posts
// In production environments, prerender all pages
// (slower builds, but faster initial page load)
const paths = posts.map((post) => ({
params: { id: post.id },
}))
// { fallback: false } means other routes should 404
return { paths, fallback: false }
}
[Previous
getStaticProps
](/docs/pages/building-your-application/data-fetching/get-static-props)
[Next
Forms and Mutations
](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"bb271ac1c4a4484822d7f9743d241080\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/create-next-app | 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
* [Interactive](#interactive)
* [Non-interactive](#non-interactive)
* [Why use Create Next App?](#why-use-create-next-app)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/06-create-next-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
[App Router](/docs/app)[API Reference](/docs/app/api-reference)create-next-app
create-next-app
===============
The easiest way to get started with Next.js is by using `create-next-app`. This CLI tool enables you to quickly start building a new Next.js application, with everything set up for you.
You can create a new app using the default Next.js template, or by using one of the [official Next.js examples](https://github.com/vercel/next.js/tree/canary/examples).
### [Interactive](#interactive)
You can create a new project interactively by running:
Terminal
npx create-next-app@latest
Terminal
yarn create next-app
Terminal
pnpm create next-app
Terminal
bunx create-next-app
You will then be asked the following prompts:
Terminal
What is your project named? my-app
Would you like to use TypeScript? No / Yes
Would you like to use ESLint? No / Yes
Would you like to use Tailwind CSS? No / Yes
Would you like to use `src/` directory? No / Yes
Would you like to use App Router? (recommended) No / Yes
Would you like to customize the default import alias (@/*)? No / Yes
Once you've answered the prompts, a new project will be created with the correct configuration depending on your answers.
### [Non-interactive](#non-interactive)
You can also pass command line arguments to set up a new project non-interactively.
Further, you can negate default options by prefixing them with `--no-` (e.g. `--no-eslint`).
See `create-next-app --help`:
Terminal
Usage: create-next-app <project-directory> [options]
Options:
-V, --version output the version number
--ts, --typescript
Initialize as a TypeScript project. (default)
--js, --javascript
Initialize as a JavaScript project.
--tailwind
Initialize with Tailwind CSS config. (default)
--eslint
Initialize with ESLint config.
--app
Initialize as an App Router project.
--src-dir
Initialize inside a `src/` directory.
--import-alias <alias-to-configure>
Specify import alias to use (default "@/*").
--use-npm
Explicitly tell the CLI to bootstrap the app using npm
--use-pnpm
Explicitly tell the CLI to bootstrap the app using pnpm
--use-yarn
Explicitly tell the CLI to bootstrap the app using Yarn
--use-bun
Explicitly tell the CLI to bootstrap the app using Bun
-e, --example [name]|[github-url]
An example to bootstrap the app with. You can use an example name
from the official Next.js repo or a public GitHub URL. The URL can use
any branch and/or subdirectory
--example-path <path-to-example>
In a rare case, your GitHub URL might contain a branch name with
a slash (e.g. bug/fix-1) and the path to the example (e.g. foo/bar).
In this case, you must specify the path to the example separately:
--example-path foo/bar
--reset-preferences
Explicitly tell the CLI to reset any stored preferences
-h, --help output usage information
### [Why use Create Next App?](#why-use-create-next-app)
`create-next-app` allows you to create a new Next.js app within seconds. It is officially maintained by the creators of Next.js, and includes a number of benefits:
* **Interactive Experience**: Running `npx create-next-app@latest` (with no arguments) launches an interactive experience that guides you through setting up a project.
* **Zero Dependencies**: Initializing a project is as quick as one second. Create Next App has zero dependencies.
* **Offline Support**: Create Next App will automatically detect if you're offline and bootstrap your project using your local package cache.
* **Support for Examples**: Create Next App can bootstrap your application using an example from the Next.js examples collection (e.g. `npx create-next-app --example api-routes`) or any public GitHub repository.
* **Tested**: The package is part of the Next.js monorepo and tested using the same integration test suite as Next.js itself, ensuring it works as expected with every release.
[Previous
webVitalsAttribution
](/docs/app/api-reference/next-config-js/webVitalsAttribution)
[Next
Edge Runtime
](/docs/app/api-reference/edge)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"aff6d1f0c12437774ee41ab859172565\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/static-assets | 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
* [Caching](#caching)
* [Robots, Favicons, and others](#robots-favicons-and-others)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/05-static-assets.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)[Optimizing](/docs/pages/building-your-application/optimizing)Static Assets
Static Assets
=============
Next.js can serve static files, like images, under a folder called `public` in the root directory. Files inside `public` can then be referenced by your code starting from the base URL (`/`).
For example, the file `public/avatars/me.png` can be viewed by visiting the `/avatars/me.png` path. The code to display that image might look like:
avatar.js
import Image from 'next/image'
export function Avatar({ id, alt }) {
return <Image src={`/avatars/${id}.png`} alt={alt} width="64" height="64" />
}
export function AvatarOfMe() {
return <Avatar id="me" alt="A portrait of me" />
}
[Caching](#caching)
-------------------
Next.js cannot safely cache assets in the `public` folder because they may change. The default caching headers applied are:
Cache-Control: public, max-age=0
[Robots, Favicons, and others](#robots-favicons-and-others)
-----------------------------------------------------------
The folder is also useful for `robots.txt`, `favicon.ico`, Google Site Verification, and any other static files (including `.html`). But make sure to not have a static file with the same name as a file in the `pages/` directory, as this will result in an error. [Read more](/docs/messages/conflicting-public-file-page).
> Good to know:
>
> * The directory must be named `public`. The name cannot be changed and it's the only directory used to serve static assets.
> * Only assets that are in the `public` directory at [build time](/docs/app/api-reference/next-cli#build) will be served by Next.js. Files added at request time won't be available. We recommend using a third-party service like [Vercel Blob](https://vercel.com/docs/storage/vercel-blob?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) for persistent file storage.
[Previous
Scripts
](/docs/pages/building-your-application/optimizing/scripts)
[Next
Bundle Analyzer
](/docs/pages/building-your-application/optimizing/bundle-analyzer)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"b0ecaf599b70edf2276951fdedf3f499\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/bundle-analyzer | 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
* [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/03-pages/01-building-your-application/05-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/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.
[
Pages Router
...
Deploying
### Production Checklist
Recommendations to ensure the best performance and user experience before taking your Next.js application to production.
](/docs/pages/building-your-application/deploying/production-checklist)
[Previous
Static Assets
](/docs/pages/building-your-application/optimizing/static-assets)
[Next
Analytics
](/docs/pages/building-your-application/optimizing/analytics)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"c34b3b055ac25114afa395b58e73c4c8\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/edge | 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
* [Network APIs](#network-apis)
* [Encoding APIs](#encoding-apis)
* [Stream APIs](#stream-apis)
* [Crypto APIs](#crypto-apis)
* [Web Standard APIs](#web-standard-apis)
* [Next.js Specific Polyfills](#nextjs-specific-polyfills)
* [Environment Variables](#environment-variables)
* [Unsupported APIs](#unsupported-apis)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/07-edge.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)Edge Runtime
Edge Runtime
============
The Next.js Edge Runtime is based on standard Web APIs, it supports the following APIs:
[Network APIs](#network-apis)
-----------------------------
API
Description
[`Blob`](https://developer.mozilla.org/docs/Web/API/Blob)
Represents a blob
[`fetch`](https://developer.mozilla.org/docs/Web/API/Fetch_API)
Fetches a resource
[`FetchEvent`](https://developer.mozilla.org/docs/Web/API/FetchEvent)
Represents a fetch event
[`File`](https://developer.mozilla.org/docs/Web/API/File)
Represents a file
[`FormData`](https://developer.mozilla.org/docs/Web/API/FormData)
Represents form data
[`Headers`](https://developer.mozilla.org/docs/Web/API/Headers)
Represents HTTP headers
[`Request`](https://developer.mozilla.org/docs/Web/API/Request)
Represents an HTTP request
[`Response`](https://developer.mozilla.org/docs/Web/API/Response)
Represents an HTTP response
[`URLSearchParams`](https://developer.mozilla.org/docs/Web/API/URLSearchParams)
Represents URL search parameters
[`WebSocket`](https://developer.mozilla.org/docs/Web/API/WebSocket)
Represents a websocket connection
[Encoding APIs](#encoding-apis)
-------------------------------
API
Description
[`atob`](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/atob)
Decodes a base-64 encoded string
[`btoa`](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/btoa)
Encodes a string in base-64
[`TextDecoder`](https://developer.mozilla.org/docs/Web/API/TextDecoder)
Decodes a Uint8Array into a string
[`TextDecoderStream`](https://developer.mozilla.org/docs/Web/API/TextDecoderStream)
Chainable decoder for streams
[`TextEncoder`](https://developer.mozilla.org/docs/Web/API/TextEncoder)
Encodes a string into a Uint8Array
[`TextEncoderStream`](https://developer.mozilla.org/docs/Web/API/TextEncoderStream)
Chainable encoder for streams
[Stream APIs](#stream-apis)
---------------------------
API
Description
[`ReadableStream`](https://developer.mozilla.org/docs/Web/API/ReadableStream)
Represents a readable stream
[`ReadableStreamBYOBReader`](https://developer.mozilla.org/docs/Web/API/ReadableStreamBYOBReader)
Represents a reader of a ReadableStream
[`ReadableStreamDefaultReader`](https://developer.mozilla.org/docs/Web/API/ReadableStreamDefaultReader)
Represents a reader of a ReadableStream
[`TransformStream`](https://developer.mozilla.org/docs/Web/API/TransformStream)
Represents a transform stream
[`WritableStream`](https://developer.mozilla.org/docs/Web/API/WritableStream)
Represents a writable stream
[`WritableStreamDefaultWriter`](https://developer.mozilla.org/docs/Web/API/WritableStreamDefaultWriter)
Represents a writer of a WritableStream
[Crypto APIs](#crypto-apis)
---------------------------
API
Description
[`crypto`](https://developer.mozilla.org/docs/Web/API/Window/crypto)
Provides access to the cryptographic functionality of the platform
[`CryptoKey`](https://developer.mozilla.org/docs/Web/API/CryptoKey)
Represents a cryptographic key
[`SubtleCrypto`](https://developer.mozilla.org/docs/Web/API/SubtleCrypto)
Provides access to common cryptographic primitives, like hashing, signing, encryption or decryption
[Web Standard APIs](#web-standard-apis)
---------------------------------------
API
Description
[`AbortController`](https://developer.mozilla.org/docs/Web/API/AbortController)
Allows you to abort one or more DOM requests as and when desired
[`Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array)
Represents an array of values
[`ArrayBuffer`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
Represents a generic, fixed-length raw binary data buffer
[`Atomics`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Atomics)
Provides atomic operations as static methods
[`BigInt`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigInt)
Represents a whole number with arbitrary precision
[`BigInt64Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array)
Represents a typed array of 64-bit signed integers
[`BigUint64Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array)
Represents a typed array of 64-bit unsigned integers
[`Boolean`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Boolean)
Represents a logical entity and can have two values: `true` and `false`
[`clearInterval`](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/clearInterval)
Cancels a timed, repeating action which was previously established by a call to `setInterval()`
[`clearTimeout`](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/clearTimeout)
Cancels a timed, repeating action which was previously established by a call to `setTimeout()`
[`console`](https://developer.mozilla.org/docs/Web/API/Console)
Provides access to the browser's debugging console
[`DataView`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/DataView)
Represents a generic view of an `ArrayBuffer`
[`Date`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Date)
Represents a single moment in time in a platform-independent format
[`decodeURI`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/decodeURI)
Decodes a Uniform Resource Identifier (URI) previously created by `encodeURI` or by a similar routine
[`decodeURIComponent`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent)
Decodes a Uniform Resource Identifier (URI) component previously created by `encodeURIComponent` or by a similar routine
[`DOMException`](https://developer.mozilla.org/docs/Web/API/DOMException)
Represents an error that occurs in the DOM
[`encodeURI`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/encodeURI)
Encodes a Uniform Resource Identifier (URI) by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character
[`encodeURIComponent`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent)
Encodes a Uniform Resource Identifier (URI) component by replacing each instance of certain characters by one, two, three, or four escape sequences representing the UTF-8 encoding of the character
[`Error`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error)
Represents an error when trying to execute a statement or accessing a property
[`EvalError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/EvalError)
Represents an error that occurs regarding the global function `eval()`
[`Float32Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Float32Array)
Represents a typed array of 32-bit floating point numbers
[`Float64Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Float64Array)
Represents a typed array of 64-bit floating point numbers
[`Function`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function)
Represents a function
[`Infinity`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Infinity)
Represents the mathematical Infinity value
[`Int8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int8Array)
Represents a typed array of 8-bit signed integers
[`Int16Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int16Array)
Represents a typed array of 16-bit signed integers
[`Int32Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Int32Array)
Represents a typed array of 32-bit signed integers
[`Intl`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl)
Provides access to internationalization and localization functionality
[`isFinite`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/isFinite)
Determines whether a value is a finite number
[`isNaN`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/isNaN)
Determines whether a value is `NaN` or not
[`JSON`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/JSON)
Provides functionality to convert JavaScript values to and from the JSON format
[`Map`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Map)
Represents a collection of values, where each value may occur only once
[`Math`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Math)
Provides access to mathematical functions and constants
[`Number`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)
Represents a numeric value
[`Object`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)
Represents the object that is the base of all JavaScript objects
[`parseFloat`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/parseFloat)
Parses a string argument and returns a floating point number
[`parseInt`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/parseInt)
Parses a string argument and returns an integer of the specified radix
[`Promise`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)
Represents the eventual completion (or failure) of an asynchronous operation, and its resulting value
[`Proxy`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Proxy)
Represents an object that is used to define custom behavior for fundamental operations (e.g. property lookup, assignment, enumeration, function invocation, etc)
[`queueMicrotask`](https://developer.mozilla.org/docs/Web/API/queueMicrotask)
Queues a microtask to be executed
[`RangeError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RangeError)
Represents an error when a value is not in the set or range of allowed values
[`ReferenceError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError)
Represents an error when a non-existent variable is referenced
[`Reflect`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Reflect)
Provides methods for interceptable JavaScript operations
[`RegExp`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp)
Represents a regular expression, allowing you to match combinations of characters
[`Set`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Set)
Represents a collection of values, where each value may occur only once
[`setInterval`](https://developer.mozilla.org/docs/Web/API/setInterval)
Repeatedly calls a function, with a fixed time delay between each call
[`setTimeout`](https://developer.mozilla.org/docs/Web/API/setTimeout)
Calls a function or evaluates an expression after a specified number of milliseconds
[`SharedArrayBuffer`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/SharedArrayBuffer)
Represents a generic, fixed-length raw binary data buffer
[`String`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)
Represents a sequence of characters
[`structuredClone`](https://developer.mozilla.org/docs/Web/API/Web_Workers_API/Structured_clone_algorithm)
Creates a deep copy of a value
[`Symbol`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Symbol)
Represents a unique and immutable data type that is used as the key of an object property
[`SyntaxError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError)
Represents an error when trying to interpret syntactically invalid code
[`TypeError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/TypeError)
Represents an error when a value is not of the expected type
[`Uint8Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)
Represents a typed array of 8-bit unsigned integers
[`Uint8ClampedArray`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint8ClampedArray)
Represents a typed array of 8-bit unsigned integers clamped to 0-255
[`Uint32Array`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Uint32Array)
Represents a typed array of 32-bit unsigned integers
[`URIError`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/URIError)
Represents an error when a global URI handling function was used in a wrong way
[`URL`](https://developer.mozilla.org/docs/Web/API/URL)
Represents an object providing static methods used for creating object URLs
[`URLPattern`](https://developer.mozilla.org/docs/Web/API/URLPattern)
Represents a URL pattern
[`URLSearchParams`](https://developer.mozilla.org/docs/Web/API/URLSearchParams)
Represents a collection of key/value pairs
[`WeakMap`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WeakMap)
Represents a collection of key/value pairs in which the keys are weakly referenced
[`WeakSet`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WeakSet)
Represents a collection of objects in which each object may occur only once
[`WebAssembly`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly)
Provides access to WebAssembly
[Next.js Specific Polyfills](#nextjs-specific-polyfills)
--------------------------------------------------------
* [`AsyncLocalStorage`](https://nodejs.org/api/async_context.html#class-asynclocalstorage)
[Environment Variables](#environment-variables)
-----------------------------------------------
You can use `process.env` to access [Environment Variables](/docs/app/building-your-application/configuring/environment-variables) for both `next dev` and `next build`.
[Unsupported APIs](#unsupported-apis)
-------------------------------------
The Edge Runtime has some restrictions including:
* Native Node.js APIs **are not supported**. For example, you can't read or write to the filesystem.
* `node_modules` _can_ be used, as long as they implement ES Modules and do not use native Node.js APIs.
* Calling `require` directly is **not allowed**. Use ES Modules instead.
The following JavaScript language features are disabled, and **will not work:**
API
Description
[`eval`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/eval)
Evaluates JavaScript code represented as a string
[`new Function(evalString)`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Function)
Creates a new function with the code provided as an argument
[`WebAssembly.compile`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/compile)
Compiles a WebAssembly module from a buffer source
[`WebAssembly.instantiate`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/instantiate)
Compiles and instantiates a WebAssembly module from a buffer source
In rare cases, your code could contain (or import) some dynamic code evaluation statements which _can not be reached at runtime_ and which can not be removed by treeshaking. You can relax the check to allow specific files with your Middleware or Edge API Route exported configuration:
export const config = {
runtime: 'edge', // for Edge API Routes only
unstable_allowDynamic: [
// allows a single file
'/lib/utilities.js',
// use a glob to allow anything in the function-bind 3rd party module
'/node_modules/function-bind/**',
],
}
`unstable_allowDynamic` is a [glob](https://github.com/micromatch/micromatch#matching-features), or an array of globs, ignoring dynamic code evaluation for specific files. The globs are relative to your application root folder.
Be warned that if these statements are executed on the Edge, _they will throw and cause a runtime error_.
[Previous
create-next-app
](/docs/app/api-reference/create-next-app)
[Next
Next.js CLI
](/docs/app/api-reference/next-cli)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"82e85bff8f8db1c0d0a88f32c5d45347\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/analytics | 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
* [Build Your Own](#build-your-own)
* [Web Vitals](#web-vitals)
* [Custom Metrics](#custom-metrics)
* [Sending results to external systems](#sending-results-to-external-systems)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/07-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/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)
---------------------------------
pages/\_app.js
import { useReportWebVitals } from 'next/web-vitals'
function MyApp({ Component, pageProps }) {
useReportWebVitals((metric) => {
console.log(metric)
})
return <Component {...pageProps} />
}
View the [API Reference](/docs/pages/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.
pages/\_app.js
import { useReportWebVitals } from 'next/web-vitals'
function MyApp({ Component, pageProps }) {
useReportWebVitals((metric) => {
switch (metric.name) {
case 'FCP': {
// handle FCP results
}
case 'LCP': {
// handle LCP results
}
// ...
}
})
return <Component {...pageProps} />
}
[Custom Metrics](#custom-metrics)
---------------------------------
In addition to the core metrics listed above, there are some additional custom metrics that measure the time it takes for the page to hydrate and render:
* `Next.js-hydration`: Length of time it takes for the page to start and finish hydrating (in ms)
* `Next.js-route-change-to-render`: Length of time it takes for a page to start rendering after a route change (in ms)
* `Next.js-render`: Length of time it takes for a page to finish render after a route change (in ms)
You can handle all the results of these metrics separately:
export function reportWebVitals(metric) {
switch (metric.name) {
case 'Next.js-hydration':
// handle hydration results
break
case 'Next.js-route-change-to-render':
// handle route-change to render results
break
case 'Next.js-render':
// handle render results
break
default:
break
}
}
These metrics work in all browsers that support the [User Timing API](https://caniuse.com/#feat=user-timing).
[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
Bundle Analyzer
](/docs/pages/building-your-application/optimizing/bundle-analyzer)
[Next
Lazy Loading
](/docs/pages/building-your-application/optimizing/lazy-loading)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"50712ff8dfb23b562c3cd31704619002\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/data-fetching/forms-and-mutations | 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
* [Examples](#examples)
* [Server-only form](#server-only-form)
* [Form validation](#form-validation)
* [Error handling](#error-handling)
* [Displaying loading state](#displaying-loading-state)
* [Redirecting](#redirecting)
* [Setting cookies](#setting-cookies)
* [Reading cookies](#reading-cookies)
* [Deleting cookies](#deleting-cookies)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/03-data-fetching/03-forms-and-mutations.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)[Data Fetching](/docs/pages/building-your-application/data-fetching)Forms and Mutations
Forms and Mutations
===================
Forms enable you to create and update data in web applications. Next.js provides a powerful way to handle form submissions and data mutations using **API Routes**.
> **Good to know:**
>
> * We will soon recommend [incrementally adopting](/docs/app/building-your-application/upgrading/app-router-migration) the App Router and using [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) for handling form submissions and data mutations. Server Actions allow you to define asynchronous server functions that can be called directly from your components, without needing to manually create an API Route.
> * API Routes [do not specify CORS headers](https://developer.mozilla.org/docs/Web/HTTP/CORS), meaning they are same-origin only by default.
> * Since API Routes run on the server, we're able to use sensitive values (like API keys) through [Environment Variables](/docs/pages/building-your-application/configuring/environment-variables) without exposing them to the client. This is critical for the security of your application.
[Examples](#examples)
---------------------
### [Server-only form](#server-only-form)
With the Pages Router, you need to manually create API endpoints to handle securely mutating data on the server.
pages/api/submit.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const data = req.body
const id = await createItem(data)
res.status(200).json({ id })
}
Then, call the API Route from the client with an event handler:
pages/index.tsx
TypeScript
JavaScriptTypeScript
import { FormEvent } from 'react'
export default function Page() {
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault()
const formData = new FormData(event.currentTarget)
const response = await fetch('/api/submit', {
method: 'POST',
body: formData,
})
// Handle response if necessary
const data = await response.json()
// ...
}
return (
<form onSubmit={onSubmit}>
<input type="text" name="name" />
<button type="submit">Submit</button>
</form>
)
}
[Form validation](#form-validation)
-----------------------------------
We recommend using HTML validation like `required` and `type="email"` for basic client-side form validation.
For more advanced server-side validation, you can use a schema validation library like [zod](https://zod.dev/) to validate the form fields before mutating the data:
pages/api/submit.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
import { z } from 'zod'
const schema = z.object({
// ...
})
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const parsed = schema.parse(req.body)
// ...
}
### [Error handling](#error-handling)
You can use React state to show an error message when a form submission fails:
pages/index.tsx
TypeScript
JavaScriptTypeScript
import React, { useState, FormEvent } from 'react'
export default function Page() {
const [isLoading, setIsLoading] = useState<boolean>(false)
const [error, setError] = useState<string | null>(null)
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault()
setIsLoading(true)
setError(null) // Clear previous errors when a new request starts
try {
const formData = new FormData(event.currentTarget)
const response = await fetch('/api/submit', {
method: 'POST',
body: formData,
})
if (!response.ok) {
throw new Error('Failed to submit the data. Please try again.')
}
// Handle response if necessary
const data = await response.json()
// ...
} catch (error) {
// Capture the error message to display to the user
setError(error.message)
console.error(error)
} finally {
setIsLoading(false)
}
}
return (
<div>
{error && <div style={{ color: 'red' }}>{error}</div>}
<form onSubmit={onSubmit}>
<input type="text" name="name" />
<button type="submit" disabled={isLoading}>
{isLoading ? 'Loading...' : 'Submit'}
</button>
</form>
</div>
)
}
[Displaying loading state](#displaying-loading-state)
-----------------------------------------------------
You can use React state to show a loading state when a form is submitting on the server:
pages/index.tsx
TypeScript
JavaScriptTypeScript
import React, { useState, FormEvent } from 'react'
export default function Page() {
const [isLoading, setIsLoading] = useState<boolean>(false)
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault()
setIsLoading(true) // Set loading to true when the request starts
try {
const formData = new FormData(event.currentTarget)
const response = await fetch('/api/submit', {
method: 'POST',
body: formData,
})
// Handle response if necessary
const data = await response.json()
// ...
} catch (error) {
// Handle error if necessary
console.error(error)
} finally {
setIsLoading(false) // Set loading to false when the request completes
}
}
return (
<form onSubmit={onSubmit}>
<input type="text" name="name" />
<button type="submit" disabled={isLoading}>
{isLoading ? 'Loading...' : 'Submit'}
</button>
</form>
)
}
### [Redirecting](#redirecting)
If you would like to redirect the user to a different route after a mutation, you can [`redirect`](/docs/pages/building-your-application/routing/api-routes#response-helpers) to any absolute or relative URL:
pages/api/submit.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const id = await addPost()
res.redirect(307, `/post/${id}`)
}
### [Setting cookies](#setting-cookies)
You can set cookies inside an API Route using the `setHeader` method on the response:
pages/api/cookie.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
res.setHeader('Set-Cookie', 'username=lee; Path=/; HttpOnly')
res.status(200).send('Cookie has been set.')
}
### [Reading cookies](#reading-cookies)
You can read cookies inside an API Route using the [`cookies`](/docs/pages/building-your-application/routing/api-routes#request-helpers) request helper:
pages/api/cookie.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const auth = req.cookies.authorization
// ...
}
### [Deleting cookies](#deleting-cookies)
You can delete cookies inside an API Route using the `setHeader` method on the response:
pages/api/cookie.ts
TypeScript
JavaScriptTypeScript
import type { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
res.setHeader('Set-Cookie', 'username=; Path=/; HttpOnly; Max-Age=0')
res.status(200).send('Cookie has been deleted.')
}
[Previous
getStaticPaths
](/docs/pages/building-your-application/data-fetching/get-static-paths)
[Next
getServerSideProps
](/docs/pages/building-your-application/data-fetching/get-server-side-props)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"fababedac1b949af3165e6e41969f27b\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/data-fetching/incremental-static-regeneration | 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
* [On-Demand Revalidation](#on-demand-revalidation)
* [Using On-Demand Revalidation](#using-on-demand-revalidation)
* [Testing on-Demand ISR during development](#testing-on-demand-isr-during-development)
* [Error handling and revalidation](#error-handling-and-revalidation)
* [Self-hosting ISR](#self-hosting-isr)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/03-data-fetching/04-incremental-static-regeneration.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)[Data Fetching](/docs/pages/building-your-application/data-fetching)Incremental Static Regeneration (ISR)
Incremental Static Regeneration (ISR)
=====================================
Examples
* [Next.js Commerce](https://nextjs.org/commerce)
* [GitHub Reactions Demo](https://reactions-demo.vercel.app/)
* [Static Tweet Demo](https://static-tweet.vercel.app/)
Next.js allows you to create or update static pages _after_ you’ve built your site. Incremental Static Regeneration (ISR) enables you to use static-generation on a per-page basis, **without needing to rebuild the entire site**. With ISR, you can retain the benefits of static while scaling to millions of pages.
> **Good to know**: The [`edge` runtime](/docs/pages/api-reference/edge) is currently not compatible with ISR, although you can leverage `stale-while-revalidate` by setting the `cache-control` header manually.
To use ISR, add the `revalidate` prop to `getStaticProps`:
function Blog({ posts }) {
return (
<ul>
{posts.map((post) => (
<li key={post.id}>{post.title}</li>
))}
</ul>
)
}
// This function gets called at build time on server-side.
// It may be called again, on a serverless function, if
// revalidation is enabled and a new request comes in
export async function getStaticProps() {
const res = await fetch('https://.../posts')
const posts = await res.json()
return {
props: {
posts,
},
// Next.js will attempt to re-generate the page:
// - When a request comes in
// - At most once every 10 seconds
revalidate: 10, // In seconds
}
}
// This function gets called at build time on server-side.
// It may be called again, on a serverless function, if
// the path has not been generated.
export async function getStaticPaths() {
const res = await fetch('https://.../posts')
const posts = await res.json()
// Get the paths we want to pre-render based on posts
const paths = posts.map((post) => ({
params: { id: post.id },
}))
// We'll pre-render only these paths at build time.
// { fallback: 'blocking' } will server-render pages
// on-demand if the path doesn't exist.
return { paths, fallback: 'blocking' }
}
export default Blog
When a request is made to a page that was pre-rendered at build time, it will initially show the cached page.
* Any requests to the page after the initial request and before 10 seconds are also cached and instantaneous.
* After the 10-second window, the next request will still show the cached (stale) page
* Next.js triggers a regeneration of the page in the background.
* Once the page generates successfully, Next.js will invalidate the cache and show the updated page. If the background regeneration fails, the old page would still be unaltered.
When a request is made to a path that hasn’t been generated, Next.js will server-render the page on the first request. Future requests will serve the static file from the cache. ISR on Vercel [persists the cache globally and handles rollbacks](https://vercel.com/docs/concepts/next.js/incremental-static-regeneration?utm_source=next-site&utm_medium=docs&utm_campaign=next-website).
> **Good to know**: Check if your upstream data provider has caching enabled by default. You might need to disable (e.g. `useCdn: false`), otherwise a revalidation won't be able to pull fresh data to update the ISR cache. Caching can occur at a CDN (for an endpoint being requested) when it returns the `Cache-Control` header.
[On-Demand Revalidation](#on-demand-revalidation)
-------------------------------------------------
If you set a `revalidate` time of `60`, all visitors will see the same generated version of your site for one minute. The only way to invalidate the cache is from someone visiting that page after the minute has passed.
Starting with `v12.2.0`, Next.js supports On-Demand Incremental Static Regeneration to manually purge the Next.js cache for a specific page. This makes it easier to update your site when:
* Content from your headless CMS is created or updated
* Ecommerce metadata changes (price, description, category, reviews, etc.)
Inside `getStaticProps`, you do not need to specify `revalidate` to use on-demand revalidation. If `revalidate` is omitted, Next.js will use the default value of `false` (no revalidation) and only revalidate the page on-demand when `revalidate()` is called.
> **Good to know**: [Middleware](/docs/pages/building-your-application/routing/middleware) won't be executed for On-Demand ISR requests. Instead, call `revalidate()` on the _exact_ path that you want revalidated. For example, if you have `pages/blog/[slug].js` and a rewrite from `/post-1` -> `/blog/post-1`, you would need to call `res.revalidate('/blog/post-1')`.
### [Using On-Demand Revalidation](#using-on-demand-revalidation)
First, create a secret token only known by your Next.js app. This secret will be used to prevent unauthorized access to the revalidation API Route. You can access the route (either manually or with a webhook) with the following URL structure:
Terminal
https://<your-site.com>/api/revalidate?secret=<token>
Next, add the secret as an [Environment Variable](/docs/pages/building-your-application/configuring/environment-variables) to your application. Finally, create the revalidation API Route:
pages/api/revalidate.js
export default async function handler(req, res) {
// Check for secret to confirm this is a valid request
if (req.query.secret !== process.env.MY_SECRET_TOKEN) {
return res.status(401).json({ message: 'Invalid token' })
}
try {
// this should be the actual path not a rewritten path
// e.g. for "/blog/[slug]" this should be "/blog/post-1"
await res.revalidate('/path-to-revalidate')
return res.json({ revalidated: true })
} catch (err) {
// If there was an error, Next.js will continue
// to show the last successfully generated page
return res.status(500).send('Error revalidating')
}
}
[View our demo](https://on-demand-isr.vercel.app) to see on-demand revalidation in action and provide feedback.
### [Testing on-Demand ISR during development](#testing-on-demand-isr-during-development)
When running locally with `next dev`, `getStaticProps` is invoked on every request. To verify your on-demand ISR configuration is correct, you will need to create a [production build](/docs/pages/api-reference/next-cli#build) and start the [production server](/docs/pages/api-reference/next-cli#production):
Terminal
$ next build
$ next start
Then, you can confirm that static pages have successfully revalidated.
[Error handling and revalidation](#error-handling-and-revalidation)
-------------------------------------------------------------------
If there is an error inside `getStaticProps` when handling background regeneration, or you manually throw an error, the last successfully generated page will continue to show. On the next subsequent request, Next.js will retry calling `getStaticProps`.
export async function getStaticProps() {
// If this request throws an uncaught error, Next.js will
// not invalidate the currently shown page and
// retry getStaticProps on the next request.
const res = await fetch('https://.../posts')
const posts = await res.json()
if (!res.ok) {
// If there is a server error, you might want to
// throw an error instead of returning so that the cache is not updated
// until the next successful request.
throw new Error(`Failed to fetch posts, received status ${res.status}`)
}
// If the request was successful, return the posts
// and revalidate every 10 seconds.
return {
props: {
posts,
},
revalidate: 10,
}
}
[Self-hosting ISR](#self-hosting-isr)
-------------------------------------
Incremental Static Regeneration (ISR) works on [self-hosted Next.js sites](/docs/pages/building-your-application/deploying#self-hosting) out of the box when you use `next start`.
Learn more about [self-hosting Next.js](/docs/pages/building-your-application/deploying#self-hosting).
[Version History](#version-history)
-----------------------------------
Version
Changes
`v14.1.0`
Custom `cacheHandler` is stable.
`v12.2.0`
On-Demand ISR is stable
`v12.1.0`
On-Demand ISR added (beta).
`v12.0.0`
[Bot-aware ISR fallback](https://nextjs.org/blog/next-12#bot-aware-isr-fallback) added.
`v9.5.0`
Base Path added.
[Previous
getServerSideProps
](/docs/pages/building-your-application/data-fetching/get-server-side-props)
[Next
Client-side Fetching
](/docs/pages/building-your-application/data-fetching/client-side)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f2e7dfdc247fa719a9019cc445d13e50\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/data-fetching/get-server-side-props | 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
* [Example](#example)
* [When should I use getServerSideProps?](#when-should-i-use-getserversideprops)
* [Behavior](#behavior)
* [Error Handling](#error-handling)
* [Edge Cases](#edge-cases)
* [Edge Runtime](#edge-runtime)
* [Caching with Server-Side Rendering (SSR)](#caching-with-server-side-rendering-ssr)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/03-data-fetching/03-get-server-side-props.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)[Data Fetching](/docs/pages/building-your-application/data-fetching)getServerSideProps
getServerSideProps
==================
`getServerSideProps` is a Next.js function that can be used to fetch data and render the contents of a page at request time.
[Example](#example)
-------------------
You can use `getServerSideProps` by exporting it from a Page Component. The example below shows how you can fetch data from a 3rd party API in `getServerSideProps`, and pass the data to the page as props:
pages/index.tsx
TypeScript
JavaScriptTypeScript
import type { InferGetServerSidePropsType, GetServerSideProps } from 'next'
type Repo = {
name: string
stargazers_count: number
}
export const getServerSideProps = (async () => {
// Fetch data from external API
const res = await fetch('https://api.github.com/repos/vercel/next.js')
const repo: Repo = await res.json()
// Pass data to the page via props
return { props: { repo } }
}) satisfies GetServerSideProps<{ repo: Repo }>
export default function Page({
repo,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
return (
<main>
<p>{repo.stargazers_count}</p>
</main>
)
}
[When should I use `getServerSideProps`?](#when-should-i-use-getserversideprops)
--------------------------------------------------------------------------------
You should use `getServerSideProps` if you need to render a page that relies on personalized user data, or information that can only be known at request time. For example, `authorization` headers or a geolocation.
If you do not need to fetch the data at request time, or would prefer to cache the data and pre-rendered HTML, we recommend using [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props).
[Behavior](#behavior)
---------------------
* `getServerSideProps` runs on the server.
* `getServerSideProps` can only be exported from a **page**.
* `getServerSideProps` returns JSON.
* When a user visits a page, `getServerSideProps` will be used to fetch data at request time, and the data is used to render the initial HTML of the page.
* `props` passed to the page component can be viewed on the client as part of the initial HTML. This is to allow the page to be [hydrated](https://react.dev/reference/react-dom/hydrate) correctly. Make sure that you don't pass any sensitive information that shouldn't be available on the client in `props`.
* When a user visits the page through [`next/link`](/docs/pages/api-reference/components/link) or [`next/router`](/docs/pages/api-reference/functions/use-router), Next.js sends an API request to the server, which runs `getServerSideProps`.
* You do not have to call a Next.js [API Route](/docs/pages/building-your-application/routing/api-routes) to fetch data when using `getServerSideProps` since the function runs on the server. Instead, you can call a CMS, database, or other third-party APIs directly from inside `getServerSideProps`.
> **Good to know:**
>
> * See [`getServerSideProps` API reference](/docs/pages/api-reference/functions/get-server-side-props) for parameters and props that can be used with `getServerSideProps`.
> * You can use the [next-code-elimination tool](https://next-code-elimination.vercel.app/) to verify what Next.js eliminates from the client-side bundle.
[Error Handling](#error-handling)
---------------------------------
If an error is thrown inside `getServerSideProps`, it will show the `pages/500.js` file. Check out the documentation for [500 page](/docs/pages/building-your-application/routing/custom-error#500-page) to learn more on how to create it. During development, this file will not be used and the development error overlay will be shown instead.
[Edge Cases](#edge-cases)
-------------------------
### [Edge Runtime](#edge-runtime)
`getServerSideProps` can be used with both [Serverless and Edge Runtimes](/docs/pages/building-your-application/rendering/edge-and-nodejs-runtimes), and you can set props in both.
However, currently in the Edge Runtime, you do not have access to the response object. This means that you cannot — for example — add cookies in `getServerSideProps`. To have access to the response object, you should **continue to use the Node.js runtime**, which is the default runtime.
You can explicitly set the runtime on a per-page basis by modifying the `config`, for example:
pages/index.js
export const config = {
runtime: 'nodejs', // or "edge"
}
export const getServerSideProps = async () => {}
### [Caching with Server-Side Rendering (SSR)](#caching-with-server-side-rendering-ssr)
You can use caching headers (`Cache-Control`) inside `getServerSideProps` to cache dynamic responses. For example, using [`stale-while-revalidate`](https://web.dev/stale-while-revalidate/).
// This value is considered fresh for ten seconds (s-maxage=10).
// If a request is repeated within the next 10 seconds, the previously
// cached value will still be fresh. If the request is repeated before 59 seconds,
// the cached value will be stale but still render (stale-while-revalidate=59).
//
// In the background, a revalidation request will be made to populate the cache
// with a fresh value. If you refresh the page, you will see the new value.
export async function getServerSideProps({ req, res }) {
res.setHeader(
'Cache-Control',
'public, s-maxage=10, stale-while-revalidate=59'
)
return {
props: {},
}
}
However, before reaching for `cache-control`, we recommend seeing if [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) with [ISR](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration) is a better fit for your use case.
[Previous
Forms and Mutations
](/docs/pages/building-your-application/data-fetching/forms-and-mutations)
[Next
Incremental Static Regeneration (ISR)
](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f22e642718e3f7415f41e3df193b7a85\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application | 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/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
Building Your Application
=========================
Next.js provides the building blocks to create flexible, full-stack web applications. The guides in **Building Your Application** explain how to use these features and how to customize your application's behavior.
The sections and pages are organized sequentially, from basic to advanced, so you can follow them step-by-step when building your Next.js application. However, you can read them in any order or skip to the pages that apply to your use case.
If you're new to Next.js, we recommend starting with the [Routing](/docs/pages/building-your-application/routing), [Rendering](/docs/pages/building-your-application/rendering), [Data Fetching](/docs/pages/building-your-application/data-fetching) and [Styling](/docs/pages/building-your-application/styling) sections, as they introduce the fundamental Next.js and web concepts to help you get started. Then, you can dive deeper into the other sections such as [Optimizing](/docs/pages/building-your-application/optimizing) and [Configuring](/docs/pages/building-your-application/configuring). Finally, once you're ready, checkout the [Deploying](/docs/pages/building-your-application/deploying) and [Upgrading](/docs/pages/building-your-application/upgrading) sections.
[
### Routing
Learn the fundamentals of routing for front-end applications with the Pages Router.
](/docs/pages/building-your-application/routing)[
### Rendering
Learn the fundamentals of rendering in React and Next.js.
](/docs/pages/building-your-application/rendering)[
### Data Fetching
Next.js allows you to fetch data in multiple ways, with pre-rendering, server-side rendering or static-site generation, and incremental static regeneration. Learn how to manage your application data in Next.js.
](/docs/pages/building-your-application/data-fetching)[
### Styling
Learn the different ways you can style your Next.js application.
](/docs/pages/building-your-application/styling)[
### Optimizing
Optimize your Next.js application for best performance and user experience.
](/docs/pages/building-your-application/optimizing)[
### Configuring
Learn how to configure your Next.js application.
](/docs/pages/building-your-application/configuring)[
### Testing
Learn how to set up Next.js with three commonly used testing tools — Cypress, Playwright, Vitest, and Jest.
](/docs/pages/building-your-application/testing)[
### Authentication
Learn how to implement authentication in Next.js, covering best practices, securing routes, authorization techniques, and session management.
](/docs/pages/building-your-application/authentication)[
### Deploying
Learn how to deploy your Next.js app to production, either managed or self-hosted.
](/docs/pages/building-your-application/deploying)[
### Upgrading
Learn how to upgrade to the latest versions of Next.js.
](/docs/pages/building-your-application/upgrading)
[Previous
Pages Router
](/docs/pages)
[Next
Routing
](/docs/pages/building-your-application/routing)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"35e47171f314cd9a306c5eee2b4674f8\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/lazy-loading | 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
* [next/dynamic](#nextdynamic)
* [Examples](#examples)
* [With named exports](#with-named-exports)
* [With no SSR](#with-no-ssr)
* [With external libraries](#with-external-libraries)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/08-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/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)
---------------------
By using `next/dynamic`, the header component will not be included in the page's initial JavaScript bundle. The page will render the Suspense `fallback` first, followed by the `Header` component when the `Suspense` boundary is resolved.
import dynamic from 'next/dynamic'
const DynamicHeader = dynamic(() => import('../components/header'), {
loading: () => <p>Loading...</p>,
})
export default function Home() {
return <DynamicHeader />
}
> **Good to know**: In `import('path/to/component')`, the path must be explicitly written. It can't be a template string nor a variable. Furthermore the `import()` has to be inside the `dynamic()` call for Next.js to be able to match webpack bundles / module ids to the specific `dynamic()` call and preload them before rendering. `dynamic()` can't be used inside of React rendering as it needs to be marked in the top level of the module for preloading to work, similar to `React.lazy`.
[With named exports](#with-named-exports)
-----------------------------------------
To dynamically import a named export, you can return it from the [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise) returned by [`import()`](https://github.com/tc39/proposal-dynamic-import#example):
components/hello.js
export function Hello() {
return <p>Hello!</p>
}
// pages/index.js
import dynamic from 'next/dynamic'
const DynamicComponent = dynamic(() =>
import('../components/hello').then((mod) => mod.Hello)
)
[With no SSR](#with-no-ssr)
---------------------------
To dynamically load a component on the client side, you can use the `ssr` option to disable server-rendering. This is useful if an external dependency or component relies on browser APIs like `window`.
import dynamic from 'next/dynamic'
const DynamicHeader = dynamic(() => import('../components/header'), {
ssr: false,
})
[With external libraries](#with-external-libraries)
---------------------------------------------------
This example uses the external library `fuse.js` for fuzzy search. The module is only loaded in the browser after the user types in the search input.
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>
)
}
[Previous
Analytics
](/docs/pages/building-your-application/optimizing/analytics)
[Next
Instrumentation
](/docs/pages/building-your-application/optimizing/instrumentation)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"26112b6081a2f4bc1b2913d8a4407f18\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/instrumentation | 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)
* [Examples](#examples)
* [Importing files with side effects](#importing-files-with-side-effects)
* [Importing runtime-specific code](#importing-runtime-specific-code)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/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')
}
}
[Previous
Lazy Loading
](/docs/pages/building-your-application/optimizing/lazy-loading)
[Next
OpenTelemetry
](/docs/pages/building-your-application/optimizing/open-telemetry)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"41bdf6664d665c19029286485d2f4dde\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/app/api-reference/next-cli | 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
* [Development](#development)
* [Turbopack](#turbopack)
* [HTTPS for Local Development](#https-for-local-development)
* [Build](#build)
* [Debug](#debug)
* [Linting](#linting)
* [Mangling](#mangling)
* [Profiling](#profiling)
* [Production](#production)
* [Keep Alive Timeout](#keep-alive-timeout)
* [Info](#info)
* [Lint](#lint)
* [Telemetry](#telemetry)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/02-app/02-api-reference/08-next-cli.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.js CLI
Next.js CLI
===========
The Next.js CLI allows you to develop, build, start your application, and more.
To get a list of the available CLI commands, run the following command inside your project directory:
Terminal
next -h
The output should look like this:
Terminal
Usage next [options] [command]
The Next.js CLI allows you to develop, build, start your application, and more.
Options:
-v, --version Outputs the Next.js version.
-h, --help Displays this message.
Commands:
build [directory] [options] Creates an optimized production build of your application.
The output displays information about each route.
dev [directory] [options] Starts Next.js in development mode with hot-code reloading,
error reporting, and more.
info [options] Prints relevant details about the current system which can be
used to report Next.js bugs.
lint [directory] [options] Runs ESLint for all files in the `/src`, `/app`, `/pages`,
`/components`, and `/lib` directories. It also provides a
guided setup to install any required dependencies if ESLint
is not already configured in your application.
start [directory] [options] Starts Next.js in production mode. The application should be
compiled with `next build` first.
telemetry [options] Allows you to enable or disable Next.js' completely
anonymous telemetry collection.
You can pass any [node arguments](https://nodejs.org/api/cli.html#cli_node_options_options) to `next` commands:
Terminal
NODE_OPTIONS='--throw-deprecation' next
NODE_OPTIONS='-r esm' next
NODE_OPTIONS='--inspect' next
> **Good to know**: Running `next` without a command is the same as running `next dev`
[Development](#development)
---------------------------
`next dev` starts the application in development mode with hot-code reloading, error reporting, and more.
To get a list of the available options with `next dev`, run the following command inside your project directory:
Terminal
next dev -h
The output should look like this:
Terminal
Usage: next dev [directory] [options]
Starts Next.js in development mode with hot-code reloading, error reporting, and more.
Arguments:
[directory] A directory on which to build the application.
If no directory is provided, the current
directory will be used.
Options:
--turbo Starts development mode using Turbopack (beta).
-p, --port <port> Specify a port number on which to start the
application. (default: 3000, env: PORT)
-H, --hostname <hostname> Specify a hostname on which to start the
application (default: 0.0.0.0).
--experimental-https Starts the server with HTTPS and generates a
self-signed certificate.
--experimental-https-key, <path> Path to a HTTPS key file.
--experimental-https-cert, <path> Path to a HTTPS certificate file.
--experimental-https-ca, <path> Path to a HTTPS certificate authority file.
--experimental-upload-trace, <traceUrl> Reports a subset of the debugging trace to a
remote HTTP URL. Includes sensitive data.
-h, --help Displays this message.
The application will start at `http://localhost:3000` by default. The default port can be changed with `-p`, like so:
Terminal
next dev -p 4000
Or using the `PORT` environment variable:
Terminal
PORT=4000 next dev
> **Good to know**:
>
> * `PORT` cannot be set in `.env` as booting up the HTTP server happens before any other code is initialized.
> * Next.js will automatically retry with another port until a port is available if a port is not specified with the CLI option `--port` or the `PORT` environment variable.
You can also set the hostname to be different from the default of `0.0.0.0`, this can be useful for making the application available for other devices on the network. The default hostname can be changed with `-H`, like so:
Terminal
next dev -H 192.168.1.2
### [Turbopack](#turbopack)
[Turbopack](/docs/architecture/turbopack) (beta), our new bundler, which is being tested and stabilized in Next.js, helps speed up local iterations while working on your application.
To use Turbopack in development mode, add the `--turbo` option:
Terminal
next dev --turbo
### [HTTPS for Local Development](#https-for-local-development)
For certain use cases like webhooks or authentication, it may be required to use HTTPS to have a secure environment on `localhost`. Next.js can generate a self-signed certificate with `next dev` as follows:
Terminal
next dev --experimental-https
You can also provide a custom certificate and key with `--experimental-https-key` and `--experimental-https-cert`. Optionally, you can provide a custom CA certificate with `--experimental-https-ca` as well.
Terminal
next dev --experimental-https --experimental-https-key ./certificates/localhost-key.pem --experimental-https-cert ./certificates/localhost.pem
`next dev --experimental-https` is only intended for development and creates a locally-trusted certificate with `mkcert`. In production, use properly issued certificates from trusted authorities. When deploying to Vercel, HTTPS is [automatically configured](https://vercel.com/docs/security/encryption) for your Next.js application.
[Build](#build)
---------------
`next build` creates an optimized production build of your application. The output displays information about each route:
Terminal
Route (app) Size First Load JS
┌ ○ / 5.3 kB 89.5 kB
├ ○ /_not-found 885 B 85.1 kB
└ ○ /about 137 B 84.4 kB
+ First Load JS shared by all 84.2 kB
├ chunks/184-d3bb186aac44da98.js 28.9 kB
├ chunks/30b509c0-f3503c24f98f3936.js 53.4 kB
└ other shared chunks (total)
○ (Static) prerendered as static content
* **Size**: The number of assets downloaded when navigating to the page client-side. The size for each route only includes its dependencies.
* **First Load JS**: The number of assets downloaded when visiting the page from the server. The amount of JS shared by all is shown as a separate metric.
Both of these values are [**compressed with gzip**](/docs/app/api-reference/next-config-js/compress). The first load is indicated by green, yellow, or red. Aim for green for performant applications.
To get a list of the available options with `next build`, run the following command inside your project directory:
Terminal
next build -h
The output should look like this:
Terminal
Usage: next build [directory] [options]
Creates an optimized production build of your application. The output displays information
about each route.
Arguments:
[directory] A directory on which to build the application. If no
provided, the current directory will be
used.
Options:
-d, --debug Enables a more verbose build output.
--profile Enables production profiling for React.
--no-lint Disables linting.
--no-mangling Disables mangling.
--experimental-app-only Builds only App Router routes.
--experimental-build-mode [mode] Uses an experimental build mode. (choices: "compile"
"generate", default: "default")
-h, --help Displays this message.
### [Debug](#debug)
You can enable more verbose build output with the `--debug` flag in `next build`.
Terminal
next build --debug
With this flag enabled additional build output like rewrites, redirects, and headers will be shown.
### [Linting](#linting)
You can disable linting for builds like so:
Terminal
next build --no-lint
### [Mangling](#mangling)
You can disable [mangling](https://en.wikipedia.org/wiki/Name_mangling) for builds like so:
Terminal
next build --no-mangling
> **Good to know**: This may affect performance and should only be used for debugging purposes.
### [Profiling](#profiling)
You can enable production profiling for React with the `--profile` flag in `next build`.
Terminal
next build --profile
After that, you can use the profiler in the same way as you would in development.
[Production](#production)
-------------------------
`next start` starts the application in production mode. The application should be compiled with [`next build`](#build) first.
To get a list of the available options with `next start`, run the follow command inside your project directory:
Terminal
next start -h
The output should look like this:
Terminal
Usage: next start [directory] [options]
Starts Next.js in production mode. The application should be compiled with `next build`
first.
Arguments:
[directory] A directory on which to start the application.
If not directory is provided, the current
directory will be used.
Options:
-p, --port <port> Specify a port number on which to start the
application. (default: 3000, env: PORT)
-H, --hostname <hostname> Specify a hostname on which to start the
application (default: 0.0.0.0).
--keepAliveTimeout <keepAliveTimeout> Specify the maximum amount of milliseconds to wait
before closing the inactive connections.
-h, --help Displays this message.
The application will start at `http://localhost:3000` by default. The default port can be changed with `-p`, like so:
Terminal
next start -p 4000
Or using the `PORT` environment variable:
Terminal
PORT=4000 next start
> **Good to know**:
>
> * `PORT` cannot be set in `.env` as booting up the HTTP server happens before any other code is initialized.
> * `next start` cannot be used with `output: 'standalone'` or `output: 'export'`.
### [Keep Alive Timeout](#keep-alive-timeout)
When deploying Next.js behind a downstream proxy (e.g. a load-balancer like AWS ELB/ALB) it's important to configure Next's underlying HTTP server with [keep-alive timeouts](https://nodejs.org/api/http.html#http_server_keepalivetimeout) that are _larger_ than the downstream proxy's timeouts. Otherwise, once a keep-alive timeout is reached for a given TCP connection, Node.js will immediately terminate that connection without notifying the downstream proxy. This results in a proxy error whenever it attempts to reuse a connection that Node.js has already terminated.
To configure the timeout values for the production Next.js server, pass `--keepAliveTimeout` (in milliseconds) to `next start`, like so:
Terminal
next start --keepAliveTimeout 70000
[Info](#info)
-------------
`next info` prints relevant details about the current system which can be used to report Next.js bugs. This information includes Operating System platform/arch/version, Binaries (Node.js, npm, Yarn, pnpm) and npm package versions (`next`, `react`, `react-dom`).
To get a list of the available options with `next info`, run the following command inside your project directory:
Terminal
next info -h
The output should look like this:
Terminal
Usage: next info [options]
Prints relevant details about the current system which can be used to report Next.js bugs.
Options:
--verbose Collections additional information for debugging.
-h, --help Displays this message.
Running `next info` will give you information like this example:
Terminal
Operating System:
Platform: linux
Arch: x64
Version: #22-Ubuntu SMP Fri Nov 5 13:21:36 UTC 2021
Available memory (MB): 31795
Available CPU cores: 16
Binaries:
Node: 16.13.0
npm: 8.1.0
Yarn: 1.22.17
pnpm: 6.24.2
Relevant Packages:
next: 14.1.1-canary.61 // Latest available version is detected (14.1.1-canary.61).
react: 18.2.0
react-dom: 18.2.0
Next.js Config:
output: N/A
This information should then be pasted into GitHub Issues.
You can also run `next info --verbose` which will print additional information about the system and the installation of packages related to `next`.
[Lint](#lint)
-------------
`next lint` runs ESLint for all files in the `pages/`, `app/`, `components/`, `lib/`, and `src/` directories. It also provides a guided setup to install any required dependencies if ESLint is not already configured in your application.
To get a list of the available options with `next lint`, run the following command inside your project directory:
Terminal
next lint -h
The output should look like this:
Terminal
Usage: next lint [directory] [options]
Runs ESLint for all files in the `/src`, `/app`, `/pages`, `/components`, and `/lib` directories. It also
provides a guided setup to install any required dependencies if ESLint is not already configured in your
application.
Arguments:
[directory] A base directory on which to lint the application.
If no directory is provided, the current directory
will be used.
Options:
-d, --dir, <dirs...> Include directory, or directories, to run ESLint.
--file, <files...> Include file, or files, to run ESLint.
--ext, [exts...] Specify JavaScript file extensions. (default:
[".js", ".mjs", ".cjs", ".jsx", ".ts", ".mts", ".cts", ".tsx"])
-c, --config, <config> Uses this configuration file, overriding all other
configuration options.
--resolve-plugins-relative-to, <rprt> Specify a directory where plugins should be resolved
from.
--strict Creates a `.eslintrc.json` file using the Next.js
strict configuration.
--rulesdir, <rulesdir...> Uses additional rules from this directory(s).
--fix Automatically fix linting issues.
--fix-type <fixType> Specify the types of fixes to apply (e.g., problem,
suggestion, layout).
--ignore-path <path> Specify a file to ignore.
--no-ignore <path> Disables the `--ignore-path` option.
--quiet Reports errors only.
--max-warnings [maxWarnings] Specify the number of warnings before triggering a
non-zero exit code. (default: -1)
-o, --output-file, <outputFile> Specify a file to write report to.
-f, --format, <format> Uses a specifc output format.
--no-inline-config Prevents comments from changing config or rules.
--report-unused-disable-directives-severity <level> Specify severity level for unused eslint-disable
directives. (choices: "error", "off", "warn")
--no-cache Disables caching.
--cache-location, <cacheLocation> Specify a location for cache.
--cache-strategy, [cacheStrategy] Specify a strategy to use for detecting changed files
in the cache. (default: "metadata")
--error-on-unmatched-pattern Reports errors when any file patterns are unmatched.
-h, --help Displays this message.
If you have other directories that you would like to lint, you can specify them using the `--dir` flag:
Terminal
next lint --dir utils
For more information on the other options, check out our [ESLint](/docs/app/building-your-application/configuring/eslint) configuration documentation.
[Telemetry](#telemetry)
-----------------------
Next.js collects **completely anonymous** telemetry data about general usage. Participation in this anonymous program is optional, and you may opt-out if you'd not like to share any information.
To get a list of the available options with `next telemetry`, run the following command in your project directory:
Terminal
next telemetry -h
The output should look like this:
Terminal
Usage: next telemetry [options]
Allows you to enable or disable Next.js' completely anonymous telemetry collection.
Options:
--enable Eanbles Next.js' telemetry collection.
--disable Disables Next.js' telemetry collection.
-h, --help Displays this message.
Learn more: https://nextjs.org/telemetry
Learn more about [Telemetry](/telemetry).
[Previous
Edge Runtime
](/docs/app/api-reference/edge)
[Next
Architecture
](/docs/architecture)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"fadd0775406293ce0ff33f97d686cc97\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/data-fetching/client-side | 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
* [Client-side data fetching with useEffect](#client-side-data-fetching-with-useeffect)
* [Client-side data fetching with SWR](#client-side-data-fetching-with-swr)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/03-data-fetching/05-client-side.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)[Data Fetching](/docs/pages/building-your-application/data-fetching)Client-side Fetching
Client-side Fetching
====================
Client-side data fetching is useful when your page doesn't require SEO indexing, when you don't need to pre-render your data, or when the content of your pages needs to update frequently. Unlike the server-side rendering APIs, you can use client-side data fetching at the component level.
If done at the page level, the data is fetched at runtime, and the content of the page is updated as the data changes. When used at the component level, the data is fetched at the time of the component mount, and the content of the component is updated as the data changes.
It's important to note that using client-side data fetching can affect the performance of your application and the load speed of your pages. This is because the data fetching is done at the time of the component or pages mount, and the data is not cached.
[Client-side data fetching with useEffect](#client-side-data-fetching-with-useeffect)
-------------------------------------------------------------------------------------
The following example shows how you can fetch data on the client side using the useEffect hook.
import { useState, useEffect } from 'react'
function Profile() {
const [data, setData] = useState(null)
const [isLoading, setLoading] = useState(true)
useEffect(() => {
fetch('/api/profile-data')
.then((res) => res.json())
.then((data) => {
setData(data)
setLoading(false)
})
}, [])
if (isLoading) return <p>Loading...</p>
if (!data) return <p>No profile data</p>
return (
<div>
<h1>{data.name}</h1>
<p>{data.bio}</p>
</div>
)
}
[Client-side data fetching with SWR](#client-side-data-fetching-with-swr)
-------------------------------------------------------------------------
The team behind Next.js has created a React hook library for data fetching called [**SWR**](https://swr.vercel.app/). It is **highly recommended** if you are fetching data on the client-side. It handles caching, revalidation, focus tracking, refetching on intervals, and more.
Using the same example as above, we can now use SWR to fetch the profile data. SWR will automatically cache the data for us and will revalidate the data if it becomes stale.
For more information on using SWR, check out the [SWR docs](https://swr.vercel.app/docs/getting-started).
import useSWR from 'swr'
const fetcher = (...args) => fetch(...args).then((res) => res.json())
function Profile() {
const { data, error } = useSWR('/api/profile-data', fetcher)
if (error) return <div>Failed to load</div>
if (!data) return <div>Loading...</div>
return (
<div>
<h1>{data.name}</h1>
<p>{data.bio}</p>
</div>
)
}
[Previous
Incremental Static Regeneration (ISR)
](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
[Next
Styling
](/docs/pages/building-your-application/styling)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"c01f26b9f706914497364344194ee095\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/routing | 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/01-routing/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)Routing
Routing
=======
The Pages Router has a file-system based router built on concepts of pages. When a file is added to the `pages` directory it's automatically available as a route. Learn more about routing in the Pages Router:
[
### Pages and Layouts
Create your first page and shared layout with the Pages Router.
](/docs/pages/building-your-application/routing/pages-and-layouts)[
### Dynamic Routes
Dynamic Routes are pages that allow you to add custom params to your URLs. Start creating Dynamic Routes and learn more here.
](/docs/pages/building-your-application/routing/dynamic-routes)[
### 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)[
### Redirecting
Learn the different ways to handle redirects in Next.js.
](/docs/pages/building-your-application/routing/redirecting)[
### Custom App
Control page initialization and add a layout that persists for all pages by overriding the default App component used by Next.js.
](/docs/pages/building-your-application/routing/custom-app)[
### Custom Document
Extend the default document markup added by Next.js.
](/docs/pages/building-your-application/routing/custom-document)[
### API Routes
Next.js supports API Routes, which allow you to build your API without leaving your Next.js app. Learn how it works here.
](/docs/pages/building-your-application/routing/api-routes)[
### Custom Errors
Override and extend the built-in Error page to handle custom errors.
](/docs/pages/building-your-application/routing/custom-error)[
### Internationalization
Next.js has built-in support for internationalized routing and language detection. Learn more here.
](/docs/pages/building-your-application/routing/internationalization)[
### Middleware
Learn how to use Middleware to run code before a request is completed.
](/docs/pages/building-your-application/routing/middleware)
[Previous
Building Your Application
](/docs/pages/building-your-application)
[Next
Pages and Layouts
](/docs/pages/building-your-application/routing/pages-and-layouts)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"1868beedff04556952bcab1ef5cc0a95\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/third-party-libraries | 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)
* [Google Third-Parties](#google-third-parties)
* [Google Tag Manager](#google-tag-manager)
* [Sending Events](#sending-events)
* [Options](#options)
* [Google Analytics](#google-analytics)
* [Sending Events](#sending-events-1)
* [Tracking Pageviews](#tracking-pageviews)
* [Options](#options-1)
* [Google Maps Embed](#google-maps-embed)
* [Options](#options-2)
* [YouTube Embed](#youtube-embed)
* [Options](#options-3)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/05-optimizing/11-third-party-libraries.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)[Optimizing](/docs/pages/building-your-application/optimizing)Third Party Libraries
Third Party Libraries
=====================
**`@next/third-parties`** is a library that provides a collection of components and utilities that improve the performance and developer experience of loading popular third-party libraries in your Next.js application.
All third-party integrations provided by `@next/third-parties` have been optimized for performance and ease of use.
[Getting Started](#getting-started)
-----------------------------------
To get started, install the `@next/third-parties` library:
Terminal
npm install @next/third-parties@latest next@latest
`@next/third-parties` is currently an **experimental** library under active development. We recommend installing it with the **latest** or **canary** flags while we work on adding more third-party integrations.
[Google Third-Parties](#google-third-parties)
---------------------------------------------
All supported third-party libraries from Google can be imported from `@next/third-parties/google`.
### [Google Tag Manager](#google-tag-manager)
The `GoogleTagManager` component can be used to instantiate a [Google Tag Manager](https://developers.google.com/tag-platform/tag-manager) container to your page. By default, it fetches the original inline script after hydration occurs on the page.
To load Google Tag Manager for all routes, include the component directly in your custom `_app` and pass in your GTM container ID:
pages/\_app.js
import { GoogleTagManager } from '@next/third-parties/google'
export default function MyApp({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<GoogleTagManager gtmId="GTM-XYZ" />
</>
)
}
To load Google Tag Manager for a single route, include the component in your page file:
pages/index.js
import { GoogleTagManager } from '@next/third-parties/google'
export default function Page() {
return <GoogleTagManager gtmId="GTM-XYZ" />
}
#### [Sending Events](#sending-events)
The `sendGTMEvent` function can be used to track user interactions on your page by sending events using the `dataLayer` object. For this function to work, the `<GoogleTagManager />` component must be included in either a parent layout, page, or component, or directly in the same file.
pages/index.js
import { sendGTMEvent } from '@next/third-parties/google'
export function EventButton() {
return (
<div>
<button
onClick={() => sendGTMEvent({ event: 'buttonClicked', value: 'xyz' })}
>
Send Event
</button>
</div>
)
}
Refer to the Tag Manager [developer documentation](https://developers.google.com/tag-platform/tag-manager/datalayer) to learn about the different variables and events that can be passed into the function.
#### [Options](#options)
Options to pass to the Google Tag Manager. For a full list of options, read the [Google Tag Manager docs](https://developers.google.com/tag-platform/tag-manager/datalayer).
Name
Type
Description
`gtmId`
Required
Your GTM container ID. Usually starts with `GTM-`.
`dataLayer`
Optional
Data layer array to instantiate the container with. Defaults to an empty array.
`dataLayerName`
Optional
Name of the data layer. Defaults to `dataLayer`.
`auth`
Optional
Value of authentication parameter (`gtm_auth`) for environment snippets.
`preview`
Optional
Value of preview parameter (`gtm_preview`) for environment snippets.
### [Google Analytics](#google-analytics)
The `GoogleAnalytics` component can be used to include [Google Analytics 4](https://developers.google.com/analytics/devguides/collection/ga4) to your page via the Google tag (`gtag.js`). By default, it fetches the original scripts after hydration occurs on the page.
> **Recommendation**: If Google Tag Manager is already included in your application, you can configure Google Analytics directly using it, rather than including Google Analytics as a separate component. Refer to the [documentation](https://developers.google.com/analytics/devguides/collection/ga4/tag-options#what-is-gtm) to learn more about the differences between Tag Manager and `gtag.js`.
To load Google Analytics for all routes, include the component directly in your custom `_app` and pass in your measurement ID:
pages/\_app.js
import { GoogleAnalytics } from '@next/third-parties/google'
export default function MyApp({ Component, pageProps }) {
return (
<>
<Component {...pageProps} />
<GoogleAnalytics gaId="G-XYZ" />
</>
)
}
To load Google Analytics for a single route, include the component in your page file:
pages/index.js
import { GoogleAnalytics } from '@next/third-parties/google'
export default function Page() {
return <GoogleAnalytics gaId="G-XYZ" />
}
#### [Sending Events](#sending-events-1)
The `sendGAEvent` function can be used to measure user interactions on your page by sending events using the `dataLayer` object. For this function to work, the `<GoogleAnalytics />` component must be included in either a parent layout, page, or component, or directly in the same file.
pages/index.js
import { sendGAEvent } from '@next/third-parties/google'
export function EventButton() {
return (
<div>
<button
onClick={() => sendGAEvent({ event: 'buttonClicked', value: 'xyz' })}
>
Send Event
</button>
</div>
)
}
Refer to the Google Analytics [developer documentation](https://developers.google.com/analytics/devguides/collection/ga4/event-parameters) to learn more about event parameters.
#### [Tracking Pageviews](#tracking-pageviews)
Google Analytics automatically tracks pageviews when the browser history state changes. This means that client-side navigations between Next.js routes will send pageview data without any configuration.
To ensure that client-side navigations are being measured correctly, verify that the [_“Enhanced Measurement”_](https://support.google.com/analytics/answer/9216061#enable_disable) property is enabled in your Admin panel and the _“Page changes based on browser history events”_ checkbox is selected.
> **Note**: If you decide to manually send pageview events, make sure to disable the default pageview measurement to avoid having duplicate data. Refer to the Google Analytics [developer documentation](https://developers.google.com/analytics/devguides/collection/ga4/views?client_type=gtag#manual_pageviews) to learn more.
#### [Options](#options-1)
Options to pass to the `<GoogleAnalytics>` component.
Name
Type
Description
`gaId`
Required
Your [measurement ID](https://support.google.com/analytics/answer/12270356). Usually starts with `G-`.
`dataLayerName`
Optional
Name of the data layer. Defaults to `dataLayer`.
### [Google Maps Embed](#google-maps-embed)
The `GoogleMapsEmbed` component can be used to add a [Google Maps Embed](https://developers.google.com/maps/documentation/embed/embedding-map) to your page. By default, it uses the `loading` attribute to lazy-load the embed below the fold.
pages/index.js
import { GoogleMapsEmbed } from '@next/third-parties/google'
export default function Page() {
return (
<GoogleMapsEmbed
apiKey="XYZ"
height={200}
width="100%"
mode="place"
q="Brooklyn+Bridge,New+York,NY"
/>
)
}
#### [Options](#options-2)
Options to pass to the Google Maps Embed. For a full list of options, read the [Google Map Embed docs](https://developers.google.com/maps/documentation/embed/embedding-map).
Name
Type
Description
`apiKey`
Required
Your api key.
`mode`
Required
[Map mode](https://developers.google.com/maps/documentation/embed/embedding-map#choosing_map_modes)
`height`
Optional
Height of the embed. Defaults to `auto`.
`width`
Optional
Width of the embed. Defaults to `auto`.
`style`
Optional
Pass styles to the iframe.
`allowfullscreen`
Optional
Property to allow certain map parts to go full screen.
`loading`
Optional
Defaults to lazy. Consider changing if you know your embed will be above the fold.
`q`
Optional
Defines map marker location. _This may be required depending on the map mode_.
`center`
Optional
Defines the center of the map view.
`zoom`
Optional
Sets initial zoom level of the map.
`maptype`
Optional
Defines type of map tiles to load.
`language`
Optional
Defines the language to use for UI elements and for the display of labels on map tiles.
`region`
Optional
Defines the appropriate borders and labels to display, based on geo-political sensitivities.
### [YouTube Embed](#youtube-embed)
The `YouTubeEmbed` component can be used to load and display a YouTube embed. This component loads faster by using [`lite-youtube-embed`](https://github.com/paulirish/lite-youtube-embed) under the hood.
pages/index.js
import { YouTubeEmbed } from '@next/third-parties/google'
export default function Page() {
return <YouTubeEmbed videoid="ogfYd705cRs" height={400} params="controls=0" />
}
#### [Options](#options-3)
Name
Type
Description
`videoid`
Required
YouTube video id.
`width`
Optional
Width of the video container. Defaults to `auto`
`height`
Optional
Height of the video container. Defaults to `auto`
`playlabel`
Optional
A visually hidden label for the play button for accessibility.
`params`
Optional
The video player params defined [here](https://developers.google.com/youtube/player_parameters#Parameters).
Params are passed as a query param string.
Eg: `params="controls=0&start=10&end=30"`
`style`
Optional
Used to apply styles to the video container.
[Previous
OpenTelemetry
](/docs/pages/building-your-application/optimizing/open-telemetry)
[Next
Configuring
](/docs/pages/building-your-application/configuring)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"dd60a9d9bd9e9e8581ff57adb921eb7f\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/styling | 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/04-styling/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)Styling
Styling
=======
Next.js supports different ways of styling your application, including:
* **Global CSS**: Simple to use and familiar for those experienced with traditional CSS, but can lead to larger CSS bundles and difficulty managing styles as the application grows.
* **CSS Modules**: Create locally scoped CSS classes to avoid naming conflicts and improve maintainability.
* **Tailwind CSS**: A utility-first CSS framework that allows for rapid custom designs by composing utility classes.
* **Sass**: A popular CSS preprocessor that extends CSS with features like variables, nested rules, and mixins.
* **CSS-in-JS**: Embed CSS directly in your JavaScript components, enabling dynamic and scoped styling.
Learn more about each approach by exploring their respective documentation:
[
### CSS Modules
Style your Next.js Application using CSS Modules.
](/docs/pages/building-your-application/styling/css-modules)[
### Tailwind CSS
Style your Next.js Application using Tailwind CSS.
](/docs/pages/building-your-application/styling/tailwind-css)[
### CSS-in-JS
Use CSS-in-JS libraries with Next.js
](/docs/pages/building-your-application/styling/css-in-js)[
### Sass
Learn how to use Sass in your Next.js application.
](/docs/pages/building-your-application/styling/sass)
[Previous
Client-side Fetching
](/docs/pages/building-your-application/data-fetching/client-side)
[Next
CSS Modules
](/docs/pages/building-your-application/styling/css-modules)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"31bdf3f75de0aa73f2f63deebbe382f8\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/styling/css-modules | 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
* [CSS Modules](#css-modules)
* [Example](#example)
* [Global Styles](#global-styles)
* [External Stylesheets](#external-stylesheets)
* [Import styles from node\_modules](#import-styles-from-node_modules)
* [Additional Features](#additional-features)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/04-styling/01-css-modules.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 Modules
CSS Modules
===========
Examples
* [Basic CSS Example](https://github.com/vercel/next.js/tree/canary/examples/basic-css)
Next.js supports different types of stylesheets, including:
* [CSS Modules](#css-modules)
* [Global Styles](#global-styles)
* [External Stylesheets](#external-stylesheets)
[CSS Modules](#css-modules)
---------------------------
Next.js has built-in support for CSS Modules using the `.module.css` extension.
CSS Modules locally scope CSS by automatically creating a unique class name. This allows you to use the same class name in different files without worrying about collisions. This behavior makes CSS Modules the ideal way to include component-level CSS.
[Example](#example)
-------------------
For example, consider a reusable `Button` component in the `components/` folder:
First, create `components/Button.module.css` with the following content:
Button.module.css
/*
You do not need to worry about .error {} colliding with any other `.css` or
`.module.css` files!
*/
.error {
color: white;
background-color: red;
}
Then, create `components/Button.js`, importing and using the above CSS file:
components/Button.js
import styles from './Button.module.css'
export function Button() {
return (
<button
type="button"
// Note how the "error" class is accessed as a property on the imported
// `styles` object.
className={styles.error}
>
Destroy
</button>
)
}
CSS Modules are **only enabled for files with the `.module.css` and `.module.sass` extensions**.
In production, all CSS Module files will be automatically concatenated into **many minified and code-split** `.css` files. These `.css` files represent hot execution paths in your application, ensuring the minimal amount of CSS is loaded for your application to paint.
[Global Styles](#global-styles)
-------------------------------
To add a stylesheet to your application, import the CSS file within `pages/_app.js`.
For example, consider the following stylesheet named `styles.css`:
styles.css
body {
font-family: 'SF Pro Text', 'SF Pro Icons', 'Helvetica Neue', 'Helvetica',
'Arial', sans-serif;
padding: 20px 20px 60px;
max-width: 680px;
margin: 0 auto;
}
Create a [`pages/_app.js` file](/docs/pages/building-your-application/routing/custom-app) if not already present. Then, [`import`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import) the `styles.css` file.
pages/\_app.js
import '../styles.css'
// This default export is required in a new `pages/_app.js` file.
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
These styles (`styles.css`) will apply to all pages and components in your application. Due to the global nature of stylesheets, and to avoid conflicts, you may **only import them inside [`pages/_app.js`](/docs/pages/building-your-application/routing/custom-app)**.
In development, expressing stylesheets this way allows your styles to be hot reloaded as you edit them—meaning you can keep application state.
In production, all CSS files will be automatically concatenated into a single minified `.css` file. The order that the CSS is concatenated will match the order the CSS is imported into the `_app.js` file. Pay special attention to imported JS modules that include their own CSS; the JS module's CSS will be concatenated following the same ordering rules as imported CSS files. For example:
import '../styles.css'
// The CSS in ErrorBoundary depends on the global CSS in styles.css,
// so we import it after styles.css.
import ErrorBoundary from '../components/ErrorBoundary'
export default function MyApp({ Component, pageProps }) {
return (
<ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
)
}
[External Stylesheets](#external-stylesheets)
---------------------------------------------
Next.js allows you to import CSS files from a JavaScript file. This is possible because Next.js extends the concept of [`import`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/import) beyond JavaScript.
### [Import styles from `node_modules`](#import-styles-from-node_modules)
Since Next.js **9.5.4**, importing a CSS file from `node_modules` is permitted anywhere in your application.
For global stylesheets, like `bootstrap` or `nprogress`, you should import the file inside `pages/_app.js`. For example:
pages/\_app.js
import 'bootstrap/dist/css/bootstrap.css'
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
For importing CSS required by a third-party component, you can do so in your component. For example:
components/example-dialog.js
import { useState } from 'react'
import { Dialog } from '@reach/dialog'
import VisuallyHidden from '@reach/visually-hidden'
import '@reach/dialog/styles.css'
function ExampleDialog(props) {
const [showDialog, setShowDialog] = useState(false)
const open = () => setShowDialog(true)
const close = () => setShowDialog(false)
return (
<div>
<button onClick={open}>Open Dialog</button>
<Dialog isOpen={showDialog} onDismiss={close}>
<button className="close-button" onClick={close}>
<VisuallyHidden>Close</VisuallyHidden>
<span aria-hidden>×</span>
</button>
<p>Hello there. I am a dialog</p>
</Dialog>
</div>
)
}
[Additional Features](#additional-features)
-------------------------------------------
Next.js includes additional features to improve the authoring experience of adding styles:
* When running locally with `next dev`, local stylesheets (either global or CSS modules) will take advantage of [Fast Refresh](/docs/architecture/fast-refresh) to instantly reflect changes as edits are saved.
* When building for production with `next build`, CSS files will be bundled into fewer minified `.css` files to reduce the number of network requests needed to retrieve styles.
* If you disable JavaScript, styles will still be loaded in the production build (`next start`). However, JavaScript is still required for `next dev` to enable [Fast Refresh](/docs/architecture/fast-refresh).
[Previous
Styling
](/docs/pages/building-your-application/styling)
[Next
Tailwind CSS
](/docs/pages/building-your-application/styling/tailwind-css)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"3e813f1a5e17fa91ba4c070170f3878c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables | 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
* [Loading Environment Variables](#loading-environment-variables)
* [Referencing Other Variables](#referencing-other-variables)
* [Bundling Environment Variables for the Browser](#bundling-environment-variables-for-the-browser)
* [Runtime Environment Variables](#runtime-environment-variables)
* [Default Environment Variables](#default-environment-variables)
* [Environment Variables on Vercel](#environment-variables-on-vercel)
* [Test Environment Variables](#test-environment-variables)
* [Environment Variable Load Order](#environment-variable-load-order)
* [Good to know](#good-to-know)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/03-environment-variables.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)[Configuring](/docs/pages/building-your-application/configuring)Environment Variables
Environment Variables
=====================
Examples
* [Environment Variables](https://github.com/vercel/next.js/tree/canary/examples/environment-variables)
Next.js comes with built-in support for environment variables, which allows you to do the following:
* [Use `.env.local` to load environment variables](#loading-environment-variables)
* [Bundle environment variables for the browser by prefixing with `NEXT_PUBLIC_`](#bundling-environment-variables-for-the-browser)
[Loading Environment Variables](#loading-environment-variables)
---------------------------------------------------------------
Next.js has built-in support for loading environment variables from `.env.local` into `process.env`.
.env.local
DB_HOST=localhost
DB_USER=myuser
DB_PASS=mypassword
This loads `process.env.DB_HOST`, `process.env.DB_USER`, and `process.env.DB_PASS` into the Node.js environment automatically allowing you to use them in [Next.js data fetching methods](/docs/pages/building-your-application/data-fetching) and [API routes](/docs/pages/building-your-application/routing/api-routes).
For example, using [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props):
pages/index.js
export async function getStaticProps() {
const db = await myDB.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS,
})
// ...
}
### [Referencing Other Variables](#referencing-other-variables)
Next.js will automatically expand variables that use `$` to reference other variables e.g. `$VARIABLE` inside of your `.env*` files. This allows you to reference other secrets. For example:
.env
TWITTER_USER=nextjs
TWITTER_URL=https://twitter.com/$TWITTER_USER
In the above example, `process.env.TWITTER_URL` would be set to `https://twitter.com/nextjs`.
> **Good to know**: If you need to use variable with a `$` in the actual value, it needs to be escaped e.g. `\$`.
[Bundling Environment Variables for the Browser](#bundling-environment-variables-for-the-browser)
-------------------------------------------------------------------------------------------------
Non-`NEXT_PUBLIC_` environment variables are only available in the Node.js environment, meaning they aren't accessible to the browser (the client runs in a different _environment_).
In order to make the value of an environment variable accessible in the browser, Next.js can "inline" a value, at build time, into the js bundle that is delivered to the client, replacing all references to `process.env.[variable]` with a hard-coded value. To tell it to do this, you just have to prefix the variable with `NEXT_PUBLIC_`. For example:
Terminal
NEXT_PUBLIC_ANALYTICS_ID=abcdefghijk
This will tell Next.js to replace all references to `process.env.NEXT_PUBLIC_ANALYTICS_ID` in the Node.js environment with the value from the environment in which you run `next build`, allowing you to use it anywhere in your code. It will be inlined into any JavaScript sent to the browser.
> **Note**: After being built, your app will no longer respond to changes to these environment variables. For instance, if you use a Heroku pipeline to promote slugs built in one environment to another environment, or if you build and deploy a single Docker image to multiple environments, all `NEXT_PUBLIC_` variables will be frozen with the value evaluated at build time, so these values need to be set appropriately when the project is built. If you need access to runtime environment values, you'll have to setup your own API to provide them to the client (either on demand or during initialization).
pages/index.js
import setupAnalyticsService from '../lib/my-analytics-service'
// 'NEXT_PUBLIC_ANALYTICS_ID' can be used here as it's prefixed by 'NEXT_PUBLIC_'.
// It will be transformed at build time to `setupAnalyticsService('abcdefghijk')`.
setupAnalyticsService(process.env.NEXT_PUBLIC_ANALYTICS_ID)
function HomePage() {
return <h1>Hello World</h1>
}
export default HomePage
Note that dynamic lookups will _not_ be inlined, such as:
// This will NOT be inlined, because it uses a variable
const varName = 'NEXT_PUBLIC_ANALYTICS_ID'
setupAnalyticsService(process.env[varName])
// This will NOT be inlined, because it uses a variable
const env = process.env
setupAnalyticsService(env.NEXT_PUBLIC_ANALYTICS_ID)
### [Runtime Environment Variables](#runtime-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, meaning
// 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.
[Default Environment Variables](#default-environment-variables)
---------------------------------------------------------------
In general only one `.env.local` file is needed. However, sometimes you might want to add some defaults for the `development` (`next dev`) or `production` (`next start`) environment.
Next.js allows you to set defaults in `.env` (all environments), `.env.development` (development environment), and `.env.production` (production environment).
`.env.local` always overrides the defaults set.
> **Good to know**: `.env`, `.env.development`, and `.env.production` files should be included in your repository as they define defaults. **`.env*.local` should be added to `.gitignore`**, as those files are intended to be ignored. `.env.local` is where secrets can be stored.
[Environment Variables on Vercel](#environment-variables-on-vercel)
-------------------------------------------------------------------
When deploying your Next.js application to [Vercel](https://vercel.com), Environment Variables can be configured [in the Project Settings](https://vercel.com/docs/projects/environment-variables?utm_medium=docs&utm_source=next-site&utm_campaign=next-website).
All types of Environment Variables should be configured there. Even Environment Variables used in Development – which can be [downloaded onto your local device](https://vercel.com/docs/concepts/projects/environment-variables#development-environment-variables?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) afterwards.
If you've configured [Development Environment Variables](https://vercel.com/docs/concepts/projects/environment-variables#development-environment-variables?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) you can pull them into a `.env.local` for usage on your local machine using the following command:
Terminal
vercel env pull .env.local
> **Good to know**: When deploying your Next.js application to [Vercel](https://vercel.com), your environment variables in `.env*` files will not be made available to Edge Runtime, unless their name are prefixed with `NEXT_PUBLIC_`. We strongly recommend managing your environment variables in [Project Settings](https://vercel.com/docs/projects/environment-variables?utm_medium=docs&utm_source=next-site&utm_campaign=next-website) instead, from where all environment variables are available.
[Test Environment Variables](#test-environment-variables)
---------------------------------------------------------
Apart from `development` and `production` environments, there is a 3rd option available: `test`. In the same way you can set defaults for development or production environments, you can do the same with a `.env.test` file for the `testing` environment (though this one is not as common as the previous two). Next.js will not load environment variables from `.env.development` or `.env.production` in the `testing` environment.
This one is useful when running tests with tools like `jest` or `cypress` where you need to set specific environment vars only for testing purposes. Test default values will be loaded if `NODE_ENV` is set to `test`, though you usually don't need to do this manually as testing tools will address it for you.
There is a small difference between `test` environment, and both `development` and `production` that you need to bear in mind: `.env.local` won't be loaded, as you expect tests to produce the same results for everyone. This way every test execution will use the same env defaults across different executions by ignoring your `.env.local` (which is intended to override the default set).
> **Good to know**: similar to Default Environment Variables, `.env.test` file should be included in your repository, but `.env.test.local` shouldn't, as `.env*.local` are intended to be ignored through `.gitignore`.
While running unit tests you can make sure to load your environment variables the same way Next.js does by leveraging the `loadEnvConfig` function from the `@next/env` package.
// The below can be used in a Jest global setup file or similar for your testing set-up
import { loadEnvConfig } from '@next/env'
export default async () => {
const projectDir = process.cwd()
loadEnvConfig(projectDir)
}
[Environment Variable Load Order](#environment-variable-load-order)
-------------------------------------------------------------------
Environment variables are looked up in the following places, in order, stopping once the variable is found.
1. `process.env`
2. `.env.$(NODE_ENV).local`
3. `.env.local` (Not checked when `NODE_ENV` is `test`.)
4. `.env.$(NODE_ENV)`
5. `.env`
For example, if `NODE_ENV` is `development` and you define a variable in both `.env.development.local` and `.env`, the value in `.env.development.local` will be used.
> **Good to know**: The allowed values for `NODE_ENV` are `production`, `development` and `test`.
[Good to know](#good-to-know)
-----------------------------
* If you are using a [`/src` directory](/docs/app/building-your-application/configuring/src-directory), `.env.*` files should remain in the root of your project.
* If the environment variable `NODE_ENV` is unassigned, Next.js automatically assigns `development` when running the `next dev` command, or `production` for all other commands.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v9.4.0`
Support `.env` and `NEXT_PUBLIC_` introduced.
[Previous
ESLint
](/docs/pages/building-your-application/configuring/eslint)
[Next
Absolute Imports and Module Path Aliases
](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"6e4997a20259edab578380494167b3aa\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/typescript | 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
* [New Projects](#new-projects)
* [Existing Projects](#existing-projects)
* [Minimum TypeScript Version](#minimum-typescript-version)
* [Static Generation and Server-side Rendering](#static-generation-and-server-side-rendering)
* [API Routes](#api-routes)
* [Custom App](#custom-app)
* [Path aliases and baseUrl](#path-aliases-and-baseurl)
* [Type checking next.config.js](#type-checking-nextconfigjs)
* [Incremental type checking](#incremental-type-checking)
* [Ignoring TypeScript Errors](#ignoring-typescript-errors)
* [Custom Type Declarations](#custom-type-declarations)
* [Version Changes](#version-changes)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/01-typescript.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)[Configuring](/docs/pages/building-your-application/configuring)TypeScript
TypeScript
==========
Next.js provides a TypeScript-first development experience for building your React application.
It comes with built-in TypeScript support for automatically installing the necessary packages and configuring the proper settings.
[New Projects](#new-projects)
-----------------------------
`create-next-app` now ships with TypeScript by default.
Terminal
npx create-next-app@latest
[Existing Projects](#existing-projects)
---------------------------------------
Add TypeScript to your project by renaming a file to `.ts` / `.tsx`. Run `next dev` and `next build` to automatically install the necessary dependencies and add a `tsconfig.json` file with the recommended config options.
If you already had a `jsconfig.json` file, copy the `paths` compiler option from the old `jsconfig.json` into the new `tsconfig.json` file, and delete the old `jsconfig.json` file.
[Minimum TypeScript Version](#minimum-typescript-version)
---------------------------------------------------------
It is highly recommended to be on at least `v4.5.2` of TypeScript to get syntax features such as [type modifiers on import names](https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#type-on-import-names) and [performance improvements](https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#real-path-sync-native).
[Static Generation and Server-side Rendering](#static-generation-and-server-side-rendering)
-------------------------------------------------------------------------------------------
For [`getStaticProps`](/docs/pages/api-reference/functions/get-static-props), [`getStaticPaths`](/docs/pages/api-reference/functions/get-static-paths), and [`getServerSideProps`](/docs/pages/api-reference/functions/get-server-side-props), you can use the `GetStaticProps`, `GetStaticPaths`, and `GetServerSideProps` types respectively:
pages/blog/\[slug\].tsx
import { GetStaticProps, GetStaticPaths, GetServerSideProps } from 'next'
export const getStaticProps = (async (context) => {
// ...
}) satisfies GetStaticProps
export const getStaticPaths = (async () => {
// ...
}) satisfies GetStaticPaths
export const getServerSideProps = (async (context) => {
// ...
}) satisfies GetServerSideProps
> **Good to know:** `satisfies` was added to TypeScript in [4.9](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html). We recommend upgrading to the latest version of TypeScript.
[API Routes](#api-routes)
-------------------------
The following is an example of how to use the built-in types for API routes:
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
res.status(200).json({ name: 'John Doe' })
}
You can also type the response data:
import type { NextApiRequest, NextApiResponse } from 'next'
type Data = {
name: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<Data>
) {
res.status(200).json({ name: 'John Doe' })
}
[Custom `App`](#custom-app)
---------------------------
If you have a [custom `App`](/docs/pages/building-your-application/routing/custom-app), you can use the built-in type `AppProps` and change file name to `./pages/_app.tsx` like so:
import type { AppProps } from 'next/app'
export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />
}
[Path aliases and baseUrl](#path-aliases-and-baseurl)
-----------------------------------------------------
Next.js automatically supports the `tsconfig.json` `"paths"` and `"baseUrl"` options.
You can learn more about this feature on the [Module Path aliases documentation](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases).
[Type checking next.config.js](#type-checking-nextconfigjs)
-----------------------------------------------------------
The `next.config.js` file must be a JavaScript file as it does not get parsed by Babel or TypeScript, however you can add some type checking in your IDE using JSDoc as below:
// @ts-check
/**
* @type {import('next').NextConfig}
**/
const nextConfig = {
/* config options here */
}
module.exports = nextConfig
[Incremental type checking](#incremental-type-checking)
-------------------------------------------------------
Since `v10.2.1` Next.js supports [incremental type checking](https://www.typescriptlang.org/tsconfig#incremental) when enabled in your `tsconfig.json`, this can help speed up type checking in larger applications.
[Ignoring TypeScript Errors](#ignoring-typescript-errors)
---------------------------------------------------------
Next.js fails your **production build** (`next build`) when TypeScript errors are present in your project.
If you'd like Next.js to dangerously produce production code even when your application has errors, you can disable the built-in type checking step.
If disabled, be sure you are running type checks as part of your build or deploy process, otherwise this can be very dangerous.
Open `next.config.js` and enable the `ignoreBuildErrors` option in the `typescript` config:
next.config.js
module.exports = {
typescript: {
// !! WARN !!
// Dangerously allow production builds to successfully complete even if
// your project has type errors.
// !! WARN !!
ignoreBuildErrors: true,
},
}
[Custom Type Declarations](#custom-type-declarations)
-----------------------------------------------------
When you need to declare custom types, you might be tempted to modify `next-env.d.ts`. However, this file is automatically generated, so any changes you make will be overwritten. Instead, you should create a new file, let's call it `new-types.d.ts`, and reference it in your `tsconfig.json`:
tsconfig.json
{
"compilerOptions": {
"skipLibCheck": true
//...truncated...
},
"include": [
"new-types.d.ts",
"next-env.d.ts",
".next/types/**/*.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": ["node_modules"]
}
[Version Changes](#version-changes)
-----------------------------------
Version
Changes
`v13.2.0`
Statically typed links are available in beta.
`v12.0.0`
[SWC](/docs/architecture/nextjs-compiler) is now used by default to compile TypeScript and TSX for faster builds.
`v10.2.1`
[Incremental type checking](https://www.typescriptlang.org/tsconfig#incremental) support added when enabled in your `tsconfig.json`.
[Previous
Configuring
](/docs/pages/building-your-application/configuring)
[Next
ESLint
](/docs/pages/building-your-application/configuring/eslint)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f1602bf285f0154665eb4313e13bfa79\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/optimizing/open-telemetry | 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)
* [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/03-pages/01-building-your-application/05-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Optimizing](/docs/pages/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/pages/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/pages/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/pages/building-your-application/optimizing/instrumentation)
[Next
Third Party Libraries
](/docs/pages/building-your-application/optimizing/third-party-libraries)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"abb81b546be3cb407874e931a0370c8b\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases | 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
* [Absolute Imports](#absolute-imports)
* [Module Aliases](#module-aliases)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/04-absolute-imports-and-module-aliases.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)[Configuring](/docs/pages/building-your-application/configuring)Absolute Imports and Module Path Aliases
Absolute Imports and Module Path Aliases
========================================
Examples
* [Absolute Imports and Aliases](https://github.com/vercel/next.js/tree/canary/examples/with-absolute-imports)
Next.js has in-built support for the `"paths"` and `"baseUrl"` options of `tsconfig.json` and `jsconfig.json` files.
These options allow you to alias project directories to absolute paths, making it easier to import modules. For example:
// before
import { Button } from '../../../components/button'
// after
import { Button } from '@/components/button'
> **Good to know**: `create-next-app` will prompt to configure these options for you.
[Absolute Imports](#absolute-imports)
-------------------------------------
The `baseUrl` configuration option allows you to import directly from the root of the project.
An example of this configuration:
tsconfig.json or jsconfig.json
{
"compilerOptions": {
"baseUrl": "."
}
}
components/button.tsx
TypeScript
JavaScriptTypeScript
export default function Button() {
return <button>Click me</button>
}
app/page.tsx
TypeScript
JavaScriptTypeScript
import Button from 'components/button'
export default function HomePage() {
return (
<>
<h1>Hello World</h1>
<Button />
</>
)
}
[Module Aliases](#module-aliases)
---------------------------------
In addition to configuring the `baseUrl` path, you can use the `"paths"` option to "alias" module paths.
For example, the following configuration maps `@/components/*` to `components/*`:
tsconfig.json or jsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/components/*": ["components/*"]
}
}
}
components/button.tsx
TypeScript
JavaScriptTypeScript
export default function Button() {
return <button>Click me</button>
}
app/page.tsx
TypeScript
JavaScriptTypeScript
import Button from '@/components/button'
export default function HomePage() {
return (
<>
<h1>Hello World</h1>
<Button />
</>
)
}
Each of the `"paths"` are relative to the `baseUrl` location. For example:
// tsconfig.json or jsconfig.json
{
"compilerOptions": {
"baseUrl": "src/",
"paths": {
"@/styles/*": ["styles/*"],
"@/components/*": ["components/*"]
}
}
}
// pages/index.js
import Button from '@/components/button'
import '@/styles/styles.css'
import Helper from 'utils/helper'
export default function HomePage() {
return (
<Helper>
<h1>Hello World</h1>
<Button />
</Helper>
)
}
[Previous
Environment Variables
](/docs/pages/building-your-application/configuring/environment-variables)
[Next
src Directory
](/docs/pages/building-your-application/configuring/src-directory)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2228c149e6d7f12a5e730f29c319e6c1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring | 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/06-configuring/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)Configuring
Configuring
===========
Next.js allows you to customize your project to meet specific requirements. This includes integrations with TypeScript, ESlint, and more, as well as internal configuration options such as Absolute Imports and Environment Variables.
[
### TypeScript
Next.js provides a TypeScript-first development experience for building your React application.
](/docs/pages/building-your-application/configuring/typescript)[
### ESLint
Next.js reports ESLint errors and warnings during builds by default. Learn how to opt-out of this behavior here.
](/docs/pages/building-your-application/configuring/eslint)[
### Environment Variables
Learn to add and access environment variables in your Next.js application.
](/docs/pages/building-your-application/configuring/environment-variables)[
### Absolute Imports and Module Path Aliases
Configure module path aliases that allow you to remap certain import paths.
](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)[
### src Directory
Save pages under the \`src\` directory as an alternative to the root \`pages\` directory.
](/docs/pages/building-your-application/configuring/src-directory)[
### MDX
Learn how to configure MDX to write JSX in your markdown files.
](/docs/pages/building-your-application/configuring/mdx)[
### AMP
With minimal config, and without leaving React, you can start adding AMP and improve the performance and speed of your pages.
](/docs/pages/building-your-application/configuring/amp)[
### Babel
Extend the babel preset added by Next.js with your own configs.
](/docs/pages/building-your-application/configuring/babel)[
### PostCSS
Extend the PostCSS config and plugins added by Next.js with your own.
](/docs/pages/building-your-application/configuring/post-css)[
### Custom Server
Start a Next.js app programmatically using a custom server.
](/docs/pages/building-your-application/configuring/custom-server)[
### Draft Mode
Next.js has draft mode to toggle between static and dynamic pages. You can learn how it works with Pages Router.
](/docs/pages/building-your-application/configuring/draft-mode)[
### Error Handling
Handle errors in your Next.js app.
](/docs/pages/building-your-application/configuring/error-handling)[
### Debugging
Learn how to debug your Next.js application with VS Code or Chrome DevTools.
](/docs/pages/building-your-application/configuring/debugging)[
### Preview Mode
Next.js has the preview mode for statically generated pages. You can learn how it works here.
](/docs/pages/building-your-application/configuring/preview-mode)[
### Content Security Policy
Learn how to set a Content Security Policy (CSP) for your Next.js application.
](/docs/pages/building-your-application/configuring/content-security-policy)
[Previous
Third Party Libraries
](/docs/pages/building-your-application/optimizing/third-party-libraries)
[Next
TypeScript
](/docs/pages/building-your-application/configuring/typescript)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a5b3f414773b7d0e75b22d200171b93f\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/src-directory | 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/06-configuring/05-src-directory.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)[Configuring](/docs/pages/building-your-application/configuring)src Directory
src Directory
=============
As an alternative to having the special Next.js `app` or `pages` directories in the root of your project, Next.js also supports the common pattern of placing application code under the `src` directory.
This separates application code from project configuration files which mostly live in the root of a project, which is preferred by some individuals and teams.
To use the `src` directory, move the `app` Router folder or `pages` Router folder to `src/app` or `src/pages` respectively.
![An example folder structure with the `src` directory](/_next/image?url=%2Fdocs%2Flight%2Fproject-organization-src-directory.png&w=3840&q=75)![An example folder structure with the `src` directory](/_next/image?url=%2Fdocs%2Fdark%2Fproject-organization-src-directory.png&w=3840&q=75)
> **Good to know**
>
> * The `/public` directory should remain in the root of your project.
> * Config files like `package.json`, `next.config.js` and `tsconfig.json` should remain in the root of your project.
> * `.env.*` files should remain in the root of your project.
> * `src/app` or `src/pages` will be ignored if `app` or `pages` are present in the root directory.
> * If you're using `src`, you'll probably also move other application folders such as `/components` or `/lib`.
> * If you're using Middleware, ensure it is placed inside the `src` directory.
> * If you're using Tailwind CSS, you'll need to add the `/src` prefix to the `tailwind.config.js` file in the [content section](https://tailwindcss.com/docs/content-configuration).
> * If you are using TypeScript paths for imports such as `@/*`, you should update the `paths` object in `tsconfig.json` to include `src/`.
[Previous
Absolute Imports and Module Path Aliases
](/docs/pages/building-your-application/configuring/absolute-imports-and-module-aliases)
[Next
MDX
](/docs/pages/building-your-application/configuring/mdx)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"117172a079bbc611ba6a75ec82555654\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/babel | 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
* [Adding Presets and Plugins](#adding-presets-and-plugins)
* [Customizing Presets and Plugins](#customizing-presets-and-plugins)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/08-babel.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)[Configuring](/docs/pages/building-your-application/configuring)Babel
Babel
=====
Examples
* [Customizing babel configuration](https://github.com/vercel/next.js/tree/canary/examples/with-custom-babel-config)
Next.js includes the `next/babel` preset to your app, which includes everything needed to compile React applications and server-side code. But if you want to extend the default Babel configs, it's also possible.
[Adding Presets and Plugins](#adding-presets-and-plugins)
---------------------------------------------------------
To start, you only need to define a `.babelrc` file (or `babel.config.js`) in the root directory of your project. If such a file is found, it will be considered as the _source of truth_, and therefore it needs to define what Next.js needs as well, which is the `next/babel` preset.
Here's an example `.babelrc` file:
.babelrc
{
"presets": ["next/babel"],
"plugins": []
}
You can [take a look at this file](https://github.com/vercel/next.js/blob/canary/packages/next/src/build/babel/preset.ts) to learn about the presets included by `next/babel`.
To add presets/plugins **without configuring them**, you can do it this way:
.babelrc
{
"presets": ["next/babel"],
"plugins": ["@babel/plugin-proposal-do-expressions"]
}
[Customizing Presets and Plugins](#customizing-presets-and-plugins)
-------------------------------------------------------------------
To add presets/plugins **with custom configuration**, do it on the `next/babel` preset like so:
.babelrc
{
"presets": [
[
"next/babel",
{
"preset-env": {},
"transform-runtime": {},
"styled-jsx": {},
"class-properties": {}
}
]
],
"plugins": []
}
To learn more about the available options for each config, visit babel's [documentation](https://babeljs.io/docs/) site.
> **Good to know**:
>
> * Next.js uses the [**current** Node.js version](https://github.com/nodejs/release#release-schedule) for server-side compilations.
> * The `modules` option on `"preset-env"` should be kept to `false`, otherwise webpack code splitting is turned off.
[Previous
AMP
](/docs/pages/building-your-application/configuring/amp)
[Next
PostCSS
](/docs/pages/building-your-application/configuring/post-css)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f41b37602d6b706d8a08dad4eac59318\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/custom-server | 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 file-system routing](#disabling-file-system-routing)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/10-custom-server.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)[Configuring](/docs/pages/building-your-application/configuring)Custom Server
Custom Server
=============
Examples
* [Custom Server](https://github.com/vercel/next.js/tree/canary/examples/custom-server)
* [SSR Caching](https://github.com/vercel/next.js/tree/canary/examples/ssr-caching)
By default, Next.js includes its own server with `next start`. If you have an existing backend, you can still use it with Next.js (this is not a custom server). A custom Next.js server allows you to start a server 100% programmatically in order to use custom server patterns. Most of the time, you will not need this - but it's available for complete customization.
> **Good to know**:
>
> * Before deciding to use a custom server, please keep in mind that it should only be used when the integrated router of Next.js can't meet your app requirements. A custom server will remove important performance optimizations, like **serverless functions** and **[Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization).**
> * A custom server **cannot** be deployed on [Vercel](https://vercel.com/solutions/nextjs).
> * Standalone output mode, does not trace custom server files and this mode outputs a separate minimal `server.js` file instead.
Take a look at the following example of a custom server:
server.js
const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')
const dev = process.env.NODE_ENV !== 'production'
const hostname = 'localhost'
const port = 3000
// when using middleware `hostname` and `port` must be provided below
const app = next({ dev, hostname, port })
const handle = app.getRequestHandler()
app.prepare().then(() => {
createServer(async (req, res) => {
try {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const { pathname, query } = parsedUrl
if (pathname === '/a') {
await app.render(req, res, '/a', query)
} else if (pathname === '/b') {
await app.render(req, res, '/b', query)
} else {
await handle(req, res, parsedUrl)
}
} catch (err) {
console.error('Error occurred handling', req.url, err)
res.statusCode = 500
res.end('internal server error')
}
})
.once('error', (err) => {
console.error(err)
process.exit(1)
})
.listen(port, () => {
console.log(`> Ready on http://${hostname}:${port}`)
})
})
> `server.js` doesn't go through babel or webpack. Make sure the syntax and sources this file requires are compatible with the current node version you are running.
To run the custom server you'll need to update the `scripts` in `package.json` like so:
package.json
{
"scripts": {
"dev": "node server.js",
"build": "next build",
"start": "NODE_ENV=production node server.js"
}
}
* * *
The custom server uses the following import to connect the server with the Next.js application:
const next = require('next')
const app = next({})
The above `next` import is a function that receives an object with the following options:
Option
Type
Description
`conf`
`Object`
The same object you would use in [next.config.js](/docs/pages/api-reference/next-config-js). Defaults to `{}`
`customServer`
`Boolean`
(_Optional_) Set to false when the server was created by Next.js
`dev`
`Boolean`
(_Optional_) Whether or not to launch Next.js in dev mode. Defaults to `false`
`dir`
`String`
(_Optional_) Location of the Next.js project. Defaults to `'.'`
`quiet`
`Boolean`
(_Optional_) Hide error messages containing server information. Defaults to `false`
`hostname`
`String`
(_Optional_) The hostname the server is running behind
`port`
`Number`
(_Optional_) The port the server is running behind
`httpServer`
`node:http#Server`
(_Optional_) The HTTP Server that Next.js is running behind
The returned `app` can then be used to let Next.js handle requests as required.
[Disabling file-system routing](#disabling-file-system-routing)
---------------------------------------------------------------
By default, `Next` will serve each file in the `pages` folder under a pathname matching the filename. If your project uses a custom server, this behavior may result in the same content being served from multiple paths, which can present problems with SEO and UX.
To disable this behavior and prevent routing based on files in `pages`, open `next.config.js` and disable the `useFileSystemPublicRoutes` config:
next.config.js
module.exports = {
useFileSystemPublicRoutes: false,
}
> Note that `useFileSystemPublicRoutes` disables filename routes from SSR; client-side routing may still access those paths. When using this option, you should guard against navigation to routes you do not want programmatically.
> You may also wish to configure the client-side router to disallow client-side redirects to filename routes; for that refer to [`router.beforePopState`](/docs/pages/api-reference/functions/use-router#routerbeforepopstate).
[Previous
PostCSS
](/docs/pages/building-your-application/configuring/post-css)
[Next
Draft Mode
](/docs/pages/building-your-application/configuring/draft-mode)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"87a334129ed7f12db356c59769aba71c\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/eslint | 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
* [ESLint Config](#eslint-config)
* [ESLint Plugin](#eslint-plugin)
* [Custom Settings](#custom-settings)
* [rootDir](#rootdir)
* [Linting Custom Directories and Files](#linting-custom-directories-and-files)
* [Caching](#caching)
* [Disabling Rules](#disabling-rules)
* [Core Web Vitals](#core-web-vitals)
* [Usage With Other Tools](#usage-with-other-tools)
* [Prettier](#prettier)
* [lint-staged](#lint-staged)
* [Migrating Existing Config](#migrating-existing-config)
* [Recommended Plugin Ruleset](#recommended-plugin-ruleset)
* [Additional Configurations](#additional-configurations)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/02-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Configuring](/docs/pages/building-your-application/configuring)ESLint
ESLint
======
Next.js provides an integrated [ESLint](https://eslint.org/) experience out of the box. Add `next lint` as a script to `package.json`:
package.json
{
"scripts": {
"lint": "next lint"
}
}
Then run `npm run lint` or `yarn lint`:
Terminal
yarn lint
If you don't already have ESLint configured in your application, you will be guided through the installation and configuration process.
Terminal
yarn lint
> You'll see a prompt like this:
>
> ? How would you like to configure ESLint?
>
> ❯ Strict (recommended)
> Base
> Cancel
One of the following three options can be selected:
* **Strict**: Includes Next.js' base ESLint configuration along with a stricter [Core Web Vitals rule-set](#core-web-vitals). This is the recommended configuration for developers setting up ESLint for the first time.
.eslintrc.json
{
"extends": "next/core-web-vitals"
}
* **Base**: Includes Next.js' base ESLint configuration.
.eslintrc.json
{
"extends": "next"
}
* **Cancel**: Does not include any ESLint configuration. Only select this option if you plan on setting up your own custom ESLint configuration.
If either of the two configuration options are selected, Next.js will automatically install `eslint` and `eslint-config-next` as dependencies in your application and create an `.eslintrc.json` file in the root of your project that includes your selected configuration.
You can now run `next lint` every time you want to run ESLint to catch errors. Once ESLint has been set up, it will also automatically run during every build (`next build`). Errors will fail the build, while warnings will not.
> If you do not want ESLint to run during `next build`, refer to the documentation for [Ignoring ESLint](/docs/pages/api-reference/next-config-js/eslint).
We recommend using an appropriate [integration](https://eslint.org/docs/user-guide/integrations#editors) to view warnings and errors directly in your code editor during development.
[ESLint Config](#eslint-config)
-------------------------------
The default configuration (`eslint-config-next`) includes everything you need to have an optimal out-of-the-box linting experience in Next.js. If you do not have ESLint already configured in your application, we recommend using `next lint` to set up ESLint along with this configuration.
> If you would like to use `eslint-config-next` along with other ESLint configurations, refer to the [Additional Configurations](#additional-configurations) section to learn how to do so without causing any conflicts.
Recommended rule-sets from the following ESLint plugins are all used within `eslint-config-next`:
* [`eslint-plugin-react`](https://www.npmjs.com/package/eslint-plugin-react)
* [`eslint-plugin-react-hooks`](https://www.npmjs.com/package/eslint-plugin-react-hooks)
* [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next)
This will take precedence over the configuration from `next.config.js`.
[ESLint Plugin](#eslint-plugin)
-------------------------------
Next.js provides an ESLint plugin, [`eslint-plugin-next`](https://www.npmjs.com/package/@next/eslint-plugin-next), already bundled within the base configuration that makes it possible to catch common issues and problems in a Next.js application. The full set of rules is as follows:
Enabled in the recommended configuration
Rule
Description
[@next/next/google-font-display](/docs/messages/google-font-display)
Enforce font-display behavior with Google Fonts.
[@next/next/google-font-preconnect](/docs/messages/google-font-preconnect)
Ensure `preconnect` is used with Google Fonts.
[@next/next/inline-script-id](/docs/messages/inline-script-id)
Enforce `id` attribute on `next/script` components with inline content.
[@next/next/next-script-for-ga](/docs/messages/next-script-for-ga)
Prefer `next/script` component when using the inline script for Google Analytics.
[@next/next/no-assign-module-variable](/docs/messages/no-assign-module-variable)
Prevent assignment to the `module` variable.
[@next/next/no-async-client-component](/docs/messages/no-async-client-component)
Prevent client components from being async functions.
[@next/next/no-before-interactive-script-outside-document](/docs/messages/no-before-interactive-script-outside-document)
Prevent usage of `next/script`'s `beforeInteractive` strategy outside of `pages/_document.js`.
[@next/next/no-css-tags](/docs/messages/no-css-tags)
Prevent manual stylesheet tags.
[@next/next/no-document-import-in-page](/docs/messages/no-document-import-in-page)
Prevent importing `next/document` outside of `pages/_document.js`.
[@next/next/no-duplicate-head](/docs/messages/no-duplicate-head)
Prevent duplicate usage of `<Head>` in `pages/_document.js`.
[@next/next/no-head-element](/docs/messages/no-head-element)
Prevent usage of `<head>` element.
[@next/next/no-head-import-in-document](/docs/messages/no-head-import-in-document)
Prevent usage of `next/head` in `pages/_document.js`.
[@next/next/no-html-link-for-pages](/docs/messages/no-html-link-for-pages)
Prevent usage of `<a>` elements to navigate to internal Next.js pages.
[@next/next/no-img-element](/docs/messages/no-img-element)
Prevent usage of `<img>` element due to slower LCP and higher bandwidth.
[@next/next/no-page-custom-font](/docs/messages/no-page-custom-font)
Prevent page-only custom fonts.
[@next/next/no-script-component-in-head](/docs/messages/no-script-component-in-head)
Prevent usage of `next/script` in `next/head` component.
[@next/next/no-styled-jsx-in-document](/docs/messages/no-styled-jsx-in-document)
Prevent usage of `styled-jsx` in `pages/_document.js`.
[@next/next/no-sync-scripts](/docs/messages/no-sync-scripts)
Prevent synchronous scripts.
[@next/next/no-title-in-document-head](/docs/messages/no-title-in-document-head)
Prevent usage of `<title>` with `Head` component from `next/document`.
@next/next/no-typos
Prevent common typos in [Next.js's data fetching functions](/docs/pages/building-your-application/data-fetching)
[@next/next/no-unwanted-polyfillio](/docs/messages/no-unwanted-polyfillio)
Prevent duplicate polyfills from Polyfill.io.
If you already have ESLint configured in your application, we recommend extending from this plugin directly instead of including `eslint-config-next` unless a few conditions are met. Refer to the [Recommended Plugin Ruleset](#recommended-plugin-ruleset) to learn more.
### [Custom Settings](#custom-settings)
#### [`rootDir`](#rootdir)
If you're using `eslint-plugin-next` in a project where Next.js isn't installed in your root directory (such as a monorepo), you can tell `eslint-plugin-next` where to find your Next.js application using the `settings` property in your `.eslintrc`:
.eslintrc.json
{
"extends": "next",
"settings": {
"next": {
"rootDir": "packages/my-app/"
}
}
}
`rootDir` can be a path (relative or absolute), a glob (i.e. `"packages/*/"`), or an array of paths and/or globs.
[Linting Custom Directories and Files](#linting-custom-directories-and-files)
-----------------------------------------------------------------------------
By default, Next.js will run ESLint for all files in the `pages/`, `app/`, `components/`, `lib/`, and `src/` directories. However, you can specify which directories using the `dirs` option in the `eslint` config in `next.config.js` for production builds:
next.config.js
module.exports = {
eslint: {
dirs: ['pages', 'utils'], // Only run ESLint on the 'pages' and 'utils' directories during production builds (next build)
},
}
Similarly, the `--dir` and `--file` flags can be used for `next lint` to lint specific directories and files:
Terminal
next lint --dir pages --dir utils --file bar.js
[Caching](#caching)
-------------------
To improve performance, information of files processed by ESLint are cached by default. This is stored in `.next/cache` or in your defined [build directory](/docs/pages/api-reference/next-config-js/distDir). If you include any ESLint rules that depend on more than the contents of a single source file and need to disable the cache, use the `--no-cache` flag with `next lint`.
Terminal
next lint --no-cache
[Disabling Rules](#disabling-rules)
-----------------------------------
If you would like to modify or disable any rules provided by the supported plugins (`react`, `react-hooks`, `next`), you can directly change them using the `rules` property in your `.eslintrc`:
.eslintrc.json
{
"extends": "next",
"rules": {
"react/no-unescaped-entities": "off",
"@next/next/no-page-custom-font": "off"
}
}
### [Core Web Vitals](#core-web-vitals)
The `next/core-web-vitals` rule set is enabled when `next lint` is run for the first time and the **strict** option is selected.
.eslintrc.json
{
"extends": "next/core-web-vitals"
}
`next/core-web-vitals` updates `eslint-plugin-next` to error on a number of rules that are warnings by default if they affect [Core Web Vitals](https://web.dev/vitals/).
> The `next/core-web-vitals` entry point is automatically included for new applications built with [Create Next App](/docs/app/api-reference/create-next-app).
[Usage With Other Tools](#usage-with-other-tools)
-------------------------------------------------
### [Prettier](#prettier)
ESLint also contains code formatting rules, which can conflict with your existing [Prettier](https://prettier.io/) setup. We recommend including [eslint-config-prettier](https://github.com/prettier/eslint-config-prettier) in your ESLint config to make ESLint and Prettier work together.
First, install the dependency:
Terminal
npm install --save-dev eslint-config-prettier
yarn add --dev eslint-config-prettier
pnpm add --save-dev eslint-config-prettier
bun add --dev eslint-config-prettier
Then, add `prettier` to your existing ESLint config:
.eslintrc.json
{
"extends": ["next", "prettier"]
}
### [lint-staged](#lint-staged)
If you would like to use `next lint` with [lint-staged](https://github.com/okonet/lint-staged) to run the linter on staged git files, you'll have to add the following to the `.lintstagedrc.js` file in the root of your project in order to specify usage of the `--file` flag.
.lintstagedrc.js
const path = require('path')
const buildEslintCommand = (filenames) =>
`next lint --fix --file ${filenames
.map((f) => path.relative(process.cwd(), f))
.join(' --file ')}`
module.exports = {
'*.{js,jsx,ts,tsx}': [buildEslintCommand],
}
[Migrating Existing Config](#migrating-existing-config)
-------------------------------------------------------
### [Recommended Plugin Ruleset](#recommended-plugin-ruleset)
If you already have ESLint configured in your application and any of the following conditions are true:
* You have one or more of the following plugins already installed (either separately or through a different config such as `airbnb` or `react-app`):
* `react`
* `react-hooks`
* `jsx-a11y`
* `import`
* You've defined specific `parserOptions` that are different from how Babel is configured within Next.js (this is not recommended unless you have [customized your Babel configuration](/docs/pages/building-your-application/configuring/babel))
* You have `eslint-plugin-import` installed with Node.js and/or TypeScript [resolvers](https://github.com/benmosher/eslint-plugin-import#resolvers) defined to handle imports
Then we recommend either removing these settings if you prefer how these properties have been configured within [`eslint-config-next`](https://github.com/vercel/next.js/blob/canary/packages/eslint-config-next/index.js) or extending directly from the Next.js ESLint plugin instead:
module.exports = {
extends: [
//...
'plugin:@next/next/recommended',
],
}
The plugin can be installed normally in your project without needing to run `next lint`:
Terminal
npm install --save-dev @next/eslint-plugin-next
yarn add --dev @next/eslint-plugin-next
pnpm add --save-dev @next/eslint-plugin-next
bun add --dev @next/eslint-plugin-next
This eliminates the risk of collisions or errors that can occur due to importing the same plugin or parser across multiple configurations.
### [Additional Configurations](#additional-configurations)
If you already use a separate ESLint configuration and want to include `eslint-config-next`, ensure that it is extended last after other configurations. For example:
.eslintrc.json
{
"extends": ["eslint:recommended", "next"]
}
The `next` configuration already handles setting default values for the `parser`, `plugins` and `settings` properties. There is no need to manually re-declare any of these properties unless you need a different configuration for your use case.
If you include any other shareable configurations, **you will need to make sure that these properties are not overwritten or modified**. Otherwise, we recommend removing any configurations that share behavior with the `next` configuration or extending directly from the Next.js ESLint plugin as mentioned above.
[Previous
TypeScript
](/docs/pages/building-your-application/configuring/typescript)
[Next
Environment Variables
](/docs/pages/building-your-application/configuring/environment-variables)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"ecd76e6e6dc8db5ff7926ad2289a7369\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/mdx | 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
* [@next/mdx](#nextmdx)
* [Getting Started](#getting-started)
* [Remote MDX](#remote-mdx)
* [Layouts](#layouts)
* [Remark and Rehype Plugins](#remark-and-rehype-plugins)
* [Frontmatter](#frontmatter)
* [Custom Elements](#custom-elements)
* [Deep Dive: How do you transform markdown into HTML?](#deep-dive-how-do-you-transform-markdown-into-html)
* [Using the Rust-based MDX compiler (Experimental)](#using-the-rust-based-mdx-compiler-experimental)
* [Helpful Links](#helpful-links)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/06-mdx.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)[Configuring](/docs/pages/building-your-application/configuring)MDX
Markdown and MDX
================
[Markdown](https://daringfireball.net/projects/markdown/syntax) is a lightweight markup language used to format text. It allows you to write using plain text syntax and convert it to structurally valid HTML. It's commonly used for writing content on websites and blogs.
You write...
I **love** using [Next.js](https://nextjs.org/)
Output:
<p>I <strong>love</strong> using <a href="https://nextjs.org/">Next.js</a></p>
[MDX](https://mdxjs.com/) is a superset of markdown that lets you write [JSX](https://react.dev/learn/writing-markup-with-jsx) directly in your markdown files. It is a powerful way to add dynamic interactivity and embed React components within your content.
Next.js can support both local MDX content inside your application, as well as remote MDX files fetched dynamically on the server. The Next.js plugin handles transforming markdown and React components into HTML, including support for usage in Server Components (the default in App Router).
[`@next/mdx`](#nextmdx)
-----------------------
The `@next/mdx` package is used to configure Next.js so it can process markdown and MDX. **It sources data from local files**, allowing you to create pages with a `.mdx` extension, directly in your `/pages` or `/app` directory.
Let's walk through how to configure and use MDX with Next.js.
[Getting Started](#getting-started)
-----------------------------------
Install packages needed to render MDX:
Terminal
npm install @next/mdx @mdx-js/loader @mdx-js/react @types/mdx
Update the `next.config.js` file at your project's root to configure it to use MDX:
next.config.js
const withMDX = require('@next/mdx')()
/** @type {import('next').NextConfig} */
const nextConfig = {
// Configure `pageExtensions` to include MDX files
pageExtensions: ['js', 'jsx', 'mdx', 'ts', 'tsx'],
// Optionally, add any other Next.js config below
}
module.exports = withMDX(nextConfig)
Then, create a new MDX page within the `/pages` directory:
your-project
├── pages
│ └── my-mdx-page.mdx
└── package.json
Now you can use markdown and import React components directly inside your MDX page:
import { MyComponent } from 'my-components'
# Welcome to my MDX page!
This is some **bold** and _italics_ text.
This is a list in markdown:
- One
- Two
- Three
Checkout my React component:
<MyComponent />
Navigating to the `/my-mdx-page` route should display your rendered MDX.
[Remote MDX](#remote-mdx)
-------------------------
If your markdown or MDX files or content lives _somewhere else_, you can fetch it dynamically on the server. This is useful for content stored in a separate local folder, CMS, database, or anywhere else. A popular community package for this use is [`next-mdx-remote`](https://github.com/hashicorp/next-mdx-remote#react-server-components-rsc--nextjs-app-directory-support).
> **Good to know**: Please proceed with caution. MDX compiles to JavaScript and is executed on the server. You should only fetch MDX content from a trusted source, otherwise this can lead to remote code execution (RCE).
The following example uses `next-mdx-remote`:
pages/my-mdx-page-remote.tsx
TypeScript
JavaScriptTypeScript
import { serialize } from 'next-mdx-remote/serialize'
import { MDXRemote, MDXRemoteSerializeResult } from 'next-mdx-remote'
interface Props {
mdxSource: MDXRemoteSerializeResult
}
export default function RemoteMdxPage({ mdxSource }: Props) {
return <MDXRemote {...mdxSource} />
}
export async function getStaticProps() {
// MDX text - can be from a local file, database, CMS, fetch, anywhere...
const res = await fetch('https:...')
const mdxText = await res.text()
const mdxSource = await serialize(mdxText)
return { props: { mdxSource } }
}
Navigating to the `/my-mdx-page-remote` route should display your rendered MDX.
[Layouts](#layouts)
-------------------
To share a layout around MDX pages, create a layout component:
components/mdx-layout.tsx
TypeScript
JavaScriptTypeScript
export default function MdxLayout({ children }: { children: React.ReactNode }) {
// Create any shared layout or styles here
return <div style={{ color: 'blue' }}>{children}</div>
}
Then, import the layout component into the MDX page, wrap the MDX content in the layout, and export it:
import MdxLayout from '../components/mdx-layout'
# Welcome to my MDX page!
export default function MDXPage({ children }) {
return <MdxLayout>{children}</MdxLayout>
}
[Remark and Rehype Plugins](#remark-and-rehype-plugins)
-------------------------------------------------------
You can optionally provide `remark` and `rehype` plugins to transform the MDX content.
For example, you can use `remark-gfm` to support GitHub Flavored Markdown.
Since the `remark` and `rehype` ecosystem is ESM only, you'll need to use `next.config.mjs` as the configuration file.
next.config.mjs
import remarkGfm from 'remark-gfm'
import createMDX from '@next/mdx'
/** @type {import('next').NextConfig} */
const nextConfig = {
// Configure `pageExtensions`` to include MDX files
pageExtensions: ['js', 'jsx', 'mdx', 'ts', 'tsx'],
// Optionally, add any other Next.js config below
}
const withMDX = createMDX({
// Add markdown plugins here, as desired
options: {
remarkPlugins: [remarkGfm],
rehypePlugins: [],
},
})
// Wrap MDX and Next.js config with each other
export default withMDX(nextConfig)
[Frontmatter](#frontmatter)
---------------------------
Frontmatter is a YAML like key/value pairing that can be used to store data about a page. `@next/mdx` does **not** support frontmatter by default, though there are many solutions for adding frontmatter to your MDX content, such as:
* [remark-frontmatter](https://github.com/remarkjs/remark-frontmatter)
* [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter)
* [gray-matter](https://github.com/jonschlinkert/gray-matter).
To access page metadata with `@next/mdx`, you can export a metadata object from within the `.mdx` file:
export const metadata = {
author: 'John Doe',
}
# My MDX page
[Custom Elements](#custom-elements)
-----------------------------------
One of the pleasant aspects of using markdown, is that it maps to native `HTML` elements, making writing fast, and intuitive:
This is a list in markdown:
- One
- Two
- Three
The above generates the following `HTML`:
<p>This is a list in markdown:</p>
<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>
When you want to style your own elements for a custom feel to your website or application, you can pass in shortcodes. These are your own custom components that map to `HTML` elements.
To do this, create a `mdx-components.tsx` file at the root of your application (the parent folder of `pages/` or `src/`) and add custom elements:
mdx-components.tsx
TypeScript
JavaScriptTypeScript
import type { MDXComponents } from 'mdx/types'
import Image, { ImageProps } from 'next/image'
// This file allows you to provide custom React components
// to be used in MDX files. You can import and use any
// React component you want, including inline styles,
// components from other libraries, and more.
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
// Allows customizing built-in components, e.g. to add styling.
h1: ({ children }) => <h1 style={{ fontSize: '100px' }}>{children}</h1>,
img: (props) => (
<Image
sizes="100vw"
style={{ width: '100%', height: 'auto' }}
{...(props as ImageProps)}
/>
),
...components,
}
}
[Deep Dive: How do you transform markdown into HTML?](#deep-dive-how-do-you-transform-markdown-into-html)
---------------------------------------------------------------------------------------------------------
React does not natively understand markdown. The markdown plaintext needs to first be transformed into HTML. This can be accomplished with `remark` and `rehype`.
`remark` is an ecosystem of tools around markdown. `rehype` is the same, but for HTML. For example, the following code snippet transforms markdown into HTML:
import { unified } from 'unified'
import remarkParse from 'remark-parse'
import remarkRehype from 'remark-rehype'
import rehypeSanitize from 'rehype-sanitize'
import rehypeStringify from 'rehype-stringify'
main()
async function main() {
const file = await unified()
.use(remarkParse) // Convert into markdown AST
.use(remarkRehype) // Transform to HTML AST
.use(rehypeSanitize) // Sanitize HTML input
.use(rehypeStringify) // Convert AST into serialized HTML
.process('Hello, Next.js!')
console.log(String(file)) // <p>Hello, Next.js!</p>
}
The `remark` and `rehype` ecosystem contains plugins for [syntax highlighting](https://github.com/atomiks/rehype-pretty-code), [linking headings](https://github.com/rehypejs/rehype-autolink-headings), [generating a table of contents](https://github.com/remarkjs/remark-toc), and more.
When using `@next/mdx` as shown above, you **do not** need to use `remark` or `rehype` directly, as it is handled for you. We're describing it here for a deeper understanding of what the `@next/mdx` package is doing underneath.
[Using the Rust-based MDX compiler (Experimental)](#using-the-rust-based-mdx-compiler-experimental)
---------------------------------------------------------------------------------------------------
Next.js supports a new MDX compiler written in Rust. This compiler is still experimental and is not recommended for production use. To use the new compiler, you need to configure `next.config.js` when you pass it to `withMDX`:
next.config.js
module.exports = withMDX({
experimental: {
mdxRs: true,
},
})
[Helpful Links](#helpful-links)
-------------------------------
* [MDX](https://mdxjs.com)
* [`@next/mdx`](https://www.npmjs.com/package/@next/mdx)
* [remark](https://github.com/remarkjs/remark)
* [rehype](https://github.com/rehypejs/rehype)
* [Markdoc](https://markdoc.dev/docs/nextjs)
[Previous
src Directory
](/docs/pages/building-your-application/configuring/src-directory)
[Next
AMP
](/docs/pages/building-your-application/configuring/amp)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"7f5ac61673cdda33e14cb81d083b3126\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/amp | 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
* [Enabling AMP](#enabling-amp)
* [Caveats](#caveats)
* [Adding AMP Components](#adding-amp-components)
* [AMP Validation](#amp-validation)
* [Custom Validators](#custom-validators)
* [Skip AMP Validation](#skip-amp-validation)
* [AMP in Static HTML Export](#amp-in-static-html-export)
* [TypeScript](#typescript)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/07-amp.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)[Configuring](/docs/pages/building-your-application/configuring)AMP
AMP
===
Examples
* [AMP](https://github.com/vercel/next.js/tree/canary/examples/amp)
With Next.js you can turn any React page into an AMP page, with minimal config, and without leaving React.
You can read more about AMP in the official [amp.dev](https://amp.dev/) site.
[Enabling AMP](#enabling-amp)
-----------------------------
To enable AMP support for a page, and to learn more about the different AMP configs, read the [API documentation for `next/amp`](/docs/pages/building-your-application/configuring/amp).
[Caveats](#caveats)
-------------------
* Only CSS-in-JS is supported. [CSS Modules](/docs/pages/building-your-application/styling) aren't supported by AMP pages at the moment. You can [contribute CSS Modules support to Next.js](https://github.com/vercel/next.js/issues/10549).
[Adding AMP Components](#adding-amp-components)
-----------------------------------------------
The AMP community provides [many components](https://amp.dev/documentation/components/) to make AMP pages more interactive. Next.js will automatically import all components used on a page and there is no need to manually import AMP component scripts:
export const config = { amp: true }
function MyAmpPage() {
const date = new Date()
return (
<div>
<p>Some time: {date.toJSON()}</p>
<amp-timeago
width="0"
height="15"
datetime={date.toJSON()}
layout="responsive"
>
.
</amp-timeago>
</div>
)
}
export default MyAmpPage
The above example uses the [`amp-timeago`](https://amp.dev/documentation/components/amp-timeago/?format=websites) component.
By default, the latest version of a component is always imported. If you want to customize the version, you can use `next/head`, as in the following example:
import Head from 'next/head'
export const config = { amp: true }
function MyAmpPage() {
const date = new Date()
return (
<div>
<Head>
<script
async
key="amp-timeago"
custom-element="amp-timeago"
src="https://cdn.ampproject.org/v0/amp-timeago-0.1.js"
/>
</Head>
<p>Some time: {date.toJSON()}</p>
<amp-timeago
width="0"
height="15"
datetime={date.toJSON()}
layout="responsive"
>
.
</amp-timeago>
</div>
)
}
export default MyAmpPage
[AMP Validation](#amp-validation)
---------------------------------
AMP pages are automatically validated with [amphtml-validator](https://www.npmjs.com/package/amphtml-validator) during development. Errors and warnings will appear in the terminal where you started Next.js.
Pages are also validated during [Static HTML export](/docs/pages/building-your-application/deploying/static-exports) and any warnings / errors will be printed to the terminal. Any AMP errors will cause the export to exit with status code `1` because the export is not valid AMP.
### [Custom Validators](#custom-validators)
You can set up custom AMP validator in `next.config.js` as shown below:
module.exports = {
amp: {
validator: './custom_validator.js',
},
}
### [Skip AMP Validation](#skip-amp-validation)
To turn off AMP validation add the following code to `next.config.js`
experimental: {
amp: {
skipValidation: true
}
}
### [AMP in Static HTML Export](#amp-in-static-html-export)
When using [Static HTML export](/docs/pages/building-your-application/deploying/static-exports) statically prerender pages, Next.js will detect if the page supports AMP and change the exporting behavior based on that.
For example, the hybrid AMP page `pages/about.js` would output:
* `out/about.html` - HTML page with client-side React runtime
* `out/about.amp.html` - AMP page
And if `pages/about.js` is an AMP-only page, then it would output:
* `out/about.html` - Optimized AMP page
Next.js will automatically insert a link to the AMP version of your page in the HTML version, so you don't have to, like so:
<link rel="amphtml" href="/about.amp.html" />
And the AMP version of your page will include a link to the HTML page:
<link rel="canonical" href="/about" />
When [`trailingSlash`](/docs/pages/api-reference/next-config-js/trailingSlash) is enabled the exported pages for `pages/about.js` would be:
* `out/about/index.html` - HTML page
* `out/about.amp/index.html` - AMP page
[TypeScript](#typescript)
-------------------------
AMP currently doesn't have built-in types for TypeScript, but it's in their roadmap ([#13791](https://github.com/ampproject/amphtml/issues/13791)).
As a workaround you can manually create a file called `amp.d.ts` inside your project and add these [custom types](https://stackoverflow.com/a/50601125).
[Previous
MDX
](/docs/pages/building-your-application/configuring/mdx)
[Next
Babel
](/docs/pages/building-your-application/configuring/babel)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"718bb886421d150f92f3fdb41ab420fd\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/post-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
* [Default Behavior](#default-behavior)
* [Customizing Target Browsers](#customizing-target-browsers)
* [CSS Modules](#css-modules)
* [Customizing Plugins](#customizing-plugins)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/09-post-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)[Configuring](/docs/pages/building-your-application/configuring)PostCSS
PostCSS
=======
Examples
* [Tailwind CSS Example](https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss)
[Default Behavior](#default-behavior)
-------------------------------------
Next.js compiles CSS for its [built-in CSS support](/docs/pages/building-your-application/styling) using PostCSS.
Out of the box, with no configuration, Next.js compiles CSS with the following transformations:
* [Autoprefixer](https://github.com/postcss/autoprefixer) automatically adds vendor prefixes to CSS rules (back to IE11).
* [Cross-browser Flexbox bugs](https://github.com/philipwalton/flexbugs) are corrected to behave like [the spec](https://www.w3.org/TR/css-flexbox-1/).
* New CSS features are automatically compiled for Internet Explorer 11 compatibility:
* [`all` Property](https://developer.mozilla.org/docs/Web/CSS/all)
* [Break Properties](https://developer.mozilla.org/docs/Web/CSS/break-after)
* [`font-variant` Property](https://developer.mozilla.org/docs/Web/CSS/font-variant)
* [Gap Properties](https://developer.mozilla.org/docs/Web/CSS/gap)
* [Media Query Ranges](https://developer.mozilla.org/docs/Web/CSS/Media_Queries/Using_media_queries#Syntax_improvements_in_Level_4)
By default, [CSS Grid](https://www.w3.org/TR/css-grid-1/) and [Custom Properties](https://developer.mozilla.org/docs/Web/CSS/var) (CSS variables) are **not compiled** for IE11 support.
To compile [CSS Grid Layout](https://developer.mozilla.org/docs/Web/CSS/grid) for IE11, you can place the following comment at the top of your CSS file:
/* autoprefixer grid: autoplace */
You can also enable IE11 support for [CSS Grid Layout](https://developer.mozilla.org/docs/Web/CSS/grid) in your entire project by configuring autoprefixer with the configuration shown below (collapsed). See ["Customizing Plugins"](#customizing-plugins) below for more information.
Click to view the configuration to enable CSS Grid Layout
postcss.config.json
{
"plugins": [
"postcss-flexbugs-fixes",
[
"postcss-preset-env",
{
"autoprefixer": {
"flexbox": "no-2009",
"grid": "autoplace"
},
"stage": 3,
"features": {
"custom-properties": false
}
}
]
]
}
CSS variables are not compiled because it is [not possible to safely do so](https://github.com/MadLittleMods/postcss-css-variables#caveats). If you must use variables, consider using something like [Sass variables](https://sass-lang.com/documentation/variables) which are compiled away by [Sass](https://sass-lang.com/).
[Customizing Target Browsers](#customizing-target-browsers)
-----------------------------------------------------------
Next.js allows you to configure the target browsers (for [Autoprefixer](https://github.com/postcss/autoprefixer) and compiled css features) through [Browserslist](https://github.com/browserslist/browserslist).
To customize browserslist, create a `browserslist` key in your `package.json` like so:
package.json
{
"browserslist": [">0.3%", "not dead", "not op_mini all"]
}
You can use the [browsersl.ist](https://browsersl.ist/?q=%3E0.3%25%2C+not+ie+11%2C+not+dead%2C+not+op_mini+all) tool to visualize what browsers you are targeting.
[CSS Modules](#css-modules)
---------------------------
No configuration is needed to support CSS Modules. To enable CSS Modules for a file, rename the file to have the extension `.module.css`.
You can learn more about [Next.js' CSS Module support here](/docs/pages/building-your-application/styling).
[Customizing Plugins](#customizing-plugins)
-------------------------------------------
> **Warning**: When you define a custom PostCSS configuration file, Next.js **completely disables** the [default behavior](#default-behavior). Be sure to manually configure all the features you need compiled, including [Autoprefixer](https://github.com/postcss/autoprefixer). You also need to install any plugins included in your custom configuration manually, i.e. `npm install postcss-flexbugs-fixes postcss-preset-env`.
To customize the PostCSS configuration, create a `postcss.config.json` file in the root of your project.
This is the default configuration used by Next.js:
postcss.config.json
{
"plugins": [
"postcss-flexbugs-fixes",
[
"postcss-preset-env",
{
"autoprefixer": {
"flexbox": "no-2009"
},
"stage": 3,
"features": {
"custom-properties": false
}
}
]
]
}
> **Good to know**: Next.js also allows the file to be named `.postcssrc.json`, or, to be read from the `postcss` key in `package.json`.
It is also possible to configure PostCSS with a `postcss.config.js` file, which is useful when you want to conditionally include plugins based on environment:
postcss.config.js
module.exports = {
plugins:
process.env.NODE_ENV === 'production'
? [
'postcss-flexbugs-fixes',
[
'postcss-preset-env',
{
autoprefixer: {
flexbox: 'no-2009',
},
stage: 3,
features: {
'custom-properties': false,
},
},
],
]
: [
// No transformations in development
],
}
> **Good to know**: Next.js also allows the file to be named `.postcssrc.js`.
Do **not use `require()`** to import the PostCSS Plugins. Plugins must be provided as strings.
> **Good to know**: If your `postcss.config.js` needs to support other non-Next.js tools in the same project, you must use the interoperable object-based format instead:
>
> module.exports = {
> plugins: {
> 'postcss-flexbugs-fixes': {},
> 'postcss-preset-env': {
> autoprefixer: {
> flexbox: 'no-2009',
> },
> stage: 3,
> features: {
> 'custom-properties': false,
> },
> },
> },
> }
[Previous
Babel
](/docs/pages/building-your-application/configuring/babel)
[Next
Custom Server
](/docs/pages/building-your-application/configuring/custom-server)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f4176e9479bf75a1bc62647f12509077\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/draft-mode | 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
* [Step 1: Create and access the API route](#step-1-create-and-access-the-api-route)
* [Securely accessing it from your Headless CMS](#securely-accessing-it-from-your-headless-cms)
* [Step 2: Update getStaticProps](#step-2-update-getstaticprops)
* [Fetch draft data](#fetch-draft-data)
* [More Details](#more-details)
* [Clear the Draft Mode cookie](#clear-the-draft-mode-cookie)
* [Works with getServerSideProps](#works-with-getserversideprops)
* [Works with API Routes](#works-with-api-routes)
* [Unique per next build](#unique-per-next-build)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Configuring](/docs/pages/building-your-application/configuring)Draft Mode
Draft Mode
==========
In the [Pages documentation](/docs/pages/building-your-application/routing/pages-and-layouts) and the [Data Fetching documentation](/docs/pages/building-your-application/data-fetching), we talked about how to pre-render a page at build time (**Static Generation**) using `getStaticProps` and `getStaticPaths`.
Static Generation 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 bypass Static Generation 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 API route](#step-1-create-and-access-the-api-route)
----------------------------------------------------------------------------------
> Take a look at the [API Routes documentation](/docs/pages/building-your-application/routing/api-routes) first if you’re not familiar with Next.js API Routes.
First, create the **API route**. It can have any name - e.g. `pages/api/draft.ts`
In this API route, you need to call `setDraftMode` on the response object.
export default function handler(req, res) {
// ...
res.setDraftMode({ enable: true })
// ...
}
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 creating an API route like below and accessing it from your browser manually:
pages/api/draft.ts
// simple example for testing it manually from your browser.
export default function handler(req, res) {
res.setDraftMode({ enable: true })
res.end('Draft mode is enabled')
}
If you open your browser’s developer tools and visit `/api/draft`, you’ll notice a `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 API route _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 draft API route is located at `pages/api/draft.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 draft API route:
* Check that the secret matches and that the `slug` parameter exists (if not, the request should fail).
* Call `res.setDraftMode`.
* Then redirect the browser to the path specified by `slug`. (The following example uses a [307 redirect](https://developer.mozilla.org/docs/Web/HTTP/Status/307)).
export default async (req, res) => {
// Check the secret and next parameters
// This secret should only be known to this API route and the CMS
if (req.query.secret !== 'MY_SECRET_TOKEN' || !req.query.slug) {
return res.status(401).json({ message: 'Invalid token' })
}
// 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(req.query.slug)
// If the slug doesn't exist prevent draft mode from being enabled
if (!post) {
return res.status(401).json({ message: 'Invalid slug' })
}
// Enable Draft Mode by setting the cookie
res.setDraftMode({ enable: true })
// Redirect to the path from the fetched post
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
res.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 `getStaticProps`](#step-2-update-getstaticprops)
----------------------------------------------------------------
The next step is to update `getStaticProps` to support draft mode.
If you request a page which has `getStaticProps` with the cookie set (via `res.setDraftMode`), then `getStaticProps` will be called at **request time** (instead of at build time).
Furthermore, it will be called with a `context` object where `context.draftMode` will be `true`.
export async function getStaticProps(context) {
if (context.draftMode) {
// dynamic data
}
}
We used `res.setDraftMode` in the draft API route, so `context.draftMode` will be `true`.
If you’re also using `getStaticPaths`, then `context.params` will also be available.
### [Fetch draft data](#fetch-draft-data)
You can update `getStaticProps` to fetch different data based on `context.draftMode`.
For example, your headless CMS might have a different API endpoint for draft posts. If so, you can modify the API endpoint URL like below:
export async function getStaticProps(context) {
const url = context.draftMode
? 'https://draft.example.com'
: 'https://production.example.com'
const res = await fetch(url)
// ...
}
That’s it! If you access the draft API route (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 an API route that calls `setDraftMode({ enable: false })`:
pages/api/disable-draft.ts
export default function handler(req, res) {
res.setDraftMode({ enable: false })
}
Then, send a request to `/api/disable-draft` to invoke the API Route. If calling this route using [`next/link`](/docs/pages/api-reference/components/link), you must pass `prefetch={false}` to prevent accidentally deleting the cookie on prefetch.
### [Works with `getServerSideProps`](#works-with-getserversideprops)
Draft Mode works with `getServerSideProps`, and is available as a `draftMode` key in the [`context`](/docs/pages/api-reference/functions/get-server-side-props#context-parameter) object.
> **Good to know**: You shouldn't set the `Cache-Control` header when using Draft Mode because it cannot be bypassed. Instead, we recommend using [ISR](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration).
### [Works with API Routes](#works-with-api-routes)
API Routes will have access to `draftMode` on the request object. For example:
export default function myApiRoute(req, res) {
if (req.draftMode) {
// get draft data
}
}
### [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
Custom Server
](/docs/pages/building-your-application/configuring/custom-server)
[Next
Error Handling
](/docs/pages/building-your-application/configuring/error-handling)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"206cf5aaa2f724ec807d42444f9e7e8b\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/error-handling | 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
* [Handling Errors in Development](#handling-errors-in-development)
* [Handling Server Errors](#handling-server-errors)
* [Handling Client Errors](#handling-client-errors)
* [Reporting Errors](#reporting-errors)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/12-error-handling.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)[Configuring](/docs/pages/building-your-application/configuring)Error Handling
Error Handling
==============
This documentation explains how you can handle development, server-side, and client-side errors.
[Handling Errors in Development](#handling-errors-in-development)
-----------------------------------------------------------------
When there is a runtime error during the development phase of your Next.js application, you will encounter an **overlay**. It is a modal that covers the webpage. It is **only** visible when the development server runs using `next dev` via `pnpm dev`, `npm run dev`, `yarn dev`, or `bun dev` and will not be shown in production. Fixing the error will automatically dismiss the overlay.
Here is an example of an overlay:
![Example of an overlay when in development mode](https://assets.vercel.com/image/upload/v1645118290/docs-assets/static/docs/error-handling/overlay.png)
[Handling Server Errors](#handling-server-errors)
-------------------------------------------------
Next.js provides a static 500 page by default to handle server-side errors that occur in your application. You can also [customize this page](/docs/pages/building-your-application/routing/custom-error#customizing-the-500-page) by creating a `pages/500.js` file.
Having a 500 page in your application does not show specific errors to the app user.
You can also use [404 page](/docs/pages/building-your-application/routing/custom-error#404-page) to handle specific runtime error like `file not found`.
[Handling Client Errors](#handling-client-errors)
-------------------------------------------------
React [Error Boundaries](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) is a graceful way to handle a JavaScript error on the client so that the other parts of the application continue working. In addition to preventing the page from crashing, it allows you to provide a custom fallback component and even log error information.
To use Error Boundaries for your Next.js application, you must create a class component `ErrorBoundary` and wrap the `Component` prop in the `pages/_app.js` file. This component will be responsible to:
* Render a fallback UI after an error is thrown
* Provide a way to reset the Application's state
* Log error information
You can create an `ErrorBoundary` class component by extending `React.Component`. For example:
class ErrorBoundary extends React.Component {
constructor(props) {
super(props)
// Define a state variable to track whether is an error or not
this.state = { hasError: false }
}
static getDerivedStateFromError(error) {
// Update state so the next render will show the fallback UI
return { hasError: true }
}
componentDidCatch(error, errorInfo) {
// You can use your own error logging service here
console.log({ error, errorInfo })
}
render() {
// Check if the error is thrown
if (this.state.hasError) {
// You can render any custom fallback UI
return (
<div>
<h2>Oops, there is an error!</h2>
<button
type="button"
onClick={() => this.setState({ hasError: false })}
>
Try again?
</button>
</div>
)
}
// Return children components in case of no error
return this.props.children
}
}
export default ErrorBoundary
The `ErrorBoundary` component keeps track of an `hasError` state. The value of this state variable is a boolean. When the value of `hasError` is `true`, then the `ErrorBoundary` component will render a fallback UI. Otherwise, it will render the children components.
After creating an `ErrorBoundary` component, import it in the `pages/_app.js` file to wrap the `Component` prop in your Next.js application.
// Import the ErrorBoundary component
import ErrorBoundary from '../components/ErrorBoundary'
function MyApp({ Component, pageProps }) {
return (
// Wrap the Component prop with ErrorBoundary component
<ErrorBoundary>
<Component {...pageProps} />
</ErrorBoundary>
)
}
export default MyApp
You can learn more about [Error Boundaries](https://react.dev/reference/react/Component#catching-rendering-errors-with-an-error-boundary) in React's documentation.
### [Reporting Errors](#reporting-errors)
To monitor client errors, use a service like [Sentry](https://github.com/vercel/next.js/tree/canary/examples/with-sentry), Bugsnag or Datadog.
[Previous
Draft Mode
](/docs/pages/building-your-application/configuring/draft-mode)
[Next
Debugging
](/docs/pages/building-your-application/configuring/debugging)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"1acb1a359db1570b5a6e3eabb4d376b0\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/preview-mode | 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
* [Step 1: Create and access a preview API route](#step-1-create-and-access-a-preview-api-route)
* [Securely accessing it from your Headless CMS](#securely-accessing-it-from-your-headless-cms)
* [Step 2: Update getStaticProps](#step-2-update-getstaticprops)
* [Fetch preview data](#fetch-preview-data)
* [More Details](#more-details)
* [Specify the Preview Mode duration](#specify-the-preview-mode-duration)
* [Clear the Preview Mode cookies](#clear-the-preview-mode-cookies)
* [previewData size limits](#previewdata-size-limits)
* [Works with getServerSideProps](#works-with-getserversideprops)
* [Works with API Routes](#works-with-api-routes)
* [Unique per next build](#unique-per-next-build)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/14-preview-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Configuring](/docs/pages/building-your-application/configuring)Preview Mode
Preview Mode
============
> **Note**: This feature is superseded by [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode).
Examples
* [WordPress Example](https://github.com/vercel/next.js/tree/canary/examples/cms-wordpress) ([Demo](https://next-blog-wordpress.vercel.app))
* [DatoCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-datocms) ([Demo](https://next-blog-datocms.vercel.app/))
* [TakeShape Example](https://github.com/vercel/next.js/tree/canary/examples/cms-takeshape) ([Demo](https://next-blog-takeshape.vercel.app/))
* [Sanity Example](https://github.com/vercel/next.js/tree/canary/examples/cms-sanity) ([Demo](https://next-blog-sanity.vercel.app/))
* [Prismic Example](https://github.com/vercel/next.js/tree/canary/examples/cms-prismic) ([Demo](https://next-blog-prismic.vercel.app/))
* [Contentful Example](https://github.com/vercel/next.js/tree/canary/examples/cms-contentful) ([Demo](https://next-blog-contentful.vercel.app/))
* [Strapi Example](https://github.com/vercel/next.js/tree/canary/examples/cms-strapi) ([Demo](https://next-blog-strapi.vercel.app/))
* [Prepr Example](https://github.com/vercel/next.js/tree/canary/examples/cms-prepr) ([Demo](https://next-blog-prepr.vercel.app/))
* [Agility CMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-agilitycms) ([Demo](https://next-blog-agilitycms.vercel.app/))
* [Cosmic Example](https://github.com/vercel/next.js/tree/canary/examples/cms-cosmic) ([Demo](https://next-blog-cosmic.vercel.app/))
* [ButterCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-buttercms) ([Demo](https://next-blog-buttercms.vercel.app/))
* [Storyblok Example](https://github.com/vercel/next.js/tree/canary/examples/cms-storyblok) ([Demo](https://next-blog-storyblok.vercel.app/))
* [GraphCMS Example](https://github.com/vercel/next.js/tree/canary/examples/cms-graphcms) ([Demo](https://next-blog-graphcms.vercel.app/))
* [Kontent Example](https://github.com/vercel/next.js/tree/canary/examples/cms-kontent-ai) ([Demo](https://next-blog-kontent.vercel.app//))
* [Umbraco Heartcore Example](https://github.com/vercel/next.js/tree/canary/examples/cms-umbraco-heartcore) ([Demo](https://next-blog-umbraco-heartcore.vercel.app/))
* [Plasmic Example](https://github.com/vercel/next.js/tree/canary/examples/cms-plasmic) ([Demo](https://nextjs-plasmic-example.vercel.app/))
* [Enterspeed Example](https://github.com/vercel/next.js/tree/canary/examples/cms-enterspeed) ([Demo](https://next-blog-demo.enterspeed.com/))
* [Makeswift Example](https://github.com/vercel/next.js/tree/canary/examples/cms-makeswift) ([Demo](https://nextjs-makeswift-example.vercel.app/))
In the [Pages documentation](/docs/pages/building-your-application/routing/pages-and-layouts) and the [Data Fetching documentation](/docs/pages/building-your-application/data-fetching), we talked about how to pre-render a page at build time (**Static Generation**) using `getStaticProps` and `getStaticPaths`.
Static Generation 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 **preview** 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 bypass Static Generation only for this specific case.
Next.js has a feature called **Preview Mode** which solves this problem. Here are instructions on how to use it.
[Step 1: Create and access a preview API route](#step-1-create-and-access-a-preview-api-route)
----------------------------------------------------------------------------------------------
> Take a look at the [API Routes documentation](/docs/pages/building-your-application/routing/api-routes) first if you’re not familiar with Next.js API Routes.
First, create a **preview API route**. It can have any name - e.g. `pages/api/preview.js` (or `.ts` if using TypeScript).
In this API route, you need to call `setPreviewData` on the response object. The argument for `setPreviewData` should be an object, and this can be used by `getStaticProps` (more on this later). For now, we’ll use `{}`.
export default function handler(req, res) {
// ...
res.setPreviewData({})
// ...
}
`res.setPreviewData` sets some **cookies** on the browser which turns on the preview mode. Any requests to Next.js containing these cookies will be considered as the **preview mode**, and the behavior for statically generated pages will change (more on this later).
You can test this manually by creating an API route like below and accessing it from your browser manually:
pages/api/preview.js
// simple example for testing it manually from your browser.
export default function handler(req, res) {
res.setPreviewData({})
res.end('Preview mode enabled')
}
If you open your browser’s developer tools and visit `/api/preview`, you’ll notice that the `__prerender_bypass` and `__next_preview_data` cookies will be set on this request.
### [Securely accessing it from your Headless CMS](#securely-accessing-it-from-your-headless-cms)
In practice, you’d want to call this API route _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 preview URLs**. If it doesn’t, you can still use this method to secure your preview URLs, but you’ll need to construct and access the preview 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 preview URLs.
**Second**, if your headless CMS supports setting custom preview URLs, specify the following as the preview URL. This assumes that your preview API route is located at `pages/api/preview.js`.
Terminal
https://<your-site>/api/preview?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 preview. If you want to preview `/posts/foo`, then you should use `&slug=/posts/foo`.
Your headless CMS might allow you to include a variable in the preview URL so that `<path>` can be set dynamically based on the CMS’s data like so: `&slug=/posts/{entry.fields.slug}`
**Finally**, in the preview API route:
* Check that the secret matches and that the `slug` parameter exists (if not, the request should fail).
* Call `res.setPreviewData`.
* Then redirect the browser to the path specified by `slug`. (The following example uses a [307 redirect](https://developer.mozilla.org/docs/Web/HTTP/Status/307)).
export default async (req, res) => {
// Check the secret and next parameters
// This secret should only be known to this API route and the CMS
if (req.query.secret !== 'MY_SECRET_TOKEN' || !req.query.slug) {
return res.status(401).json({ message: 'Invalid token' })
}
// 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(req.query.slug)
// If the slug doesn't exist prevent preview mode from being enabled
if (!post) {
return res.status(401).json({ message: 'Invalid slug' })
}
// Enable Preview Mode by setting the cookies
res.setPreviewData({})
// Redirect to the path from the fetched post
// We don't redirect to req.query.slug as that might lead to open redirect vulnerabilities
res.redirect(post.slug)
}
If it succeeds, then the browser will be redirected to the path you want to preview with the preview mode cookies being set.
[Step 2: Update `getStaticProps`](#step-2-update-getstaticprops)
----------------------------------------------------------------
The next step is to update `getStaticProps` to support the preview mode.
If you request a page which has `getStaticProps` with the preview mode cookies set (via `res.setPreviewData`), then `getStaticProps` will be called at **request time** (instead of at build time).
Furthermore, it will be called with a `context` object where:
* `context.preview` will be `true`.
* `context.previewData` will be the same as the argument used for `setPreviewData`.
export async function getStaticProps(context) {
// If you request this page with the preview mode cookies set:
//
// - context.preview will be true
// - context.previewData will be the same as
// the argument used for `setPreviewData`.
}
We used `res.setPreviewData({})` in the preview API route, so `context.previewData` will be `{}`. You can use this to pass session information from the preview API route to `getStaticProps` if necessary.
If you’re also using `getStaticPaths`, then `context.params` will also be available.
### [Fetch preview data](#fetch-preview-data)
You can update `getStaticProps` to fetch different data based on `context.preview` and/or `context.previewData`.
For example, your headless CMS might have a different API endpoint for draft posts. If so, you can use `context.preview` to modify the API endpoint URL like below:
export async function getStaticProps(context) {
// If context.preview is true, append "/preview" to the API endpoint
// to request draft data instead of published data. This will vary
// based on which headless CMS you're using.
const res = await fetch(`https://.../${context.preview ? 'preview' : ''}`)
// ...
}
That’s it! If you access the preview API route (with `secret` and `slug`) from your headless CMS or manually, you should now be able to see the preview content. And if you update your draft without publishing, you should be able to preview the draft.
Set this as the preview URL on your headless CMS or access manually, and you should be able to see the preview.
Terminal
https://<your-site>/api/preview?secret=<token>&slug=<path>
[More Details](#more-details)
-----------------------------
> **Good to know**: during rendering `next/router` exposes an `isPreview` flag, see the [router object docs](/docs/pages/api-reference/functions/use-router#router-object) for more info.
### [Specify the Preview Mode duration](#specify-the-preview-mode-duration)
`setPreviewData` takes an optional second parameter which should be an options object. It accepts the following keys:
* `maxAge`: Specifies the number (in seconds) for the preview session to last for.
* `path`: Specifies the path the cookie should be applied under. Defaults to `/` enabling preview mode for all paths.
setPreviewData(data, {
maxAge: 60 * 60, // The preview mode cookies expire in 1 hour
path: '/about', // The preview mode cookies apply to paths with /about
})
### [Clear the Preview Mode cookies](#clear-the-preview-mode-cookies)
By default, no expiration date is set for Preview Mode cookies, so the preview session ends when the browser is closed.
To clear the Preview Mode cookies manually, create an API route that calls `clearPreviewData()`:
pages/api/clear-preview-mode-cookies.js
export default function handler(req, res) {
res.clearPreviewData({})
}
Then, send a request to `/api/clear-preview-mode-cookies` to invoke the API Route. If calling this route using [`next/link`](/docs/pages/api-reference/components/link), you must pass `prefetch={false}` to prevent calling `clearPreviewData` during link prefetching.
If a path was specified in the `setPreviewData` call, you must pass the same path to `clearPreviewData`:
pages/api/clear-preview-mode-cookies.js
export default function handler(req, res) {
const { path } = req.query
res.clearPreviewData({ path })
}
### [`previewData` size limits](#previewdata-size-limits)
You can pass an object to `setPreviewData` and have it be available in `getStaticProps`. However, because the data will be stored in a cookie, there’s a size limitation. Currently, preview data is limited to 2KB.
### [Works with `getServerSideProps`](#works-with-getserversideprops)
The preview mode works on `getServerSideProps` as well. It will also be available on the `context` object containing `preview` and `previewData`.
> **Good to know**: You shouldn't set the `Cache-Control` header when using Preview Mode because it cannot be bypassed. Instead, we recommend using [ISR](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration).
### [Works with API Routes](#works-with-api-routes)
API Routes will have access to `preview` and `previewData` under the request object. For example:
export default function myApiRoute(req, res) {
const isPreview = req.preview
const previewData = req.previewData
// ...
}
### [Unique per `next build`](#unique-per-next-build)
Both the bypass cookie value and the private key for encrypting the `previewData` change when `next build` is completed. This ensures that the bypass cookie can’t be guessed.
> **Good to know**: To test Preview Mode locally over HTTP your browser will need to allow third-party cookies and local storage access.
[Previous
Debugging
](/docs/pages/building-your-application/configuring/debugging)
[Next
Content Security Policy
](/docs/pages/building-your-application/configuring/content-security-policy)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"5ede97a797a67ee70e92834e655e73a1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/testing | 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
* [Types of tests](#types-of-tests)
* [Guides](#guides)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/07-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
[Pages Router](/docs/pages)[Building Your Application](/docs/pages/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.
[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 Next.js with Vitest and React Testing Library - two popular unit testing libraries.
](/docs/pages/building-your-application/testing/vitest)[
### Jest
Learn how to set up Next.js with Jest for Unit Testing.
](/docs/pages/building-your-application/testing/jest)[
### Playwright
Learn how to set up Next.js with Playwright for End-to-End (E2E) and Integration testing.
](/docs/pages/building-your-application/testing/playwright)[
### Cypress
Learn how to set up Next.js with Cypress for End-to-End (E2E) and Component Testing.
](/docs/pages/building-your-application/testing/cypress)
[Previous
Content Security Policy
](/docs/pages/building-your-application/configuring/content-security-policy)
[Next
Vitest
](/docs/pages/building-your-application/testing/vitest)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2c9acc7524602285dfbe90d490f673cc\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/debugging | 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
* [Debugging with VS Code](#debugging-with-vs-code)
* [Using the Debugger in Jetbrains WebStorm](#using-the-debugger-in-jetbrains-webstorm)
* [Debugging with Chrome DevTools](#debugging-with-chrome-devtools)
* [Client-side code](#client-side-code)
* [Server-side code](#server-side-code)
* [Debugging on Windows](#debugging-on-windows)
* [More information](#more-information)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-configuring/13-debugging.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)[Configuring](/docs/pages/building-your-application/configuring)Debugging
Debugging
=========
This documentation explains how you can debug your Next.js frontend and backend code with full source maps support using either the [VS Code debugger](https://code.visualstudio.com/docs/editor/debugging) or [Chrome DevTools](https://developers.google.com/web/tools/chrome-devtools).
Any debugger that can attach to Node.js can also be used to debug a Next.js application. You can find more details in the Node.js [Debugging Guide](https://nodejs.org/en/docs/guides/debugging-getting-started/).
[Debugging with VS Code](#debugging-with-vs-code)
-------------------------------------------------
Create a file named `.vscode/launch.json` at the root of your project with the following content:
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: debug server-side",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev"
},
{
"name": "Next.js: debug client-side",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000"
},
{
"name": "Next.js: debug full stack",
"type": "node-terminal",
"request": "launch",
"command": "npm run dev",
"serverReadyAction": {
"pattern": "- Local:.+(https?://.+)",
"uriFormat": "%s",
"action": "debugWithChrome"
}
}
]
}
`npm run dev` can be replaced with `yarn dev` if you're using Yarn or `pnpm dev` if you're using pnpm.
If you're [changing the port number](/docs/pages/api-reference/next-cli#development) your application starts on, replace the `3000` in `http://localhost:3000` with the port you're using instead.
If you're running Next.js from a directory other than root (for example, if you're using Turborepo) then you need to add `cwd` to the server-side and full stack debugging tasks. For example, `"cwd": "${workspaceFolder}/apps/web"`.
Now go to the Debug panel (`Ctrl+Shift+D` on Windows/Linux, `⇧+⌘+D` on macOS), select a launch configuration, then press `F5` or select **Debug: Start Debugging** from the Command Palette to start your debugging session.
[Using the Debugger in Jetbrains WebStorm](#using-the-debugger-in-jetbrains-webstorm)
-------------------------------------------------------------------------------------
Click the drop down menu listing the runtime configuration, and click `Edit Configurations...`. Create a `Javascript Debug` debug configuration with `http://localhost:3000` as the URL. Customize to your liking (e.g. Browser for debugging, store as project file), and click `OK`. Run this debug configuration, and the selected browser should automatically open. At this point, you should have 2 applications in debug mode: the NextJS node application, and the client/ browser application.
[Debugging with Chrome DevTools](#debugging-with-chrome-devtools)
-----------------------------------------------------------------
### [Client-side code](#client-side-code)
Start your development server as usual by running `next dev`, `npm run dev`, or `yarn dev`. Once the server starts, open `http://localhost:3000` (or your alternate URL) in Chrome. Next, open Chrome's Developer Tools (`Ctrl+Shift+J` on Windows/Linux, `⌥+⌘+I` on macOS), then go to the **Sources** tab.
Now, any time your client-side code reaches a [`debugger`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/debugger) statement, code execution will pause and that file will appear in the debug area. You can also press `Ctrl+P` on Windows/Linux or `⌘+P` on macOS to search for a file and set breakpoints manually. Note that when searching here, your source files will have paths starting with `webpack://_N_E/./`.
### [Server-side code](#server-side-code)
To debug server-side Next.js code with Chrome DevTools, you need to pass the [`--inspect`](https://nodejs.org/api/cli.html#cli_inspect_host_port) flag to the underlying Node.js process:
Terminal
NODE_OPTIONS='--inspect' next dev
If you're using `npm run dev` or `yarn dev` then you should update the `dev` script on your `package.json`:
package.json
{
"scripts": {
"dev": "NODE_OPTIONS='--inspect' next dev"
}
}
Launching the Next.js dev server with the `--inspect` flag will look something like this:
Terminal
Debugger listening on ws://127.0.0.1:9229/0cf90313-350d-4466-a748-cd60f4e47c95
For help, see: https://nodejs.org/en/docs/inspector
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
> Be aware that running `NODE_OPTIONS='--inspect' npm run dev` or `NODE_OPTIONS='--inspect' yarn dev` won't work. This would try to start multiple debuggers on the same port: one for the npm/yarn process and one for Next.js. You would then get an error like `Starting inspector on 127.0.0.1:9229 failed: address already in use` in your console.
Once the server starts, open a new tab in Chrome and visit `chrome://inspect`, where you should see your Next.js application inside the **Remote Target** section. Click **inspect** under your application to open a separate DevTools window, then go to the **Sources** tab.
Debugging server-side code here works much like debugging client-side code with Chrome DevTools, except that when you search for files here with `Ctrl+P` or `⌘+P`, your source files will have paths starting with `webpack://{application-name}/./` (where `{application-name}` will be replaced with the name of your application according to your `package.json` file).
### [Debugging on Windows](#debugging-on-windows)
Windows users may run into an issue when using `NODE_OPTIONS='--inspect'` as that syntax is not supported on Windows platforms. To get around this, install the [`cross-env`](https://www.npmjs.com/package/cross-env) package as a development dependency (`-D` with `npm` and `yarn`) and replace the `dev` script with the following.
package.json
{
"scripts": {
"dev": "cross-env NODE_OPTIONS='--inspect' next dev"
}
}
`cross-env` will set the `NODE_OPTIONS` environment variable regardless of which platform you are on (including Mac, Linux, and Windows) and allow you to debug consistently across devices and operating systems.
> **Good to know**: Ensure Windows Defender is disabled on your machine. This external service will check _every file read_, which has been reported to greatly increase Fast Refresh time with `next dev`. This is a known issue, not related to Next.js, but it does affect Next.js development.
[More information](#more-information)
-------------------------------------
To learn more about how to use a JavaScript debugger, take a look at the following documentation:
* [Node.js debugging in VS Code: Breakpoints](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_breakpoints)
* [Chrome DevTools: Debug JavaScript](https://developers.google.com/web/tools/chrome-devtools/javascript)
[Previous
Error Handling
](/docs/pages/building-your-application/configuring/error-handling)
[Next
Preview Mode
](/docs/pages/building-your-application/configuring/preview-mode)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a9387983204e64bf3cdecf9bd0d24487\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/testing/jest | 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
* [Quickstart](#quickstart)
* [Manual setup](#manual-setup)
* [Setting up Jest (with Babel)](#setting-up-jest-with-babel)
* [Handling stylesheets and image imports](#handling-stylesheets-and-image-imports)
* [Handling Fonts](#handling-fonts)
* [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/03-pages/01-building-your-application/07-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Testing](/docs/pages/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).
[Setting up Jest (with Babel)](#setting-up-jest-with-babel)
-----------------------------------------------------------
If you opt out of the [Next.js Compiler](/docs/architecture/nextjs-compiler) and use Babel instead, you will need to manually configure Jest and install `babel-jest` and `identity-obj-proxy` in addition to the packages above.
Here are the recommended options to configure Jest for Next.js:
jest.config.js
module.exports = {
collectCoverage: true,
// on node 14.x coverage provider v8 offers good speed and more or less good report
coverageProvider: 'v8',
collectCoverageFrom: [
'**/*.{js,jsx,ts,tsx}',
'!**/*.d.ts',
'!**/node_modules/**',
'!<rootDir>/out/**',
'!<rootDir>/.next/**',
'!<rootDir>/*.config.js',
'!<rootDir>/coverage/**',
],
moduleNameMapper: {
// Handle CSS imports (with CSS modules)
// https://jestjs.io/docs/webpack#mocking-css-modules
'^.+\\.module\\.(css|sass|scss)$': 'identity-obj-proxy',
// Handle CSS imports (without CSS modules)
'^.+\\.(css|sass|scss)$': '<rootDir>/__mocks__/styleMock.js',
// Handle image imports
// https://jestjs.io/docs/webpack#handling-static-assets
'^.+\\.(png|jpg|jpeg|gif|webp|avif|ico|bmp|svg)$/i': `<rootDir>/__mocks__/fileMock.js`,
// Handle module aliases
'^@/components/(.*)$': '<rootDir>/components/$1',
// Handle @next/font
'@next/font/(.*)': `<rootDir>/__mocks__/nextFontMock.js`,
// Handle next/font
'next/font/(.*)': `<rootDir>/__mocks__/nextFontMock.js`,
// Disable server-only
'server-only': `<rootDir>/__mocks__/empty.js`,
},
// Add more setup options before each test is run
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
testPathIgnorePatterns: ['<rootDir>/node_modules/', '<rootDir>/.next/'],
testEnvironment: 'jsdom',
transform: {
// Use babel-jest to transpile tests with the next/babel preset
// https://jestjs.io/docs/configuration#transform-objectstring-pathtotransformer--pathtotransformer-object
'^.+\\.(js|jsx|ts|tsx)$': ['babel-jest', { presets: ['next/babel'] }],
},
transformIgnorePatterns: [
'/node_modules/',
'^.+\\.module\\.(css|sass|scss)$',
],
}
You can learn more about each configuration option in the [Jest docs](https://jestjs.io/docs/configuration). We also recommend reviewing [`next/jest` configuration](https://github.com/vercel/next.js/blob/e02fe314dcd0ae614c65b505c6daafbdeebb920e/packages/next/src/build/jest/jest.ts) to see how Next.js configures Jest.
### [Handling stylesheets and image imports](#handling-stylesheets-and-image-imports)
Stylesheets and images aren't used in the tests but importing them may cause errors, so they will need to be mocked.
Create the mock files referenced in the configuration above - `fileMock.js` and `styleMock.js` - inside a `__mocks__` directory:
\_\_mocks\_\_/fileMock.js
module.exports = 'test-file-stub'
\_\_mocks\_\_/styleMock.js
module.exports = {}
For more information on handling static assets, please refer to the [Jest Docs](https://jestjs.io/docs/webpack#handling-static-assets).
[Handling Fonts](#handling-fonts)
---------------------------------
To handle fonts, create the `nextFontMock.js` file inside the `__mocks__` directory, and add the following configuration:
\_\_mocks\_\_/nextFontMock.js
module.exports = new Proxy(
{},
{
get: function getter() {
return () => ({
className: 'className',
variable: 'variable',
style: { fontFamily: 'fontFamily' },
})
},
}
)
[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 `<Home />` component successfully renders a heading:
export default function Home() {
return <h1>Home</h1>
}
\_\_tests\_\_/index.test.js
import '@testing-library/jest-dom'
import { render, screen } from '@testing-library/react'
import Home from '../pages/index'
describe('Home', () => {
it('renders a heading', () => {
render(<Home />)
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 Home from '../pages/index'
it('renders homepage unchanged', () => {
const { container } = render(<Home />)
expect(container).toMatchSnapshot()
})
> **Good to know**: Test files should not be included inside the Pages Router because any files inside the Pages Router are considered routes.
[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/pages/building-your-application/testing/vitest)
[Next
Playwright
](/docs/pages/building-your-application/testing/playwright)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"35bc12e5941f21fe7f0f725c2aab861e\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/testing/cypress | 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
* [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/03-pages/01-building-your-application/07-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Testing](/docs/pages/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.
[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:
pages/index.js
import Link from 'next/link'
export default function Home() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
pages/about.js
import Link from 'next/link'
export default function About() {
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.js
import AboutPage from '../../pages/about'
describe('<AboutPage />', () => {
it('should render and display expected content', () => {
// Mount the React component for the About page
cy.mount(<AboutPage />)
// The new page should contain an h1 with "About page"
cy.get('h1').contains('About')
// Validate that a link with the expected URL is present
// *Following* the link is better suited to an E2E test
cy.get('a[href="/"]').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/pages/building-your-application/testing/playwright)
[Next
Authentication
](/docs/pages/building-your-application/authentication)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"6d775d5758204279b409441c46456431\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/testing/playwright | 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
* [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/03-pages/01-building-your-application/07-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Testing](/docs/pages/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:
pages/index.ts
import Link from 'next/link'
export default function Home() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
pages/about.ts
import Link from 'next/link'
export default function About() {
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/pages/building-your-application/testing/jest)
[Next
Cypress
](/docs/pages/building-your-application/testing/cypress)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"09e52868128bd40004112796076067a1\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/testing/vitest | 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
* [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/03-pages/01-building-your-application/07-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Testing](/docs/pages/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:
pages/index.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
export default function Page() {
return (
<div>
<h1>Home</h1>
<Link href="/about">About</Link>
</div>
)
}
\_\_tests\_\_/index.test.tsx
TypeScript
JavaScriptTypeScript
import { expect, test } from 'vitest'
import { render, screen } from '@testing-library/react'
import Page from '../pages/index'
test('Page', () => {
render(<Page />)
expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined()
})
[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/pages/building-your-application/testing)
[Next
Jest
](/docs/pages/building-your-application/testing/jest)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"eb17ef68f64f8b8e3e6e570604a3c9a3\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/authentication | 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
* [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 API Routes](#protecting-api-routes)
* [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/03-pages/01-building-your-application/08-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
[Pages Router](/docs/pages)[Building Your Application](/docs/pages/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 sends a request that is handled by an API route.
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:
pages/login.tsx
TypeScript
JavaScriptTypeScript
import { FormEvent } from 'react'
import { useRouter } from 'next/router'
export default function LoginPage() {
const router = useRouter()
async function handleSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault()
const formData = new FormData(event.currentTarget)
const email = formData.get('email')
const password = formData.get('password')
const response = await fetch('/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password }),
})
if (response.ok) {
router.push('/profile')
} else {
// Handle errors
}
}
return (
<form onSubmit={handleSubmit}>
<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 triggers a function that sends a POST request to an API route (`/api/auth/login`).
You can then call your Authentication Provider's API in the API route to handle authentication:
pages/api/auth/login.ts
TypeScript
JavaScriptTypeScript
import { NextApiRequest, NextApiResponse } from 'next'
import { signIn } from '@/auth'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const { email, password } = req.body
await signIn('credentials', { email, password })
res.status(200).json({ success: true })
} catch (error) {
if (error.type === 'CredentialsSignin') {
res.status(401).json({ error: 'Invalid credentials.' })
} else {
res.status(500).json({ error: 'Something went wrong.' })
}
}
}
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.
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.
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).
### [Protecting API Routes](#protecting-api-routes)
API Routes in Next.js are essential for handling server-side logic and data management. It's crucial to secure these routes to ensure that only authorized users can access specific functionalities. This typically involves verifying the user's authentication status and their role-based permissions.
Here's an example of securing an API Route:
pages/api/route.ts
TypeScript
JavaScriptTypeScript
import { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const session = await getSession(req)
// Check if the user is authenticated
if (!session) {
res.status(401).json({
error: 'User is not authenticated',
})
return
}
// Check if the user has the 'admin' role
if (session.user.role !== 'admin') {
res.status(401).json({
error: 'Unauthorized access: User does not have admin privileges.',
})
return
}
// Proceed with the route for authorized users
// ... implementation of the API Route
}
This example demonstrates an API Route 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.
### [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:**
pages/api/login.ts
TypeScript
JavaScriptTypeScript
import { serialize } from 'cookie'
import type { NextApiRequest, NextApiResponse } from 'next'
export default function handler(req: NextApiRequest, res: NextApiResponse) {
const sessionData = req.body
const encryptedSessionData = encrypt(sessionData)
const cookie = serialize('session', encryptedSessionData, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
maxAge: 60 * 60 * 24 * 7, // One week
path: '/',
})
res.setHeader('Set-Cookie', cookie)
res.status(200).json({ message: 'Successfully set cookie!' })
}
### [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**:
pages/api/create-session.ts
TypeScript
JavaScriptTypeScript
import db from '../../lib/db'
import { NextApiRequest, NextApiResponse } from 'next'
export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
try {
const user = req.body
const sessionId = generateSessionId()
await db.insertSession({
sessionId,
userId: user.id,
createdAt: new Date(),
})
res.status(200).json({ sessionId })
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' })
}
}
### [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/pages/building-your-application/testing/cypress)
[Next
Deploying
](/docs/pages/building-your-application/deploying)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"67fbcbd6849b7550b31b7e3454a75301\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/configuring/content-security-policy | 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
* [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)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/06-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Configuring](/docs/pages/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.
[Previous
Preview Mode
](/docs/pages/building-your-application/configuring/preview-mode)
[Next
Testing
](/docs/pages/building-your-application/testing)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"8e64640c02616e71d2dd62cb4f335c47\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/codemods | 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)
* [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/03-pages/01-building-your-application/10-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Upgrading](/docs/pages/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/pages/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/pages/building-your-application/upgrading)
[Next
From Pages to App
](/docs/pages/building-your-application/upgrading/app-router-migration)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"9057d729dac18284e9076c72f8a8d6fc\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/deploying | 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
* [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)
* [Manual Graceful Shutdowns](#manual-graceful-shutdowns)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/09-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
[Pages Router](/docs/pages)[Building Your Application](/docs/pages/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.
[Manual Graceful Shutdowns](#manual-graceful-shutdowns)
-------------------------------------------------------
When self-hosting, you might want to run code when the server shuts down on `SIGTERM` or `SIGINT` signals.
You can set the env variable `NEXT_MANUAL_SIG_HANDLE` to `true` and then register a handler for that signal inside your `_document.js` file. You will need to register the environment variable directly in the `package.json` script, and not in the `.env` file.
> **Good to know**: Manual signal handling is not available in `next dev`.
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "NEXT_MANUAL_SIG_HANDLE=true next start"
}
}
pages/\_document.js
if (process.env.NEXT_MANUAL_SIG_HANDLE) {
process.on('SIGTERM', () => {
console.log('Received SIGTERM: cleaning up')
process.exit(0)
})
process.on('SIGINT', () => {
console.log('Received SIGINT: cleaning up')
process.exit(0)
})
}
[
### Production Checklist
Recommendations to ensure the best performance and user experience before taking your Next.js application to production.
](/docs/pages/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/pages/building-your-application/deploying/static-exports)[
### Multi Zones
Learn how to use multi zones to deploy multiple Next.js apps as a single app.
](/docs/pages/building-your-application/deploying/multi-zones)[
### Continuous Integration (CI) Build Caching
Learn how to configure CI to cache Next.js builds
](/docs/pages/building-your-application/deploying/ci-build-caching)
[Previous
Authentication
](/docs/pages/building-your-application/authentication)
[Next
Production Checklist
](/docs/pages/building-your-application/deploying/production-checklist)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"7a75ffd1bb3b60f5b7cbd4e58aca12e7\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/deploying/production-checklist | 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
* [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/03-pages/01-building-your-application/09-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Deploying](/docs/pages/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:
* **[Code-splitting](/docs/pages/building-your-application/routing/pages-and-layouts):** Next.js automatically code-splits your application code by pages. This means only the code needed for the current page is loaded on navigation. You may also consider [lazy loading](/docs/pages/building-your-application/optimizing/lazy-loading) third-party libraries, where appropriate.
* **[Prefetching](/docs/pages/api-reference/components/link#prefetch):** 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.
* **[Automatic Static Optimization](/docs/pages/building-your-application/rendering/automatic-static-optimization):** Next.js automatically determines that a page is static (can be pre-rendered) if it has no blocking data requirements. Optimized pages can be cached, and served to the end-user from multiple CDN locations. You may opt into [Server-side Rendering](/docs/pages/building-your-application/data-fetching/get-server-side-props), 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)
* **[`<Link>` component](/docs/pages/building-your-application/routing/linking-and-navigating):** Use the `<Link>` component for client-side navigation and prefetching.
* **[Custom Errors](/docs/pages/building-your-application/routing/custom-error):** Gracefully handle [500](/docs/pages/building-your-application/routing/custom-error#500-page) and [404 errors](/docs/pages/building-your-application/routing/custom-error#404-page)
### [Data fetching and caching](#data-fetching-and-caching)
* **[API Routes](/docs/pages/building-your-application/routing/api-routes):** Use Route Handlers to access your backend resources, and prevent sensitive secrets from being exposed to the client.
* **[Data Caching](/docs/pages/building-your-application/data-fetching/get-static-props):** Verify whether your data requests are being cached or not, and opt into caching, where appropriate. Ensure requests that don't use `getStaticProps` are cached where appropriate.
* **[Incremental Static Regeneration](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration):** Use Incremental Static Regeneration to update static pages after they've been built, without rebuilding your entire site.
* **[Static Images](/docs/pages/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)
* **[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)
* **[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)
* **[`<Head>` Component](/docs/pages/api-reference/components/head):** Use the `next/head` component to add page titles, descriptions, and more.
### [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).
### [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/pages/building-your-application/deploying)
[Next
Static Exports
](/docs/pages/building-your-application/deploying/static-exports)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"2e3929ffec39b65c53f4a75d2d232450\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/deploying/ci-build-caching | 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
* [Vercel](#vercel)
* [CircleCI](#circleci)
* [Travis CI](#travis-ci)
* [GitLab CI](#gitlab-ci)
* [Netlify CI](#netlify-ci)
* [AWS CodeBuild](#aws-codebuild)
* [GitHub Actions](#github-actions)
* [Bitbucket Pipelines](#bitbucket-pipelines)
* [Heroku](#heroku)
* [Azure Pipelines](#azure-pipelines)
* [Jenkins (Pipeline)](#jenkins-pipeline)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/09-deploying/04-ci-build-caching.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)[Deploying](/docs/pages/building-your-application/deploying)Continuous Integration (CI) Build Caching
Continuous Integration (CI) Build Caching
=========================================
To improve build performance, Next.js saves a cache to `.next/cache` that is shared between builds.
To take advantage of this cache in Continuous Integration (CI) environments, your CI workflow will need to be configured to correctly persist the cache between builds.
> If your CI is not configured to persist `.next/cache` between builds, you may see a [No Cache Detected](/docs/messages/no-cache) error.
Here are some example cache configurations for common CI providers:
[Vercel](#vercel)
-----------------
Next.js caching is automatically configured for you. There's no action required on your part.
[CircleCI](#circleci)
---------------------
Edit your `save_cache` step in `.circleci/config.yml` to include `.next/cache`:
steps:
- save_cache:
key: dependency-cache-{{ checksum "yarn.lock" }}
paths:
- ./node_modules
- ./.next/cache
If you do not have a `save_cache` key, please follow CircleCI's [documentation on setting up build caching](https://circleci.com/docs/2.0/caching/).
[Travis CI](#travis-ci)
-----------------------
Add or merge the following into your `.travis.yml`:
cache:
directories:
- $HOME/.cache/yarn
- node_modules
- .next/cache
[GitLab CI](#gitlab-ci)
-----------------------
Add or merge the following into your `.gitlab-ci.yml`:
cache:
key: ${CI_COMMIT_REF_SLUG}
paths:
- node_modules/
- .next/cache/
[Netlify CI](#netlify-ci)
-------------------------
Use [Netlify Plugins](https://www.netlify.com/products/build/plugins/) with [`@netlify/plugin-nextjs`](https://www.npmjs.com/package/@netlify/plugin-nextjs).
[AWS CodeBuild](#aws-codebuild)
-------------------------------
Add (or merge in) the following to your `buildspec.yml`:
cache:
paths:
- 'node_modules/**/*' # Cache `node_modules` for faster `yarn` or `npm i`
- '.next/cache/**/*' # Cache Next.js for faster application rebuilds
[GitHub Actions](#github-actions)
---------------------------------
Using GitHub's [actions/cache](https://github.com/actions/cache), add the following step in your workflow file:
uses: actions/cache@v4
with:
# See here for caching with `yarn` https://github.com/actions/cache/blob/main/examples.md#node---yarn or you can leverage caching with actions/setup-node https://github.com/actions/setup-node
path: |
~/.npm
${{ github.workspace }}/.next/cache
# Generate a new cache whenever packages or source files change.
key: ${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-${{ hashFiles('**/*.js', '**/*.jsx', '**/*.ts', '**/*.tsx') }}
# If source files changed but packages didn't, rebuild from a prior cache.
restore-keys: |
${{ runner.os }}-nextjs-${{ hashFiles('**/package-lock.json') }}-
[Bitbucket Pipelines](#bitbucket-pipelines)
-------------------------------------------
Add or merge the following into your `bitbucket-pipelines.yml` at the top level (same level as `pipelines`):
definitions:
caches:
nextcache: .next/cache
Then reference it in the `caches` section of your pipeline's `step`:
- step:
name: your_step_name
caches:
- node
- nextcache
[Heroku](#heroku)
-----------------
Using Heroku's [custom cache](https://devcenter.heroku.com/articles/nodejs-support#custom-caching), add a `cacheDirectories` array in your top-level package.json:
"cacheDirectories": [".next/cache"]
[Azure Pipelines](#azure-pipelines)
-----------------------------------
Using Azure Pipelines' [Cache task](https://docs.microsoft.com/en-us/azure/devops/pipelines/tasks/utility/cache), add the following task to your pipeline yaml file somewhere prior to the task that executes `next build`:
- task: Cache@2
displayName: 'Cache .next/cache'
inputs:
key: next | $(Agent.OS) | yarn.lock
path: '$(System.DefaultWorkingDirectory)/.next/cache'
[Jenkins (Pipeline)](#jenkins-pipeline)
---------------------------------------
Using Jenkins' [Job Cacher](https://www.jenkins.io/doc/pipeline/steps/jobcacher/) plugin, add the following build step to your `Jenkinsfile` where you would normally run `next build` or `npm install`:
stage("Restore npm packages") {
steps {
// Writes lock-file to cache based on the GIT_COMMIT hash
writeFile file: "next-lock.cache", text: "$GIT_COMMIT"
cache(caches: [
arbitraryFileCache(
path: "node_modules",
includes: "**/*",
cacheValidityDecidingFile: "package-lock.json"
)
]) {
sh "npm install"
}
}
}
stage("Build") {
steps {
// Writes lock-file to cache based on the GIT_COMMIT hash
writeFile file: "next-lock.cache", text: "$GIT_COMMIT"
cache(caches: [
arbitraryFileCache(
path: ".next/cache",
includes: "**/*",
cacheValidityDecidingFile: "next-lock.cache"
)
]) {
// aka `next build`
sh "npm run build"
}
}
}
[Previous
Multi Zones
](/docs/pages/building-your-application/deploying/multi-zones)
[Next
Upgrading
](/docs/pages/building-your-application/upgrading)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"5107eee81996e81f37dfa48200946ca2\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/deploying/static-exports | 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
* [Configuration](#configuration)
* [Supported Features](#supported-features-1)
* [Image Optimization](#image-optimization)
* [Unsupported Features](#unsupported-features)
* [Deploying](#deploying)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/09-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Deploying](/docs/pages/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.
> **Good to know**: We recommend using the App Router for enhanced static export support.
[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.
You can utilize [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props) and [`getStaticPaths`](/docs/pages/building-your-application/data-fetching/get-static-paths) to generate an HTML file for each page in your `pages` directory (or more for [dynamic routes](/docs/app/building-your-application/routing/dynamic-routes)).
[Supported Features](#supported-features-1)
-------------------------------------------
The majority of core Next.js features needed to build a static site are supported, including:
* [Dynamic Routes when using `getStaticPaths`](/docs/app/building-your-application/routing/dynamic-routes)
* Prefetching with `next/link`
* Preloading JavaScript
* [Dynamic Imports](/docs/pages/building-your-application/optimizing/lazy-loading)
* Any styling options (e.g. CSS Modules, styled-jsx)
* [Client-side data fetching](/docs/pages/building-your-application/data-fetching/client-side)
* [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props)
* [`getStaticPaths`](/docs/pages/building-your-application/data-fetching/get-static-paths)
### [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} />
}
[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:
* [Internationalized Routing](/docs/pages/building-your-application/routing/internationalization)
* [API Routes](/docs/pages/building-your-application/routing/api-routes)
* [Rewrites](/docs/pages/api-reference/next-config-js/rewrites)
* [Redirects](/docs/pages/api-reference/next-config-js/redirects)
* [Headers](/docs/pages/api-reference/next-config-js/headers)
* [Middleware](/docs/pages/building-your-application/routing/middleware)
* [Incremental Static Regeneration](/docs/pages/building-your-application/data-fetching/incremental-static-regeneration)
* [Image Optimization](/docs/pages/building-your-application/optimizing/images) with the default `loader`
* [Draft Mode](/docs/pages/building-your-application/configuring/draft-mode)
* [`getStaticPaths` with `fallback: true`](/docs/pages/api-reference/functions/get-static-paths#fallback-true)
* [`getStaticPaths` with `fallback: 'blocking'`](/docs/pages/api-reference/functions/get-static-paths#fallback-blocking)
* [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props)
[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/pages/building-your-application/deploying/production-checklist)
[Next
Multi Zones
](/docs/pages/building-your-application/deploying/multi-zones)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"c402928d8763f1fb83013e4035391729\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/version-14 | 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
* [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/03-pages/01-building-your-application/10-upgrading/05-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Upgrading](/docs/pages/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
Migrating from Create React App
](/docs/pages/building-your-application/upgrading/from-create-react-app)
[Next
Version 13
](/docs/pages/building-your-application/upgrading/version-13)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"490f0038b909f000c702c35fee5eaa75\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading | 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/10-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
[Pages Router](/docs/pages)[Building Your Application](/docs/pages/building-your-application)Upgrading
Upgrading
=========
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/pages/building-your-application/upgrading/codemods)[
### From Pages to App
Learn how to upgrade your existing Next.js application from the Pages Router to the App Router.
](/docs/pages/building-your-application/upgrading/app-router-migration)[
### Migrating from Vite
Learn how to migrate your existing React application from Vite to Next.js.
](/docs/pages/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/pages/building-your-application/upgrading/from-create-react-app)[
### Version 14
Upgrade your Next.js Application from Version 13 to 14.
](/docs/pages/building-your-application/upgrading/version-14)[
### Version 13
Upgrade your Next.js Application from Version 12 to 13.
](/docs/pages/building-your-application/upgrading/version-13)[
### Version 12
Upgrade your Next.js Application from Version 11 to Version 12.
](/docs/pages/building-your-application/upgrading/version-12)[
### Version 11
Upgrade your Next.js Application from Version 10 to Version 11.
](/docs/pages/building-your-application/upgrading/version-11)[
### Version 10
Upgrade your Next.js Application from Version 9 to Version 10.
](/docs/pages/building-your-application/upgrading/version-10)[
### Version 9
Upgrade your Next.js Application from Version 8 to Version 9.
](/docs/pages/building-your-application/upgrading/version-9)
[Previous
Continuous Integration (CI) Build Caching
](/docs/pages/building-your-application/deploying/ci-build-caching)
[Next
Codemods
](/docs/pages/building-your-application/upgrading/codemods)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"b7a28bbe852b56b44a9f0a6f78ed57bc\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/deploying/multi-zones | 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
* [How to define a zone](#how-to-define-a-zone)
* [How to merge zones](#how-to-merge-zones)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/09-deploying/03-multi-zones.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)[Deploying](/docs/pages/building-your-application/deploying)Multi Zones
Multi Zones
===========
Examples
* [With Zones](https://github.com/vercel/next.js/tree/canary/examples/with-zones)
A zone is a single deployment of a Next.js app. You can have multiple zones and merge them as a single app.
For example, let's say you have the following apps:
* An app for serving `/blog/**`
* Another app for serving all other pages
With multi zones support, you can merge both these apps into a single one allowing your customers to browse it using a single URL, but you can develop and deploy both apps independently.
[How to define a zone](#how-to-define-a-zone)
---------------------------------------------
There are no zone related APIs. You only need to do the following:
* Make sure to keep only the pages you need in your app, meaning that an app can't have pages from another app, if app `A` has `/blog` then app `B` shouldn't have it too.
* Make sure to configure a [basePath](/docs/app/api-reference/next-config-js/basePath) to avoid conflicts with pages and static files.
[How to merge zones](#how-to-merge-zones)
-----------------------------------------
You can merge zones using [`rewrites`](/docs/pages/api-reference/next-config-js/rewrites) in one of the apps or any HTTP proxy.
For [Next.js on Vercel](https://vercel.com?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) applications, you can use a [monorepo](https://vercel.com/blog/monorepos-are-changing-how-teams-build-software?utm_source=next-site&utm_medium=docs&utm_campaign=next-website) to deploy both apps with a single `git push`.
[Previous
Static Exports
](/docs/pages/building-your-application/deploying/static-exports)
[Next
Continuous Integration (CI) Build Caching
](/docs/pages/building-your-application/deploying/ci-build-caching)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"c9cb5f662cc73ca333ec4ed4d51685b4\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/from-vite | 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
* [Why Switch?](#why-switch)
* [Slow initial page loading time](#slow-initial-page-loading-time)
* [No automatic code splitting](#no-automatic-code-splitting)
* [Network waterfalls](#network-waterfalls)
* [Fast and intentional loading states](#fast-and-intentional-loading-states)
* [Choose the data fetching strategy](#choose-the-data-fetching-strategy)
* [Middleware](#middleware)
* [Built-in Optimizations](#built-in-optimizations)
* [Migration Steps](#migration-steps)
* [Step 1: Install the Next.js Dependency](#step-1-install-the-nextjs-dependency)
* [Step 2: Create the Next.js Configuration File](#step-2-create-the-nextjs-configuration-file)
* [Step 3: Update TypeScript Configuration](#step-3-update-typescript-configuration)
* [Step 4: Create the Root Layout](#step-4-create-the-root-layout)
* [Step 5: Create the Entrypoint Page](#step-5-create-the-entrypoint-page)
* [Step 6: Update Static Image Imports](#step-6-update-static-image-imports)
* [Step 7: Migrate the Environment Variables](#step-7-migrate-the-environment-variables)
* [Step 8: Update Scripts in package.json](#step-8-update-scripts-in-packagejson)
* [Step 9: Clean Up](#step-9-clean-up)
* [Next Steps](#next-steps)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/10-upgrading/03-from-vite.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)[Upgrading](/docs/pages/building-your-application/upgrading)Migrating from Vite
Migrating from Vite
===================
This guide will help you migrate an existing Vite application to Next.js.
[Why Switch?](#why-switch)
--------------------------
There are several reasons why you might want to switch from Vite to Next.js:
### [Slow initial page loading time](#slow-initial-page-loading-time)
If you have built your application with the [default Vite plugin for React](https://github.com/vitejs/vite-plugin-react/tree/main/packages/plugin-react), your application is a purely client-side application. Client-side only applications, also known as single-page applications (SPAs), often experience slow initial page loading time. This happens due to a couple of reasons:
1. The browser needs to wait for the React code and your entire application bundle to download and run before your code is able to send requests to load some data.
2. Your application code grows with every new feature and extra dependency you add.
### [No automatic code splitting](#no-automatic-code-splitting)
The previous issue of slow loading times can be somewhat managed with code splitting. However, if you try to do code splitting manually, you'll often make performance worse. It's easy to inadvertently introduce network waterfalls when code-splitting manually. Next.js provides automatic code splitting built into its router.
### [Network waterfalls](#network-waterfalls)
A common cause of poor performance occurs when applications make sequential client-server requests to fetch data. One common pattern for data fetching in an SPA is to initially render a placeholder, and then fetch data after the component has mounted. Unfortunately, this means that a child component that fetches data can't start fetching until the parent component has finished loading its own data.
While fetching data on the client is supported with Next.js, it also gives you the option to shift data fetching to the server, which can eliminate client-server waterfalls.
### [Fast and intentional loading states](#fast-and-intentional-loading-states)
With built-in support for [streaming through React Suspense](/docs/app/building-your-application/routing/loading-ui-and-streaming#streaming-with-suspense), you can be more intentional about which parts of your UI you want to load first and in what order without introducing network waterfalls.
This enables you to build pages that are faster to load and eliminate [layout shifts](https://vercel.com/blog/how-core-web-vitals-affect-seo).
### [Choose the data fetching strategy](#choose-the-data-fetching-strategy)
Depending on your needs, Next.js allows you to choose your data fetching strategy on a page and component basis. You can decide to fetch at build time, at request time on the server, or on the client. For example, you can fetch data from your CMS and render your blog posts at build time, which can then be efficiently cached on a CDN.
### [Middleware](#middleware)
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. This is especially useful to avoid having a flash of unauthenticated content when the user visits an authenticated-only page by redirecting the user to a login page. The middleware is also useful for experimentation and [internationalization](/docs/app/building-your-application/routing/internationalization).
### [Built-in Optimizations](#built-in-optimizations)
[Images](/docs/app/building-your-application/optimizing/images), [fonts](/docs/app/building-your-application/optimizing/fonts), and [third-party scripts](/docs/app/building-your-application/optimizing/scripts) often have significant impact on an application's performance. Next.js comes with built-in components that automatically optimize those for you.
[Migration Steps](#migration-steps)
-----------------------------------
Our goal with this migration is to get a working Next.js application as quickly as possible, so that you can then adopt Next.js features incrementally. To begin with, we'll keep it as a purely client-side application (SPA) without migrating your existing router. This helps minimize the chances of encountering issues during the migration process and reduces merge conflicts.
### [Step 1: Install the Next.js Dependency](#step-1-install-the-nextjs-dependency)
The first thing you need to do is to install `next` as a dependency:
Terminal
npm install next@latest
### [Step 2: Create the Next.js Configuration File](#step-2-create-the-nextjs-configuration-file)
Create a `next.config.mjs` at the root of your project. This file will hold your [Next.js configuration options](/docs/app/api-reference/next-config-js).
next.config.mjs
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export', // Outputs a Single-Page Application (SPA).
distDir: './dist', // Changes the build output directory to `./dist/`.
}
export default nextConfig
> **Good to know:** You can use either `.js` or `.mjs` for your Next.js configuration file.
### [Step 3: Update TypeScript Configuration](#step-3-update-typescript-configuration)
If you're using TypeScript, you need to update your `tsconfig.json` file with the following changes to make it compatible with Next.js. If you're not using TypeScript, you can skip this step.
1. Remove the [project reference](https://www.typescriptlang.org/tsconfig#references) to `tsconfig.node.json`
2. Add `./dist/types/**/*.ts` and `./next-env.d.ts` to the [`include` array](https://www.typescriptlang.org/tsconfig#include)
3. Add `./node_modules` to the [`exclude` array](https://www.typescriptlang.org/tsconfig#exclude)
4. Add `{ "name": "next" }` to the [`plugins` array in `compilerOptions`](https://www.typescriptlang.org/tsconfig#plugins): `"plugins": [{ "name": "next" }]`
5. Set [`esModuleInterop`](https://www.typescriptlang.org/tsconfig#esModuleInterop) to `true`: `"esModuleInterop": true`
6. Set [`jsx`](https://www.typescriptlang.org/tsconfig#jsx) to `preserve`: `"jsx": "preserve"`
7. Set [`allowJs`](https://www.typescriptlang.org/tsconfig#allowJs) to `true`: `"allowJs": true`
8. Set [`forceConsistentCasingInFileNames`](https://www.typescriptlang.org/tsconfig#forceConsistentCasingInFileNames) to `true`: `"forceConsistentCasingInFileNames": true`
9. Set [`incremental`](https://www.typescriptlang.org/tsconfig#incremental) to `true`: `"incremental": true`
Here's an example of a working `tsconfig.json` with those changes:
tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"esModuleInterop": true,
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"incremental": true,
"plugins": [{ "name": "next" }]
},
"include": ["./src", "./dist/types/**/*.ts", "./next-env.d.ts"],
"exclude": ["./node_modules"]
}
You can find more information about configuring TypeScript on the [Next.js docs](/docs/app/building-your-application/configuring/typescript#typescript-plugin).
### [Step 4: Create the Root Layout](#step-4-create-the-root-layout)
A Next.js [App Router](/docs/app) application must include a [root layout](/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required) file, which is a [React Server Component](/docs/app/building-your-application/rendering/server-components) that will wrap all pages in your application. This file is defined at the top level of the `app` directory.
The closest equivalent to the root layout file in a Vite application is the [`index.html` file](https://vitejs.dev/guide/#index-html-and-project-root), which contains your `<html>`, `<head>`, and `<body>` tags.
In this step, you'll convert your `index.html` file into a root layout file:
1. Create a new `app` directory in your `src` directory.
2. Create a new `layout.tsx` file inside that `app` directory:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return null
}
> **Good to know**: `.js`, `.jsx`, or `.tsx` extensions can be used for Layout files.
3. Copy the content of your `index.html` file into the previously created `<RootLayout>` component while replacing the `body.div#root` and `body.script` tags with `<div id="root">{children}</div>`:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/icon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My App</title>
<meta name="description" content="My App is a..." />
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
4. Next.js already includes by default the [meta charset](https://developer.mozilla.org/docs/Web/HTML/Element/meta#charset) and [meta viewport](https://developer.mozilla.org/docs/Web/HTML/Viewport_meta_tag) tags, so you can safely remove those from your `<head>`:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<link rel="icon" type="image/svg+xml" href="/icon.svg" />
<title>My App</title>
<meta name="description" content="My App is a..." />
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
5. Any [metadata files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) such as `favicon.ico`, `icon.png`, `robots.txt` are automatically added to the application `<head>` tag as long as you have them placed into the top level of the `app` directory. After moving [all supported files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) into the `app` directory you can safely delete their `<link>` tags:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<title>My App</title>
<meta name="description" content="My App is a..." />
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
6. Finally, Next.js can manage your last `<head>` tags with the [Metadata API](/docs/app/building-your-application/optimizing/metadata). Move your final metadata info into an exported [`metadata` object](/docs/app/api-reference/functions/generate-metadata#metadata-object):
app/layout.tsx
TypeScript
JavaScriptTypeScript
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: 'My App',
description: 'My App is a...',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
With the above changes, you shifted from declaring everything in your `index.html` to using Next.js' convention-based approach built into the framework ([Metadata API](/docs/app/building-your-application/optimizing/metadata)). This approach enables you to more easily improve your SEO and web shareability of your pages.
### [Step 5: Create the Entrypoint Page](#step-5-create-the-entrypoint-page)
On Next.js you declare an entrypoint for your application by creating a `page.tsx` file. The closest equivalent of this file on Vite is your `main.tsx` file. In this step, you’ll set up the entrypoint of your application.
1. **Create a `[[...slug]]` directory in your `app` directory.**
Since in this guide we're aiming first to set up our Next.js as an SPA (Single Page Application), you need your page entrypoint to catch all possible routes of your application. For that, create a new `[[...slug]]` directory in your `app` directory.
This directory is what is called an [optional catch-all route segment](/docs/app/building-your-application/routing/dynamic-routes#optional-catch-all-segments). Next.js uses a file-system based router where [directories are used to define routes](/docs/app/building-your-application/routing/defining-routes#creating-routes). This special directory will make sure that all routes of your application will be directed to its containing `page.tsx` file.
2. **Create a new `page.tsx` file inside the `app/[[...slug]]` directory with the following content:**
app/\[\[...slug\]\]/page.tsx
TypeScript
JavaScriptTypeScript
import '../../index.css'
export function generateStaticParams() {
return [{ slug: [''] }]
}
export default function Page() {
return '...' // We'll update this
}
> **Good to know**: `.js`, `.jsx`, or `.tsx` extensions can be used for Page files.
This file is a [Server Component](/docs/app/building-your-application/rendering/server-components). When you run `next build`, the file is prerendered into a static asset. It does _not_ require any dynamic code.
This file imports our global CSS and tells [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params) we are only going to generate one route, the index route at `/`.
Now, let's move the rest of our Vite application which will run client-only.
app/\[\[...slug\]\]/client.tsx
TypeScript
JavaScriptTypeScript
'use client'
import React from 'react'
import dynamic from 'next/dynamic'
const App = dynamic(() => import('../../App'), { ssr: false })
export function ClientOnly() {
return <App />
}
This file is a [Client Component](/docs/app/building-your-application/rendering/client-components), defined by the `'use client'` directive. Client Components are still [prerendered to HTML](/docs/app/building-your-application/rendering/client-components#how-are-client-components-rendered) on the server before being sent to the client.
Since we want a client-only application to start, we can configure Next.js to disable prerendering from the `App` component down.
const App = dynamic(() => import('../../App'), { ssr: false })
Now, update your entrypoint page to use the new component:
app/\[\[...slug\]\]/page.tsx
TypeScript
JavaScriptTypeScript
import '../../index.css'
import { ClientOnly } from './client'
export function generateStaticParams() {
return [{ slug: [''] }]
}
export default function Page() {
return <ClientOnly />
}
### [Step 6: Update Static Image Imports](#step-6-update-static-image-imports)
Next.js handles static image imports slightly different from Vite. With Vite, importing an image file will return its public URL as a string:
App.tsx
import image from './img.png' // `image` will be '/assets/img.2d8efhg.png' in production
export default function App() {
return <img src={image} />
}
With Next.js, static image imports return an object. The object can then be used directly with the Next.js [`<Image>` component](/docs/app/api-reference/components/image), or you can use the object's `src` property with your existing `<img>` tag.
The `<Image>` component has the added benefits of [automatic image optimization](/docs/app/building-your-application/optimizing/images). The `<Image>` component automatically sets the `width` and `height` attributes of the resulting `<img>` based on the image's dimensions. This prevents layout shifts when the image loads. However, this can cause issues if your app contains images with only one of their dimensions being styled without the other styled to `auto`. When not styled to `auto`, the dimension will default to the `<img>` dimension attribute's value, which can cause the image to appear distorted.
Keeping the `<img>` tag will reduce the amount of changes in your application and prevent the above issues. You can then optionally later migrate to the `<Image>` component to take advantage of optimizing images by [configuring a loader](/docs/app/building-your-application/optimizing/images#loaders), or moving to the default Next.js server which has automatic image optimization.
1. **Convert absolute import paths for images imported from `/public` into relative imports:**
// Before
import logo from '/logo.png'
// After
import logo from '../public/logo.png'
2. **Pass the image `src` property instead of the whole image object to your `<img>` tag:**
// Before
<img src={logo} />
// After
<img src={logo.src} />
Alternatively, you can reference the public URL for the image asset based on the filename. For example, `public/logo.png` will serve the image at `/logo.png` for your application, which would be the `src` value.
> **Warning:** If you're using TypeScript, you might encounter type errors when accessing the `src` property. You can safely ignore those for now. They will be fixed by the end of this guide.
### [Step 7: Migrate the Environment Variables](#step-7-migrate-the-environment-variables)
Next.js has support for `.env` [environment variables](/docs/app/building-your-application/configuring/environment-variables) similar to Vite. The main difference is the prefix used to expose environment variables on the client-side.
* Change all environment variables with the `VITE_` prefix to `NEXT_PUBLIC_`.
Vite exposes a few built-in environment variables on the special `import.meta.env` object which aren’t supported by Next.js. You need to update their usage as follows:
* `import.meta.env.MODE` ⇒ `process.env.NODE_ENV`
* `import.meta.env.PROD` ⇒ `process.env.NODE_ENV === 'production'`
* `import.meta.env.DEV` ⇒ `process.env.NODE_ENV !== 'production'`
* `import.meta.env.SSR` ⇒ `typeof window !== 'undefined'`
Next.js also doesn't provide a built-in `BASE_URL` environment variable. However, you can still configure one, if you need it:
1. **Add the following to your `.env` file:**
.env
# ...
NEXT_PUBLIC_BASE_PATH="/some-base-path"
2. **Set [`basePath`](/docs/app/api-reference/next-config-js/basePath) to `process.env.NEXT_PUBLIC_BASE_PATH` in your `next.config.mjs` file:**
next.config.mjs
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export', // Outputs a Single-Page Application (SPA).
distDir: './dist', // Changes the build output directory to `./dist/`.
basePath: process.env.NEXT_PUBLIC_BASE_PATH, // Sets the base path to `/some-base-path`.
}
export default nextConfig
3. **Update `import.meta.env.BASE_URL` usages to `process.env.NEXT_PUBLIC_BASE_PATH`**
### [Step 8: Update Scripts in `package.json`](#step-8-update-scripts-in-packagejson)
You should now be able to run your application to test if you successfully migrated to Next.js. But before that, you need to update your `scripts` in your `package.json` with Next.js related commands, and add `.next` and `next-env.d.ts` to your `.gitignore`:
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}
.gitignore
# ...
.next
next-env.d.ts
dist
Now run `npm run dev`, and open [`http://localhost:3000`](http://localhost:3000). You should see your application now running on Next.js.
> **Example:** Check out [this pull request](https://github.com/inngest/vite-to-nextjs/pull/1) for a working example of a Vite application migrated to Next.js.
### [Step 9: Clean Up](#step-9-clean-up)
You can now clean up your codebase from Vite related artifacts:
* Delete `main.tsx`
* Delete `index.html`
* Delete `vite-env.d.ts`
* Delete `tsconfig.node.json`
* Delete `vite.config.ts`
* Uninstall Vite dependencies
[Next Steps](#next-steps)
-------------------------
If everything went according to plan, you now have a functioning Next.js application running as a single-page application. However, you aren't yet taking advantage of most of Next.js' benefits, but you can now start making incremental changes to reap all the benefits. Here's what you might want to do next:
* Migrate from React Router to the [Next.js App Router](/docs/app/building-your-application/routing) to get:
* Automatic code splitting
* [Streaming Server-Rendering](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [React Server Components](/docs/app/building-your-application/rendering/server-components)
* [Optimize images with the `<Image>` component](/docs/app/building-your-application/optimizing/images)
* [Optimize fonts with `next/font`](/docs/app/building-your-application/optimizing/fonts)
* [Optimize third-party scripts with the `<Script>` component](/docs/app/building-your-application/optimizing/scripts)
* [Update your ESLint configuration to support Next.js rules](/docs/app/building-your-application/configuring/eslint)
[Previous
From Pages to App
](/docs/pages/building-your-application/upgrading/app-router-migration)
[Next
Migrating from Create React App
](/docs/pages/building-your-application/upgrading/from-create-react-app)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f2f0ee5e9f338576ede8920289062e6b\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/app-router-migration | 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
* [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/03-pages/01-building-your-application/10-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
[Pages Router](/docs/pages)[...](/docs/pages/building-your-application)[Upgrading](/docs/pages/building-your-application/upgrading)From Pages to App
From Pages to App
=================
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/pages/building-your-application/upgrading/codemods)
[Next
Migrating from Vite
](/docs/pages/building-your-application/upgrading/from-vite)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"a3b955b2b79ffeb89f60b902a62c7b59\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/version-12 | 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
* [Upgrading to 12.2](#upgrading-to-122)
* [Upgrading to 12.0](#upgrading-to-120)
* [SWC replacing Babel](#swc-replacing-babel)
* [SWC replacing Terser for minification](#swc-replacing-terser-for-minification)
* [Improvements to styled-jsx CSS parsing](#improvements-to-styled-jsx-css-parsing)
* [next/image changed wrapping element](#nextimage-changed-wrapping-element)
* [HMR connection now uses a WebSocket](#hmr-connection-now-uses-a-websocket)
* [Webpack 4 support has been removed](#webpack-4-support-has-been-removed)
* [target option deprecated](#target-option-deprecated)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/10-upgrading/07-version-12.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)[Upgrading](/docs/pages/building-your-application/upgrading)Version 12
Version 12
==========
To upgrade to version 12, run the following command:
Terminal
npm i next@12 react@17 react-dom@17 eslint-config-next@12
Terminal
yarn add next@12 react@17 react-dom@17 eslint-config-next@12
Terminal
pnpm up next@12 react@17 react-dom@17 eslint-config-next@12
Terminal
bun add next@12 react@17 react-dom@17 eslint-config-next@12
> **Good to know:** If you are using TypeScript, ensure you also upgrade `@types/react` and `@types/react-dom` to their corresponding versions.
### [Upgrading to 12.2](#upgrading-to-122)
[Middleware](/docs/messages/middleware-upgrade-guide) - If you were using Middleware prior to `12.2`, please see the [upgrade guide](/docs/messages/middleware-upgrade-guide) for more information.
### [Upgrading to 12.0](#upgrading-to-120)
[Minimum Node.js Version](https://nodejs.org/en/) - The minimum Node.js version has been bumped from `12.0.0` to `12.22.0` which is the first version of Node.js with native ES Modules support.
[Minimum React Version](https://react.dev/learn/add-react-to-an-existing-project) - The minimum required React version is `17.0.2`. To upgrade you can run the following command in the terminal:
Terminal
npm install react@latest react-dom@latest
yarn add react@latest react-dom@latest
pnpm update react@latest react-dom@latest
bun add react@latest react-dom@latest
#### [SWC replacing Babel](#swc-replacing-babel)
Next.js now uses the Rust-based compiler [SWC](https://swc.rs/) to compile JavaScript/TypeScript. This new compiler is up to 17x faster than Babel when compiling individual files and up to 5x faster Fast Refresh.
Next.js provides full backward compatibility with applications that have [custom Babel configuration](/docs/pages/building-your-application/configuring/babel). All transformations that Next.js handles by default like styled-jsx and tree-shaking of `getStaticProps` / `getStaticPaths` / `getServerSideProps` have been ported to Rust.
When an application has a custom Babel configuration, Next.js will automatically opt-out of using SWC for compiling JavaScript/Typescript and will fall back to using Babel in the same way that it was used in Next.js 11.
Many of the integrations with external libraries that currently require custom Babel transformations will be ported to Rust-based SWC transforms in the near future. These include but are not limited to:
* Styled Components
* Emotion
* Relay
In order to prioritize transforms that will help you adopt SWC, please provide your `.babelrc` on [this feedback thread](https://github.com/vercel/next.js/discussions/30174).
#### [SWC replacing Terser for minification](#swc-replacing-terser-for-minification)
You can opt-in to replacing Terser with SWC for minifying JavaScript up to 7x faster using a flag in `next.config.js`:
next.config.js
module.exports = {
swcMinify: true,
}
Minification using SWC is an opt-in flag to ensure it can be tested against more real-world Next.js applications before it becomes the default in Next.js 12.1. If you have feedback about minification, please leave it on [this feedback thread](https://github.com/vercel/next.js/discussions/30237).
#### [Improvements to styled-jsx CSS parsing](#improvements-to-styled-jsx-css-parsing)
On top of the Rust-based compiler we've implemented a new CSS parser based on the one used for the styled-jsx Babel transform. This new parser has improved handling of CSS and now errors when invalid CSS is used that would previously slip through and cause unexpected behavior.
Because of this change invalid CSS will throw an error during development and `next build`. This change only affects styled-jsx usage.
#### [`next/image` changed wrapping element](#nextimage-changed-wrapping-element)
`next/image` now renders the `<img>` inside a `<span>` instead of `<div>`.
If your application has specific CSS targeting span such as `.container span`, upgrading to Next.js 12 might incorrectly match the wrapping element inside the `<Image>` component. You can avoid this by restricting the selector to a specific class such as `.container span.item` and updating the relevant component with that className, such as `<span className="item" />`.
If your application has specific CSS targeting the `next/image` `<div>` tag, for example `.container div`, it may not match anymore. You can update the selector `.container span`, or preferably, add a new `<div className="wrapper">` wrapping the `<Image>` component and target that instead such as `.container .wrapper`.
The `className` prop is unchanged and will still be passed to the underlying `<img>` element.
See the [documentation](/docs/pages/building-your-application/optimizing/images#styling) for more info.
#### [HMR connection now uses a WebSocket](#hmr-connection-now-uses-a-websocket)
Previously, Next.js used a [server-sent events](https://developer.mozilla.org/docs/Web/API/Server-sent_events) connection to receive HMR events. Next.js 12 now uses a WebSocket connection.
In some cases when proxying requests to the Next.js dev server, you will need to ensure the upgrade request is handled correctly. For example, in `nginx` you would need to add the following configuration:
location /_next/webpack-hmr {
proxy_pass http://localhost:3000/_next/webpack-hmr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
If you are using Apache (2.x), you can add the following configuration to enable web sockets to the server. Review the port, host name and server names.
<VirtualHost *:443>
# ServerName yourwebsite.local
ServerName "${WEBSITE_SERVER_NAME}"
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
# Next.js 12 uses websocket
<Location /_next/webpack-hmr>
RewriteEngine On
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule /(.*) ws://localhost:3000/_next/webpack-hmr/$1 [P,L]
ProxyPass ws://localhost:3000/_next/webpack-hmr retry=0 timeout=30
ProxyPassReverse ws://localhost:3000/_next/webpack-hmr
</Location>
</VirtualHost>
For custom servers, such as `express`, you may need to use `app.all` to ensure the request is passed correctly, for example:
app.all('/_next/webpack-hmr', (req, res) => {
nextjsRequestHandler(req, res)
})
#### [Webpack 4 support has been removed](#webpack-4-support-has-been-removed)
If you are already using webpack 5 you can skip this section.
Next.js has adopted webpack 5 as the default for compilation in Next.js 11. As communicated in the [webpack 5 upgrading documentation](/docs/messages/webpack5) Next.js 12 removes support for webpack 4.
If your application is still using webpack 4 using the opt-out flag, you will now see an error linking to the [webpack 5 upgrading documentation](/docs/messages/webpack5).
#### [`target` option deprecated](#target-option-deprecated)
If you do not have `target` in `next.config.js` you can skip this section.
The target option has been deprecated in favor of built-in support for tracing what dependencies are needed to run a page.
During `next 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.
If you are currently using the `target` option set to `serverless`, please read the [documentation on how to leverage the new output](/docs/pages/api-reference/next-config-js/output).
[Previous
Version 13
](/docs/pages/building-your-application/upgrading/version-13)
[Next
Version 11
](/docs/pages/building-your-application/upgrading/version-11)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f5cb9fbca73062a253d38062e345e9b4\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/version-13 | 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
* [Upgrading from 12 to 13](#upgrading-from-12-to-13)
* [v13 Summary](#v13-summary)
* [Migrating shared features](#migrating-shared-features)
* [<Image/> Component](#image-component)
* [<Link> Component](#link-component)
* [<Script> Component](#script-component)
* [Font Optimization](#font-optimization)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/10-upgrading/06-version-13.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)[Upgrading](/docs/pages/building-your-application/upgrading)Version 13
Version 13
==========
[Upgrading from 12 to 13](#upgrading-from-12-to-13)
---------------------------------------------------
To update to Next.js version 13, run the following command using your preferred package manager:
Terminal
npm i next@13 react@latest react-dom@latest eslint-config-next@13
Terminal
yarn add next@13 react@latest react-dom@latest eslint-config-next@13
Terminal
pnpm i next@13 react@latest react-dom@latest eslint-config-next@13
Terminal
bun add next@13 react@latest react-dom@latest eslint-config-next@13
> **Good to know:** If you are using TypeScript, ensure you also upgrade `@types/react` and `@types/react-dom` to their latest versions.
### [v13 Summary](#v13-summary)
* The [Supported Browsers](/docs/architecture/supported-browsers) have been changed to drop Internet Explorer and target modern browsers.
* The minimum Node.js version has been bumped from 12.22.0 to 16.14.0, since 12.x and 14.x have reached end-of-life.
* The minimum React version has been bumped from 17.0.2 to 18.2.0.
* The `swcMinify` configuration property was changed from `false` to `true`. See [Next.js Compiler](/docs/architecture/nextjs-compiler) for more info.
* The `next/image` import was renamed to `next/legacy/image`. The `next/future/image` import was renamed to `next/image`. A [codemod is available](/docs/pages/building-your-application/upgrading/codemods#next-image-to-legacy-image) to safely and automatically rename your imports.
* The `next/link` child can no longer be `<a>`. Add the `legacyBehavior` prop to use the legacy behavior or remove the `<a>` to upgrade. A [codemod is available](/docs/pages/building-your-application/upgrading/codemods#new-link) to automatically upgrade your code.
* The `target` configuration property has been removed and superseded by [Output File Tracing](/docs/pages/api-reference/next-config-js/output).
[Migrating shared features](#migrating-shared-features)
-------------------------------------------------------
Next.js 13 introduces a new [`app` directory](/docs/app/building-your-application/routing) with new features and conventions. However, upgrading to Next.js 13 does **not** require using the new [`app` directory](/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 many 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.
Starting in Next.js 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](/docs/pages/building-your-application/upgrading/codemods#next-image-to-legacy-image): This codemod will safely and automatically rename `next/image` imports to `next/legacy/image` to maintain the same behavior as Next.js 12. We recommend running this codemod to quickly update to Next.js 13 automatically.
* [next-image-experimental](/docs/pages/building-your-application/upgrading/codemods#next-image-experimental): After running the previous codemod, you can optionally run this experimental codemod to upgrade `next/legacy/image` to the new `next/image`, which will remove unused props and add inline styles. Please note this codemod is experimental and only covers static usage (such as `<Image src={img} layout="responsive" />`) but not dynamic usage (such as `<Image {...props} />`).
Alternatively, you can manually update by following the [migration guide](/docs/pages/building-your-application/upgrading/codemods#next-image-experimental) and also see the [legacy comparison](/docs/pages/api-reference/components/image-legacy#comparison).
### [`<Link>` Component](#link-component)
The [`<Link>` Component](/docs/pages/api-reference/components/link) 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/pages/building-your-application/upgrading/codemods#new-link).
### [`<Script>` Component](#script-component)
The behavior of [`next/script`](/docs/pages/api-reference/components/script) has been updated to support both `pages` and `app`. If incrementally adopting `app`, read the [upgrade guide](/docs/pages/building-your-application/upgrading).
### [Font Optimization](#font-optimization)
Previously, Next.js helped you optimize fonts by inlining font CSS. Version 13 introduces the new [`next/font`](/docs/pages/building-your-application/optimizing/fonts) module which gives you the ability to customize your font loading experience while still ensuring great performance and privacy.
See [Optimizing Fonts](/docs/pages/building-your-application/optimizing/fonts) to learn how to use `next/font`.
[Previous
Version 14
](/docs/pages/building-your-application/upgrading/version-14)
[Next
Version 12
](/docs/pages/building-your-application/upgrading/version-12)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"88d3c49f8c6185cc0f3fbb8b86cc85ea\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/api-reference/components/font | 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
* [Font Function Arguments](#font-function-arguments)
* [src](#src)
* [weight](#weight)
* [style](#style)
* [subsets](#subsets)
* [axes](#axes)
* [display](#display)
* [preload](#preload)
* [fallback](#fallback)
* [adjustFontFallback](#adjustfontfallback)
* [variable](#variable)
* [declarations](#declarations)
* [Applying Styles](#applying-styles)
* [className](#classname)
* [style](#style-1)
* [CSS Variables](#css-variables)
* [Using a font definitions file](#using-a-font-definitions-file)
* [Version Changes](#version-changes)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/02-api-reference/01-components/font.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/api-reference)[Components](/docs/pages/api-reference/components)Font
Font Module
===========
This API reference will help you understand how to use [`next/font/google`](/docs/app/building-your-application/optimizing/fonts#google-fonts) and [`next/font/local`](/docs/app/building-your-application/optimizing/fonts#local-fonts). For features and usage, please see the [Optimizing Fonts](/docs/app/building-your-application/optimizing/fonts) page.
### [Font Function Arguments](#font-function-arguments)
For usage, review [Google Fonts](/docs/app/building-your-application/optimizing/fonts#google-fonts) and [Local Fonts](/docs/app/building-your-application/optimizing/fonts#local-fonts).
Key
`font/google`
`font/local`
Type
Required
[`src`](#src)
String or Array of Objects
Yes
[`weight`](#weight)
String or Array
Required/Optional
[`style`](#style)
String or Array
\-
[`subsets`](#subsets)
Array of Strings
\-
[`axes`](#axes)
Array of Strings
\-
[`display`](#display)
String
\-
[`preload`](#preload)
Boolean
\-
[`fallback`](#fallback)
Array of Strings
\-
[`adjustFontFallback`](#adjustfontfallback)
Boolean or String
\-
[`variable`](#variable)
String
\-
[`declarations`](#declarations)
Array of Objects
\-
### [`src`](#src)
The path of the font file as a string or an array of objects (with type `Array<{path: string, weight?: string, style?: string}>`) relative to the directory where the font loader function is called.
Used in `next/font/local`
* Required
Examples:
* `src:'./fonts/my-font.woff2'` where `my-font.woff2` is placed in a directory named `fonts` inside the `app` directory
* `src:[{path: './inter/Inter-Thin.ttf', weight: '100',},{path: './inter/Inter-Regular.ttf',weight: '400',},{path: './inter/Inter-Bold-Italic.ttf', weight: '700',style: 'italic',},]`
* if the font loader function is called in `app/page.tsx` using `src:'../styles/fonts/my-font.ttf'`, then `my-font.ttf` is placed in `styles/fonts` at the root of the project
### [`weight`](#weight)
The font [`weight`](https://fonts.google.com/knowledge/glossary/weight) with the following possibilities:
* A string with possible values of the weights available for the specific font or a range of values if it's a [variable](https://fonts.google.com/variablefonts) font
* An array of weight values if the font is not a [variable google font](https://fonts.google.com/variablefonts). It applies to `next/font/google` only.
Used in `next/font/google` and `next/font/local`
* Required if the font being used is **not** [variable](https://fonts.google.com/variablefonts)
Examples:
* `weight: '400'`: A string for a single weight value - for the font [`Inter`](https://fonts.google.com/specimen/Inter?query=inter), the possible values are `'100'`, `'200'`, `'300'`, `'400'`, `'500'`, `'600'`, `'700'`, `'800'`, `'900'` or `'variable'` where `'variable'` is the default)
* `weight: '100 900'`: A string for the range between `100` and `900` for a variable font
* `weight: ['100','400','900']`: An array of 3 possible values for a non variable font
### [`style`](#style)
The font [`style`](https://developer.mozilla.org/docs/Web/CSS/font-style) with the following possibilities:
* A string [value](https://developer.mozilla.org/docs/Web/CSS/font-style#values) with default value of `'normal'`
* An array of style values if the font is not a [variable google font](https://fonts.google.com/variablefonts). It applies to `next/font/google` only.
Used in `next/font/google` and `next/font/local`
* Optional
Examples:
* `style: 'italic'`: A string - it can be `normal` or `italic` for `next/font/google`
* `style: 'oblique'`: A string - it can take any value for `next/font/local` but is expected to come from [standard font styles](https://developer.mozilla.org/docs/Web/CSS/font-style)
* `style: ['italic','normal']`: An array of 2 values for `next/font/google` - the values are from `normal` and `italic`
### [`subsets`](#subsets)
The font [`subsets`](https://fonts.google.com/knowledge/glossary/subsetting) defined by an array of string values with the names of each subset you would like to be [preloaded](/docs/app/building-your-application/optimizing/fonts#specifying-a-subset). Fonts specified via `subsets` will have a link preload tag injected into the head when the [`preload`](#preload) option is true, which is the default.
Used in `next/font/google`
* Optional
Examples:
* `subsets: ['latin']`: An array with the subset `latin`
You can find a list of all subsets on the Google Fonts page for your font.
### [`axes`](#axes)
Some variable fonts have extra `axes` that can be included. By default, only the font weight is included to keep the file size down. The possible values of `axes` depend on the specific font.
Used in `next/font/google`
* Optional
Examples:
* `axes: ['slnt']`: An array with value `slnt` for the `Inter` variable font which has `slnt` as additional `axes` as shown [here](https://fonts.google.com/variablefonts?vfquery=inter#font-families). You can find the possible `axes` values for your font by using the filter on the [Google variable fonts page](https://fonts.google.com/variablefonts#font-families) and looking for axes other than `wght`
### [`display`](#display)
The font [`display`](https://developer.mozilla.org/docs/Web/CSS/@font-face/font-display) with possible string [values](https://developer.mozilla.org/docs/Web/CSS/@font-face/font-display#values) of `'auto'`, `'block'`, `'swap'`, `'fallback'` or `'optional'` with default value of `'swap'`.
Used in `next/font/google` and `next/font/local`
* Optional
Examples:
* `display: 'optional'`: A string assigned to the `optional` value
### [`preload`](#preload)
A boolean value that specifies whether the font should be [preloaded](/docs/app/building-your-application/optimizing/fonts#preloading) or not. The default is `true`.
Used in `next/font/google` and `next/font/local`
* Optional
Examples:
* `preload: false`
### [`fallback`](#fallback)
The fallback font to use if the font cannot be loaded. An array of strings of fallback fonts with no default.
* Optional
Used in `next/font/google` and `next/font/local`
Examples:
* `fallback: ['system-ui', 'arial']`: An array setting the fallback fonts to `system-ui` or `arial`
### [`adjustFontFallback`](#adjustfontfallback)
* For `next/font/google`: A boolean value that sets whether an automatic fallback font should be used to reduce [Cumulative Layout Shift](https://web.dev/cls/). The default is `true`.
* For `next/font/local`: A string or boolean `false` value that sets whether an automatic fallback font should be used to reduce [Cumulative Layout Shift](https://web.dev/cls/). The possible values are `'Arial'`, `'Times New Roman'` or `false`. The default is `'Arial'`.
Used in `next/font/google` and `next/font/local`
* Optional
Examples:
* `adjustFontFallback: false`: for `next/font/google`
* `adjustFontFallback: 'Times New Roman'`: for `next/font/local`
### [`variable`](#variable)
A string value to define the CSS variable name to be used if the style is applied with the [CSS variable method](#css-variables).
Used in `next/font/google` and `next/font/local`
* Optional
Examples:
* `variable: '--my-font'`: The CSS variable `--my-font` is declared
### [`declarations`](#declarations)
An array of font face [descriptor](https://developer.mozilla.org/docs/Web/CSS/@font-face#descriptors) key-value pairs that define the generated `@font-face` further.
Used in `next/font/local`
* Optional
Examples:
* `declarations: [{ prop: 'ascent-override', value: '90%' }]`
[Applying Styles](#applying-styles)
-----------------------------------
You can apply the font styles in three ways:
* [`className`](#classname)
* [`style`](#style-1)
* [CSS Variables](#css-variables)
### [`className`](#classname)
Returns a read-only CSS `className` for the loaded font to be passed to an HTML element.
<p className={inter.className}>Hello, Next.js!</p>
### [`style`](#style-1)
Returns a read-only CSS `style` object for the loaded font to be passed to an HTML element, including `style.fontFamily` to access the font family name and fallback fonts.
<p style={inter.style}>Hello World</p>
### [CSS Variables](#css-variables)
If you would like to set your styles in an external style sheet and specify additional options there, use the CSS variable method.
In addition to importing the font, also import the CSS file where the CSS variable is defined and set the variable option of the font loader object as follows:
app/page.tsx
TypeScript
JavaScriptTypeScript
import { Inter } from 'next/font/google'
import styles from '../styles/component.module.css'
const inter = Inter({
variable: '--font-inter',
})
To use the font, set the `className` of the parent container of the text you would like to style to the font loader's `variable` value and the `className` of the text to the `styles` property from the external CSS file.
app/page.tsx
TypeScript
JavaScriptTypeScript
<main className={inter.variable}>
<p className={styles.text}>Hello World</p>
</main>
Define the `text` selector class in the `component.module.css` CSS file as follows:
styles/component.module.css
.text {
font-family: var(--font-inter);
font-weight: 200;
font-style: italic;
}
In the example above, the text `Hello World` is styled using the `Inter` font and the generated font fallback with `font-weight: 200` and `font-style: italic`.
[Using a font definitions file](#using-a-font-definitions-file)
---------------------------------------------------------------
Every time you call the `localFont` or Google font function, that font will be hosted as one instance in your application. Therefore, if you need to use the same font in multiple places, you should load it in one place and import the related font object where you need it. This is done using a font definitions file.
For example, create a `fonts.ts` file in a `styles` folder at the root of your app directory.
Then, specify your font definitions as follows:
styles/fonts.ts
TypeScript
JavaScriptTypeScript
import { Inter, Lora, Source_Sans_3 } from 'next/font/google'
import localFont from 'next/font/local'
// define your variable fonts
const inter = Inter()
const lora = Lora()
// define 2 weights of a non-variable font
const sourceCodePro400 = Source_Sans_3({ weight: '400' })
const sourceCodePro700 = Source_Sans_3({ weight: '700' })
// define a custom local font where GreatVibes-Regular.ttf is stored in the styles folder
const greatVibes = localFont({ src: './GreatVibes-Regular.ttf' })
export { inter, lora, sourceCodePro400, sourceCodePro700, greatVibes }
You can now use these definitions in your code as follows:
app/page.tsx
TypeScript
JavaScriptTypeScript
import { inter, lora, sourceCodePro700, greatVibes } from '../styles/fonts'
export default function Page() {
return (
<div>
<p className={inter.className}>Hello world using Inter font</p>
<p style={lora.style}>Hello world using Lora font</p>
<p className={sourceCodePro700.className}>
Hello world using Source_Sans_3 font with weight 700
</p>
<p className={greatVibes.className}>My title in Great Vibes font</p>
</div>
)
}
To make it easier to access the font definitions in your code, you can define a path alias in your `tsconfig.json` or `jsconfig.json` files as follows:
tsconfig.json
{
"compilerOptions": {
"paths": {
"@/fonts": ["./styles/fonts"]
}
}
}
You can now import any font definition as follows:
app/about/page.tsx
TypeScript
JavaScriptTypeScript
import { greatVibes, sourceCodePro400 } from '@/fonts'
[Version Changes](#version-changes)
-----------------------------------
Version
Changes
`v13.2.0`
`@next/font` renamed to `next/font`. Installation no longer required.
`v13.0.0`
`@next/font` was added.
[Previous
Components
](/docs/pages/api-reference/components)
[Next
<Head>
](/docs/pages/api-reference/components/head)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"c7b02de60ede7a96ed9c18d20e3e7c3d\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/from-create-react-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
* [Why Switch?](#why-switch)
* [Slow initial page loading time](#slow-initial-page-loading-time)
* [No automatic code splitting](#no-automatic-code-splitting)
* [Network waterfalls](#network-waterfalls)
* [Fast and intentional loading states](#fast-and-intentional-loading-states)
* [Choose the data fetching strategy](#choose-the-data-fetching-strategy)
* [Middleware](#middleware)
* [Built-in Optimizations](#built-in-optimizations)
* [Migration Steps](#migration-steps)
* [Step 1: Install the Next.js Dependency](#step-1-install-the-nextjs-dependency)
* [Step 2: Create the Next.js Configuration File](#step-2-create-the-nextjs-configuration-file)
* [Step 3: Update TypeScript Configuration](#step-3-update-typescript-configuration)
* [Step 4: Create the Root Layout](#step-4-create-the-root-layout)
* [Step 5: Metadata](#step-5-metadata)
* [Step 6: Styles](#step-6-styles)
* [Step 7: Create the Entrypoint Page](#step-7-create-the-entrypoint-page)
* [Step 8: Update Static Image Imports](#step-8-update-static-image-imports)
* [Step 9: Migrate the Environment Variables](#step-9-migrate-the-environment-variables)
* [Step 10: Update Scripts in package.json](#step-10-update-scripts-in-packagejson)
* [Step 11: Clean Up](#step-11-clean-up)
* [Bundler Compatibility](#bundler-compatibility)
* [Next Steps](#next-steps)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/10-upgrading/04-from-create-react-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)[Upgrading](/docs/pages/building-your-application/upgrading)Migrating from Create React App
Migrating from Create React App
===============================
This guide will help you migrate an existing Create React App site to Next.js.
[Why Switch?](#why-switch)
--------------------------
There are several reasons why you might want to switch from Create React App to Next.js:
### [Slow initial page loading time](#slow-initial-page-loading-time)
Create React App uses purely client-side React. Client-side only applications, also known as single-page applications (SPAs), often experience slow initial page loading time. This happens due to a couple of reasons:
1. The browser needs to wait for the React code and your entire application bundle to download and run before your code is able to send requests to load data.
2. Your application code grows with every new feature and dependency you add.
### [No automatic code splitting](#no-automatic-code-splitting)
The previous issue of slow loading times can be somewhat managed with code splitting. However, if you try to do code splitting manually, you'll often make performance worse. It's easy to inadvertently introduce network waterfalls when code-splitting manually. Next.js provides automatic code splitting built into its router.
### [Network waterfalls](#network-waterfalls)
A common cause of poor performance occurs when applications make sequential client-server requests to fetch data. One common pattern for data fetching in an SPA is to initially render a placeholder, and then fetch data after the component has mounted. Unfortunately, this means that a child component that fetches data can't start fetching until the parent component has finished loading its own data.
While fetching data on the client is supported with Next.js, it also gives you the option to shift data fetching to the server, which can eliminate client-server waterfalls.
### [Fast and intentional loading states](#fast-and-intentional-loading-states)
With built-in support for [streaming through React Suspense](/docs/app/building-your-application/routing/loading-ui-and-streaming#streaming-with-suspense), you can be more intentional about which parts of your UI you want to load first and in what order without introducing network waterfalls.
This enables you to build pages that are faster to load and eliminate [layout shifts](https://vercel.com/blog/how-core-web-vitals-affect-seo).
### [Choose the data fetching strategy](#choose-the-data-fetching-strategy)
Depending on your needs, Next.js allows you to choose your data fetching strategy on a page and component basis. You can decide to fetch at build time, at request time on the server, or on the client. For example, you can fetch data from your CMS and render your blog posts at build time, which can then be efficiently cached on a CDN.
### [Middleware](#middleware)
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. This is especially useful to avoid having a flash of unauthenticated content when the user visits an authenticated-only page by redirecting the user to a login page. The middleware is also useful for experimentation and [internationalization](/docs/app/building-your-application/routing/internationalization).
### [Built-in Optimizations](#built-in-optimizations)
[Images](/docs/app/building-your-application/optimizing/images), [fonts](/docs/app/building-your-application/optimizing/fonts), and [third-party scripts](/docs/app/building-your-application/optimizing/scripts) often have significant impact on an application's performance. Next.js comes with built-in components that automatically optimize those for you.
[Migration Steps](#migration-steps)
-----------------------------------
Our goal with this migration is to get a working Next.js application as quickly as possible, so that you can then adopt Next.js features incrementally. To begin with, we'll keep it as a purely client-side application (SPA) without migrating your existing router. This helps minimize the chances of encountering issues during the migration process and reduces merge conflicts.
### [Step 1: Install the Next.js Dependency](#step-1-install-the-nextjs-dependency)
The first thing you need to do is to install `next` as a dependency:
Terminal
npm install next@latest
### [Step 2: Create the Next.js Configuration File](#step-2-create-the-nextjs-configuration-file)
Create a `next.config.mjs` at the root of your project. This file will hold your [Next.js configuration options](/docs/app/api-reference/next-config-js).
next.config.mjs
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'export', // Outputs a Single-Page Application (SPA).
distDir: './dist', // Changes the build output directory to `./dist/`.
}
export default nextConfig
### [Step 3: Update TypeScript Configuration](#step-3-update-typescript-configuration)
If you're using TypeScript, you need to update your `tsconfig.json` file with the following changes to make it compatible with Next.js:
tsconfig.json
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"baseUrl": ".",
"incremental": true,
"plugins": [
{
"name": "next"
}
],
"strictNullChecks": true
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"./dist/types/**/*.ts"
],
"exclude": ["node_modules"]
}
You can find more information about configuring TypeScript on the [Next.js docs](/docs/app/building-your-application/configuring/typescript#typescript-plugin).
### [Step 4: Create the Root Layout](#step-4-create-the-root-layout)
A Next.js [App Router](/docs/app) application must include a [root layout](/docs/app/building-your-application/routing/pages-and-layouts#root-layout-required) file, which is a [React Server Component](/docs/app/building-your-application/rendering/server-components) that will wrap all pages in your application. This file is defined at the top level of the `app` directory.
The closest equivalent to the root layout file in a CRA application is the `index.html` file, which contains your `<html>`, `<head>`, and `<body>` tags.
In this step, you'll convert your `index.html` file into a root layout file:
1. Create a new `app` directory in your `src` directory.
2. Create a new `layout.tsx` file inside that `app` directory:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return null
}
> **Good to know**: `.js`, `.jsx`, or `.tsx` extensions can be used for Layout files.
Copy the content of your `index.html` file into the previously created `<RootLayout>` component while replacing the `body.div#root` and `body.script` tags with `<div id="root">{children}</div>`:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>React App</title>
<meta name="description" content="Web site created..." />
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
> **Good to know**: We'll ignore the [manifest file](/docs/app/api-reference/file-conventions/metadata), additional iconography other than the favicon, and [testing configuration](/docs/app/building-your-application/testing), but if these are requirements, Next.js also supports these options.
### [Step 5: Metadata](#step-5-metadata)
Next.js already includes by default the [meta charset](https://developer.mozilla.org/docs/Web/HTML/Element/meta#charset) and [meta viewport](https://developer.mozilla.org/docs/Web/HTML/Viewport_meta_tag) tags, so you can safely remove those from your `<head>`:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<title>React App</title>
<meta name="description" content="Web site created..." />
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
Any [metadata files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) such as `favicon.ico`, `icon.png`, `robots.txt` are automatically added to the application `<head>` tag as long as you have them placed into the top level of the `app` directory. After moving [all supported files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) into the `app` directory you can safely delete their `<link>` tags:
app/layout.tsx
TypeScript
JavaScriptTypeScript
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<head>
<title>React App</title>
<meta name="description" content="Web site created..." />
</head>
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
Finally, Next.js can manage your last `<head>` tags with the [Metadata API](/docs/app/building-your-application/optimizing/metadata). Move your final metadata info into an exported [`metadata` object](/docs/app/api-reference/functions/generate-metadata#metadata-object):
app/layout.tsx
TypeScript
JavaScriptTypeScript
import type { Metadata } from 'next'
export const metadata: Metadata = {
title: 'React App',
description: 'Web site created with Next.js.',
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>
<div id="root">{children}</div>
</body>
</html>
)
}
With the above changes, you shifted from declaring everything in your `index.html` to using Next.js' convention-based approach built into the framework ([Metadata API](/docs/app/building-your-application/optimizing/metadata)). This approach enables you to more easily improve your SEO and web shareability of your pages.
### [Step 6: Styles](#step-6-styles)
Like Create React App, Next.js has built-in support for [CSS Modules](/docs/app/building-your-application/styling/css-modules).
If you're using a global CSS file, import it into your `app/layout.tsx` file:
app/layout.tsx
TypeScript
JavaScriptTypeScript
import '../index.css'
// ...
If you're using Tailwind, you'll need to install `postcss` and `autoprefixer`:
Terminal
npm install postcss autoprefixer
Then, create a `postcss.config.js` file at the root of your project:
postcss.config.js
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
}
### [Step 7: Create the Entrypoint Page](#step-7-create-the-entrypoint-page)
On Next.js you declare an entrypoint for your application by creating a `page.tsx` file. The closest equivalent of this file on CRA is your `src/index.tsx` file. In this step, you’ll set up the entry point of your application.
**Create a `[[...slug]]` directory in your `app` directory.**
Since this guide is aiming to first set up our Next.js as an SPA (Single Page Application), you need your page entry point to catch all possible routes of your application. For that, create a new `[[...slug]]` directory in your `app` directory.
This directory is what is called an [optional catch-all route segment](/docs/app/building-your-application/routing/dynamic-routes#optional-catch-all-segments). Next.js uses a file-system based router where [directories are used to define routes](/docs/app/building-your-application/routing/defining-routes#creating-routes). This special directory will make sure that all routes of your application will be directed to its containing `page.tsx` file.
**Create a new `page.tsx` file inside the `app/[[...slug]]` directory with the following content:**
app/\[\[...slug\]\]/page.tsx
TypeScript
JavaScriptTypeScript
import '../../index.css'
export function generateStaticParams() {
return [{ slug: [''] }]
}
export default function Page() {
return '...' // We'll update this
}
This file is a [Server Component](/docs/app/building-your-application/rendering/server-components). When you run `next build`, the file is prerendered into a static asset. It does _not_ require any dynamic code.
This file imports our global CSS and tells [`generateStaticParams`](/docs/app/api-reference/functions/generate-static-params) we are only going to generate one route, the index route at `/`.
Now, let's move the rest of our CRA application which will run client-only.
app/\[\[...slug\]\]/client.tsx
TypeScript
JavaScriptTypeScript
'use client'
import React from 'react'
import dynamic from 'next/dynamic'
const App = dynamic(() => import('../../App'), { ssr: false })
export function ClientOnly() {
return <App />
}
This file is a [Client Component](/docs/app/building-your-application/rendering/client-components), defined by the `'use client'` directive. Client Components are still [prerendered to HTML](/docs/app/building-your-application/rendering/client-components#how-are-client-components-rendered) on the server before being sent to the client.
Since we want a client-only application to start, we can configure Next.js to disable prerendering from the `App` component down.
const App = dynamic(() => import('../../App'), { ssr: false })
Now, update your entrypoint page to use the new component:
app/\[\[...slug\]\]/page.tsx
TypeScript
JavaScriptTypeScript
import '../../index.css'
import { ClientOnly } from './client'
export function generateStaticParams() {
return [{ slug: [''] }]
}
export default function Page() {
return <ClientOnly />
}
### [Step 8: Update Static Image Imports](#step-8-update-static-image-imports)
Next.js handles static image imports slightly different from CRA. With CRA, importing an image file will return its public URL as a string:
App.tsx
import image from './img.png'
export default function App() {
return <img src={image} />
}
With Next.js, static image imports return an object. The object can then be used directly with the Next.js [`<Image>` component](/docs/app/api-reference/components/image), or you can use the object's `src` property with your existing `<img>` tag.
The `<Image>` component has the added benefits of [automatic image optimization](/docs/app/building-your-application/optimizing/images). The `<Image>` component automatically sets the `width` and `height` attributes of the resulting `<img>` based on the image's dimensions. This prevents layout shifts when the image loads. However, this can cause issues if your app contains images with only one of their dimensions being styled without the other styled to `auto`. When not styled to `auto`, the dimension will default to the `<img>` dimension attribute's value, which can cause the image to appear distorted.
Keeping the `<img>` tag will reduce the amount of changes in your application and prevent the above issues. You can then optionally later migrate to the `<Image>` component to take advantage of optimizing images by [configuring a loader](/docs/app/building-your-application/optimizing/images#loaders), or moving to the default Next.js server which has automatic image optimization.
**Convert absolute import paths for images imported from `/public` into relative imports:**
// Before
import logo from '/logo.png'
// After
import logo from '../public/logo.png'
**Pass the image `src` property instead of the whole image object to your `<img>` tag:**
// Before
<img src={logo} />
// After
<img src={logo.src} />
Alternatively, you can reference the public URL for the image asset based on the filename. For example, `public/logo.png` will serve the image at `/logo.png` for your application, which would be the `src` value.
> **Warning:** If you're using TypeScript, you might encounter type errors when accessing the `src` property. You can safely ignore those for now. They will be fixed by the end of this guide.
### [Step 9: Migrate the Environment Variables](#step-9-migrate-the-environment-variables)
Next.js has support for `.env` [environment variables](/docs/app/building-your-application/configuring/environment-variables) similar to CRA.
The main difference is the prefix used to expose environment variables on the client-side. Change all environment variables with the `REACT_APP_` prefix to `NEXT_PUBLIC_`.
### [Step 10: Update Scripts in `package.json`](#step-10-update-scripts-in-packagejson)
You should now be able to run your application to test if you successfully migrated to Next.js. But before that, you need to update your `scripts` in your `package.json` with Next.js related commands, and add `.next`, `next-env.d.ts`, and `dist` to your `.gitignore` file:
package.json
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}
.gitignore
# ...
.next
next-env.d.ts
dist
Now run `npm run dev`, and open [`http://localhost:3000`](http://localhost:3000). You should see your application now running on Next.js.
### [Step 11: Clean Up](#step-11-clean-up)
You can now clean up your codebase from Create React App related artifacts:
* Delete `src/index.tsx`
* Delete `public/index.html`
* Delete `reportWebVitals` setup
* Uninstall CRA dependencies (`react-scripts`)
[Bundler Compatibility](#bundler-compatibility)
-----------------------------------------------
Create React App and Next.js both default to using webpack for bundling.
When migrating your CRA application to Next.js, you might have a custom webpack configuration you're looking to migrate. Next.js supports providing a [custom webpack configuration](/docs/app/api-reference/next-config-js/webpack).
Further, Next.js has support for [Turbopack](/docs/app/api-reference/next-config-js/turbo) through `next dev --turbo` to improve your local dev performance. Turbopack supports some [webpack loaders](/docs/app/api-reference/next-config-js/turbo) as well for compatibility and incremental adoption.
[Next Steps](#next-steps)
-------------------------
If everything went according to plan, you now have a functioning Next.js application running as a single-page application. However, you aren't yet taking advantage of most of Next.js' benefits, but you can now start making incremental changes to reap all the benefits. Here's what you might want to do next:
* Migrate from React Router to the [Next.js App Router](/docs/app/building-your-application/routing) to get:
* Automatic code splitting
* [Streaming Server-Rendering](/docs/app/building-your-application/routing/loading-ui-and-streaming)
* [React Server Components](/docs/app/building-your-application/rendering/server-components)
* [Optimize images with the `<Image>` component](/docs/app/building-your-application/optimizing/images)
* [Optimize fonts with `next/font`](/docs/app/building-your-application/optimizing/fonts)
* [Optimize third-party scripts with the `<Script>` component](/docs/app/building-your-application/optimizing/scripts)
* [Update your ESLint configuration to support Next.js rules](/docs/app/building-your-application/configuring/eslint)
> **Good to know:** Using a static export [does not currently support](https://github.com/vercel/next.js/issues/54393) using the `useParams` hook.
[Previous
Migrating from Vite
](/docs/pages/building-your-application/upgrading/from-vite)
[Next
Version 14
](/docs/pages/building-your-application/upgrading/version-14)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"fcab0152a85751296b1b24bbfd916d16\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/api-reference/components | 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/02-api-reference/01-components/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)[API Reference](/docs/pages/api-reference)Components
Components
==========
[
### Font
API Reference for the Font Module
](/docs/pages/api-reference/components/font)[
### <Head>
Add custom elements to the \`head\` of your page with the built-in Head component.
](/docs/pages/api-reference/components/head)[
### <Image>
Optimize Images in your Next.js Application using the built-in \`next/image\` Component.
](/docs/pages/api-reference/components/image)[
### <Image> (Legacy)
Backwards compatible Image Optimization with the Legacy Image component.
](/docs/pages/api-reference/components/image-legacy)[
### <Link>
API reference for the <Link> component.
](/docs/pages/api-reference/components/link)[
### <Script>
Optimize third-party scripts in your Next.js application using the built-in \`next/script\` Component.
](/docs/pages/api-reference/components/script)
[Previous
API Reference
](/docs/pages/api-reference)
[Next
Font
](/docs/pages/api-reference/components/font)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"b944a7da5c329a054499e24513e7f1eb\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/version-11 | 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
* [Webpack 5](#webpack-5)
* [Cleaning the distDir is now a default](#cleaning-the-distdir-is-now-a-default)
* [PORT is now supported for next dev and next start](#port-is-now-supported-for-next-dev-and-next-start)
* [next.config.js customization to import images](#nextconfigjs-customization-to-import-images)
* [Remove super.componentDidCatch() from pages/\_app.js](#remove-supercomponentdidcatch-from-pages_appjs)
* [Remove Container from pages/\_app.js](#remove-container-from-pages_appjs)
* [Remove props.url usage from page components](#remove-propsurl-usage-from-page-components)
* [Remove unsized property on next/image](#remove-unsized-property-on-nextimage)
* [Remove modules property on next/dynamic](#remove-modules-property-on-nextdynamic)
* [Remove Head.rewind](#remove-headrewind)
* [Moment.js locales excluded by default](#momentjs-locales-excluded-by-default)
* [Update usage of router.events](#update-usage-of-routerevents)
* [React 16 to 17](#react-16-to-17)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/10-upgrading/08-version-11.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)[Upgrading](/docs/pages/building-your-application/upgrading)Version 11
Version 11
==========
To upgrade to version 11, run the following command:
Terminal
npm i next@11 react@17 react-dom@17
Terminal
yarn add next@11 react@17 react-dom@17
Terminal
pnpm up next@11 react@17 react-dom@17
Terminal
bun add next@11 react@17 react-dom@17
> **Good to know:** If you are using TypeScript, ensure you also upgrade `@types/react` and `@types/react-dom` to their corresponding versions.
### [Webpack 5](#webpack-5)
Webpack 5 is now the default for all Next.js applications. If you did not have a custom webpack configuration, your application is already using webpack 5. If you do have a custom webpack configuration, you can refer to the [Next.js webpack 5 documentation](/docs/messages/webpack5) for upgrade guidance.
### [Cleaning the `distDir` is now a default](#cleaning-the-distdir-is-now-a-default)
The build output directory (defaults to `.next`) is now cleared by default except for the Next.js caches. You can refer to [the cleaning `distDir` RFC](https://github.com/vercel/next.js/discussions/6009) for more information.
If your application was relying on this behavior previously you can disable the new default behavior by adding the `cleanDistDir: false` flag in `next.config.js`.
### [`PORT` is now supported for `next dev` and `next start`](#port-is-now-supported-for-next-dev-and-next-start)
Next.js 11 supports the `PORT` environment variable to set the port the application runs on. Using `-p`/`--port` is still recommended but if you were prohibited from using `-p` in any way you can now use `PORT` as an alternative:
Example:
PORT=4000 next start
### [`next.config.js` customization to import images](#nextconfigjs-customization-to-import-images)
Next.js 11 supports static image imports with `next/image`. This new feature relies on being able to process image imports. If you previously added the `next-images` or `next-optimized-images` packages you can either move to the new built-in support using `next/image` or disable the feature:
next.config.js
module.exports = {
images: {
disableStaticImages: true,
},
}
### [Remove `super.componentDidCatch()` from `pages/_app.js`](#remove-supercomponentdidcatch-from-pages_appjs)
The `next/app` component's `componentDidCatch` was deprecated in Next.js 9 as it's no longer needed and has since been a no-op. In Next.js 11, it was removed.
If your `pages/_app.js` has a custom `componentDidCatch` method you can remove `super.componentDidCatch` as it is no longer needed.
### [Remove `Container` from `pages/_app.js`](#remove-container-from-pages_appjs)
This export was deprecated in Next.js 9 as it's no longer needed and has since been a no-op with a warning during development. In Next.js 11 it was removed.
If your `pages/_app.js` imports `Container` from `next/app` you can remove `Container` as it was removed. Learn more in [the documentation](/docs/messages/app-container-deprecated).
### [Remove `props.url` usage from page components](#remove-propsurl-usage-from-page-components)
This property was deprecated in Next.js 4 and has since shown a warning during development. With the introduction of `getStaticProps` / `getServerSideProps` these methods already disallowed the usage of `props.url`. In Next.js 11, it was removed completely.
You can learn more in [the documentation](/docs/messages/url-deprecated).
### [Remove `unsized` property on `next/image`](#remove-unsized-property-on-nextimage)
The `unsized` property on `next/image` was deprecated in Next.js 10.0.1. You can use `layout="fill"` instead. In Next.js 11 `unsized` was removed.
### [Remove `modules` property on `next/dynamic`](#remove-modules-property-on-nextdynamic)
The `modules` and `render` option for `next/dynamic` were deprecated in Next.js 9.5. This was done in order to make the `next/dynamic` API closer to `React.lazy`. In Next.js 11, the `modules` and `render` options were removed.
This option hasn't been mentioned in the documentation since Next.js 8 so it's less likely that your application is using it.
If your application does use `modules` and `render` you can refer to [the documentation](/docs/messages/next-dynamic-modules).
### [Remove `Head.rewind`](#remove-headrewind)
`Head.rewind` has been a no-op since Next.js 9.5, in Next.js 11 it was removed. You can safely remove your usage of `Head.rewind`.
### [Moment.js locales excluded by default](#momentjs-locales-excluded-by-default)
Moment.js includes translations for a lot of locales by default. Next.js now automatically excludes these locales by default to optimize bundle size for applications using Moment.js.
To load a specific locale use this snippet:
import moment from 'moment'
import 'moment/locale/ja'
moment.locale('ja')
You can opt-out of this new default by adding `excludeDefaultMomentLocales: false` to `next.config.js` if you do not want the new behavior, do note it's highly recommended to not disable this new optimization as it significantly reduces the size of Moment.js.
### [Update usage of `router.events`](#update-usage-of-routerevents)
In case you're accessing `router.events` during rendering, in Next.js 11 `router.events` is no longer provided during pre-rendering. Ensure you're accessing `router.events` in `useEffect`:
useEffect(() => {
const handleRouteChange = (url, { shallow }) => {
console.log(
`App is changing to ${url} ${
shallow ? 'with' : 'without'
} shallow routing`
)
}
router.events.on('routeChangeStart', handleRouteChange)
// If the component is unmounted, unsubscribe
// from the event with the `off` method:
return () => {
router.events.off('routeChangeStart', handleRouteChange)
}
}, [router])
If your application uses `router.router.events` which was an internal property that was not public please make sure to use `router.events` as well.
[React 16 to 17](#react-16-to-17)
---------------------------------
React 17 introduced a new [JSX Transform](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html) that brings a long-time Next.js feature to the wider React ecosystem: Not having to `import React from 'react'` when using JSX. When using React 17 Next.js will automatically use the new transform. This transform does not make the `React` variable global, which was an unintended side-effect of the previous Next.js implementation. A [codemod is available](/docs/pages/building-your-application/upgrading/codemods#add-missing-react-import) to automatically fix cases where you accidentally used `React` without importing it.
Most applications already use the latest version of React, with Next.js 11 the minimum React version has been updated to 17.0.2.
To upgrade you can run the following command:
npm install react@latest react-dom@latest
Or using `yarn`:
yarn add react@latest react-dom@latest
[Previous
Version 12
](/docs/pages/building-your-application/upgrading/version-12)
[Next
Version 10
](/docs/pages/building-your-application/upgrading/version-10)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"fdbf07e2726d29a82df9d1021382c5d5\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/version-9 | 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
* [Production Deployment on Vercel](#production-deployment-on-vercel)
* [Check your Custom App File (pages/\_app.js)](#check-your-custom-app-file-pages_appjs)
* [Breaking Changes](#breaking-changes)
* [@zeit/next-typescript is no longer necessary](#zeitnext-typescript-is-no-longer-necessary)
* [The config key is now an export on a page](#the-config-key-is-now-an-export-on-a-page)
* [next/dynamic no longer renders "loading..." by default while loading](#nextdynamic-no-longer-renders-loading-by-default-while-loading)
* [withAmp has been removed in favor of an exported configuration object](#withamp-has-been-removed-in-favor-of-an-exported-configuration-object)
* [next export no longer exports pages as index.html](#next-export-no-longer-exports-pages-as-indexhtml)
* [pages/api/ is treated differently](#pagesapi-is-treated-differently)
* [Deprecated Features](#deprecated-features)
* [next/dynamic has deprecated loading multiple modules at once](#nextdynamic-has-deprecated-loading-multiple-modules-at-once)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/01-building-your-application/10-upgrading/10-version-9.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)[Upgrading](/docs/pages/building-your-application/upgrading)Version 9
Upgrading to Version 9
======================
To upgrade to version 9, run the following command:
Terminal
npm i next@9
Terminal
yarn add next@9
Terminal
pnpm up next@9
Terminal
bun add next@9
> **Good to know:** If you are using TypeScript, ensure you also upgrade `@types/react` and `@types/react-dom` to their corresponding versions.
[Production Deployment on Vercel](#production-deployment-on-vercel)
-------------------------------------------------------------------
If you previously configured `routes` in your `vercel.json` file for dynamic routes, these rules can be removed when leveraging Next.js 9's new [Dynamic Routing feature](/docs/pages/building-your-application/routing/dynamic-routes).
Next.js 9's dynamic routes are **automatically configured on [Vercel](https://vercel.com/)** and do not require any `vercel.json` customization.
You can read more about [Dynamic Routing here](/docs/pages/building-your-application/routing/dynamic-routes).
[Check your Custom App File (`pages/_app.js`)](#check-your-custom-app-file-pages_appjs)
---------------------------------------------------------------------------------------
If you previously copied the [Custom `<App>`](/docs/pages/building-your-application/routing/custom-app) example, you may be able to remove your `getInitialProps`.
Removing `getInitialProps` from `pages/_app.js` (when possible) is important to leverage new Next.js features!
The following `getInitialProps` does nothing and may be removed:
class MyApp extends App {
// Remove me, I do nothing!
static async getInitialProps({ Component, ctx }) {
let pageProps = {}
if (Component.getInitialProps) {
pageProps = await Component.getInitialProps(ctx)
}
return { pageProps }
}
render() {
// ... etc
}
}
[Breaking Changes](#breaking-changes)
-------------------------------------
### [`@zeit/next-typescript` is no longer necessary](#zeitnext-typescript-is-no-longer-necessary)
Next.js will now ignore usage `@zeit/next-typescript` and warn you to remove it. Please remove this plugin from your `next.config.js`.
Remove references to `@zeit/next-typescript/babel` from your custom `.babelrc` (if present).
The usage of [`fork-ts-checker-webpack-plugin`](https://github.com/Realytics/fork-ts-checker-webpack-plugin/issues) should also be removed from your `next.config.js`.
TypeScript Definitions are published with the `next` package, so you need to uninstall `@types/next` as they would conflict.
The following types are different:
> This list was created by the community to help you upgrade, if you find other differences please send a pull-request to this list to help other users.
From:
import { NextContext } from 'next'
import { NextAppContext, DefaultAppIProps } from 'next/app'
import { NextDocumentContext, DefaultDocumentIProps } from 'next/document'
to
import { NextPageContext } from 'next'
import { AppContext, AppInitialProps } from 'next/app'
import { DocumentContext, DocumentInitialProps } from 'next/document'
### [The `config` key is now an export on a page](#the-config-key-is-now-an-export-on-a-page)
You may no longer export a custom variable named `config` from a page (i.e. `export { config }` / `export const config ...`). This exported variable is now used to specify page-level Next.js configuration like Opt-in AMP and API Route features.
You must rename a non-Next.js-purposed `config` export to something different.
### [`next/dynamic` no longer renders "loading..." by default while loading](#nextdynamic-no-longer-renders-loading-by-default-while-loading)
Dynamic components will not render anything by default while loading. You can still customize this behavior by setting the `loading` property:
import dynamic from 'next/dynamic'
const DynamicComponentWithCustomLoading = dynamic(
() => import('../components/hello2'),
{
loading: () => <p>Loading</p>,
}
)
### [`withAmp` has been removed in favor of an exported configuration object](#withamp-has-been-removed-in-favor-of-an-exported-configuration-object)
Next.js now has the concept of page-level configuration, so the `withAmp` higher-order component has been removed for consistency.
This change can be **automatically migrated by running the following commands in the root of your Next.js project:**
Terminal
curl -L https://github.com/vercel/next-codemod/archive/master.tar.gz | tar -xz --strip=2 next-codemod-master/transforms/withamp-to-config.js npx jscodeshift -t ./withamp-to-config.js pages/**/*.js
To perform this migration by hand, or view what the codemod will produce, see below:
**Before**
import { withAmp } from 'next/amp'
function Home() {
return <h1>My AMP Page</h1>
}
export default withAmp(Home)
// or
export default withAmp(Home, { hybrid: true })
**After**
export default function Home() {
return <h1>My AMP Page</h1>
}
export const config = {
amp: true,
// or
amp: 'hybrid',
}
### [`next export` no longer exports pages as `index.html`](#next-export-no-longer-exports-pages-as-indexhtml)
Previously, exporting `pages/about.js` would result in `out/about/index.html`. This behavior has been changed to result in `out/about.html`.
You can revert to the previous behavior by creating a `next.config.js` with the following content:
next.config.js
module.exports = {
trailingSlash: true,
}
### [`pages/api/` is treated differently](#pagesapi-is-treated-differently)
Pages in `pages/api/` are now considered [API Routes](https://nextjs.org/blog/next-9#api-routes). Pages in this directory will no longer contain a client-side bundle.
[Deprecated Features](#deprecated-features)
-------------------------------------------
### [`next/dynamic` has deprecated loading multiple modules at once](#nextdynamic-has-deprecated-loading-multiple-modules-at-once)
The ability to load multiple modules at once has been deprecated in `next/dynamic` to be closer to React's implementation (`React.lazy` and `Suspense`).
Updating code that relies on this behavior is relatively straightforward! We've provided an example of a before/after to help you migrate your application:
**Before**
import dynamic from 'next/dynamic'
const HelloBundle = dynamic({
modules: () => {
const components = {
Hello1: () => import('../components/hello1').then((m) => m.default),
Hello2: () => import('../components/hello2').then((m) => m.default),
}
return components
},
render: (props, { Hello1, Hello2 }) => (
<div>
<h1>{props.title}</h1>
<Hello1 />
<Hello2 />
</div>
),
})
function DynamicBundle() {
return <HelloBundle title="Dynamic Bundle" />
}
export default DynamicBundle
**After**
import dynamic from 'next/dynamic'
const Hello1 = dynamic(() => import('../components/hello1'))
const Hello2 = dynamic(() => import('../components/hello2'))
function HelloBundle({ title }) {
return (
<div>
<h1>{title}</h1>
<Hello1 />
<Hello2 />
</div>
)
}
function DynamicBundle() {
return <HelloBundle title="Dynamic Bundle" />
}
export default DynamicBundle
[Previous
Version 10
](/docs/pages/building-your-application/upgrading/version-10)
[Next
API Reference
](/docs/pages/api-reference)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"f47557393cfb6b4c7f7c3f944ad0a764\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/building-your-application/upgrading/version-10 | 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/10-upgrading/09-version-10.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)[Upgrading](/docs/pages/building-your-application/upgrading)Version 10
Version 10
==========
There were no breaking changes between versions 9 and 10.
To upgrade to version 10, run the following command:
Terminal
npm i next@10
Terminal
yarn add next@10
Terminal
pnpm up next@10
Terminal
bun add next@10
> **Good to know:** If you are using TypeScript, ensure you also upgrade `@types/react` and `@types/react-dom` to their corresponding versions.
[Previous
Version 11
](/docs/pages/building-your-application/upgrading/version-11)
[Next
Version 9
](/docs/pages/building-your-application/upgrading/version-9)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"629891f3dd7a13a21febd8537a160280\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/api-reference | 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/02-api-reference/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)API Reference
API Reference
=============
[
### Components
API Reference for Next.js built-in components in the Pages Router.
](/docs/pages/api-reference/components)[
### Functions
API Reference for Functions and Hooks in Pages Router.
](/docs/pages/api-reference/functions)[
### next.config.js Options
Learn about the options available in next.config.js for the Pages Router.
](/docs/pages/api-reference/next-config-js)[
### create-next-app
create-next-app
](/docs/pages/api-reference/create-next-app)[
### Next.js CLI
Next.js CLI
](/docs/pages/api-reference/next-cli)[
### Edge Runtime
API Reference for the Edge Runtime.
](/docs/pages/api-reference/edge)
[Previous
Version 9
](/docs/pages/building-your-application/upgrading/version-9)
[Next
Components
](/docs/pages/api-reference/components)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"cc8ba65ed2a71c9e33421dac7214a6d9\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/api-reference/components/head | 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
* [Avoid duplicated tags](#avoid-duplicated-tags)
* [Use minimal nesting](#use-minimal-nesting)
* [Use next/script for scripts](#use-nextscript-for-scripts)
* [No html or body tags](#no-html-or-body-tags)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/02-api-reference/01-components/head.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/api-reference)[Components](/docs/pages/api-reference/components)<Head>
<Head>
======
Examples
* [Head Elements](https://github.com/vercel/next.js/tree/canary/examples/head-elements)
* [Layout Component](https://github.com/vercel/next.js/tree/canary/examples/layout-component)
We expose a built-in component for appending elements to the `head` of the page:
import Head from 'next/head'
function IndexPage() {
return (
<div>
<Head>
<title>My page title</title>
</Head>
<p>Hello world!</p>
</div>
)
}
export default IndexPage
[Avoid duplicated tags](#avoid-duplicated-tags)
-----------------------------------------------
To avoid duplicate tags in your `head` you can use the `key` property, which will make sure the tag is only rendered once, as in the following example:
import Head from 'next/head'
function IndexPage() {
return (
<div>
<Head>
<title>My page title</title>
<meta property="og:title" content="My page title" key="title" />
</Head>
<Head>
<meta property="og:title" content="My new title" key="title" />
</Head>
<p>Hello world!</p>
</div>
)
}
export default IndexPage
In this case only the second `<meta property="og:title" />` is rendered. `meta` tags with duplicate `key` attributes are automatically handled.
> The contents of `head` get cleared upon unmounting the component, so make sure each page completely defines what it needs in `head`, without making assumptions about what other pages added.
[Use minimal nesting](#use-minimal-nesting)
-------------------------------------------
`title`, `meta` or any other elements (e.g. `script`) need to be contained as **direct** children of the `Head` element, or wrapped into maximum one level of `<React.Fragment>` or arrays—otherwise the tags won't be correctly picked up on client-side navigations.
[Use `next/script` for scripts](#use-nextscript-for-scripts)
------------------------------------------------------------
We recommend using [`next/script`](/docs/pages/building-your-application/optimizing/scripts) in your component instead of manually creating a `<script>` in `next/head`.
[No `html` or `body` tags](#no-html-or-body-tags)
-------------------------------------------------
You **cannot** use `<Head>` to set attributes on `<html>` or `<body>` tags. This will result in an `next-head-count is missing` error. `next/head` can only handle tags inside the HTML `<head>` tag.
[Previous
Font
](/docs/pages/api-reference/components/font)
[Next
<Image>
](/docs/pages/api-reference/components/image)
Was this helpful?
supported.
Send |
{
"contentLength": null,
"etag": "W/\"1105530dac260b1824e27c85a1193b90\"",
"lastModifiedHeader": null,
"lastModifiedHtml": null
} | https://nextjs.org/docs/pages/api-reference/components/link | 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
* [Props](#props)
* [href (required)](#href-required)
* [replace](#replace)
* [scroll](#scroll)
* [prefetch](#prefetch)
* [Other Props](#other-props)
* [legacyBehavior](#legacybehavior)
* [passHref](#passhref)
* [scroll](#scroll-1)
* [shallow](#shallow)
* [locale](#locale)
* [Examples](#examples)
* [Linking to Dynamic Routes](#linking-to-dynamic-routes)
* [If the child is a custom component that wraps an <a> tag](#if-the-child-is-a-custom-component-that-wraps-an-a-tag)
* [If the child is a functional component](#if-the-child-is-a-functional-component)
* [With URL Object](#with-url-object)
* [Replace the URL instead of push](#replace-the-url-instead-of-push)
* [Disable scrolling to the top of the page](#disable-scrolling-to-the-top-of-the-page)
* [Middleware](#middleware)
* [Version History](#version-history)
[Edit this page on GitHub](https://github.com/vercel/next.js/edit/canary/docs/03-pages/02-api-reference/01-components/link.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/api-reference)[Components](/docs/pages/api-reference/components)<Link>
<Link>
======
Examples
* [Hello World](https://github.com/vercel/next.js/tree/canary/examples/hello-world)
* [Active className on Link](https://github.com/vercel/next.js/tree/canary/examples/active-class-name)
`<Link>` is a React component that extends the HTML `<a>` element to provide [prefetching](/docs/app/building-your-application/routing/linking-and-navigating#2-prefetching) and client-side navigation between routes. It is the primary way to navigate between routes in Next.js.
For an example, consider a `pages` directory with the following files:
* `pages/index.js`
* `pages/about.js`
* `pages/blog/[slug].js`
We can have a link to each of these pages like so:
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
[Props](#props)
---------------
Here's a summary of the props available for the Link Component:
Prop
Example
Type
Required
[`href`](#href-required)
`href="/dashboard"`
String or Object
Yes
[`replace`](#replace)
`replace={false}`
Boolean
\-
[`scroll`](#scroll)
`scroll={false}`
Boolean
\-
[`prefetch`](#prefetch)
`prefetch={false}`
Boolean
\-
> **Good to know**: `<a>` tag attributes such as `className` or `target="_blank"` can be added to `<Link>` as props and will be passed to the underlying `<a>` element.
### [`href` (required)](#href-required)
The path or URL to navigate to.
<Link href="/dashboard">Dashboard</Link>
`href` can also accept an object, for example:
// Navigate to /about?name=test
<Link
href={{
pathname: '/about',
query: { name: 'test' },
}}
>
About
</Link>
### [`replace`](#replace)
**Defaults to `false`.** When `true`, `next/link` will replace the current history state instead of adding a new URL into the [browser’s history](https://developer.mozilla.org/docs/Web/API/History_API) stack.
app/page.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
export default function Page() {
return (
<Link href="/dashboard" replace>
Dashboard
</Link>
)
}
### [`scroll`](#scroll)
**Defaults to `true`.** The default behavior of `<Link>` **is to scroll to the top of a new route or to maintain the scroll position for backwards and forwards navigation.** When `false`, `next/link` will _not_ scroll to the top of the page after a navigation.
app/page.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
export default function Page() {
return (
<Link href="/dashboard" scroll={false}>
Dashboard
</Link>
)
}
> **Good to know**:
>
> * Next.js will scroll to the [Page](/docs/app/building-your-application/routing/pages-and-layouts#pages) if it is not visible in the viewport upon navigation.
### [`prefetch`](#prefetch)
Prefetching happens when a `<Link />` component enters the user's viewport (initially or through scroll). Next.js prefetches and loads the linked route (denoted by the `href`) and data in the background to improve the performance of client-side navigations. Prefetching is only enabled in production.
* **`true` (default)**: The full route and its data will be prefetched.
* `false`: Prefetching will not happen when entering the viewport, but will happen on hover. If you want to completely remove fetching on hover as well, consider using an `<a>` tag or [incrementally adopting](/docs/app/building-your-application/upgrading/app-router-migration) the App Router, which enables disabling prefetching on hover too.
pages/index.tsx
TypeScript
JavaScriptTypeScript
import Link from 'next/link'
export default function Page() {
return (
<Link href="/dashboard" prefetch={false}>
Dashboard
</Link>
)
}
[Other Props](#other-props)
---------------------------
### [`legacyBehavior`](#legacybehavior)
An `<a>` element is no longer required as a child of `<Link>`. Add the `legacyBehavior` prop to use the legacy behavior or remove the `<a>` to upgrade. A [codemod is available](/docs/app/building-your-application/upgrading/codemods#new-link) to automatically upgrade your code.
> **Good to know**: when `legacyBehavior` is not set to `true`, all [`anchor`](https://developer.mozilla.org/docs/Web/HTML/Element/a) tag properties can be passed to `next/link` as well such as, `className`, `onClick`, etc.
### [`passHref`](#passhref)
Forces `Link` to send the `href` property to its child. Defaults to `false`
### [`scroll`](#scroll-1)
Scroll to the top of the page after a navigation. Defaults to `true`
### [`shallow`](#shallow)
Update the path of the current page without rerunning [`getStaticProps`](/docs/pages/building-your-application/data-fetching/get-static-props), [`getServerSideProps`](/docs/pages/building-your-application/data-fetching/get-server-side-props) or [`getInitialProps`](/docs/pages/api-reference/functions/get-initial-props). Defaults to `false`
### [`locale`](#locale)
The active locale is automatically prepended. `locale` allows for providing a different locale. When `false` `href` has to include the locale as the default behavior is disabled.
[Examples](#examples)
---------------------
### [Linking to Dynamic Routes](#linking-to-dynamic-routes)
For dynamic routes, it can be handy to use template literals to create the link's path.
For example, you can generate a list of links to the dynamic route `pages/blog/[slug].js`
pages/blog/index.js
import Link from 'next/link'
function Posts({ posts }) {
return (
<ul>
{posts.map((post) => (
<li key={post.id}>
<Link href={`/blog/${post.slug}`}>{post.title}</Link>
</li>
))}
</ul>
)
}
export default Posts
### [If the child is a custom component that wraps an `<a>` tag](#if-the-child-is-a-custom-component-that-wraps-an-a-tag)
If the child of `Link` is a custom component that wraps an `<a>` tag, you must add `passHref` to `Link`. This is necessary if you’re using libraries like [styled-components](https://styled-components.com/). Without this, the `<a>` tag will not have the `href` attribute, which hurts your site's accessibility and might affect SEO. If you're using [ESLint](/docs/pages/building-your-application/configuring/eslint#eslint-plugin), there is a built-in rule `next/link-passhref` to ensure correct usage of `passHref`.
import Link from 'next/link'
import styled from 'styled-components'
// This creates a custom component that wraps an <a> tag
const RedLink = styled.a`
color: red;
`
function NavLink({ href, name }) {
return (
<Link href={href} passHref legacyBehavior>
<RedLink>{name}</RedLink>
</Link>
)
}
export default NavLink
* If you’re using [emotion](https://emotion.sh/)’s JSX pragma feature (`@jsx jsx`), you must use `passHref` even if you use an `<a>` tag directly.
* The component should support `onClick` property to trigger navigation correctly
### [If the child is a functional component](#if-the-child-is-a-functional-component)
If the child of `Link` is a functional component, in addition to using `passHref` and `legacyBehavior`, you must wrap the component in [`React.forwardRef`](https://react.dev/reference/react/forwardRef):
import Link from 'next/link'
// `onClick`, `href`, and `ref` need to be passed to the DOM element
// for proper handling
const MyButton = React.forwardRef(({ onClick, href }, ref) => {
return (
<a href={href} onClick={onClick} ref={ref}>
Click Me
</a>
)
})
function Home() {
return (
<Link href="/about" passHref legacyBehavior>
<MyButton />
</Link>
)
}
export default Home
### [With URL Object](#with-url-object)
`Link` can also receive a URL object and it will automatically format it to create the URL string. Here's how to do it:
import Link from 'next/link'
function Home() {
return (
<ul>
<li>
<Link
href={{
pathname: '/about',
query: { name: 'test' },
}}
>
About us
</Link>
</li>
<li>
<Link
href={{
pathname: '/blog/[slug]',
query: { slug: 'my-post' },
}}
>
Blog Post
</Link>
</li>
</ul>
)
}
export default Home
The above example has a link to:
* A predefined route: `/about?name=test`
* A [dynamic route](/docs/app/building-your-application/routing/dynamic-routes): `/blog/my-post`
You can use every property as defined in the [Node.js URL module documentation](https://nodejs.org/api/url.html#url_url_strings_and_url_objects).
### [Replace the URL instead of push](#replace-the-url-instead-of-push)
The default behavior of the `Link` component is to `push` a new URL into the `history` stack. You can use the `replace` prop to prevent adding a new entry, as in the following example:
<Link href="/about" replace>
About us
</Link>
### [Disable scrolling to the top of the page](#disable-scrolling-to-the-top-of-the-page)
The default behavior of `Link` is to scroll to the top of the page. When there is a hash defined it will scroll to the specific id, like a normal `<a>` tag. To prevent scrolling to the top / hash `scroll={false}` can be added to `Link`:
<Link href="/#hashid" scroll={false}>
Disables scrolling to the top
</Link>
### [Middleware](#middleware)
It's common to use [Middleware](/docs/app/building-your-application/routing/middleware) for authentication or other purposes that involve rewriting the user to a different page. In order for the `<Link />` component to properly prefetch links with rewrites via Middleware, you need to tell Next.js both the URL to display and the URL to prefetch. This is required to avoid un-necessary fetches to middleware to know the correct route to prefetch.
For example, if you want to serve a `/dashboard` route that has authenticated and visitor views, you may add something similar to the following in your Middleware to redirect the user to the correct page:
middleware.js
export function middleware(req) {
const nextUrl = req.nextUrl
if (nextUrl.pathname === '/dashboard') {
if (req.cookies.authToken) {
return NextResponse.rewrite(new URL('/auth/dashboard', req.url))
} else {
return NextResponse.rewrite(new URL('/public/dashboard', req.url))
}
}
}
In this case, you would want to use the following code in your `<Link />` component:
import Link from 'next/link'
import useIsAuthed from './hooks/useIsAuthed'
export default function Page() {
const isAuthed = useIsAuthed()
const path = isAuthed ? '/auth/dashboard' : '/public/dashboard'
return (
<Link as="/dashboard" href={path}>
Dashboard
</Link>
)
}
> **Good to know**: If you're using [Dynamic Routes](/docs/app/building-your-application/routing/dynamic-routes), you'll need to adapt your `as` and `href` props. For example, if you have a Dynamic Route like `/dashboard/authed/[user]` that you want to present differently via middleware, you would write: `<Link href={{ pathname: '/dashboard/authed/[user]', query: { user: username } }} as="/dashboard/[user]">Profile</Link>`.
[Version History](#version-history)
-----------------------------------
Version
Changes
`v13.0.0`
No longer requires a child `<a>` tag. A [codemod](/docs/app/building-your-application/upgrading/codemods#remove-a-tags-from-link-components) is provided to automatically update your codebase.
`v10.0.0`
`href` props pointing to a dynamic route are automatically resolved and no longer require an `as` prop.
`v8.0.0`
Improved prefetching performance.
`v1.0.0`
`next/link` introduced.
[Previous
<Image> (Legacy)
](/docs/pages/api-reference/components/image-legacy)
[Next
<Script>
](/docs/pages/api-reference/components/script)
Was this helpful?
supported.
Send |