Next.js Discord

Discord Forum

How to effectively redirect to another page on a server component?

Answered
LeonTM posted this in #help-forum
Open in Discord
Hello, I wondered how to redirect to another url with a server component since window is undefined. I found the nextjs docs article about the redirect() and permanentRedirect() functions. I have tried both but had no constant success what so ever. Both functions do not redirect all the time, but I also was not able to track down when they do so.
So how to do those redirects on a server component, and if it is by using one of the two functions, how to use them properly.
Answered by gin
redirect() ?
View full answer

11 Replies

Does anyone see the first message?
So again,
Hello, I wondered how to redirect to another url on a server component since window is undefined. I found the nextjs docs article about the redirect() and permanentRedirect() function and tried both out. I am was not able to get constant redirects when testing the server component but I also could not track down what needs to be for the page to redirect.
So does anyone know how to properly redirect on a server component and if it is by using one of the two functions, how do I use them correctly.
For context: I am trying to redirect from the /choose page where the user chooses between different subscriptions and a /confirm page where the user confirms his order with email and first + last name. On the /choose page a cookie is modified and after the successful modification there should be a redirect from the /choose page to the /confirm page
I am using the method inside of a server action that is given by a server component
Answer
Yes, but why is it not redirecting all the time. It just sometimes redirects
@gin
do u get any errors
No it just doesnt do anything
Okay, I think I have found the error. The button wasnt registering the hit properly, now it works
Sorry for wasting your time