Files
edueasy/middleware.ts
T

56 lines
1.5 KiB
TypeScript

import { NextResponse } from "next/server"
import type { NextRequest } from "next/server"
const CHILD_HOST = "mate"
const PARENT_HOST = "simulacro"
const isDev = process.env.NODE_ENV === "development"
export function middleware(request: NextRequest) {
const hostname = request.headers.get("host") || ""
const { pathname } = request.nextUrl
if (
pathname.startsWith("/api") ||
pathname.startsWith("/_next") ||
pathname.startsWith("/static") ||
pathname === "/favicon.ico"
) {
return NextResponse.next()
}
if (isDev) {
if (pathname.startsWith("/child") || pathname.startsWith("/parent")) {
return NextResponse.next()
}
const url = request.nextUrl.clone()
url.pathname = "/child"
return NextResponse.rewrite(url)
}
if (hostname.includes(CHILD_HOST)) {
if (pathname === "/" || pathname.startsWith("/child")) {
return NextResponse.next()
}
const url = request.nextUrl.clone()
url.pathname = `/child${pathname === "/" ? "" : pathname}`
return NextResponse.rewrite(url)
}
if (hostname.includes(PARENT_HOST)) {
if (pathname === "/" || pathname.startsWith("/parent")) {
return NextResponse.next()
}
const url = request.nextUrl.clone()
url.pathname = `/parent${pathname === "/" ? "" : pathname}`
return NextResponse.rewrite(url)
}
const url = request.nextUrl.clone()
url.pathname = "/child"
return NextResponse.rewrite(url)
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
}