text
stringlengths
49
1.09k
source
stringclasses
119 values
id
stringlengths
14
15
This cookies instance is read-only. To set cookies, you need to return a new Response using the Set-Cookie header. app/api/route.ts import { cookies } from 'next/headers' export async function GET(request: Request) { const cookieStore = cookies() const token = cookieStore.get('token') return new Response('Hello, Next.js!', { status: 200, headers: { 'Set-Cookie': `token=${token.value}` }, }) } Alternatively, you can use abstractions on top of the underlying Web APIs to read cookies (NextRequest): app/api/route.ts import { type NextRequest } from 'next/server' export async function GET(request: NextRequest) { const token = request.cookies.get('token') } Headers
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-6
const token = request.cookies.get('token') } Headers You can read headers with headers from next/headers. This server function can be called directly in a Route Handler, or nested inside of another function. This headers instance is read-only. To set headers, you need to return a new Response with new headers. app/api/route.ts import { headers } from 'next/headers' export async function GET(request: Request) { const headersList = headers() const referer = headersList.get('referer') return new Response('Hello, Next.js!', { status: 200, headers: { referer: referer }, }) } Alternatively, you can use abstractions on top of the underlying Web APIs to read headers (NextRequest): app/api/route.ts import { type NextRequest } from 'next/server'
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-7
app/api/route.ts import { type NextRequest } from 'next/server' export async function GET(request: NextRequest) { const requestHeaders = new Headers(request.headers) } Redirects app/api/route.ts import { redirect } from 'next/navigation' export async function GET(request: Request) { redirect('https://nextjs.org/') } Dynamic Route Segments We recommend reading the Defining Routes page before continuing. Route Handlers can use Dynamic Segments to create request handlers from dynamic data. app/items/[slug]/route.ts export async function GET( request: Request, { params }: { params: { slug: string } } ) { const slug = params.slug // 'a', 'b', or 'c' }
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-8
const slug = params.slug // 'a', 'b', or 'c' } RouteExample URLparamsapp/items/[slug]/route.js/items/a{ slug: 'a' }app/items/[slug]/route.js/items/b{ slug: 'b' }app/items/[slug]/route.js/items/c{ slug: 'c' } Streaming Streaming is commonly used in combination with Large Language Models (LLMs), such an OpenAI, for AI-generated content. Learn more about the AI SDK. app/api/completion/route.ts import { Configuration, OpenAIApi } from 'openai-edge' import { OpenAIStream, StreamingTextResponse } from 'ai' const config = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }) const openai = new OpenAIApi(config) export const runtime = 'edge'
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-9
export const runtime = 'edge' export async function POST(req: Request) { const { prompt } = await req.json() const response = await openai.createCompletion({ model: 'text-davinci-003', stream: true, temperature: 0.6, prompt: 'What is Next.js?', }) const stream = OpenAIStream(response) return new StreamingTextResponse(stream) } These abstractions use the Web APIs to create a stream. You can also use the underlying Web APIs directly. app/api/route.ts // https://developer.mozilla.org/en-US/docs/Web/API/ReadableStream#convert_async_iterator_to_stream function iteratorToStream(iterator: any) { return new ReadableStream({ async pull(controller) { const { value, done } = await iterator.next()
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-10
const { value, done } = await iterator.next() if (done) { controller.close() } else { controller.enqueue(value) } }, }) } function sleep(time: number) { return new Promise((resolve) => { setTimeout(resolve, time) }) } const encoder = new TextEncoder() async function* makeIterator() { yield encoder.encode('<p>One</p>') await sleep(200) yield encoder.encode('<p>Two</p>') await sleep(200) yield encoder.encode('<p>Three</p>') } export async function GET() { const iterator = makeIterator() const stream = iteratorToStream(iterator) return new Response(stream) } Request Body
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-11
return new Response(stream) } Request Body You can read the Request body using the standard Web API methods: app/items/route.ts import { NextResponse } from 'next/server' export async function POST(request: Request) { const res = await request.json() return NextResponse.json({ res }) } Request Body FormData You can read the FormData using the the request.formData() function: app/items/route.ts import { NextResponse } from 'next/server' export async function POST(request: Request) { const formData = await request.formData() const name = formData.get('name') const email = formData.get('email') return NextResponse.json({ name, email }) } Since formData data are all strings, you may want to use zod-form-data to validate the request and retrieve data in the format you prefer (e.g. number).
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-12
CORS You can set CORS headers on a Response using the standard Web API methods: app/api/route.ts export async function GET(request: Request) { return new Response('Hello, Next.js!', { status: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type, Authorization', }, }) } Edge and Node.js Runtimes Route Handlers have an isomorphic Web API to support both Edge and Node.js runtimes seamlessly, including support for streaming. Since Route Handlers use the same route segment configuration as Pages and Layouts, they support long-awaited features like general-purpose statically regenerated Route Handlers. You can use the runtime segment config option to specify the runtime: export const runtime = 'edge' // 'nodejs' is the default
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-13
export const runtime = 'edge' // 'nodejs' is the default Non-UI Responses You can use Route Handlers to return non-UI content. Note that sitemap.xml, robots.txt, app icons, and open graph images all have built-in support. app/rss.xml/route.ts export async function GET() { return new Response(`<?xml version="1.0" encoding="UTF-8" ?> <rss version="2.0"> <channel> <title>Next.js Documentation</title> <link>https://nextjs.org/docs</link> <description>The React Framework for the Web</description> </channel> </rss>`) } Segment Config Options Route Handlers use the same route segment configuration as pages and layouts. app/items/route.ts export const dynamic = 'auto' export const dynamicParams = true
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-14
app/items/route.ts export const dynamic = 'auto' export const dynamicParams = true export const revalidate = false export const fetchCache = 'auto' export const runtime = 'nodejs' export const preferredRegion = 'auto' See the API reference for more details.
https://nextjs.org/docs/app/building-your-application/routing/router-handlers
bca5fbe12397-15
InternationalizationNext.js enables you to configure the routing and rendering of content to support multiple languages. Making your site adaptive to different locales includes translated content (localization) and internationalized routes. Terminology Locale: An identifier for a set of language and formatting preferences. This usually includes the preferred language of the user and possibly their geographic region. en-US: English as spoken in the United States nl-NL: Dutch as spoken in the Netherlands nl: Dutch, no specific region Routing Overview It’s recommended to use the user’s language preferences in the browser to select which locale to use. Changing your preferred language will modify the incoming Accept-Language header to your application. For example, using the following libraries, you can look at an incoming Request to determine which locale to select, based on the Headers, locales you plan to support, and the default locale. middleware.js import { match } from '@formatjs/intl-localematcher'
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-0
middleware.js import { match } from '@formatjs/intl-localematcher' import Negotiator from 'negotiator' let headers = { 'accept-language': 'en-US,en;q=0.5' } let languages = new Negotiator({ headers }).languages() let locales = ['en-US', 'nl-NL', 'nl'] let defaultLocale = 'en-US' match(languages, locales, defaultLocale) // -> 'en-US' Routing can be internationalized by either the sub-path (/fr/products) or domain (my-site.fr/products). With this information, you can now redirect the user based on the locale inside Middleware. middleware.js import { NextResponse } from 'next/server' let locales = ['en-US', 'nl-NL', 'nl'] // Get the preferred locale, similar to above or using a library function getLocale(request) { ... }
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-1
function getLocale(request) { ... } export function middleware(request) { // Check if there is any supported locale in the pathname const pathname = request.nextUrl.pathname const pathnameIsMissingLocale = locales.every( (locale) => !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}` ) // Redirect if there is no locale if (pathnameIsMissingLocale) { const locale = getLocale(request) // e.g. incoming request is /products // The new URL is now /en-US/products return NextResponse.redirect( new URL(`/${locale}/${pathname}`, request.url) ) } } export const config = { matcher: [ // Skip all internal paths (_next) '/((?!_next).*)',
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-2
// Skip all internal paths (_next) '/((?!_next).*)', // Optional: only run on root (/) URL // '/' ], } Finally, ensure all special files inside app/ are nested under app/[lang]. This enables the Next.js router to dynamically handle different locales in the route, and forward the lang parameter to every layout and page. For example: app/[lang]/page.js // You now have access to the current locale // e.g. /en-US/products -> `lang` is "en-US" export default async function Page({ params: { lang } }) { return ... } The root layout can also be nested in the new folder (e.g. app/[lang]/layout.js). Localization
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-3
Localization Changing displayed content based on the user’s preferred locale, or localization, is not something specific to Next.js. The patterns described below would work the same with any web application. Let’s assume we want to support both English and Dutch content inside our application. We might maintain two different “dictionaries”, which are objects that give us a mapping from some key to a localized string. For example: dictionaries/en.json { "products": { "cart": "Add to Cart" } } dictionaries/nl.json { "products": { "cart": "Toevoegen aan Winkelwagen" } } We can then create a getDictionary function to load the translations for the requested locale: app/[lang]/dictionaries.js import 'server-only' const dictionaries = {
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-4
app/[lang]/dictionaries.js import 'server-only' const dictionaries = { en: () => import('./dictionaries/en.json').then((module) => module.default), nl: () => import('./dictionaries/nl.json').then((module) => module.default), } export const getDictionary = async (locale) => dictionaries[locale]() Given the currently selected language, we can fetch the dictionary inside of a layout or page. app/[lang]/page.js import { getDictionary } from './dictionaries' export default async function Page({ params: { lang } }) { const dict = await getDictionary(lang) // en return <button>{dict.products.cart}</button> // Add to Cart }
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-5
return <button>{dict.products.cart}</button> // Add to Cart } Because all layouts and pages in the app/ directory default to Server Components, we do not need to worry about the size of the translation files affecting our client-side JavaScript bundle size. This code will only run on the server, and only the resulting HTML will be sent to the browser. Static Generation To generate static routes for a given set of locales, we can use generateStaticParams with any page or layout. This can be global, for example, in the root layout: app/[lang]/layout.js export async function generateStaticParams() { return [{ lang: 'en-US' }, { lang: 'de' }] } export default function Root({ children, params }) { return ( <html lang={params.lang}> <body>{children}</body> </html> ) } Examples
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-6
</html> ) } Examples Minimal i18n routing and translations next-intl
https://nextjs.org/docs/app/building-your-application/routing/internationalization
84f11a9b4025-7
Loading UI and StreamingThe special file loading.js helps you create meaningful Loading UI with React Suspense. With this convention, you can show an instant loading state from the server while the content of a route segment loads. The new content is automatically swapped in once rendering is complete. Instant Loading States An instant loading state is fallback UI that is shown immediately upon navigation. You can pre-render loading indicators such as skeletons and spinners, or a small but meaningful part of future screens such as a cover photo, title, etc. This helps users understand the app is responding and provides a better user experience. Create a loading state by adding a loading.js file inside a folder. app/dashboard/loading.tsx export default function Loading() { // You can add any UI inside Loading, including a Skeleton. return <LoadingSkeleton /> }
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming
89c74743c9ee-0
return <LoadingSkeleton /> } In the same folder, loading.js will be nested inside layout.js. It will automatically wrap the page.js file and any children below in a <Suspense> boundary. Good to know: Navigation is immediate, even with server-centric routing. Navigation is interruptible, meaning changing routes does not need to wait for the content of the route to fully load before navigating to another route. Shared layouts remain interactive while new route segments load. Recommendation: Use the loading.js convention for route segments (layouts and pages) as Next.js optimizes this functionality. Streaming with Suspense In addition to loading.js, you can also manually create Suspense Boundaries for your own UI components. The App Router supports streaming with Suspense for both Node.js and Edge runtimes. What is Streaming?
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming
89c74743c9ee-1
What is Streaming? To learn how Streaming works in React and Next.js, it's helpful to understand Server-Side Rendering (SSR) and its limitations. With SSR, there's a series of steps that need to be completed before a user can see and interact with a page: First, all data for a given page is fetched on the server. The server then renders the HTML for the page. The HTML, CSS, and JavaScript for the page are sent to the client. A non-interactive user interface is shown using the generated HTML, and CSS. Finally, React hydrates the user interface to make it interactive. These steps are sequential and blocking, meaning the server can only render the HTML for a page once all the data has been fetched. And, on the client, React can only hydrate the UI once the code for all components in the page has been downloaded.
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming
89c74743c9ee-2
SSR with React and Next.js helps improve the perceived loading performance by showing a non-interactive page to the user as soon as possible. However, it can still be slow as all data fetching on server needs to be completed before the page can be shown to the user. Streaming allows you to break down the page's HTML into smaller chunks and progressively send those chunks from the server to the client. This enables parts of the page to be displayed sooner, without waiting for all the data to load before any UI can be rendered. Streaming works well with React's component model because each component can be considered a chunk. Components that have higher priority (e.g. product information) or that don't rely on data can be sent first (e.g. layout), and React can start hydration earlier. Components that have lower priority (e.g. reviews, related products) can be sent in the same server request after their data has been fetched.
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming
89c74743c9ee-3
Streaming is particularly beneficial when you want to prevent long data requests from blocking the page from rendering as it can reduce the Time To First Byte (TTFB) and First Contentful Paint (FCP). It also helps improve Time to Interactive (TTI), especially on slower devices. Example <Suspense> works by wrapping a component that performs an asynchronous action (e.g. fetch data), showing fallback UI (e.g. skeleton, spinner) while it's happening, and then swapping in your component once the action completes. app/dashboard/page.tsx import { Suspense } from 'react' import { PostFeed, Weather } from './Components' export default function Posts() { return ( <section> <Suspense fallback={<p>Loading feed...</p>}> <PostFeed /> </Suspense> <Suspense fallback={<p>Loading weather...</p>}> <Weather />
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming
89c74743c9ee-4
<Suspense fallback={<p>Loading weather...</p>}> <Weather /> </Suspense> </section> ) } By using Suspense, you get the benefits of: Streaming Server Rendering - Progressively rendering HTML from the server to the client. Selective Hydration - React prioritizes what components to make interactive first based on user interaction. For more Suspense examples and use cases, please see the React Documentation. SEO Next.js will wait for data fetching inside generateMetadata to complete before streaming UI to the client. This guarantees the first part of a streamed response includes <head> tags. Since streaming is server-rendered, it does not impact SEO. You can use the Mobile Friendly Test tool from Google to see how your page appears to Google's web crawlers and view the serialized HTML (source).
https://nextjs.org/docs/app/building-your-application/routing/loading-ui-and-streaming
89c74743c9ee-5
Defining Routes We recommend reading the Routing Fundamentals page before continuing. This page will guide you through how to define and organize routes in your Next.js application. Creating Routes Next.js uses a file-system based router where folders are used to define routes. Each folder represents a route segment that maps to a URL segment. To create a nested route, you can nest folders inside each other. A special page.js file is used to make route segments publicly accessible. In this example, the /dashboard/analytics URL path is not publicly accessible because it does not have a corresponding page.js file. This folder could be used to store components, stylesheets, images, or other colocated files. Good to know: .js, .jsx, or .tsx file extensions can be used for special files. Creating UI
https://nextjs.org/docs/app/building-your-application/routing/defining-routes
d19ca5281f3a-0
Creating UI Special file conventions are used to create UI for each route segment. The most common are pages to show UI unique to a route, and layouts to show UI that is shared across multiple routes. For example, to create your first page, add a page.js file inside the app directory and export a React component: app/page.tsx export default function Page() { return <h1>Hello, Next.js!</h1> }
https://nextjs.org/docs/app/building-your-application/routing/defining-routes
d19ca5281f3a-1
Linking and NavigatingThe Next.js router uses server-centric routing with client-side navigation. It supports instant loading states and concurrent rendering. This means navigation maintains client-side state, avoids expensive re-renders, is interruptible, and doesn't cause race conditions. There are two ways to navigate between routes: <Link> Component useRouter Hook This page will go through how to use <Link>, useRouter(), and dive deeper into how navigation works. <Link> Component <Link> is a React component that extends the HTML <a> element to provide prefetching and client-side navigation between routes. It is the primary way to navigate between routes in Next.js. To use <Link>, import it from next/link, and pass a href prop to the component: app/page.tsx import Link from 'next/link' export default function Page() { return <Link href="/dashboard">Dashboard</Link> }
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-0
return <Link href="/dashboard">Dashboard</Link> } There are optional props you can pass to <Link>. See the API reference for more information. Examples Linking to Dynamic Segments When linking to dynamic segments, you can use template literals and interpolation to generate a list of links. For example, to generate a list of blog posts: app/blog/PostList.js import Link from 'next/link' export default function PostList({ posts }) { return ( <ul> {posts.map((post) => ( <li key={post.id}> <Link href={`/blog/${post.slug}`}>{post.title}</Link> </li> ))} </ul> ) } Checking Active Links
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-1
))} </ul> ) } Checking Active Links You can use usePathname() to determine if a link is active. For example, to add a class to the active link, you can check if the current pathname matches the href of the link: app/ui/Navigation.js 'use client' import { usePathname } from 'next/navigation' import Link from 'next/link' export function Navigation({ navLinks }) { const pathname = usePathname() return ( <> {navLinks.map((link) => { const isActive = pathname.startsWith(link.href) return ( <Link className={isActive ? 'text-blue' : 'text-black'} href={link.href} key={link.name} > {link.name} </Link> )
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-2
> {link.name} </Link> ) })} </> ) } Scrolling to an id The default behavior of <Link> is to scroll to the top of the route segment that has changed. When there is an id defined in href, it will scroll to the specific id, similarly to a normal <a> tag. useRouter() Hook The useRouter hook allows you to programmatically change routes inside Client Components. To use useRouter, import it from next/navigation, and call the hook inside your Client Component: app/page.js 'use client' import { useRouter } from 'next/navigation' export default function Page() { const router = useRouter() return ( <button type="button" onClick={() => router.push('/dashboard')}> Dashboard </button> ) }
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-3
Dashboard </button> ) } The useRouter provides methods such as push(), refresh(), and more. See the API reference for more information. Recommendation: Use the <Link> component to navigate between routes unless you have a specific requirement for using useRouter. How Navigation Works A route transition is initiated using <Link> or calling router.push(). The router updates the URL in the browser's address bar. The router avoids unnecessary work by re-using segments that haven't changed (e.g. shared layouts) from the client-side cache. This is also referred to as partial rendering. If the conditions of soft navigation are met, the router fetches the new segment from the cache rather than the server. If not, the router performs a hard navigation and fetches the Server Component payload from the server. If created, loading UI is shown from the server while the payload is being fetched.
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-4
If created, loading UI is shown from the server while the payload is being fetched. The router uses the cached or fresh payload to render the new segments on the client. Client-side Caching of Rendered Server Components Good to know: This client-side cache is different from the server-side Next.js HTTP cache. The new router has an in-memory client-side cache that stores the rendered result of Server Components (payload). The cache is split by route segments which allows invalidation at any level and ensures consistency across concurrent renders. As users navigate around the app, the router will store the payload of previously fetched segments and prefetched segments in the cache. This means, for certain cases, the router can re-use the cache instead of making a new request to the server. This improves performance by avoiding re-fetching data and re-rendering components unnecessarily. Invalidating the Cache
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-5
Invalidating the Cache Server Actions can be used to revalidate data on-demand by path (revalidatePath) or by cache tag (revalidateTag). Prefetching Prefetching is a way to preload a route in the background before it's visited. The rendered result of prefetched routes is added to the router's client-side cache. This makes navigating to a prefetched route near-instant. By default, routes are prefetched as they become visible in the viewport when using the <Link> component. This can happen when the page first loads or through scrolling. Routes can also be programmatically prefetched using the prefetch method of the useRouter() hook. Static and Dynamic Routes: If the route is static, all the Server Component payloads for the route segments will be prefetched.
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-6
If the route is dynamic, the payload from the first shared layout down until the first loading.js file is prefetched. This reduces the cost of prefetching the whole route dynamically and allows instant loading states for dynamic routes. Good to know: Prefetching is only enabled in production. Prefetching can be disabled by passing prefetch={false} to <Link>. Soft Navigation On navigation, the cache for changed segments is reused (if it exists), and no new requests are made to the server for data. Conditions for Soft Navigation On navigation, Next.js will use soft navigation if the route you are navigating to has been prefetched, and either doesn't include dynamic segments or has the same dynamic parameters as the current route. For example, consider the following route that includes a dynamic [team] segment: /dashboard/[team]/*. The cached segments below /dashboard/[team]/* will only be invalidated when the [team] parameter changes.
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-7
Navigating from /dashboard/team-red/* to /dashboard/team-red/* will be a soft navigation. Navigating from /dashboard/team-red/* to /dashboard/team-blue/* will be a hard navigation. Hard Navigation On navigation, the cache is invalidated and the server refetches data and re-renders the changed segments. Back/Forward Navigation Back and forward navigation (popstate event) has a soft navigation behavior. This means, the client-side cache is re-used and navigation is near-instant. Focus and Scroll Management By default, Next.js will set focus and scroll into view the segment that's changed on navigation.
https://nextjs.org/docs/app/building-your-application/routing/linking-and-navigating
2a30fc3db42e-8
Pages and Layouts We recommend reading the Routing Fundamentals and Defining Routes pages before continuing. The App Router inside Next.js 13 introduced new file conventions to easily create pages, shared layouts, and templates. This page will guide you through how to use these special files in your Next.js application. Pages A page is UI that is unique to a route. You can define pages by exporting a component from a page.js file. Use nested folders to define a route and a page.js file to make the route publicly accessible. Create your first page by adding a page.js file inside the app directory: app/page.tsx // `app/page.tsx` is the UI for the `/` URL export default function Page() { return <h1>Hello, Home page!</h1> } app/dashboard/page.tsx // `app/dashboard/page.tsx` is the UI for the `/dashboard` URL export default function Page() {
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-0
export default function Page() { return <h1>Hello, Dashboard Page!</h1> } Good to know: A page is always the leaf of the route subtree. .js, .jsx, or .tsx file extensions can be used for Pages. A page.js file is required to make a route segment publicly accessible. Pages are Server Components by default but can be set to a Client Component. Pages can fetch data. View the Data Fetching section for more information. Layouts A layout is UI that is shared between multiple pages. On navigation, layouts preserve state, remain interactive, and do not re-render. Layouts can also be nested. You can define a layout by default exporting a React component from a layout.js file. The component should accept a children prop that will be populated with a child layout (if it exists) or a child page during rendering. app/dashboard/layout.tsx export default function DashboardLayout({
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-1
app/dashboard/layout.tsx export default function DashboardLayout({ children, // will be a page or nested layout }: { children: React.ReactNode }) { return ( <section> {/* Include shared UI here e.g. a header or sidebar */} <nav></nav> {children} </section> ) } Good to know: The top-most layout is called the Root Layout. This required layout is shared across all pages in an application. Root layouts must contain html and body tags. Any route segment can optionally define its own Layout. These layouts will be shared across all pages in that segment. Layouts in a route are nested by default. Each parent layout wraps child layouts below it using the React children prop. You can use Route Groups to opt specific route segments in and out of shared layouts.
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-2
You can use Route Groups to opt specific route segments in and out of shared layouts. Layouts are Server Components by default but can be set to a Client Component. Layouts can fetch data. View the Data Fetching section for more information. Passing data between a parent layout and its children is not possible. However, you can fetch the same data in a route more than once, and React will automatically dedupe the requests without affecting performance. Layouts do not have access to the current route segment(s). To access route segments, you can use useSelectedLayoutSegment or useSelectedLayoutSegments in a Client Component. .js, .jsx, or .tsx file extensions can be used for Layouts. A layout.js and page.js file can be defined in the same folder. The layout will wrap the page. Root Layout (Required)
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-3
Root Layout (Required) The root layout is defined at the top level of the app directory and applies to all routes. This layout enables you to modify the initial HTML returned from the server. app/layout.tsx export default function RootLayout({ children, }: { children: React.ReactNode }) { return ( <html lang="en"> <body>{children}</body> </html> ) } Good to know: 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. You can use the built-in SEO support to manage <head> HTML elements, for example, the <title> element. You can use route groups to create multiple root layouts. See an example here. The root layout is a Server Component by default and can not be set to a Client Component.
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-4
The root layout is a Server Component by default and can not be set to a Client Component. Migrating from the pages directory: The root layout replaces the _app.js and _document.js files. View the migration guide. Nesting Layouts Layouts defined inside a folder (e.g. app/dashboard/layout.js) apply to specific route segments (e.g. acme.com/dashboard) and render when those segments are active. By default, layouts in the file hierarchy are nested, which means they wrap child layouts via their children prop. app/dashboard/layout.tsx export default function DashboardLayout({ children, }: { children: React.ReactNode }) { return <section>{children}</section> } Good to know: Only the root layout can contain <html> and <body> tags.
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-5
Only the root layout can contain <html> and <body> tags. If you were to combine the two layouts above, the root layout (app/layout.js) would wrap the dashboard layout (app/dashboard/layout.js), which would wrap route segments inside app/dashboard/*. The two layouts would be nested as such: You can use Route Groups to opt specific route segments in and out of shared layouts. Templates Templates are similar to layouts in that they wrap each child layout or page. Unlike layouts that persist across routes and maintain state, templates create a new instance for each of their children on navigation. This means that when a user navigates between routes that share a template, a new instance of the component is mounted, DOM elements are recreated, state is not preserved, and effects are re-synchronized. There may be cases where you need those specific behaviors, and templates would be a more suitable option than layouts. For example:
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-6
Enter/exit animations using CSS or animation libraries. Features that rely on useEffect (e.g logging page views) and useState (e.g a per-page feedback form). To change the default framework behavior. For example, Suspense Boundaries inside layouts only show the fallback the first time the Layout is loaded and not when switching pages. For templates, the fallback is shown on each navigation. Recommendation: We recommend using Layouts unless you have a specific reason to use Template. A template can be defined by exporting a default React component from a template.js file. The component should accept a children prop which will be nested segments. app/template.tsx export default function Template({ children }: { children: React.ReactNode }) { return <div>{children}</div> } The rendered output of a route segment with a layout and a template will be as such: Output <Layout> {/* Note that the template is given a unique key. */}
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-7
Output <Layout> {/* Note that the template is given a unique key. */} <Template key={routeParam}>{children}</Template> </Layout> Modifying <head> In the app directory, you can modify the <head> HTML elements such as title and meta using the built-in SEO support. Metadata can be defined by exporting a metadata object or generateMetadata function in a layout.js or page.js file. app/page.tsx import { Metadata } from 'next' export const metadata: Metadata = { title: 'Next.js', } export default function Page() { return '...' } Good to know: You should not manually add <head> tags such as <title> and <meta> to root layouts. Instead, you should use the Metadata API which automatically handles advanced requirements such as streaming and de-duplicating <head> elements.
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-8
Learn more about available metadata options in the API reference.
https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts
cfe7de3b4dec-9
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 for more details. 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. Example middleware.ts 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 = {
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-0
// See "Matching Paths" below to learn more export const config = { matcher: '/about/:path*', } Matching Paths Middleware will be invoked for every route in your project. The following is the execution order: headers from next.config.js redirects from next.config.js Middleware (rewrites, redirects, etc.) beforeFiles (rewrites) from next.config.js Filesystem routes (public/, _next/static/, pages/, app/, etc.) afterFiles (rewrites) from next.config.js Dynamic Routes (/blog/[slug]) fallback (rewrites) from next.config.js There are two ways to define which paths Middleware will run on: Custom matcher config Conditional statements Matcher matcher allows you to filter Middleware to run on specific paths. middleware.js export const config = { matcher: '/about/:path*', }
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-1
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).*)', ], }
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-2
], } 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: MUST start with / Can include named parameters: /about/:path matches /about/a and /about/b but not /about/a/c 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 Can use regular expression enclosed in parenthesis: /about/(.*) is the same as /about/:path* Read more details on path-to-regexp 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 middleware.ts import { NextResponse } from 'next/server'
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-3
Conditional Statements middleware.ts 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 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: rewrite to a route (Page or Route Handler) that produces a response
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-4
rewrite to a route (Page or Route Handler) that produces a response return a NextResponse directly. See Producing a Response 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. 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. For outgoing responses, cookies have the following methods get, getAll, set, and delete. middleware.ts 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
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-5
// 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')
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-6
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=/test` header. return response } 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 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')
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-7
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 error depending on your backend web server configuration. 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) middleware.ts import { NextRequest, NextResponse } from 'next/server' import { isAuthenticated } from '@lib/auth'
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-8
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 new NextResponse( JSON.stringify({ success: false, message: 'authentication failed' }), { status: 401, headers: { 'content-type': 'application/json' } } ) } } Advanced Middleware Flags In v13.1 of Next.js two additional flags were introduced for middleware, skipMiddlewareUrlNormalize and skipTrailingSlashRedirect to handle advanced use cases.
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-9
skipTrailingSlashRedirect allows disabling Next.js default redirects for adding or removing trailing slashes allowing custom handling inside middleware which can allow maintaining the trailing slash for some paths but not others allowing easier incremental migrations. 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) }
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-10
return NextResponse.redirect(req.nextUrl) } } skipMiddlewareUrlNormalize allows disabling the URL normalizing Next.js does to make handling direct visits and client-transitions the same. There are some advanced cases where you need full control using the original URL which this unlocks. 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 } Version History
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-11
// without the flag this would be normalized to /hello } Version History VersionChangesv13.1.0Advanced Middleware flags addedv13.0.0Middleware can modify request headers, response headers, and send responsesv12.2.0Middleware is stable, please see the upgrade guidev12.0.9Enforce absolute URLs in Edge Runtime (PR)v12.0.0Middleware (Beta) added
https://nextjs.org/docs/app/building-your-application/routing/middleware
d714de5ad9e8-12
Intercepting RoutesIntercepting routes allows you to load a route within the current layout while keeping the context for the current page. This routing paradigm can be useful when you want to "intercept" a certain route to show a different route. For example, when clicking on a photo from within a feed, a modal overlaying the feed should show up with the photo. In this case, Next.js intercepts the /feed route and "masks" this URL to show /photo/123 instead. However, when navigating to the photo directly by for example when clicking a shareable URL or by refreshing the page, the entire photo page should render instead of the modal. No route interception should occur. Convention Intercepting routes can be defined with the (..) convention, which is similar to relative path convention ../ but for segments. You can use: (.) to match segments on the same level (..) to match segments one level above
https://nextjs.org/docs/app/building-your-application/routing/intercepting-routes
205742354301-0
(.) to match segments on the same level (..) to match segments one level above (..)(..) to match segments two levels above (...) to match segments from the root app directory For example, you can intercept the photo segment from within the feed segment by creating a (..)photo directory. Note that the (..) convention is based on route segments, not the file-system. Examples Modals Intercepting Routes can be used together with Parallel Routes to create modals. Using this pattern to create modals overcomes some common challenges when working with modals, by allowing you to: Make the modal content shareable through a URL Preserve context when the page is refreshed, instead of closing the modal Close the modal on backwards navigation rather than going to the previous route Reopen the modal on forwards navigation
https://nextjs.org/docs/app/building-your-application/routing/intercepting-routes
205742354301-1
Reopen the modal on forwards navigation In the above example, the path to the photo segment can use the (..) matcher since @modal is a slot and not a segment. This means that the photo route is only one segment level higher, despite being two file-system levels higher. Other examples could include opening a login modal in a top navbar while also having a dedicated /login page, or opening a shopping cart in a side modal. View an example of modals with Intercepted and Parallel Routes.
https://nextjs.org/docs/app/building-your-application/routing/intercepting-routes
205742354301-2
Dynamic RoutesWhen you don't know the exact segment names ahead of time and want to create routes from dynamic data, you can use Dynamic Segments that are filled in at request time or prerendered at build time. Convention A Dynamic Segment can be created by wrapping a folder's name in square brackets: [folderName]. For example, [id] or [slug]. Dynamic Segments are passed as the params prop to layout, page, route, and generateMetadata functions. Example For example, a blog could include the following route app/blog/[slug]/page.js where [slug] is the Dynamic Segment for blog posts. app/blog/[slug]/page.tsx export default function Page({ params }: { params: { slug: string } }) { return <div>My Post: {params.slug}</div> }
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
f91f50f8d40a-0
return <div>My Post: {params.slug}</div> } RouteExample URLparamsapp/blog/[slug]/page.js/blog/a{ slug: 'a' }app/blog/[slug]/page.js/blog/b{ slug: 'b' }app/blog/[slug]/page.js/blog/c{ slug: 'c' } See the generateStaticParams() page to learn how to generate the params for the segment. Good to know: Dynamic Segments are equivalent to Dynamic Routes in the pages directory. Generating Static Params The generateStaticParams function can be used in combination with dynamic route segments to statically generate routes at build time instead of on-demand at request time. app/blog/[slug]/page.tsx export async function generateStaticParams() { const posts = await fetch('https://.../posts').then((res) => res.json()) return posts.map((post) => ({ slug: post.slug,
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
f91f50f8d40a-1
return posts.map((post) => ({ slug: post.slug, })) } The primary benefit of the generateStaticParams function is its smart retrieval of data. If content is fetched within the generateStaticParams function using a fetch request, the requests are automatically deduplicated. This means a fetch request with the same arguments across multiple generateStaticParams, Layouts, and Pages will only be made once, which decreases build times. Use the migration guide if you are migrating from the pages directory. See generateStaticParams server function documentation for more information and advanced use cases. Catch-all Segments Dynamic Segments can be extended to catch-all subsequent segments by adding an ellipsis inside the brackets [...folderName]. For example, app/shop/[...slug]/page.js will match /shop/clothes, but also /shop/clothes/tops, /shop/clothes/tops/t-shirts, and so on.
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
f91f50f8d40a-2
RouteExample URLparamsapp/shop/[...slug]/page.js/shop/a{ slug: ['a'] }app/shop/[...slug]/page.js/shop/a/b{ slug: ['a', 'b'] }app/shop/[...slug]/page.js/shop/a/b/c{ slug: ['a', 'b', 'c'] } Optional Catch-all Segments Catch-all Segments can be made optional by including the parameter in double square brackets: [[...folderName]]. For example, app/shop/[[...slug]]/page.js will also match /shop, in addition to /shop/clothes, /shop/clothes/tops, /shop/clothes/tops/t-shirts. The difference between catch-all and optional catch-all segments is that with optional, the route without the parameter is also matched (/shop in the example above).
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
f91f50f8d40a-3
RouteExample URLparamsapp/shop/[[...slug]]/page.js/shop{}app/shop/[[...slug]]/page.js/shop/a{ slug: ['a'] }app/shop/[[...slug]]/page.js/shop/a/b{ slug: ['a', 'b'] }app/shop/[[...slug]]/page.js/shop/a/b/c{ slug: ['a', 'b', 'c'] } TypeScript When using TypeScript, you can add types for params depending on your configured route segment. app/blog/[slug]/page.tsx export default function Page({ params }: { params: { slug: string } }) { return <h1>My Page</h1> } Routeparams Type Definitionapp/blog/[slug]/page.js{ slug: string }app/shop/[...slug]/page.js{ slug: string[] }app/[categoryId]/[itemId]/page.js{ categoryId: string, itemId: string }
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
f91f50f8d40a-4
Good to know: This may be done automatically by the TypeScript plugin in the future.
https://nextjs.org/docs/app/building-your-application/routing/dynamic-routes
f91f50f8d40a-5
Parallel RoutesParallel Routing allows you to simultaneously or conditionally render one or more pages in the same layout. For highly dynamic sections of an app, such as dashboards and feeds on social sites, Parallel Routing can be used to implement complex routing patterns. For example, you can simultaneously render the team and analytics pages. Parallel Routing allows you to define independent error and loading states for each route as they're being streamed in independently. Parallel Routing also allows you to conditionally render a slot based on certain conditions, such as authentication state. This enables fully separated code on the same URL. Convention Parallel routes are created using named slots. Slots are defined with the @folder convention, and are passed to the same-level layout as props. Slots are not route segments and do not affect the URL structure. The file path /@team/members would be accessible at /members. For example, the following file structure defines two explicit slots: @analytics and @team.
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-0
For example, the following file structure defines two explicit slots: @analytics and @team. The folder structure above means that the component in app/layout.js now accepts the @analytics and @team slots props, and can render them in parallel alongside the children prop: app/layout.tsx export default function Layout(props: { children: React.ReactNode analytics: React.ReactNode team: React.ReactNode }) { return ( <> {props.children} {props.team} {props.analytics} </> ) } Good to know: The children prop is an implicit slot that does not need to be mapped to a folder. This means app/page.js is equivalent to app/@children/page.js. Unmatched Routes By default, the content rendered within a slot will match the current URL.
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-1
Unmatched Routes By default, the content rendered within a slot will match the current URL. In the case of an unmatched slot, the content that Next.js renders differs based on the routing technique and folder structure. default.js You can define a default.js file to render as a fallback when Next.js cannot recover a slot's active state based on the current URL. Consider the following folder structure. The @team slot has a settings directory, but @analytics does not. If you were to navigate from the root / to /settings, the content that gets rendered is different based on the type of navigation and the availability of the default.js file. With @analytics/default.jsWithout @analytics/default.jsSoft Navigation@team/settings/page.js and @analytics/page.js@team/settings/page.js and @analytics/page.jsHard Navigation@team/settings/page.js and @analytics/default.js404 Soft Navigation
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-2
Soft Navigation On a soft navigation - Next.js will render the slot's previously active state, even if it doesn't match the current URL. Hard Navigation On a hard navigation - a navigation that requires a full page reload - Next.js will first try to render the unmatched slot's default.js file. If that's not available, a 404 gets rendered. The 404 for unmatched routes helps ensure that you don't accidentally render a route that shouldn't be parallel rendered. useSelectedLayoutSegment(s) Both useSelectedLayoutSegment and useSelectedLayoutSegments accept a parallelRoutesKey, which allows you read the active route segment within that slot. app/layout.tsx 'use client' import { useSelectedLayoutSegment } from 'next/navigation' export default async function Layout(props: { //... authModal: React.ReactNode }) { const loginSegments = useSelectedLayoutSegment('authModal')
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-3
}) { const loginSegments = useSelectedLayoutSegment('authModal') // ... } When a user navigates to @authModal/login, or /login in the URL bar, loginSegments will be equal to the string "login". Examples Modals Parallel Routing can be used to render modals. The @authModal slot renders a <Modal> component that can be shown by navigating to a matching route, for example /login. app/layout.tsx export default async function Layout(props: { // ... authModal: React.ReactNode }) { return ( <> {/* ... */} {props.authModal} </> ) } app/@authModal/login/page.tsx import { Modal } from 'components/modal' export default function Login() { return ( <Modal> <h1>Login</h1>
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-4
return ( <Modal> <h1>Login</h1> {/* ... */} </Modal> ) } To ensure that the contents of the modal don't get rendered when it's not active, you can create a default.js file that returns null. app/@authModal/default.tsx export default function Default() { return null } Dismissing a modal If a modal was initiated through client navigation, e.g. by using <Link href="/login">, you can dismiss the modal by calling router.back() or by using a Link component. app/@authModal/login/page.tsx 'use client' import { useRouter } from 'next/navigation' import { Modal } from 'components/modal' export default async function Login() { const router = useRouter() return ( <Modal>
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-5
const router = useRouter() return ( <Modal> <span onClick={() => router.back()}>Close modal</span> <h1>Login</h1> ... </Modal> ) } More information on modals is covered in the Intercepting Routes section. If you want to navigate elsewhere and dismiss a modal, you can also use a catch-all route. app/@authModal/[...catchAll]/page.tsx export default function CatchAll() { return null } Catch-all routes take precedence over default.js. Conditional Routes Parallel Routes can be used to implement conditional routing. For example, you can render a @dashboard or @login route depending on the authentication state. app/layout.tsx import { getUser } from '@/lib/auth' export default function Layout({ dashboard, login, }: { dashboard: React.ReactNode
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-6
dashboard, login, }: { dashboard: React.ReactNode login: React.ReactNode }) { const isLoggedIn = getUser() return isLoggedIn ? dashboard : login }
https://nextjs.org/docs/app/building-your-application/routing/parallel-routes
dcbde6e320fb-7
Project Organization and File ColocationApart from routing folder and file conventions, Next.js is unopinionated about how you organize and colocate your project files. This page shares default behavior and features you can use to organize your project. Safe colocation by default Project organization features Project organization strategies Safe colocation by default In the app directory, nested folder hierarchy defines route structure. Each folder represents a route segment that is mapped to a corresponding segment in a URL path. However, even though route structure is defined through folders, a route is not publicly accessible until a page.js or route.js file is added to a route segment. And, even when a route is made publicly accessible, only the content returned by page.js or route.js is sent to the client. This means that project files can be safely colocated inside route segments in the app directory without accidentally being routable. Good to know:
https://nextjs.org/docs/app/building-your-application/routing/colocation
0a8e13fe6b82-0
Good to know: This is different from the pages directory, where any file in pages is considered a route. While you can colocate your project files in app you don't have to. If you prefer, you can keep them outside the app directory. Project organization features Next.js provides several features to help you organize your project. Private Folders Private folders can be created by prefixing a folder with an underscore: _folderName This indicates the folder is a private implementation detail and should not be considered by the routing system, thereby opting the folder and all its subfolders out of routing. Since files in the app directory can be safely colocated by default, private folders are not required for colocation. However, they can be useful for: Separating UI logic from routing logic. Consistently organizing internal files across a project and the Next.js ecosystem. Sorting and grouping files in code editors.
https://nextjs.org/docs/app/building-your-application/routing/colocation
0a8e13fe6b82-1
Sorting and grouping files in code editors. Avoiding potential naming conflicts with future Next.js file conventions. Good to know While not a framework convention, you might also consider marking files outside private folders as "private" using the same underscore pattern. You can create URL segments that start with an underscore by prefixing the folder name with %5F (the URL-encoded form of an underscore): %5FfolderName. If you don't use private folders, it would be helpful to know Next.js special file conventions to prevent unexpected naming conflicts. Route Groups Route groups can be created by wrapping a folder in parenthesis: (folderName) This indicates the folder is for organizational purposes and should not be included in the route's URL path. Route groups are useful for: Organizing routes into groups e.g. by site section, intent, or team. Enabling nested layouts in the same route segment level:
https://nextjs.org/docs/app/building-your-application/routing/colocation
0a8e13fe6b82-2
Enabling nested layouts in the same route segment level: Creating multiple nested layouts in the same segment, including multiple root layouts Adding a layout to a subset of routes in a common segment src Directory Next.js supports storing application code (including app) inside an optional src directory. This separates application code from project configuration files which mostly live in the root of a project. Module Path Aliases Next.js supports Module Path Aliases which make it easier to read and maintain imports across deeply nested project files. app/dashboard/settings/analytics/page.js // before import { Button } from '../../../components/button' // after import { Button } from '@/components/button' Project organization strategies There is no "right" or "wrong" way when it comes to organizing your own files and folders in a Next.js project.
https://nextjs.org/docs/app/building-your-application/routing/colocation
0a8e13fe6b82-3
The following section lists a very high-level overview of common strategies. The simplest takeaway is to choose a strategy that works for you and your team and be consistent across the project. Good to know: In our examples below, we're using components and lib folders as generalized placeholders, their naming has no special framework significance and your projects might use other folders like ui, utils, hooks, styles, etc. Store project files outside of app This strategy stores all application code in shared folders in the root of your project and keeps the app directory purely for routing purposes. Store project files in top-level folders inside of app This strategy stores all application code in shared folders in the root of the app directory. Split project files by feature or route This strategy stores globally shared application code in the root app directory and splits more specific application code into the route segments that use them.
https://nextjs.org/docs/app/building-your-application/routing/colocation
0a8e13fe6b82-4
Route GroupsIn the app directory, nested folders are normally mapped to URL paths. However, you can mark a folder as a Route Group to prevent the folder from being included in the route's URL path. This allows you to organize your route segments and project files into logical groups without affecting the URL path structure. Route groups are useful for: Organizing routes into groups e.g. by site section, intent, or team. Enabling nested layouts in the same route segment level: Creating multiple nested layouts in the same segment, including multiple root layouts Adding a layout to a subset of routes in a common segment Convention A route group can be created by wrapping a folder's name in parenthesis: (folderName) Examples Organize routes without affecting the URL path To organize routes without affecting the URL, create a group to keep related routes together. The folders in parenthesis will be omitted from the URL (e.g. (marketing) or (shop)).
https://nextjs.org/docs/app/building-your-application/routing/route-groups
b9cadd55c338-0
Even though routes inside (marketing) and (shop) share the same URL hierarchy, you can create a different layout for each group by adding a layout.js file inside their folders. Opting specific segments into a layout To opt specific routes into a layout, create a new route group (e.g. (shop)) and move the routes that share the same layout into the group (e.g. account and cart). The routes outside of the group will not share the layout (e.g. checkout). Creating multiple root layouts To create multiple root layouts, remove the top-level layout.js file, and add a layout.js file inside each route groups. This is useful for partitioning an application into sections that have a completely different UI or experience. The <html> and <body> tags need to be added to each root layout. In the example above, both (marketing) and (shop) have their own root layout. Good to know:
https://nextjs.org/docs/app/building-your-application/routing/route-groups
b9cadd55c338-1
Good to know: The naming of route groups has no special significance other than for organization. They do not affect the URL path. Routes that include a route group should not resolve to the same URL path as other routes. For example, since route groups don't affect URL structure, (marketing)/about/page.js and (shop)/about/page.js would both resolve to /about and cause an error. If you use multiple root layouts without a top-level layout.js file, your home page.js file should be defined in one of the route groups, For example: app/(marketing)/page.js. Navigating across multiple root layouts will cause a full page load (as opposed to a client-side navigation). For example, navigating from /cart that uses app/(shop)/layout.js to /blog that uses app/(marketing)/layout.js will cause a full page load. This only applies to multiple root layouts.
https://nextjs.org/docs/app/building-your-application/routing/route-groups
b9cadd55c338-2
Static and Dynamic RenderingIn Next.js, a route can be statically or dynamically rendered. In a static route, components are rendered on the server at build time. The result of the work is cached and reused on subsequent requests. In a dynamic route, components are rendered on the server at request time. Static Rendering (Default) By default, Next.js statically renders routes to improve performance. This means all the rendering work is done ahead of time and can be served from a Content Delivery Network (CDN) geographically closer to the user. Static Data Fetching (Default) By default, Next.js will cache the result of fetch() requests that do not specifically opt out of caching behavior. This means that fetch requests that do not set a cache option will use the force-cache option. If any fetch requests in the route use the revalidate option, the route will be re-rendered statically during revalidation.
https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic-rendering
41cfb77f0402-0
To learn more about caching data fetching requests, see the Caching and Revalidating page. Dynamic Rendering During static rendering, if a dynamic function or a dynamic fetch() request (no caching) is discovered, Next.js will switch to dynamically rendering the whole route at request time. Any cached data requests can still be re-used during dynamic rendering. This table summarizes how dynamic functions and caching affect the rendering behavior of a route: Data FetchingDynamic FunctionsRenderingStatic (Cached)NoStaticStatic (Cached)YesDynamicNot CachedNoDynamicNot CachedYesDynamic Note how dynamic functions always opt the route into dynamic rendering, regardless of whether the data fetching is cached or not. In other words, static rendering is dependent not only on the data fetching behavior, but also on the dynamic functions used in the route.
https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic-rendering
41cfb77f0402-1
Good to know: In the future, Next.js will introduce hybrid server-side rendering where layouts and pages in a route can be independently statically or dynamically rendered, instead of the whole route. Dynamic Functions Dynamic functions rely on information that can only be known at request time such as a user's cookies, current requests headers, or the URL's search params. In Next.js, these dynamic functions are: Using cookies() or headers() in a Server Component will opt the whole route into dynamic rendering at request time. Using useSearchParams() in Client Components will skip static rendering and instead render all Client Components up to the nearest parent Suspense boundary on the client. We recommend wrapping the Client Component that uses useSearchParams() in a <Suspense/> boundary. This will allow any Client Components above it to be statically rendered. Example. Using the searchParams Pages prop will opt the page into dynamic rendering at request time. Dynamic Data Fetching
https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic-rendering
41cfb77f0402-2
Dynamic Data Fetching Dynamic data fetches are fetch() requests that specifically opt out of caching behavior by setting the cache option to 'no-store' or revalidate to 0. The caching options for all fetch requests in a layout or page can also be set using the segment config object. To learn more about Dynamic Data Fetching, see the Data Fetching page.
https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic-rendering
41cfb77f0402-3
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. Next.js has two server runtimes where you can render parts of your application code: Node.js Runtime Edge Runtime Each runtime has its own set of APIs. Please refer to the Node.js Docs and Edge Docs for the full list of available APIs. Both runtimes can also support streaming depending on your deployment infrastructure. By default, the app directory uses the Node.js runtime. However, you can opt into different runtimes (e.g. Edge) on a per-route basis. 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.
https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes
e595fbf5bdeb-0
NodeServerlessEdgeCold Boot/~250msInstantHTTP StreamingYesYesYesIOAllAllfetchScalability/HighHighestSecurityNormalHighHighLatencyNormalLowLowestnpm PackagesAllAllA smaller subset 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, this limit includes imported packages, fonts and files, and will vary depending on your deployment infrastructure. Node.js 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.
https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes
e595fbf5bdeb-1
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 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 including imported packages, fonts, and files. The downside compared to routes using the Edge 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". Examples Segment Runtime Option
https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes
e595fbf5bdeb-2
Examples Segment Runtime Option You can specify a runtime for individual route segments in your Next.js application. To do so, declare a variable called runtime and export it. The variable must be a string, and must have a value of either 'nodejs' or 'edge' runtime.The following example demonstrates a page route segment that exports a runtime with a value of 'edge':app/page.tsx export const runtime = 'edge' // 'nodejs' (default) | 'edge'You can also define runtime on a layout level, which will make all routes under the layout run on the edge runtime:app/layout.tsx export const runtime = 'edge' // 'nodejs' (default) | 'edge'If the segment runtime is not set, the default nodejs runtime will be used. You do not need to use the runtime option if you do not plan to change from the Node.js runtime.
https://nextjs.org/docs/app/building-your-application/rendering/edge-and-nodejs-runtimes
e595fbf5bdeb-3
Server ActionsServer Actions are an alpha feature in Next.js, built on top of React Actions. They enable server-side data mutations, reduced client-side JavaScript, and progressively enhanced forms. They can be defined inside Server Components and/or called from Client Components: With Server Components: app/add-to-cart.js import { cookies } from 'next/headers' // Server action defined inside a Server Component export default function AddToCart({ productId }) { async function addItem(data) { 'use server' const cartId = cookies().get('cartId')?.value await saveToDb({ cartId, data }) } return ( <form action={addItem}> <button type="submit">Add to Cart</button> </form> ) } With Client Components: app/actions.js 'use server' export async function addItem(data) {
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-0
app/actions.js 'use server' export async function addItem(data) { const cartId = cookies().get('cartId')?.value await saveToDb({ cartId, data }) } app/add-to-cart.js 'use client' import { addItem } from './actions.js' // Server Action being called inside a Client Component export default function AddToCart({ productId }) { return ( <form action={addItem}> <button type="submit">Add to Cart</button> </form> ) } Good to know: Using Server Actions will opt into running the React experimental channel. React Actions, useOptimistic, and useFormStatus are not a Next.js or React Server Components specific features.
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-1
Next.js integrates React Actions into the Next.js router, bundler, and caching system, including adding data mutation APIs like revalidateTag and revalidatePath. Convention You can enable Server Actions in your Next.js project by enabling the experimental serverActions flag. next.config.js module.exports = { experimental: { serverActions: true, }, } Creation Server Actions can be defined in two places: Inside the component that uses it (Server Components only) In a separate file (Client and Server Components), for reusability. You can define multiple Server Actions in a single file. With Server Components Create a Server Action by defining an asynchronous function with the "use server" directive at the top of the function body. This function should have serializable arguments and a serializable return value based on the React Server Components protocol. app/server-component.js export default function ServerComponent() { async function myAction() {
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-2
app/server-component.js export default function ServerComponent() { async function myAction() { 'use server' // ... } } With Client Components If you're using a Server Action inside a Client Component, create your action in a separate file with the "use server" directive at the top of the file. Then, import the Server Action into your Client Component: app/actions.js 'use server' export async function myAction() { // ... } app/client-component.js 'use client' import { myAction } from './actions' export default function ClientComponent() { return ( <form action={myAction}> <button type="submit">Add to Cart</button> </form> ) }
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-3
</form> ) } Good to know: When using a top-level "use server" directive, all exports below will be considered Server Actions. You can have multiple Server Actions in a single file. Invocation You can invoke Server Actions using the following methods: Using action: React's action prop allows invoking a Server Action on a <form> element. Using formAction: React's formAction prop allows handling <button>, <input type="submit">, and <input type="image"> elements in a <form>. Custom Invocation with startTransition: Invoke Server Actions without using action or formAction by using startTransition. This method disables Progressive Enhancement. action You can use React's action prop to invoke a Server Action on a form element. Server Actions passed with the action prop act as asynchronous side effects in response to user interaction. app/add-to-cart.js import { cookies } from 'next/headers'
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-4
app/add-to-cart.js import { cookies } from 'next/headers' export default function AddToCart({ productId }) { async function addItem(data) { 'use server' const cartId = cookies().get('cartId')?.value await saveToDb({ cartId, data }) } return ( <form action={addItem}> <button type="submit">Add to Cart</button> </form> ) } Good to know: An action is similar to the HTML primitive action formAction You can use formAction prop to handle Form Actions on elements such as button, input type="submit", and input type="image". The formAction prop takes precedence over the form's action. app/form.js export default function Form() { async function handleSubmit() { 'use server' // ... }
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-5
async function handleSubmit() { 'use server' // ... } async function submitImage() { 'use server' // ... } return ( <form action={handleSubmit}> <input type="text" name="name" /> <input type="image" formAction={submitImage} /> <button type="submit">Submit</button> </form> ) } Good to know: A formAction is the HTML primitive formaction. React now allows you to pass functions to this attribute. Custom invocation using startTransition You can also invoke Server Actions without using action or formAction. You can achieve this by using startTransition provided by the useTransition hook, which can be useful if you want to use Server Actions outside of forms, buttons, or inputs.
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-6
Good to know: Using startTransition disables the out-of-the-box Progressive Enhancement. app/components/example-client-component.js 'use client' import { useTransition } from 'react' import { addItem } from '../actions' function ExampleClientComponent({ id }) { let [isPending, startTransition] = useTransition() return ( <button onClick={() => startTransition(() => addItem(id))}> Add To Cart </button> ) } app/actions.js 'use server' export async function addItem(id) { await addItemToDb(id) // Marks all product pages for revalidating revalidatePath('/product/[id]') } Custom invocation without startTransition If you aren't doing Server Mutations, you can directly pass the function as a prop like any other function.
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-7
app/posts/[id]/page.tsx import kv from '@vercel/kv' import LikeButton from './like-button' export default function Page({ params }: { params: { id: string } }) { async function increment() { 'use server' await kv.incr(`post:id:${params.id}`) } return <LikeButton increment={increment} /> } app/post/[id]/like-button.tsx 'use client' export default function LikeButton({ increment, }: { increment: () => Promise<void> }) { return ( <button onClick={async () => { await increment() }} > Like </button> ) } Enhancements Experimental useOptimistic
https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions
cc4a940c4965-8