Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ec5537c

Browse files
committed
fix: filter out middleware requests in logging
1 parent 2e44b49 commit ec5537c

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

packages/next/src/server/next-server.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,12 +1117,14 @@ export default class NextNodeServer extends BaseServer<
11171117
const { originalResponse } = normalizedRes
11181118

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

11211122
const reqCallback = () => {
11221123
// we don't log for non-route requests
1123-
const isRouteRequest = getRequestMeta(req).match
1124+
const routeMatch = getRequestMeta(req).match
1125+
11241126
const isRSC = isRSCRequestCheck(normalizedReq)
1125-
if (!isRouteRequest || isRSC) return
1127+
if (!routeMatch || isRSC || isMiddlewareRequest) return
11261128

11271129
const reqEnd = Date.now()
11281130
const fetchMetrics = normalizedReq.fetchMetrics || []
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
import Link from 'next/link'
2+
13
export default function Page() {
2-
return 'hello world'
4+
return (
5+
<>
6+
<Link href={'/link'}>/link</Link>
7+
</>
8+
)
39
}

test/e2e/app-dir/logging/fetch-logging.test.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import stripAnsi from 'strip-ansi'
44
import { retry } from 'next-test-utils'
55
import { nextTestSetup } from 'e2e-utils'
66

7-
const cahceReasonRe = /Cache (missed|skipped) reason: /
7+
const cacheReasonRegex = /Cache (missed|skipped) reason: /
88

99
interface ParsedLog {
1010
method: string
@@ -17,13 +17,13 @@ interface ParsedLog {
1717
function parseLogsFromCli(cliOutput: string) {
1818
const logs = stripAnsi(cliOutput)
1919
.split('\n')
20-
.filter((log) => cahceReasonRe.test(log) || log.includes('GET'))
20+
.filter((log) => cacheReasonRegex.test(log) || log.includes('GET'))
2121

2222
return logs.reduce<ParsedLog[]>((parsedLogs, log) => {
23-
if (cahceReasonRe.test(log)) {
23+
if (cacheReasonRegex.test(log)) {
2424
// cache miss/skip reason
2525
// Example of `log`: "│ │ Cache skipped reason: (cache: no-cache)"
26-
const reasonSegment = log.split(cahceReasonRe, 3)[2].trim()
26+
const reasonSegment = log.split(cacheReasonRegex, 3)[2].trim()
2727
const reason = reasonSegment.slice(1, -1)
2828
parsedLogs[parsedLogs.length - 1].cache = reason
2929
} else {
@@ -162,8 +162,10 @@ describe('app-dir - logging', () => {
162162
await browser.elementByCss('a').click()
163163
await browser.waitForElementByCss('h2')
164164
const logs = stripAnsi(next.cliOutput.slice(outputIndex))
165-
expect(logs).not.toContain('GET /_next/static')
166-
expect(logs).not.toContain('GET /foo?_rsc')
165+
expect(logs).not.toContain('/_next/static')
166+
expect(logs).not.toContain('?_rsc')
167+
// Only show `GET /` once
168+
expect(logs.split('GET /').length).toBe(2)
167169
})
168170
}
169171
} else {

0 commit comments

Comments
 (0)