-
Notifications
You must be signed in to change notification settings - Fork 13
/
middleware.ts
59 lines (49 loc) · 1.74 KB
/
middleware.ts
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
59
import createMiddleware from 'next-intl/middleware';
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { routing } from './i18n/routing';
const middleware = createMiddleware(routing);
function detectSearchEngine(userAgent: string): string | null {
const searchEngines = [
{ name: 'Googlebot', pattern: /Googlebot/ },
{ name: 'Bingbot', pattern: /Bingbot/ },
{ name: 'Baiduspider', pattern: /Baiduspider/ },
{ name: 'YandexBot', pattern: /YandexBot/ },
{ name: 'DuckDuckBot', pattern: /DuckDuckBot/ },
{ name: 'Slurp', pattern: /Slurp/ }, // Yahoo's bot
{ name: 'Sogou', pattern: /Sogou/ },
{ name: 'AhrefsBot', pattern: /AhrefsBot/ },
{ name: 'SemrushBot', pattern: /SemrushBot/ },
{ name: 'Naverbot', pattern: /Naverbot/ },
];
for (const engine of searchEngines) {
if (engine.pattern.test(userAgent)) {
return engine.name;
}
}
return null;
}
export default async function customMiddleware(request: NextRequest) {
console.log(`Current request URL: ${request.url}`);
const userAgent = request.headers.get('user-agent') || '';
const searchEngine = detectSearchEngine(userAgent);
if (searchEngine) {
console.log(`Detected search engine crawler: ${searchEngine}`);
}
const pathname = request.nextUrl.pathname;
if (/^\/(p|indiehackers|startup\/yc|taaft)\//.test(pathname)) {
const newUrl = new URL(`/en${pathname}`, request.url);
return NextResponse.redirect(newUrl, 301);
}
return middleware(request);
}
export const config = {
matcher: [
'/',
'/(en|zh|es|ar|hi|pt|ja|ru|id|tr)/:path*',
'/(p|indiehackers|startup/yc|taaft|topic|category)/:path*',
'/search/:path*',
'/favorites',
'/email/:path*'
]
};