Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Document redirect parameters #51987

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 26 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,32 @@ 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 | Default | Description |
| --------- | ----------------------- | ----------- | ----------------------------------------------------------- |
| `path` | `string` | - | The URL to redirect to. Can be a relative or absolute path. |
| `type` | `'replace'` or `'push'` | `'replace'` | The type of redirect to perform. |

By default, `redirect` will replace the current URL in the browser history stack. If you want to add a new entry to the history stack (e.g. when using it in a server action to allow the user to return to the previous page), use `push` instead.
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved

## 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 +49,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. |