Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply Next.js best practices for RSC boundaries, async APIs, routing, metadata, and optimization.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
async-patterns.md
1# Async Patterns23In Next.js 15+, `params`, `searchParams`, `cookies()`, and `headers()` are asynchronous.45## Async Params and SearchParams67Always type them as `Promise<...>` and await them.89### Pages and Layouts1011```tsx12type Props = { params: Promise<{ slug: string }> }1314export default async function Page({ params }: Props) {15const { slug } = await params16}17```1819### Route Handlers2021```tsx22export async function GET(23request: Request,24{ params }: { params: Promise<{ id: string }> }25) {26const { id } = await params27}28```2930### SearchParams3132```tsx33type Props = {34params: Promise<{ slug: string }>35searchParams: Promise<{ query?: string }>36}3738export default async function Page({ params, searchParams }: Props) {39const { slug } = await params40const { query } = await searchParams41}42```4344### Synchronous Components4546Use `React.use()` for non-async components:4748```tsx49import { use } from 'react'5051type Props = { params: Promise<{ slug: string }> }5253export default function Page({ params }: Props) {54const { slug } = use(params)55}56```5758### generateMetadata5960```tsx61type Props = { params: Promise<{ slug: string }> }6263export async function generateMetadata({ params }: Props): Promise<Metadata> {64const { slug } = await params65return { title: slug }66}67```6869## Async Cookies and Headers7071```tsx72import { cookies, headers } from 'next/headers'7374export default async function Page() {75const cookieStore = await cookies()76const headersList = await headers()7778const theme = cookieStore.get('theme')79const userAgent = headersList.get('user-agent')80}81```8283## Migration Codemod8485```bash86npx @next/codemod@latest next-async-request-api .87```88