Next.js Discord

Discord Forum

NextResponse.redirect in GET route failing in production

Answered
American posted this in #help-forum
Open in Discord
AmericanOP
Hello!

I'm running the following in a GET route handler:
return NextResponse.redirect(new URL(url, req.url));


this works well locally but when I use this in production I'm always redirected to http://0.0.0.0:3000/url. Am I missing a env var setting?
Answered by joulev
oh really? then try redirect from next/navigation
View full answer

41 Replies

AmericanOP
This is my full file:
import { type NextRequest, NextResponse } from "next/server";
import { getUserSession } from "~/server/actions/helpers";

const redirect = (url: string, req: NextRequest) => {
  console.log("⬅️ Redirecting to", url, req.url);
  return NextResponse.redirect(new URL(url, req.url));
};

export const GET = async (req: NextRequest) => {
  const session = await getUserSession();
  if (!session) {
    return redirect("/login", req);
  }
  if (!session.user.onboarded) {
    return redirect("/onboarding", req);
  }
  switch (session.user.role) {
    case "STUDENT":
      return redirect("/elev", req);

    case "TEACHER":
      return redirect("/larare", req);

    case "ROOT":
      return redirect("/test", req);
  }
  return redirect("/", req);
};


it's located in src/app/(auth)/logged-in/route.ts, and when I go to localhost:3000/logged-in I get redirected to the correct url, but when I deploy this in production visiting example.com/logged-in redirects me to 0.0.0.0:3000/logged-in.

I'm thinking I need to "tell" next what URL it's deployed on maybe? It's not running on vercel
can you redirect by just using relational urls? (not using the actual req url and just /login)
it could be that you have a reverse proxy for your server, and it connects via localhost:3000, so relational should let the user use what they currently use
AmericanOP
How do I do that? This is just from the docs
just NextResponse.redirect("/test")
AmericanOP
Thank you. Deploying now and will try it out!
even shorter, just redirect("/test") no need of NextResponse
but what risky said should work
@riský just `NextResponse.redirect("/test")`
AmericanOP
That didn't work sadly:
code: 'ERR_INVALID_URL'
input: '/larare',
@joulev even shorter, just `redirect("/test")` no need of `NextResponse`
Yeah, I was thinking about redirect from next/navigation
@joulev even shorter, just `redirect("/test")` no need of `NextResponse`
AmericanOP
That's how I started but that wasn't really working well in route-files
@American That didn't work sadly: code: 'ERR_INVALID_URL' input: '/larare',
oh really? then try redirect from next/navigation
Answer
We're you returning it
AmericanOP
I'm gonna try it again, but as I remember nextjs would generate the correct page, but the URL would be wrong. This in turn made <form action={serverAction} not work and that's why I tried with NextResponse
Well both redirects should do the same thing I thought...
@American I'm gonna try it again, but as I remember nextjs would generate the correct page, but the URL would be wrong. This in turn made `<form action={serverAction}` not work and that's why I tried with NextResponse
in a route handler then it should work well because that part of the nextjs source code i have fully read entirely and the logic is too simple to even have bugs
in a server component then i don't know, it has some niche cases where it doesn't work by design
@riský We're you returning it
AmericanOP
I don't think so, the docs say I don't have to
deploying now with next/navigation to try it again
I have found that you have to return the redirect in route handler (at least some time ago)
AmericanOP
Okay, testing now without return first
which means it doesn't require return
you don't need return throw new Error()
@joulev wrong (?) it just throws `NEXT_REDIRECT`
That's what I thought, but in a few versions ago, it was the only way to redirect
no, i don't recall any versions when it required an explicit return. can you provide a bit more info?
redirect() itself is very simple. it's literally throw NEXT_REDIRECT
and in javascript you don't need to return a throw because it is a throw
@riský I fixed this problem bu doing return https://discord.com/channels/752553802359505017/1155408529851678740/1155422337487876137
that's a type error, related to the infinite PR glitch you already know
not a runtime error. runtime nextjs never required return before redirect() ever since redirect() was a thing
making explicit return was not the only way to handle this type error
Ahh yeah, wrong example (just used search), but I know that I did have to put a return for success once
anyway, i just tested it again, and it works as intended
AmericanOP
Seems to working now 🤔 I wonder what I did differently this time around
Thanks for the help!
AmericanOP
Coming back to this, how can I re-throw the "NEXT_REDIRECT" error?
Like
if (e instanceof NextRedirectError) {

or similar?
I just moved the redirect outside the try/catch
AmericanOP
Awesome, thanks!