-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
47 lines (39 loc) · 1.39 KB
/
Copy pathmiddleware.ts
File metadata and controls
47 lines (39 loc) · 1.39 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
import { NextRequest, NextResponse } from 'next/server'
export function middleware(request: NextRequest) {
// Performance monitoring
const start = Date.now()
// Clone the request headers to modify them
const requestHeaders = new Headers(request.headers)
requestHeaders.set('x-start-time', start.toString())
const response = NextResponse.next({
request: {
headers: requestHeaders
}
})
// Add security headers (backup to next.config.js)
response.headers.set('X-Content-Type-Options', 'nosniff')
response.headers.set('X-Frame-Options', 'SAMEORIGIN')
response.headers.set('X-XSS-Protection', '1; mode=block')
// Add performance timing header for monitoring
const duration = Date.now() - start
response.headers.set('X-Response-Time', `${duration}ms`)
// Log performance metrics in development
if (process.env.NODE_ENV === 'development') {
console.log(`${request.method} ${request.nextUrl.pathname} - ${duration}ms`)
}
return response
}
// Configure which paths the middleware runs on
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - og/ (OG image generation)
*/
'/((?!api|_next/static|_next/image|favicon.ico|og).*)',
],
}