douyinWEB/frontend/src/middleware.ts
eric abf37e9abd fix: 放行 PWA manifest/sw 免登录访问
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-14 22:21:33 -05:00

45 lines
1.1 KiB
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const PUBLIC = ["/login", "/privacy", "/terms"];
const PUBLIC_ASSETS = [
"/manifest.webmanifest",
"/sw.js",
"/favicon.ico",
"/icon-192.png",
"/icon-512.png",
"/apple-touch-icon.png",
];
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
if (
pathname.startsWith("/_next") ||
pathname.startsWith("/favicon") ||
PUBLIC_ASSETS.includes(pathname) ||
pathname.match(/\.(ico|png|jpg|jpeg|svg|webp|css|js|webmanifest|map)$/)
) {
return NextResponse.next();
}
const session = request.cookies.get("dy_session")?.value;
const isLogin = PUBLIC.some((p) => pathname === p || pathname.startsWith(`${p}/`));
if (!session && !isLogin) {
const url = request.nextUrl.clone();
url.pathname = "/login";
url.searchParams.set("next", pathname);
return NextResponse.redirect(url);
}
if (session && isLogin) {
return NextResponse.redirect(new URL("/", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next/static|_next/image).*)"],
};