Files
fquiz/web/src/middleware.ts
T
2026-05-02 13:56:23 +08:00

58 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
const RESERVED_PREFIXES = [
"/api",
"/_next",
"/favicon.ico",
"/robots.txt",
"/sitemap.xml",
];
const PUBLIC_FILE = /\.[^/]+$/;
function isBypassedPath(pathname: string): boolean {
if (pathname === "/") {
return true;
}
if (PUBLIC_FILE.test(pathname)) {
return true;
}
return RESERVED_PREFIXES.some(
(prefix) => pathname === prefix || pathname.startsWith(`${prefix}/`),
);
}
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (isBypassedPath(pathname)) {
return NextResponse.next();
}
// Keep backward compatibility for existing /admin links.
if (pathname === "/admin" || pathname === "/admin/") {
const url = request.nextUrl.clone();
url.pathname = "/users";
return NextResponse.redirect(url);
}
if (pathname === "/dashboard" || pathname === "/dashboard/") {
const url = request.nextUrl.clone();
url.pathname = "/users";
return NextResponse.redirect(url);
}
if (pathname.startsWith("/admin/")) {
const url = request.nextUrl.clone();
url.pathname = pathname.slice("/admin".length) || "/users";
return NextResponse.redirect(url);
}
// New public URLs without /admin are internally rewritten to existing routes.
const url = request.nextUrl.clone();
url.pathname = `/admin${pathname}`;
return NextResponse.rewrite(url);
}
export const config = {
matcher: "/:path*",
};