middleware
This commit is contained in:
+30
-5
@@ -1,11 +1,36 @@
|
|||||||
import { NextRequest } from 'next/server';
|
import { NextResponse } from "next/server";
|
||||||
|
import type { NextRequest } from "next/server";
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
// Map host → tenant path
|
||||||
export function middleware(request: NextRequest) {
|
const HOST_MAP: Record<string, string> = {
|
||||||
|
"theboote.tahavol-mr.ir": "/boote",
|
||||||
|
"thesun.tahavol-mr.ir": "/suncafe",
|
||||||
|
// دامنههای جدید اینجا اضافه میشوند
|
||||||
|
};
|
||||||
|
|
||||||
|
export function middleware(req: NextRequest) {
|
||||||
|
const host = req.headers.get("host") || "";
|
||||||
|
const tenantPath = HOST_MAP[host.toLowerCase()] || "";
|
||||||
|
|
||||||
|
if (!tenantPath) {
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
const pathname = req.nextUrl.pathname;
|
||||||
|
// مسیرهای api و استاتیک را rewrite نکن
|
||||||
|
if (
|
||||||
|
pathname.startsWith("/api") ||
|
||||||
|
pathname.startsWith("/_next") ||
|
||||||
|
pathname.includes(".")
|
||||||
|
) {
|
||||||
|
return NextResponse.next();
|
||||||
|
}
|
||||||
|
|
||||||
|
const url = req.nextUrl.clone();
|
||||||
|
url.pathname = `${tenantPath}${pathname}`;
|
||||||
|
return NextResponse.rewrite(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
// only applies this middleware to files in the app directory
|
|
||||||
export const config = {
|
export const config = {
|
||||||
matcher: '/((?!api|static|favicon.ico|.*\\..*|_next).*)'
|
matcher: "/:path*",
|
||||||
};
|
};
|
||||||
Reference in New Issue
Block a user