-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
49 lines (42 loc) · 1.19 KB
/
middleware.ts
File metadata and controls
49 lines (42 loc) · 1.19 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
/**
* Next.js middleware for request/response logging
*/
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { logger } from "./lib/logger";
export function middleware(request: NextRequest) {
const startTime = Date.now();
const { pathname, search } = request.nextUrl;
const method = request.method;
// Log incoming request
logger.info("Incoming request", {
method,
path: pathname,
query: search,
userAgent: request.headers.get("user-agent") || "unknown",
});
// Continue with the request
const response = NextResponse.next();
// Log response time
const duration = Date.now() - startTime;
logger.info("Request completed", {
method,
path: pathname,
duration: `${duration}ms`,
status: response.status,
});
return response;
}
// Configure which routes to run middleware on
export const config = {
matcher: [
/*
* Match all request paths except:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};