From efd4ec0cfa65b3893d1248b480650b07dea5f640 Mon Sep 17 00:00:00 2001 From: hamid zarghami Date: Wed, 25 Feb 2026 19:06:11 +0330 Subject: [PATCH] middlware update --- src/middleware.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/middleware.ts b/src/middleware.ts index f9a2da9..e1eab93 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -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); }