Unverified Commit 26c07309 authored by Delba de Oliveira's avatar Delba de Oliveira Committed by GitHub
parent bb8cf5e3
...@@ -3,11 +3,34 @@ title: redirect ...@@ -3,11 +3,34 @@ title: redirect
description: API Reference for the redirect function. 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" ```jsx filename="app/team/[id]/page.js"
import { redirect } from 'next/navigation' import { redirect } from 'next/navigation'
...@@ -28,8 +51,8 @@ export default async function Profile({ params }) { ...@@ -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 | | Version | Changes |
| --------- | ---------------------- | | --------- | ---------------------- |
| `v13.0.0` | `notFound` introduced. | | `v13.0.0` | `redirect` introduced. |
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment