Skip to content

Commit

Permalink
docs(redirect): add typescript example and fix params as promise (#73963
Browse files Browse the repository at this point in the history
)

Hi Team.

This PR enhances the redirect function documentation by adding
TypeScript examples, and fixing the examples where the params object was
destructured without being awaited.

Co-authored-by: JJ Kasper <[email protected]>
  • Loading branch information
devpla and ijjk authored Dec 18, 2024
1 parent 747fe60 commit c4967b4
Showing 1 changed file with 29 additions and 2 deletions.
31 changes: 29 additions & 2 deletions docs/01-app/04-api-reference/04-functions/redirect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,32 @@ The `type` parameter has no effect when used in Server Components.

Invoking the `redirect()` function throws a `NEXT_REDIRECT` error and terminates rendering of the route segment in which it was thrown.

```jsx filename="app/team/[id]/page.js"
```tsx filename="app/team/[id]/page.tsx" switcher
import { redirect } from 'next/navigation'

async function fetchTeam(id: string) {
const res = await fetch('https://...')
if (!res.ok) return undefined
return res.json()
}

export default async function Profile({
params,
}: {
params: Promise<{ id: string }>
}) {
const { id } = await params
const team = await fetchTeam(id)

if (!team) {
redirect('/login')
}

// ...
}
```

```jsx filename="app/team/[id]/page.js" switcher
import { redirect } from 'next/navigation'

async function fetchTeam(id) {
Expand All @@ -54,7 +79,9 @@ async function fetchTeam(id) {
}

export default async function Profile({ params }) {
const team = await fetchTeam(params.id)
const { id } = await params
const team = await fetchTeam(id)

if (!team) {
redirect('/login')
}
Expand Down

0 comments on commit c4967b4

Please sign in to comment.