vercel / platforms

A full-stack Next.js app with multi-tenancy and custom domain support. Built with Next.js App Router and the Vercel Domains API.
https://app.vercel.pub
5.73k stars 764 forks source link

How does redirect preserve the subdomain when printing req.url doesn't show the subdomain #423

Open RahulBirCodes opened 4 months ago

RahulBirCodes commented 4 months ago

In the middleware at this part where it matches requests accessing the app subdomain:

if (hostname == `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) {
    const session = await getToken({ req });
    if (!session && path !== "/login") {
      return NextResponse.redirect(new URL("/login", req.url));
    } else if (session && path == "/login") {
      return NextResponse.redirect(new URL("/", req.url));
    }
    return NextResponse.rewrite(
      new URL(`/app${path === "/" ? "" : path}`, req.url),
    );
  }

when I print out the value of req.url it only gives me http://localhost:3000, however it correctly redirects to app.localhost:3000/login. So the req.url is clearly preserving the value somehow of the domain but doesn't show that when it prints. However, in the rewrite where we add /app to the path we also use req.url but if req.url is preserving the subdomain won't this lead to issues when it reaches the server? Not sure if I'm missing something but any clarity on this is appreciated!