redirect & permanentRedirect
Programmatic navigation and the try/catch gotcha.
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.
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.
Server Action form redirect
Illustrative progressive-enhancement submission
"use server";
import { redirect } from "next/navigation";
export async function createPost() {
await savePost(); // illustrative mutation
redirect("/dashboard");
// Execution stops here.
console.log("This never runs");
}- 1
Save the post
The Server Action awaits its mutation.
- 2
Throw NEXT_REDIRECT
redirect() stops execution. The log below it never runs.
- 3
Framework handles the throw
Next.js handles the redirect outside your try/catch.
303 See Other
Location: /dashboard. The browser follows with a GET.
Trace complete. Code after redirect is unreachable.
This is a model, not a live request. No post is saved and no navigation runs. With JavaScript, a Server Action redirect performs client navigation; other redirect contexts normally use 307.
Check yourself
Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.
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
throwing an error. - Call
redirect()outside thetryblock so your mutation error handler does not swallow it.
Done with this concept?
Mark it complete to track your progress. No login needed.