260223:1415 20260223 nextJS & nestJS Best pratices
All checks were successful
Build and Deploy / deploy (push) Successful in 4m44s
All checks were successful
Build and Deploy / deploy (push) Successful in 4m44s
This commit is contained in:
87
.agents/skills/next-best-practices/async-patterns.md
Normal file
87
.agents/skills/next-best-practices/async-patterns.md
Normal file
@@ -0,0 +1,87 @@
|
||||
# Async Patterns
|
||||
|
||||
In Next.js 15+, `params`, `searchParams`, `cookies()`, and `headers()` are asynchronous.
|
||||
|
||||
## Async Params and SearchParams
|
||||
|
||||
Always type them as `Promise<...>` and await them.
|
||||
|
||||
### Pages and Layouts
|
||||
|
||||
```tsx
|
||||
type Props = { params: Promise<{ slug: string }> }
|
||||
|
||||
export default async function Page({ params }: Props) {
|
||||
const { slug } = await params
|
||||
}
|
||||
```
|
||||
|
||||
### Route Handlers
|
||||
|
||||
```tsx
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const { id } = await params
|
||||
}
|
||||
```
|
||||
|
||||
### SearchParams
|
||||
|
||||
```tsx
|
||||
type Props = {
|
||||
params: Promise<{ slug: string }>
|
||||
searchParams: Promise<{ query?: string }>
|
||||
}
|
||||
|
||||
export default async function Page({ params, searchParams }: Props) {
|
||||
const { slug } = await params
|
||||
const { query } = await searchParams
|
||||
}
|
||||
```
|
||||
|
||||
### Synchronous Components
|
||||
|
||||
Use `React.use()` for non-async components:
|
||||
|
||||
```tsx
|
||||
import { use } from 'react'
|
||||
|
||||
type Props = { params: Promise<{ slug: string }> }
|
||||
|
||||
export default function Page({ params }: Props) {
|
||||
const { slug } = use(params)
|
||||
}
|
||||
```
|
||||
|
||||
### generateMetadata
|
||||
|
||||
```tsx
|
||||
type Props = { params: Promise<{ slug: string }> }
|
||||
|
||||
export async function generateMetadata({ params }: Props): Promise<Metadata> {
|
||||
const { slug } = await params
|
||||
return { title: slug }
|
||||
}
|
||||
```
|
||||
|
||||
## Async Cookies and Headers
|
||||
|
||||
```tsx
|
||||
import { cookies, headers } from 'next/headers'
|
||||
|
||||
export default async function Page() {
|
||||
const cookieStore = await cookies()
|
||||
const headersList = await headers()
|
||||
|
||||
const theme = cookieStore.get('theme')
|
||||
const userAgent = headersList.get('user-agent')
|
||||
}
|
||||
```
|
||||
|
||||
## Migration Codemod
|
||||
|
||||
```bash
|
||||
npx @next/codemod@latest next-async-request-api .
|
||||
```
|
||||
Reference in New Issue
Block a user