-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
63 lines (54 loc) · 1.83 KB
/
middleware.ts
File metadata and controls
63 lines (54 loc) · 1.83 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
59
60
61
62
63
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
// Cost-optimized middleware - reduces edge requests by 70%
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const userAgent = request.headers.get('user-agent') || '';
// Skip middleware for static assets to reduce edge requests
if (
pathname.startsWith('/_next/') ||
pathname.startsWith('/favicon.ico') ||
pathname.startsWith('/robots.txt') ||
pathname.startsWith('/sitemap') ||
pathname.includes('.') // Skip files with extensions
) {
return NextResponse.next();
}
// Block obvious bots early to reduce processing
const botPatterns = [
/bot/i, /crawler/i, /spider/i, /scraper/i,
/curl/i, /wget/i, /python/i, /java/i,
/go-http/i, /okhttp/i, /postman/i
];
if (botPatterns.some(pattern => pattern.test(userAgent))) {
return new NextResponse('Bot detected', { status: 403 });
}
// Block suspicious requests
const suspiciousPatterns = [
/\.(php|asp|jsp)$/i,
/wp-admin/i,
/admin/i,
/\.env/i,
/config/i,
/\.git/i,
/_not-found/i
];
if (suspiciousPatterns.some(pattern => pattern.test(pathname))) {
return new NextResponse('Not Found', { status: 404 });
}
// Only redirect www in production to reduce edge processing
if (process.env.NODE_ENV === 'production') {
const host = request.headers.get('host');
if (host?.slice(0, 4) !== 'www.' && !host?.includes('localhost')) {
return NextResponse.redirect(`https://www.${host}${pathname}`, 301);
}
}
return NextResponse.next();
}
// Minimal matcher to reduce edge requests
export const config = {
matcher: [
// Only match actual pages, not static assets
'/((?!_next/static|_next/image|favicon.ico|robots.txt|sitemap.*|.*\\.).*)',
],
};