middlware update
This commit is contained in:
@@ -13,22 +13,43 @@ export function middleware(req: NextRequest) {
|
||||
const host = req.headers.get("host") || "";
|
||||
const tenantPath = HOST_MAP[host.toLowerCase()] || "";
|
||||
|
||||
console.log("[middleware] host:", host, "| tenantPath:", tenantPath || "(none)");
|
||||
|
||||
if (!tenantPath) {
|
||||
console.log("[middleware] → next() (no tenant for host)");
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const pathname = req.nextUrl.pathname;
|
||||
console.log("[middleware] pathname:", pathname);
|
||||
|
||||
// مسیرهای api و استاتیک را rewrite نکن
|
||||
if (
|
||||
pathname.startsWith("/api") ||
|
||||
pathname.startsWith("/_next") ||
|
||||
pathname.includes(".")
|
||||
) {
|
||||
console.log("[middleware] → next() (api/static path)");
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
// اگر کاربر ریشه دامنه را باز کرده، به مسیر tenant ریدایرکت کن (آدرسبار /passata و غیره نشون بده)
|
||||
if (pathname === "/") {
|
||||
const url = req.nextUrl.clone();
|
||||
url.pathname = tenantPath;
|
||||
console.log("[middleware] → redirect to:", url.pathname);
|
||||
return NextResponse.redirect(url);
|
||||
}
|
||||
|
||||
// اگر مسیر از قبل با tenant یکی است (مثلاً /passata یا /passata/menu)، rewrite نکن
|
||||
if (pathname === tenantPath || pathname.startsWith(tenantPath + "/")) {
|
||||
console.log("[middleware] → next() (path already has tenant)");
|
||||
return NextResponse.next();
|
||||
}
|
||||
|
||||
const url = req.nextUrl.clone();
|
||||
url.pathname = `${tenantPath}${pathname}`;
|
||||
console.log("[middleware] → rewrite to:", url.pathname);
|
||||
return NextResponse.rewrite(url);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user