Next.js Discord

Discord Forum

redirect() isn't working?

Answered
Bee posted this in #help-forum
Open in Discord
BeeOP
Hi, I added redirect() to my code and it doesn't seem to redirect to the desired endpoint when clicked. window.location.href = URL seems to work but I'm unsure what I did wrong with the former. I am directing from /docs/api to /docs/api/:id, could such be causing the issue? Thanks
Answered by joulev
redirect() is not server-only but it needs to be caught by a React error boundary for the redirect to work. onClick is not under a React error boundary, so redirect() doesn't work there. use router.push instead
View full answer

8 Replies

What's the code you're using in your redirect
Might want to try router.push
Or just stick with window.location
@Jesse What's the code you're using in your redirect
BeeOP
It's within an onClick event
@Bee It's within an `onClick` event
I would use router.push for that
I think redirect() is server-only
redirect() is not server-only but it needs to be caught by a React error boundary for the redirect to work. onClick is not under a React error boundary, so redirect() doesn't work there. use router.push instead
Answer
BeeOP
Using <Link> worked alright, thanks regardless