# redirect & permanentRedirect

> Programmatic navigation and the try/catch gotcha.

Subject: Next.js | Lesson 21 of 65 | 4 min read
Interactive lesson: https://codewithanimation.in/nextjs/redirect-api
Previous: [<Link> Component](https://codewithanimation.in/nextjs/link-component.md) | Next: [Request Memoization](https://codewithanimation.in/nextjs/request-memoization.md)

## Programmatic routing

While the `<Link>` component handles user clicks, there are many scenarios where you need to navigate programmatically, like after a successful form submission or when an unauthenticated user tries to access a protected route.

Next.js provides the `redirect()` and `permanentRedirect()` functions from `next/navigation` for exactly this purpose.

## Status codes depend on context

In a Server Action, `redirect()` performs client navigation when JavaScript is available. A progressive-enhancement form submission receives 303 See Other, so the browser follows the destination with a GET.

Outside Server Actions, `redirect()` normally uses 307 Temporary Redirect and `permanentRedirect()` normally uses 308 Permanent Redirect. A 308 signals a permanent URL move; it does not guarantee search ranking. Server Action form submissions use 303 with either function.

## The throw mechanism

Here is the most critical architectural detail about `redirect()`: it does not return a value. Instead, it internally `throws` a special Next.js error.

The throw stops execution, so statements after the call do not run. Next.js handles navigation according to context: client navigation for JavaScript-enabled Server Actions, 303 for progressive-enhancement action forms, or normally 307/308 elsewhere. In streaming contexts, it can insert redirect metadata into the response.

## Common mistake: The try/catch gotcha

Because `redirect()` throws an error, wrapping it inside a standard `try/catch` block is a massive trap.

A catch block can swallow the redirect throw and prevent navigation. Handle mutation failures in your `try/catch`, then call `redirect()` outside the `try` block, after the mutation succeeds.

## Tip: Client vs Server

You can use `redirect()` in Server Components, Server Actions, Route Handlers, and even Client Components (during the render phase).

However, if you need to programmatically navigate inside an event handler (like `onClick`) in a Client Component, you must use the `useRouter().push()` method instead.

## Try it: The Throw Mechanism

Replay an illustrative progressive-enhancement Server Action form trace: the mutation completes, redirect throws, and the framework responds with 303 See Other. This model performs no database write or real navigation.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/nextjs/redirect-api

## Check yourself

### Question 1

Why is it dangerous to call redirect() inside a try/catch block?

- A. It causes an infinite redirect loop
- B. It throws a special error that the catch block will swallow
- C. It converts a 307 redirect into a 500 error
- D. It forces the component to render on the client

Correct answer: B

Next.js relies on the thrown error bubbling up to the framework level. If your `catch` block swallows it, Next.js never knows a redirect was requested.

- Why not A: It actually stops the redirect entirely.
- Why not C: The catch block swallows the error, so no 500 is sent unless your catch block manually throws one.
- Why not D: It has no effect on rendering environments.

### Question 2

Which function should you use after a user successfully submits a 'Create Post' form?

- A. redirect()
- B. permanentRedirect()
- C. useRouter().replace()
- D. window.location.href

Correct answer: A

Use `redirect()` after the successful Server Action mutation. With JavaScript it performs client navigation; a progressive-enhancement form receives 303 See Other. A permanent URL relocation is a different use case. Outside Server Actions, redirect normally uses 307 and permanentRedirect normally uses 308.

- Why not B: A successful form submission is a temporary state change, not a permanent relocation of the URL.
- Why not C: While possible on the client, redirect() is the standard for Server Actions.
- Why not D: Avoid this in Next.js as it breaks the SPA experience.

## Remember this

- Server Action redirects use client navigation with JavaScript, or 303 for progressive-enhancement forms; otherwise redirect normally uses 307.
- permanentRedirect normally uses 308 outside Server Actions; Server Action form submissions use 303.
- Both functions work by `throw`ing an error.
- Call `redirect()` outside the `try` block so your mutation error handler does not swallow it.
