Next.js Discord

Discord Forum

Redirect without port

Answered
Xander posted this in #help-forum
Open in Discord
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.
Answered by Xander
To summarise for the forum:

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.
View full answer

10 Replies

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
oh lol yeah there is a try-catch
how do you rethrow them?
yep that works!
To summarise for the forum:

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