platforms icon indicating copy to clipboard operation
platforms copied to clipboard

Exemple?

Open lucasdesouza-dev opened this issue 1 year ago • 1 comments

          May be a little convoluted but I was just trying to do something like this.  This is what I came up with as a workaround for local dev and it seems to work.  Basically, I just treated the root domain as a subdomain when developing locally.

I have some variables define that determine the domain I am targeting and will later use this in my middleware to filter where I want things to be routed.

export const ROOT_DOMAIN = `${ROOT}.com`;

export const ROOT_HOSTNAMES = new Set<string>([
  ROOT_DOMAIN,
  "root.localhost:3000",
]);

export const APP_DOMAIN =
  process.env.NEXT_PUBLIC_VERCEL_ENV === "production"
    ? `https://app.${ROOT_DOMAIN}`
    : "http://localhost:3000";

export const APP_HOSTNAMES = new Set<string>([
  `app.${ROOT_DOMAIN}`,
  "localhost:3000",
]);

The key here is making sure the root domain is treated as a subdomain root.localhost:3000 and the subdomain we are targeting is treated as the root localhost:3000.

Next, my middleware function uses the previously defined variables to determine our routes. I have the folders setup as the actual domain names for clarity here

export function middleware(req: NextRequest) {
  const { domain, path, key } = parse(req);

  // rewrite app to `/app.domain.com` folder
  if (APP_HOSTNAMES.has(domain)) {
    return NextResponse.rewrite(
      new URL(`/app.domain.com${path === "/" ? "" : path}`, req.url),
    );
  }

  // rewrite admin to `/admin.domain.com` folder
  if (ADMIN_HOSTNAMES.has(domain)) {
    return NextResponse.rewrite(
      new URL(`/admin.domain.com${path === "/" ? "" : path}`, req.url),
    );
  }

  // rewrite root to `/domain.com` folder
  if (ROOT_HOSTNAMES.has(domain)) {
    return NextResponse.rewrite(
      new URL(`/domain.com${path === "/" ? "" : path}`, req.url),
    );
  }

  // rewrite everything else to `/[domain]/[slug] dynamic route
  return NextResponse.rewrite(new URL(`/${domain}${path}`, req.url));
}

Lastly, we can now go target the standard localhost:3000 and http://localhost:3000/api/auth/callback/google for our Google auth!

Originally posted by @CrutchTheClutch in https://github.com/vercel/platforms/issues/329#issuecomment-2182022845

lucasdesouza-dev avatar Sep 20 '24 21:09 lucasdesouza-dev

Seems people run into this fairly frequently when first starting with the platforms starter kit. +1 a proper example should be added

CrutchTheClutch avatar Sep 21 '24 04:09 CrutchTheClutch

👋 We've updated this repo to have a more simplified example + a new guide:

  • https://github.com/vercel/platforms/pull/451
  • https://vercel.com/guides/nextjs-multi-tenant-application

leerob avatar May 11 '25 14:05 leerob

I'm running into the same problem, and I'm not sure why this is closed as completed - the example is still not clear on how to solve this Google problem?

haowjern avatar Sep 28 '25 03:09 haowjern