Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(vercel): handle vercel previews #325

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -38,7 +43,6 @@ export default async function middleware(req: NextRequest) {
);
}


// special case for `vercel.pub` domain
if (hostname === "vercel.pub") {
return NextResponse.redirect(
Expand All @@ -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));
}

Expand Down