Skip to content

Commit

Permalink
polish sync-dynamic-apis error page
Browse files Browse the repository at this point in the history
  • Loading branch information
ztanner committed Oct 9, 2024
1 parent e72cbf3 commit f8c0c30
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions errors/sync-dynamic-apis.mdx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
---
title: dynamic APIs are async
description: This is a migration guide for dynamic APIs that have become async when access was previously possible directly.
title: Dynamic APIs are Asynchronous
description: Learn more about why accessing certain APIs synchronously now warns.
---

## Why This Warning Occurred

Somewhere in your code you used a dynamic API and accessed one of its properties directly.
Somewhere in your code you used an API that opts into [dynamic rendering](/docs/app/building-your-application/rendering/server-components#dynamic-functions).

Dynamic APIs are:

- `params` and `searchParams`
- The `params` and `searchParams` props that get provided to pages, layouts, metadata APIs, and route handlers.
- `cookies()`, `draftMode()`, and `headers()` from `next/headers`

For example:
In Next 15, these APIs have been made asynchronous. You can read more about this in the Next.js 15 [Upgrade Guide](/docs/app/building-your-application/upgrading/version-15)

For example, the following code will issue a warning:

```js filename="app/[id]/page.js"
function Page({ params }) {
Expand All @@ -28,30 +31,26 @@ In future versions, these APIs will be async and direct access will not work as

## Possible Ways to Fix It

The `next-async-request-api` codemod can fix many of these cases automatically:
The [`next-async-request-api` codemod](/docs/app/building-your-application/upgrading/codemods#next-async-request-api) can fix many of these cases automatically:

```bash
$ npx @next/codemod@canary next-async-request-api .
```

The codemod cannot cover all cases, so you may need to manually adjust some code.

The dynamic APIs are now async and return a Promise.

If the warning occured on the Server (e.g. a route handler, or a Server Component),
you must `await` the dynamic API to access its properties:

```js filename="app/[id]/page.js"


function Page({ params }) {
// asynchronous access of `params.id`.
const { id } = await params
return <p>ID: {id}</p>
}
```

If the warning occured on the Client (e.g. a Client component),
If the warning occured in a synchronous component (e.g. a Client component),
you must use `React.use()` to unwrap the Promise first:

```js filename="app/[id]/page.js"
Expand All @@ -65,8 +64,7 @@ function Page({ params }) {
}
```

Keep in mind that you can delay the unwrapping (either via `await` or `React.use`)
until further down in your component tree when you actually need the value.
You don't have to unwrap the Promise immediately at the segment level (Page, Layout, etc).

Unwraping the Promise later will allow Next.js to statically render more of your page before the Page is actually requested.
> **Good to know**:
>
> You can delay unwrapping the Promise (either with `await` or `React.use`) until you actually need to consume the value.
> This will allow Next.js to statically render more of your page.

0 comments on commit f8c0c30

Please sign in to comment.