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", ]); export function middleware(request: NextRequest) { const { pathname, search } = request.nextUrl; // /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 && 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("/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/|.*\\..*).*)", ], };