nomadro/src/middleware.ts
Eric 308974bf5e Harden platform upgrade: ops admin, live config, shared service proxy, and smoke deploys.
Ship Operations console polish, PocketBase social fixtures, and portal join-leads without local DBs or probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 01:20:55 +08:00

124 lines
3.2 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;
if (pathname === '/notifications' || pathname.startsWith('/notifications/')) {
const url = request.nextUrl.clone();
url.pathname = `/social${pathname}`;
return NextResponse.redirect(url, 308);
}
// Site home is 游民社区: serve /social at `/`, canonicalize bare /social → /
if (pathname === "/" || pathname === "") {
const url = request.nextUrl.clone();
url.pathname = "/social";
return NextResponse.rewrite(url);
}
if (pathname === "/social" || pathname === "/social/") {
const url = request.nextUrl.clone();
url.pathname = "/";
url.search = search;
return NextResponse.redirect(url, 308);
}
// 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/|.*\\..*).*)",
],
};