-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmiddleware.ts
More file actions
58 lines (48 loc) · 1.69 KB
/
middleware.ts
File metadata and controls
58 lines (48 loc) · 1.69 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { createI18nMiddleware } from 'fumadocs-core/i18n';
import { i18n } from '@/lib/i18n';
import {
NextFetchEvent,
NextRequest,
NextResponse,
URLPattern,
} from 'next/server';
export async function middleware(request: NextRequest, event: NextFetchEvent) {
const { pathname } = request.nextUrl;
if (
request.nextUrl.hostname === 'sealos.io' &&
pathname.startsWith('/zh-cn')
) {
return NextResponse.redirect(new URL(`https://sealos.run${pathname}`));
}
if (request.nextUrl.hostname === 'sealos.run' && pathname.startsWith('/en')) {
return NextResponse.redirect(new URL(`https://sealos.io${pathname}`));
}
// Redirect English customers pages to homepage
if (pathname === '/en/customers' || pathname.startsWith('/en/customers/')) {
return NextResponse.redirect(new URL('/', request.url));
}
// If default language is English, also redirect /customers to homepage
if (
(pathname === '/customers' || pathname.startsWith('/customers/')) &&
i18n.defaultLanguage === 'en'
) {
return NextResponse.redirect(new URL('/', request.url));
}
if (pathname === '/robots.txt') {
return NextResponse.rewrite(new URL('/api/robots', request.url));
}
const i18nMiddleware = createI18nMiddleware(i18n);
// @ts-ignore
const response = i18nMiddleware(request, event);
// 将路径信息添加到 headers,供服务端 layout 使用
if (response) {
const resp = await response;
resp?.headers.set('x-pathname', pathname);
}
return response;
}
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|images/|icons/|favicon/|favicon.ico|logo.svg|Deploy-on-Sealos.svg|sitemap.xml|llms.txt|rss.xml|ai-faqs\..*\.json).*)/',
],
};