middlware update

This commit is contained in:
hamid zarghami
2026-02-25 19:06:11 +03:30
parent 4dc98c48d9
commit efd4ec0cfa
+21
View File
@@ -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);
}