fix: middleware

This commit is contained in:
shinya
2025-06-30 22:57:32 +08:00
parent 79c7a6851f
commit afef6d9865
2 changed files with 8 additions and 4 deletions

View File

@@ -24,7 +24,7 @@ COPY . .
# 在构建阶段也显式设置 DOCKER_ENV
# 确保 Next.js 在编译时即选择 Node Runtime 而不是 Edge Runtime
RUN find ./src -type f -print0 \
RUN find ./src -type f -name "route.ts" -print0 \
| xargs -0 sed -i "s/export const runtime = 'edge';/export const runtime = 'nodejs';/g"
ENV DOCKER_ENV=true

View File

@@ -1,7 +1,5 @@
import { NextRequest, NextResponse } from 'next/server';
export const runtime = 'edge';
// 全站(含 /api鉴权中间件运行于 Edge Runtime。
export async function middleware(req: NextRequest) {
const { pathname, search } = req.nextUrl;
@@ -20,12 +18,18 @@ export async function middleware(req: NextRequest) {
return NextResponse.next();
}
// 内部请求标记,避免递归拦截
if (req.headers.get('x-internal-auth') === 'true') {
return NextResponse.next();
}
// 通过后端接口验证登录状态GET /api/login
const origin = req.nextUrl.origin;
const verifyRes = await fetch(`${origin}/api/login`, {
method: 'GET',
headers: {
Cookie: req.headers.get('cookie') || '',
'x-internal-auth': 'true',
},
});
@@ -47,6 +51,6 @@ export async function middleware(req: NextRequest) {
// 2. 指定哪些路径使用 middleware
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|manifest.json|icons|logo.png|screenshot.png).*)',
'/((?!_next/static|_next/image|favicon.ico|manifest.json|icons|logo.png|screenshot.png|api/login).*)',
],
};