redirect() in Next.js

redirect() is a server-side navigation function in that instantly sends users to another route before rendering the page.

What is redirect()?

It stops the current rendering process and immediately navigates the user to a different page.

How does it work?

import { redirect } from "next/navigation";

redirect("/login");

When called, rendering stops and the user is instantly redirected.

Why use redirect()?

Example — Protected Page

if (!isLoggedIn) {
  redirect("/login");
}

If the user is not logged in, they are immediately sent to the login page.

Real-World Example

An expired session automatically redirects users to login instead of showing broken or restricted content.

Interview Tip

redirect() is a server-side navigation tool used to instantly reroute users, commonly for authentication and protected routes.