Redirect in Try-Catch

redirect() inside try/catch — Next.js redirect throws, catch swallows it

warningReliabilityredirect-in-try-catch

Why this matters

Next.js redirect() works by throwing a special error. If it's inside a try/catch, the catch block swallows the redirect and the user stays on the current page.

Bad
export default async function Page() {
  try {
    const session = await getSession();
    if (!session) {
      redirect("/login"); // Caught by catch!
    }
    const data = await fetchData();
    return <Dashboard data={data} />;
  } catch (error) {
    return <p>Something went wrong</p>;
  }
}
Good
export default async function Page() {
  const session = await getSession();
  if (!session) {
    redirect("/login"); // Outside try/catch
  }

  try {
    const data = await fetchData();
    return <Dashboard data={data} />;
  } catch (error) {
    return <p>Something went wrong</p>;
  }
}

How to fix

Move redirect() calls outside of try/catch blocks. In Next.js, you can also check for the NEXT_REDIRECT error type and re-throw it.

All rules