Skip to content

Commit

Permalink
fix: filter out middleware requests in logging
Browse files Browse the repository at this point in the history
  • Loading branch information
huozhi committed Apr 16, 2024
1 parent bde8951 commit 6f323e0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
6 changes: 4 additions & 2 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1132,12 +1132,14 @@ export default class NextNodeServer extends BaseServer {
'originalResponse' in _res ? _res.originalResponse : _res

const reqStart = Date.now()
const isMiddlewareRequest = req.headers['x-middleware-invoke']

const reqCallback = () => {
// we don't log for non-route requests
const isRouteRequest = getRequestMeta(req).match
const routeMatch = getRequestMeta(req).match

const isRSC = isRSCRequestCheck(req)
if (!isRouteRequest || isRSC) return
if (!routeMatch || isRSC || isMiddlewareRequest) return

const reqEnd = Date.now()
const fetchMetrics = normalizedReq.fetchMetrics || []
Expand Down
8 changes: 7 additions & 1 deletion test/e2e/app-dir/logging/app/page.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import Link from 'next/link'

export default function Page() {
return 'hello world'
return (
<>
<Link href={'/link'}>/link</Link>
</>
)
}
14 changes: 8 additions & 6 deletions test/e2e/app-dir/logging/fetch-logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import stripAnsi from 'strip-ansi'
import { retry } from 'next-test-utils'
import { nextTestSetup } from 'e2e-utils'

const cahceReasonRe = /Cache (missed|skipped) reason: /
const cacheReasonRegex = /Cache (missed|skipped) reason: /

interface ParsedLog {
method: string
Expand All @@ -17,13 +17,13 @@ interface ParsedLog {
function parseLogsFromCli(cliOutput: string) {
const logs = stripAnsi(cliOutput)
.split('\n')
.filter((log) => cahceReasonRe.test(log) || log.includes('GET'))
.filter((log) => cacheReasonRegex.test(log) || log.includes('GET'))

return logs.reduce<ParsedLog[]>((parsedLogs, log) => {
if (cahceReasonRe.test(log)) {
if (cacheReasonRegex.test(log)) {
// cache miss/skip reason
// Example of `log`: "│ │ Cache skipped reason: (cache: no-cache)"
const reasonSegment = log.split(cahceReasonRe, 3)[2].trim()
const reasonSegment = log.split(cacheReasonRegex, 3)[2].trim()
const reason = reasonSegment.slice(1, -1)
parsedLogs[parsedLogs.length - 1].cache = reason
} else {
Expand Down Expand Up @@ -162,8 +162,10 @@ describe('app-dir - logging', () => {
await browser.elementByCss('a').click()
await browser.waitForElementByCss('h2')
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
expect(logs).not.toContain('GET /_next/static')
expect(logs).not.toContain('GET /foo?_rsc')
expect(logs).not.toContain('/_next/static')
expect(logs).not.toContain('?_rsc')
// Only show `GET /` once
expect(logs.split('GET /').length).toBe(2)
})
}
} else {
Expand Down

0 comments on commit 6f323e0

Please sign in to comment.