-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
37 lines (30 loc) · 1.41 KB
/
middleware.ts
File metadata and controls
37 lines (30 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import createMiddleware from "next-intl/middleware";
import { routing } from "./i18n/routing";
import { NextRequest, NextResponse } from "next/server";
// 创建国际化中间件
const handleI18nRouting = createMiddleware(routing);
export default function middleware(request: NextRequest) {
// 首先处理国际化路由
const response = handleI18nRouting(request);
// 添加SEO和性能优化头部
response.headers.set('X-DNS-Prefetch-Control', 'on');
response.headers.set('X-Content-Type-Options', 'nosniff');
response.headers.set('Referrer-Policy', 'origin-when-cross-origin');
response.headers.set('X-Frame-Options', 'DENY');
// 性能优化缓存策略
const { pathname } = request.nextUrl;
if (pathname.startsWith('/imgs/') || pathname.startsWith('/icons/') || pathname.endsWith('.svg') || pathname.endsWith('.png') || pathname.endsWith('.jpg')) {
response.headers.set('Cache-Control', 'public, max-age=31536000, immutable');
} else if (pathname === '/' || pathname.startsWith('/create') || pathname.startsWith('/video/')) {
// 对主要页面设置适当的缓存
response.headers.set('Cache-Control', 'public, s-maxage=3600, stale-while-revalidate=86400');
}
return response;
}
export const config = {
matcher: [
"/",
"/(en|en-US|zh|zh-CN|zh-TW|zh-HK|zh-MO|ja|ko|ru|fr|de|ar|es|it)/:path*",
"/((?!privacy-policy|terms-of-service|api/|_next|_vercel|.*\\..*).*)",
],
};