Next.js Discord

Discord Forum

Setting a different host for requests in Next.js

Unanswered
Large garden bumble bee posted this in #help-forum
Open in Discord
Large garden bumble beeOP
In Next.js, does anyone know how to change the base url? The use case is in my middleware function
function isExternalHost(url: URL) {
  return url.host !== process.env.NEXT_PUBLIC_SECURE_HOST_URL;
}

function middleware(request: NextRequest) {
  const redirect = request.nextUrl.searchParams.get("redirect");
  const redirectUrl = new URL(redirect, request.url);
  if (isExternalHost(redirectUrl)) {
    console.warn("can't redirect :(");
    return NextResponse.next({ request });
  }
  return NextResponse.redirect(redirectUrl);
}

The problem is, I updated /etc/hosts to https://localhost.<my-domain>:3000 but next is intercepting requests as http://localhost:3000

Is there any way to make it such that it is sending the requests through my aliased url rather than http://localhost:3000?

2 Replies