From ffc386ed93d5d579fdfddea2ab7f9f8d2f99e8c9 Mon Sep 17 00:00:00 2001 From: Rainnny7 Date: Wed, 9 Oct 2024 21:23:57 -0400 Subject: [PATCH] prod request logging Took 16 minutes --- src/middleware.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/middleware.ts diff --git a/src/middleware.ts b/src/middleware.ts new file mode 100644 index 0000000..2f795b3 --- /dev/null +++ b/src/middleware.ts @@ -0,0 +1,26 @@ +import { type NextRequest, NextResponse } from "next/server"; + +export function middleware(request: NextRequest): NextResponse { + const before: number = Date.now(); + const response: NextResponse = NextResponse.next(); + if (process.env.NODE_ENV === "production") { + const ip: string | null = request.headers.get("CF-Connecting-IP"); + console.log( + `${ip} | ${request.method} ${request.nextUrl.pathname} ${response.status} in ${(Date.now() - before).toFixed(0)}ms` + ); + } + return response; +} + +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) + */ + "/((?!api|_next/static|_next/image|favicon.ico).*)", + ], +};