Skip to content

Commit

Permalink
docs: update "Migrate to async Dynamic APIs" docs (vercel#72852)
Browse files Browse the repository at this point in the history
### What?
Update the example code with Codemods applied in [Migrate to async
Dynamic
APIs](https://nextjs.org/docs/app/building-your-application/upgrading/codemods#migrate-to-async-dynamic-apis)
docs

Co-authored-by: Delba de Oliveira <[email protected]>
  • Loading branch information
chogyejin and delbaoliveira authored Nov 19, 2024
1 parent 20dc573 commit 172f3e3
Showing 1 changed file with 20 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,25 @@ Transforms into:

```tsx
import { use } from 'react'
import { cookies, headers, type UnsafeUnwrappedCookies } from 'next/headers'

const token = (await cookies()).get('token')
import {
cookies,
headers,
type UnsafeUnwrappedCookies,
type UnsafeUnwrappedHeaders,
} from 'next/headers'
const token = (cookies() as unknown as UnsafeUnwrappedCookies).get('token')

function useToken() {
const token = use(cookies()).get('token')
return token
}

export default function Page() {
export default async function Page() {
const name = (await cookies()).get('name')
}

function getHeader() {
return (headers() as UnsafeUnwrappedCookies).get('x-foo')
return (headers() as unknown as UnsafeUnwrappedHeaders).get('x-foo')
}
```

Expand All @@ -126,6 +130,7 @@ export default function Page({
}

export function generateMetadata({ params }: { params: { slug: string } }) {
const { slug } = params
return {
title: `My Page - ${slug}`,
}
Expand All @@ -136,18 +141,22 @@ Transforms into:

```tsx
// page.tsx
export default function Page(props: {
params: { slug: string }
searchParams: { [key: string]: string | string[] | undefined }
export default async function Page(props: {
params: Promise<{ slug: string }>
searchParams: Promise<{ [key: string]: string | string[] | undefined }>
}) {
const { value } = await props.searchParams
const searchParams = await props.searchParams
const { value } = searchParams
if (value === 'foo') {
// ...
}
}

export function generateMetadata(props: { params: { slug: string } }) {
const { slug } = await props.params
export async function generateMetadata(props: {
params: Promise<{ slug: string }>
}) {
const params = await props.params
const { slug } = params
return {
title: `My Page - ${slug}`,
}
Expand Down

0 comments on commit 172f3e3

Please sign in to comment.