how to redirect in Route handler
Answered
Param san posted this in #help-forum
How can I redirect to a url of my website from the route handler after the Post request.
93 Replies
It only applies to GET requests where the user directly open the endpoint in browser. For redirecting users after POST request, please use client side navigation like
router.push.ahh yeah that
Thanks for quick help @fuma @riský
I think router.push cannot work as it can only be used in client side pages. There has to be any other way.
how are you running the post request?
@riský how are you running the post request?
I don't know what do you mean exactly.
where are you doing the actual post req
like what calls that function
Cant u use redirect ()?
@riský where are you doing the actual post req
I am getting the data from POST function
@Clown Cant u use redirect ()?
It didn't seem to work.
@Clown Cant u use redirect ()?
we don't know if he is calling the post req on page tho
like where is your fetch request
@riský we don't know if he is calling the post req on page tho
I am making an API call and getting a callback to this route.
I am logging the data in DB and sending the user to another page.
but where do you want your redirection?
@Param san Click to see attachment
Offtopic but are you seriously slicing the url with indexes?
to /orders
so, you should be able to just return redirect
@riský so, you should be able to just return redirect
Let me try again.
@Clown Offtopic but are you seriously slicing the url with indexes?
Yea, is there any other way to get the current URL?
so to make this clear, your user is doing a post request (the page - like from oauth), and you want to redirect them
@Param san Yea, is there any other way to get the current URL?
Pretty sure nextrequest does.
With req.nextUrl
@Clown Pretty sure nextrequest does.
I tried but it didn't work too. I have to check again about it I guess.
@riský yeah then ^ should work
Ohh let me check and get back to you
@riský yeah then ^ should work
Didn't work, it just kep loading.
can you show your new code
@Param san Click to see attachment
This is it
when you use redirect
hmm can you try just redirecting to
"/"and console.log before then (as maybe one of your things takes too long)
and also can you try removing
const res = NextResponse.next() (i don't see the purpose here)When I stopped the terminal, it showed this 🤣
so that means that the connection was staying alove with no end...
@riský can you try this tho ^
I already did
and any console.log to see before redirect occures
@riský and any console.log to see before redirect occures
Didn't had any uncommented one.
so uncomment it
ahhh it just doesn't want to redirect l(
and you is on latest nextjs version? can you also try canary
hmmm it does request /page
does it take long to render
@riský does it take long to render
It's still loading
like try in release mode (next build && next start)
and check the network tab to see if the response had resolved
@riský and check the network tab to see if the response had resolved
Idk how to do that.
Is there anything in particular I have to look for?
ctrl shift i and network tab in dev tools@Param san Is there anything in particular I have to look for?
just see if anything is happening (like 2 multiple requests)
@Param san Oops
hmmmm, but maybe you need to return redirect
and if all else fails try
return NextResponse.redirect("/")Answer
@riský and if all else fails try `return NextResponse.redirect("/")`
Nope, just returning it worked.
yay, but the docs are wrong ðŸ˜
i personally prefer returning it tho
Payment gateways API not working now 🥲
at least one issue is solved 🙂
you should create a new thread for that i think...
but mainly as idk much of supabase (i don't use it)
It's alright!
ahhh the docs wrong... i shouldn't trust it ig
Going to be fixed at the next release
yay!
but imo, returning redirect is better than just using it
@Param san Click to see attachment
they have a PR to fix that (i wouldn't consider that a fix though because it allows
void....), in the meantime just add explicit return typeexport async function POST(): Promise<Response>why does types have to be so pain for this
Always been
i just look at docs and expect it as good code
99.9% of the case correct
Anyway #off-topic
@fuma Anyway <#766433464055496744>
It hasn't answered.
@Param san Payment gateways API not working now 🥲
You should open another thread for it. It’s no longer related to your title “redirect not workingâ€
And I assume your problem with redirect is solved as you said