Part 03 / Topic 6 of 7
useRouter vs redirect(): two tools, two different places
Both exist to send a visitor somewhere else, and the installed package's own redirecting guide draws the line by where the code runs:
| API | Where it runs | Fires from |
|---|---|---|
redirect() (next/navigation) |
Server Components, Server Functions, Route Handlers | During rendering — it throws, it does not return |
useRouter() (next/navigation) |
Client Components | An event handler — a click, a submit, a timer |
The guide is explicit that these are not interchangeable: "redirect can be called in Client Components during the rendering process but not in event handlers. You can use the useRouter hook instead." There is no onClick on the server, so a button's click handler can only ever reach for useRouter().push.
useRouter, in a Client Component event handler — app/store/_components/QuickJump.tsx, a "Surprise me" button on Apna Bazaar's home page:
"use client";
import { useRouter } from "next/navigation";
const productIds = ["p1", "p2", "p3"];
export function QuickJump() {
const router = useRouter();
return (
<button
type="button"
onClick={() => {
const id = productIds[Math.floor(Math.random() * productIds.length)];
router.push(`/store/products/${id}`);
}}
>
Surprise me
</button>
);
}
The button itself renders and builds cleanly — confirmed directly, since this is exactly the kind of claim curl and next build can check:
$ curl -s http://localhost:PORT/store | grep -o '<button[^>]*>[^<]*</button>'
<button type="button">Surprise me</button>
What a click then does — the router.push call itself — is a browser interaction this harness's curl/build convention cannot exercise (the same limitation section 3 already named for template.tsx's remount). The behavior itself is directly from the installed package's reference: router.push "adds a new entry to the browser history stack" and performs the same client-side transition section 4 described for <Link>.
redirect(), in a Server Component — app/store/(shop)/go/[id]/page.tsx models a short link, /store/go/p1, that always forwards to the real product page:
import { redirect } from "next/navigation";
import { products } from "../../../lib/products";
export function generateStaticParams() {
return products.map((product) => ({ id: product.id }));
}
export default async function GoPage({ params }: PageProps<"/store/go/[id]">) {
const { id } = await params;
redirect(`/store/products/${id}`);
}
(generateStaticParams is here for the same reason Part 2 section 4 required it on /store/products/[id] — cacheComponents: true refuses to prerender a dynamic route with unknown params.)
Real response, curled against a running next dev:
$ curl -s -D - -o /dev/null http://localhost:PORT/store/go/p2 | grep -iE "^(HTTP|location|cache-control)"
HTTP/1.1 307 Temporary Redirect
location: /store/products/p2
Cache-Control: no-store
307, not 308 — the installed package's own redirect reference says exactly this: outside a Server Action, redirect "serves a 307 HTTP redirect response," reserving 308 (permanent) for permanentRedirect, a sibling function this part doesn't use. The dev server's own error overlay also names the exact internal signal, the same way Part 2 section 8 captured notFound()'s digest:
$ curl -s http://localhost:PORT/store/go/p2 | grep -o 'data-next-error-digest="[^"]*"'
data-next-error-digest="NEXT_REDIRECT;replace;/store/products/p2;307;"
NEXT_REDIRECT is redirect()'s own throw-based signal, the same pattern notFound() uses with NEXT_HTTP_ERROR_FALLBACK;404 — both functions work by throwing a recognizable error that Next.js's own machinery catches, not by returning a value you check.
Full files: examples/07-quick-jump-use-router.tsx, examples/08-go-redirect-page.tsx.
Done here? The practice set for Part 3 — including the solutions — is on the part page.