-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
82 lines (71 loc) · 2.21 KB
/
middleware.ts
File metadata and controls
82 lines (71 loc) · 2.21 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
const response = NextResponse.next();
const { pathname, searchParams } = request.nextUrl;
// Handle canonical URLs - remove query parameters for SEO
if (searchParams.has('from') && pathname.startsWith('/projects/')) {
const canonicalUrl = new URL(pathname, request.url);
response.headers.set(
'Link',
`<${canonicalUrl.toString()}>; rel="canonical"`
);
}
// Add security headers
response.headers.set('X-Content-Type-Options', 'nosniff');
response.headers.set('X-Frame-Options', 'DENY');
response.headers.set('X-XSS-Protection', '1; mode=block');
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
// Add cache headers based on path
// Static assets - long cache
if (pathname.match(/\.(js|css|png|jpg|jpeg|gif|svg|ico|woff|woff2)$/)) {
response.headers.set(
'Cache-Control',
'public, max-age=31536000, immutable'
);
}
// API routes - no cache
else if (pathname.startsWith('/api/')) {
response.headers.set(
'Cache-Control',
'no-cache, no-store, must-revalidate'
);
response.headers.set('Pragma', 'no-cache');
response.headers.set('Expires', '0');
}
// Dynamic pages - short cache with revalidation
else if (
pathname.startsWith('/projects/') ||
pathname.startsWith('/people/')
) {
response.headers.set(
'Cache-Control',
'public, max-age=60, stale-while-revalidate=300'
);
}
// List pages - medium cache
else if (pathname === '/projects' || pathname === '/people') {
response.headers.set(
'Cache-Control',
'public, max-age=300, stale-while-revalidate=600'
);
}
// Homepage - short cache
else if (pathname === '/') {
response.headers.set(
'Cache-Control',
'public, max-age=300, stale-while-revalidate=600'
);
}
return response;
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
*/
'/((?!_next/static|_next/image|favicon.ico).*)',
],
};