nomadro/middleware.ts
eric 56b29205e7 Integrate four verticals into nomadro portal with shared packages and API gateway.
Mount /vip with nomadvip+payjsapi, add @nomadro/pay|pb|sdk, apps/api, /admin shell, and backup/rollback scripts under git.nomadro.cn workflow.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-28 20:16:44 -05:00

105 lines
2.6 KiB
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
/**
* Compat redirects for vertical apps mounted under path prefixes.
* Old standalone URLs → portal mounts.
*/
const COMMUNITY_ROOTS = new Set([
"about",
"ai",
"blog",
"careers",
"changelog",
"chat",
"compare",
"contact",
"cookies",
"dating",
"destinations",
"digital",
"feedback",
"gigs",
"guidelines",
"help",
"join",
"login",
"map",
"meetups",
"members",
"next-stop",
"notifications",
"plan",
"press",
"pricing",
"privacy",
"profile",
"report",
"safety",
"services",
"settings",
"submit",
"support",
"terms",
"tools",
"videos",
]);
/** Standalone nomadvip paths → /vip/* (do not steal /book) */
const VIP_ROOTS = new Set(["topic", "paid", "cloudphone"]);
export function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl;
// Alias: /ebook → /book (product mount path)
if (pathname === "/ebook" || pathname.startsWith("/ebook/")) {
const url = request.nextUrl.clone();
url.pathname = pathname.replace(/^\/ebook(?=\/|$)/, "/book");
return NextResponse.redirect(url, 308);
}
// /read and /read/* already handled by app/read routes.
// Extra: bare community routes that used to live at site root.
const seg = pathname.split("/").filter(Boolean)[0];
if (
seg &&
VIP_ROOTS.has(seg) &&
!pathname.startsWith("/vip/")
) {
const url = request.nextUrl.clone();
url.pathname = `/vip${pathname}`;
return NextResponse.redirect(url, 308);
}
if (
seg &&
COMMUNITY_ROOTS.has(seg) &&
!pathname.startsWith("/community/") &&
pathname !== "/community"
) {
const url = request.nextUrl.clone();
url.pathname = `/community${pathname}`;
return NextResponse.redirect(url, 308);
}
// Fix login?next=/foo → next=/community/foo when missing prefix
if (pathname === "/community/login" || pathname === "/login") {
const next = request.nextUrl.searchParams.get("next");
if (next && next.startsWith("/") && !next.startsWith("/community") && !next.startsWith("/book") && !next.startsWith("/nav") && !next.startsWith("/vip") && !next.startsWith("/http")) {
const url = request.nextUrl.clone();
if (pathname === "/login") {
url.pathname = "/community/login";
}
url.searchParams.set("next", `/community${next === "/" ? "" : next}`);
return NextResponse.redirect(url, 308);
}
}
return NextResponse.next();
}
export const config = {
matcher: [
"/((?!_next/static|_next/image|favicon.ico|icons/|api/|.*\\..*).*)",
],
};