redirect function lets you redirect the user to another URL. It can be used in Server Components, Route Handlers, and Server Functions.
redirect()
meta redirect when used inside a streaming context). In Server Actions it issues a 303 See Other.
Parameters
The URL to redirect to. Can be a relative or absolute path.
The redirect type. Defaults to
'push' in Server Actions and 'replace' everywhere else.RedirectType enum for the type parameter:
The
type parameter has no effect in Server Components.Returns
redirect does not return a value. It throws a NEXT_REDIRECT error internally, so it does not need return redirect(...) — the TypeScript type is never.
permanentRedirect()
redirect but issues a 308 Permanent Redirect instead of 307.
Behavior
- In Server Actions and Route Handlers, call
redirectoutside anytry/catchblock, since it works by throwing an error. redirectcan be called in Client Components during rendering, but not inside event handlers. UseuseRouterfor event-driven navigation.redirectaccepts absolute URLs for external redirects.- To redirect before rendering, use
redirectsinnext.config.js.
Why 307 and 308?
Traditional302 and 301 redirects caused browsers to downgrade POST requests to GET. Using 307 (temporary) and 308 (permanent) preserves the original HTTP method.
Examples
Server Component
app/team/[id]/page.tsx
Client Component (during render)
components/client-redirect.tsx
When used in a Client Component during initial SSR, this performs a server-side redirect.
Via a Server Action
app/actions.ts
app/client-form.tsx
Version history
| Version | Changes |
|---|---|
v13.0.0 | redirect introduced. |
