Skip to content

Commit f7bb86e

Browse files
Fixed next edge runtime duration calculation method error.
1 parent ac8d972 commit f7bb86e

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/middleware.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ function shouldLog(pathname: string): boolean {
107107
* Enhanced middleware function with comprehensive HTTP access logging
108108
*/
109109
export function middleware(request: NextRequest): NextResponse {
110-
// Use high-resolution time for accurate duration
111-
const startTime = process.hrtime()
110+
// Use Date.now() for Edge Runtime compatibility
111+
const startTime = Date.now()
112112
const { pathname, search } = request.nextUrl
113113
const method = request.method
114114
const userAgent = request.headers.get('user-agent') || 'unknown'
@@ -120,17 +120,15 @@ export function middleware(request: NextRequest): NextResponse {
120120

121121
// Only log if this request should be logged
122122
if (shouldLog(pathname)) {
123-
// Add a custom header to track response time (in nanoseconds)
124-
const startTimeNs = (startTime[0] * 1e9 + startTime[1]).toString()
125-
response.headers.set('x-request-start', startTimeNs)
123+
// Add a custom header to track response time (in ms)
124+
response.headers.set('x-request-start', startTime.toString())
126125

127126
// Log the request immediately (before processing)
128127
const timestamp = new Date().toISOString()
129128
const fullUrl = pathname + search
130129
const status = response.status
131-
// Calculate high-resolution duration in ms (may include decimals)
132-
const diff = process.hrtime(startTime)
133-
const duration = diff[0] * 1000 + diff[1] / 1e6
130+
// Calculate duration in ms
131+
const duration = Date.now() - startTime
134132

135133
logger.info(
136134
`${colors.gray}[${timestamp}]${colors.reset} ` +

0 commit comments

Comments
 (0)