Next.js Discord

Discord Forum

How to redirect to another website?

Unanswered
Bengal posted this in #help-forum
Open in Discord
Avatar
BengalOP
I got a third party url I need to redirect to from my server component. Ideally, I would like this to work without javascript enabled. Using redirect from next/navigation seems to throw an error. What are the alternatives?

Thanks

11 Replies

Avatar
BengalOP
The next.config is a permanent redirect to a hardcoded URL, mine is dynamic.
The other solution requires javascript, ideally I want a solution that works with javascript disabled
Avatar
Ok so @Bengal
I remember the nextjs team mentioning that router.push might work with js disabled (yes I know it needs a client components.. black magic what can you say)
Now I'm not sure
So give it a try
What's the error you get btw using redirect for an external url.. since redirect should just work default with an absolute url
Avatar
BengalOP
I'll check it out, I forgot the error and moved on. I can update the thread tomorrow to get more data
Thanks!
Avatar
you can use permanentRedirect
https://nextjs.org/docs/app/building-your-application/routing/redirecting#permanentredirect-function
permanentRedirect also accepts absolute URLs and can be used to redirect to external links.
Avatar
1. it throwing error is how it works (so if you have try catch, you need to rethrow the redirect error
2. you really need to think about perm redirect as removing that cache from clients takes some time