Skip to content

Commit

Permalink
[Docs] Document redirect parameters (#51987)
Browse files Browse the repository at this point in the history
  • Loading branch information
delbaoliveira committed Jul 6, 2023
1 parent bb8cf5e commit 26c0730
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions docs/02-app/02-api-reference/04-functions/redirect.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,34 @@ title: redirect
description: API Reference for the redirect function.
---

The `redirect` function allows you to redirect the user to another URL. If you need to redirect to a 404, you can use the [`notFound` function](/docs/app/api-reference/functions/not-found).
The `redirect` function allows you to redirect the user to another URL. `redirect` can be used in Server Components, Client Components, [Route Handlers](/docs/app/building-your-application/routing/router-handlers), and [Server Actions](/docs/app/building-your-application/data-fetching/server-actions).

## `redirect()`
If you need to redirect to a 404, use the [`notFound` function](/docs/app/api-reference/functions/not-found) instead.

Invoking the `redirect()` function throws a `NEXT_REDIRECT` error and terminates rendering of the route segment in which it was thrown. `redirect()` can be called with a relative or an absolute URL.
## Parameters

The `redirect` function accepts two arguments:

```
redirect(path, type)
```

| Parameter | Type | Description |
| --------- | ------------------------------------------------------------- | ----------------------------------------------------------- |
| `path` | `string` | The URL to redirect to. Can be a relative or absolute path. |
| `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. |

By default, `redirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions)) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter.

The `type` parameter has no effect when used in Server Components.

## Returns

`redirect` does not return any value.

## Example

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"
import { redirect } from 'next/navigation'
Expand All @@ -28,8 +51,8 @@ export default async function Profile({ params }) {
}
```

> **Good to know**: `redirect()` does not require you to use `return redirect()` due to using the TypeScript [`never`](https://www.typescriptlang.org/docs/handbook/2/functions.html#never) type.
> **Good to know**: `redirect` does not require you to use `return redirect()` as it uses the TypeScript [`never`](https://www.typescriptlang.org/docs/handbook/2/functions.html#never) type.
| Version | Changes |
| --------- | ---------------------- |
| `v13.0.0` | `notFound` introduced. |
| `v13.0.0` | `redirect` introduced. |

0 comments on commit 26c0730

Please sign in to comment.