Redirect without port
Answered
Xander posted this in #help-forum
XanderOP
I use github codespaces which exposes ports like this:
When I do
http://localhost:3000
-> https://github-codespace-name-300.github.dev
When I do
NextResponse.redirect(new URL("/", request.url))
it redirects to https://github-codespace-name-300.github.dev:3000
which breaks it.Answered by Xander
To summarise for the forum:
use
Ensure to rethrow with [unstable_rethrow](https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow) if inside a try-catch.
use
redirect("/")
instead of return NextResponse.redirect(...)
.Ensure to rethrow with [unstable_rethrow](https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow) if inside a try-catch.
10 Replies
@Xander I use github codespaces which exposes ports like this:
`http://localhost:3000` -> `https://github-codespace-name-300.github.dev`
When I do `NextResponse.redirect(new URL("/", request.url))` it redirects to
`https://github-codespace-name-300.github.dev:3000` which breaks it.
If you use
redirect("/")
instead, does it work? Not NextResponse.redirect, just the same redirect you use in server componentsXanderOP
Error during authentication callback: Error: NEXT_REDIRECT
at GET (src/app/auth/callback/route.ts:51:17)
49 |
50 | // Redirect to the home page
> 51 | redirect("/")
| ^
this is in a route handler
@Xander
Error during authentication callback: Error: NEXT_REDIRECT
at GET (src/app/auth/callback/route.ts:51:17)
49 |
50 | // Redirect to the home page
> 51 | redirect("/")
| ^
Do you have some try/catch on it or something? redirect() should work in route handlers assuming it is not caught by anything
XanderOP
oh lol yeah there is a try-catch
how do you rethrow them?
XanderOP
yep that works!
XanderOP
To summarise for the forum:
use
Ensure to rethrow with [unstable_rethrow](https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow) if inside a try-catch.
use
redirect("/")
instead of return NextResponse.redirect(...)
.Ensure to rethrow with [unstable_rethrow](https://nextjs.org/docs/app/api-reference/functions/unstable_rethrow) if inside a try-catch.
Answer