Next.js · Middle · Question 28 of 75

What happens with redirect() inside try/catch in a Server Action?

try {
  await save(data);
  redirect('/done');
} catch (e) {
  return { error: 'Failed to save' };
}

Explanation

redirect() and notFound() work by throwing special exceptions. A regular catch swallows them. The fix is to call redirect after the try/catch block or rethrow internal errors (unstable_rethrow(e)).

Further reading

Problem with a question

Next.js · Middle · question 28

Test yourself

Take the Next.js · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz