In this video, you'll learn everything about the redirect() function from next/navigation in the Next.js App Router. We’ll cover when and how to use it inside Server Actions, what common errors you might face, and how to fix them effectively—especially when using try-catch blocks.
⚠️ Stop misusing client-side redirection like router.push()—use the powerful server-side redirect() instead for secure, fast, and cleaner navigation. Whether you're working with form submissions, page loaders, or server logic — this is a must-know feature for all Next.js 14+ developers. BONUS: Learn why redirect() must not be used inside try-catch, and what’s the correct approach.
⌛TIMELINE⏳ 0:00 – What is `redirect()` in next/navigation? 1:05 – `redirect()` Function Rules & Restrictions 3:35 – Why You Get "Redirect Error" in Server Actions 4:15 – How to Fix Redirect Errors in try-catch Blocks 5:30 – Correct Way: `redirect()` Outside try-catch 5:15 – Using `redirect()` in Server Actions (Live Demo) 6:25 – What’s Next? Upcoming Video Sneak Peek
Hello, what's up guys? Today in this
video we will see redirect function in our
next application. Yes, guys, today
's video is very important.
And here you can see
what is the work of redirect on the official website. Let
me give you a simple use case.
This is currently a contact form from Lets. Let's
assume this is not a contact form, this is a
registration form. So whenever someone
registers and if it is successful
, we would
like to directly redirect them to the login page. And if
they login successfully then
we would like to redirect them to the home page directly.
Right? So this is where your
redirect comes in handy. And this is where there are
lots of ends. For example, if you are
fetching some data and if it is successful,
you would want to fetch it somewhere else, i.e.
redirect it somewhere else, or you would want to
redirect it somewhere else by clicking on a button.
Basically this
redirect is useful in many places. The most important thing is that
I told you this. The moment if you are
registering for the first time then
you should get the on the spot login home page.
So there are many use cases. But you
need to understand that nexus is all about your server
component and client component, it's a
pull stack framework. So here are some things
you need to keep in mind. And that
is this redirect function
through which you can redirect. It
runs only on your server components. Yes guys, if you
work on the client component then you cannot
use this redirect function there. There's
something more to him which we'll
see in tomorrow's video. But
keep this video in mind today. If you are working
in server side component, basically server component
and there if you want to
use this function of redirect and for that we can go
with redirect.
Here you can see very clearly that this
function allows you to redirect the
user to another URL. Now this
is very important. Redirects can be used
in server components, route handlers, and
server actions. And in our contact form
we are using a server action and server
action is very important. You
need to understand how now you
can perform any action in the server with the help
of your server actions. Ok? Basically we are
talking about the server component. And if
I tell you, you will see it somewhere here
or this is it. Look, this
server action is very important. Then we
have a form action. Then we have a use
action state. Then we looked at the use form
status. So today we have to look at redirects.
Ok? I know, there has been too much talk.
Now let us start the video without any delay.
I will go here.
So this is our code. Ok? Don't
do anything. I want that if
anyone fills our contact form and if
it is successful then he should be directly
redirected to our home page.
Simple best use case right? You should understand that this is
not a contact form. This
is your registration form. See. So here
we are returning this particular object. Right
now I don't want to return anything.
I
would like to redirect them directly. Ok? And
pay attention to one thing.
When you are writing a redirect. So here we
have a redirect and here also we have a
redirect. Right? Butt which
redirect do we need? We Need This Next
Forward slash navigation. This is the
redirect we need. So let me
go back here. I'll hit enter on this one.
So make sure we need to
import this redirect function from a
next ah hfan navigation package. Ok?
Forward slash basically. So
where do you need to go inside this redirect?
Redirect whatever your URL is, I will
tell me to go to the home page and simple one
forward slash represents your home page,
you know, I save it in control s,
now if I go here and fill the data, so
guys, you can see, if I
fill the data, then look,
I got a loading animation, it is very awesome, but here we are getting an
error,
what are we getting here, we are getting an error,
now why are we getting an error, I will tell you whether
they have written it here or not,
I have to see, look carefully at these
all actions and route handlers, the redirect should be
called outside the try block when
using a try catch statement,
write outside the try block, you are saying else, if you
want to write in try catch, see, the redirect
throws an error, so it should be called outside
the try block when using the try catch
statement, the redirect, which is when you are
using try catch, throws an error there.
So that's why when
using a try catch it is basically
saying use it outside of it. So let me tell you this.
Look, there are two ways now,
what can you do here or you can
basically add this redirect outside the try,
but why should I add it outside the try,
right? So now if this one throw is
returning to you, then basically it
returns some throw in response. So what should we do?
Here is the pay code. Look if it is throwing you an
error. Ok? If you were to
use this without try, Next would
automatically handle whatever error or throw
it was causing. But if you
use try catch then it cannot handle it. It
's simple, when you are using try catch it is
throwing an error.
We just have to catch that error. And
we have to throw it away again ourselves.
We have to say that whatever our error
message is, if it is next underscore
redirect, throw error. That's all you have to
write. Having written this much, you no longer have any
tension.
You can very easily write the redirect inside a try catch. Right?
So now I'll do control S save. Let me
refresh it here once. So guys,
now you can see if I click on send,
boom guys, see, this is it, how did
you like it, isn't it, how awesome, here
he has clearly said that these server
actions route handlers redirect should be called
outside the try block when using
a try catch statement, but my condition was
something like this, isn't it, brother, this is such a scene, either
you could have done one thing,
call a function inside this try catch and define it outside and do it
there, otherwise you could have
removed this entire thing like this, even if
you had done it here and had not written this try catch,
it would still have done your work. Then I don't
have to do anything here to get an error message like this. There is
no need to throw any errors manually. Is it
okay if you do this too? I go back to the contact again.
So, you can see guys, if I do this also
and see, my work is done. So, that
means if you are using redirect inside try catch
, then we need to pass basically
we need to write that one piece of code. Where
we get the error message, is this the error message
? If it is then through error. I
will have to write this down. Else there is no tension. Just
use this function called direct redirect. Please mention which
route you want to redirect to.
And next forward slash
navigation, you
just have to import it from the package. Ok? But let me set
it back to normal. This is
it. And remember, redirects
only work on your server components,
server actions or route handlers.
This doesn't work for you in the client component.
What if I need to redirect in some way on the end client component? I will tell you that
in tomorrow's video. That's it for this video today.
I hope you
liked the video. I know the video was short but
important. Right, that's why I
made it alone. But please
do check out the rest of the videos too. If you
like the video then please like and
share it with your friends. And the next target is
8 lakh crore. If you haven't done it yet, please do it. I will
see you in tomorrow's video.
Till then guys thank you so much. Take care.
[music]