nextjs route.ts request.url is always localhost:8080 even on production
Unanswered
Sun bear posted this in #help-forum
Sun bearOP
Hi, so I am using a route to add a cookie and redirect to my homepage. But for some reason request.url is always localhost..
I also use a middleware to redirect if the user is not authenticated but in here this does work and does not return localhost.
the console.log I added to the route.ts returned this:
Why does this happen..
import { NextResponse } from "next/server";
export async function GET(
request: Request,
{ params }: { params: { userId: string } },
) {
console.log(new URL("/", request.url), request.url);
const response = NextResponse.redirect(new URL("/", request.url));
response.cookies.set({
name: "referral",
value: params.userId,
});
return response;
}I also use a middleware to redirect if the user is not authenticated but in here this does work and does not return localhost.
export async function middleware(request: NextRequest) {
const token = request.cookies.get("token");
if (!token) return NextResponse.redirect(new URL("/", request.url));
const user = await getUser();
if (!user) return NextResponse.redirect(new URL("/", request.url));
return NextResponse.next();
}the console.log I added to the route.ts returned this:
[frontend] [2024-02-21 11:12:08] URL {
[frontend] [2024-02-21 11:12:08] href: 'https://localhost:8080/',
[frontend] [2024-02-21 11:12:08] origin: 'https://localhost:8080',
[frontend] [2024-02-21 11:12:08] protocol: 'https:',
[frontend] [2024-02-21 11:12:08] username: '',
[frontend] [2024-02-21 11:12:08] password: '',
[frontend] [2024-02-21 11:12:08] host: 'localhost:8080',
[frontend] [2024-02-21 11:12:08] hostname: 'localhost',
[frontend] [2024-02-21 11:12:08] port: '8080',
[frontend] [2024-02-21 11:12:08] pathname: '/',
[frontend] [2024-02-21 11:12:08] search: '',
[frontend] [2024-02-21 11:12:08] searchParams: URLSearchParams {},
[frontend] [2024-02-21 11:12:08] hash: ''
[frontend] [2024-02-21 11:12:08] } https://localhost:8080/r/1Why does this happen..