From af9794c384d7c8345dbba071a30e92b2013e983e Mon Sep 17 00:00:00 2001 From: Omer Cohen Date: Wed, 27 Sep 2023 17:42:25 +0300 Subject: [PATCH 1/3] feat(vercel): handle vercel previews get the hostname from searchParams so it works with vercel previews --- middleware.ts | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/middleware.ts b/middleware.ts index bc51f223e..9a9f1f5b2 100644 --- a/middleware.ts +++ b/middleware.ts @@ -15,18 +15,23 @@ export const config = { }; export default async function middleware(req: NextRequest) { - const url = req.nextUrl; + const url = new URL(req.url); + + if (process.env.VERCEL_ENV != "production" && !url.searchParams.has("host")) { + url.searchParams.set("host", "app"); + return NextResponse.redirect(url); + } // Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000) - const hostname = req.headers - .get("host")! - .replace(".localhost:3000", `.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`); + const hostname = (url.searchParams.get("host") ?? req.headers.get("host")!) + .replace(".localhost:3000", ``) + .replace(`.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`, ``); // Get the pathname of the request (e.g. /, /about, /blog/first-post) const path = url.pathname; // rewrites for app pages - if (hostname == `app.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`) { + if (hostname == "app") { const session = await getToken({ req }); if (!session && path !== "/login") { return NextResponse.redirect(new URL("/login", req.url)); @@ -38,7 +43,6 @@ export default async function middleware(req: NextRequest) { ); } - // special case for `vercel.pub` domain if (hostname === "vercel.pub") { return NextResponse.redirect( @@ -47,10 +51,7 @@ export default async function middleware(req: NextRequest) { } // rewrite root application to `/home` folder - if ( - hostname === "localhost:3000" || - hostname === process.env.NEXT_PUBLIC_ROOT_DOMAIN - ) { + if (hostname === "" || hostname === "www") { return NextResponse.rewrite(new URL(`/home${path}`, req.url)); } From e07a3ec88c7441422e693f235ea2f4e84e2c18aa Mon Sep 17 00:00:00 2001 From: Omer Cohen Date: Wed, 27 Sep 2023 15:25:15 +0000 Subject: [PATCH 2/3] clean it up --- middleware.ts | 34 +++++++++++++++------------------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/middleware.ts b/middleware.ts index 9a9f1f5b2..1d8bdc19f 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,5 +1,5 @@ -import { NextRequest, NextResponse } from "next/server"; import { getToken } from "next-auth/jwt"; +import { NextRequest, NextResponse } from "next/server"; export const config = { matcher: [ @@ -15,7 +15,7 @@ export const config = { }; export default async function middleware(req: NextRequest) { - const url = new URL(req.url); + const url = new URL(req.nextUrl); if (process.env.VERCEL_ENV != "production" && !url.searchParams.has("host")) { url.searchParams.set("host", "app"); @@ -23,38 +23,34 @@ export default async function middleware(req: NextRequest) { } // Get hostname of request (e.g. demo.vercel.pub, demo.localhost:3000) - const hostname = (url.searchParams.get("host") ?? req.headers.get("host")!) - .replace(".localhost:3000", ``) - .replace(`.${process.env.NEXT_PUBLIC_ROOT_DOMAIN}`, ``); + const subdomain = url.searchParams.get("host") ?? req.headers.get("host")!; // Get the pathname of the request (e.g. /, /about, /blog/first-post) const path = url.pathname; // rewrites for app pages - if (hostname == "app") { + if (subdomain == "app") { 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), - ); - } - - // special case for `vercel.pub` domain - if (hostname === "vercel.pub") { - return NextResponse.redirect( - "https://vercel.com/blog/platforms-starter-kit", - ); + return rewrite(`/app${path === "/" ? "" : path}`, req); } // rewrite root application to `/home` folder - if (hostname === "" || hostname === "www") { - return NextResponse.rewrite(new URL(`/home${path}`, req.url)); + if (subdomain === "" || subdomain === "www") { + return rewrite(`/home${path}`, req); } // rewrite everything else to `/[domain]/[path] dynamic route - return NextResponse.rewrite(new URL(`/${hostname}${path}`, req.url)); + return rewrite(`/${subdomain}${path}`, req); } + +const rewrite = (url: string, req: NextRequest) => { + const rewrite = new URL(url, req.url); + const res = NextResponse.rewrite(rewrite); + res.headers.set("x-rewrite", rewrite.toString()); + return res; +}; From 3e107f5d36b586a4f2ff9120601dc92a8e805088 Mon Sep 17 00:00:00 2001 From: Omer Cohen Date: Wed, 27 Sep 2023 15:28:29 +0000 Subject: [PATCH 3/3] vercel.pub --- middleware.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/middleware.ts b/middleware.ts index 1d8bdc19f..388f8b85d 100644 --- a/middleware.ts +++ b/middleware.ts @@ -38,7 +38,13 @@ export default async function middleware(req: NextRequest) { } return rewrite(`/app${path === "/" ? "" : path}`, req); } - + // special case for `vercel.pub` domain + if (subdomain === "vercel.pub") { + return NextResponse.redirect( + "https://vercel.com/blog/platforms-starter-kit", + ); + } + // rewrite root application to `/home` folder if (subdomain === "" || subdomain === "www") { return rewrite(`/home${path}`, req);