Skip to content

Commit

Permalink
fix(log): skip logging non-route requests (#63973)
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 authored Apr 3, 2024
1 parent 2db296e commit add2e6e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 14 deletions.
6 changes: 4 additions & 2 deletions packages/next/src/server/base-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import type { NodeNextRequest, NodeNextResponse } from './base-http/node'
import type { WebNextRequest, WebNextResponse } from './base-http/web'
import type { PagesAPIRouteMatch } from './future/route-matches/pages-api-route-match'
import type { AppRouteRouteHandlerContext } from './future/route-modules/app-route/module'
import type { Server as HTTPServer } from 'http'
import type { Server as HTTPServer, IncomingMessage } from 'http'
import type { MiddlewareMatcher } from '../build/analysis/get-page-static-info'
import type { TLSSocket } from 'tls'
import type { PathnameNormalizer } from './future/normalizers/request/pathname-normalizer'
Expand Down Expand Up @@ -3489,7 +3489,9 @@ export default abstract class Server<ServerOptions extends Options = Options> {
}
}

function isRSCRequestCheck(req: BaseNextRequest): boolean {
export function isRSCRequestCheck(
req: IncomingMessage | BaseNextRequest
): boolean {
return (
req.headers[RSC_HEADER.toLowerCase()] === '1' ||
Boolean(getRequestMeta(req, 'isRSCRequest'))
Expand Down
18 changes: 6 additions & 12 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import type {
RouteHandler,
NextEnabledDirectories,
} from './base-server'
import BaseServer, { NoFallbackError } from './base-server'
import BaseServer, { NoFallbackError, isRSCRequestCheck } from './base-server'
import { getMaybePagePath, getPagePath, requireFontManifest } from './require'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
Expand Down Expand Up @@ -1097,24 +1097,18 @@ export default class NextNodeServer extends BaseServer {
if (this.renderOpts.dev) {
const { blue, green, yellow, red, gray, white } =
require('../lib/picocolors') as typeof import('../lib/picocolors')
const _req = req as NodeNextRequest | IncomingMessage
const _res = res as NodeNextResponse | ServerResponse
const origReq = 'originalRequest' in _req ? _req.originalRequest : _req
const origRes =
'originalResponse' in _res ? _res.originalResponse : _res

const reqStart = Date.now()

const reqCallback = () => {
// if we already logged in a render worker
// don't log again in the router worker.
// we also don't log for middleware alone
if (
(normalizedReq as any).didInvokePath ||
origReq.headers['x-middleware-invoke']
) {
return
}
// we don't log for non-route requests
const isRouteRequest = getRequestMeta(req).match
const isRSC = isRSCRequestCheck(req)
if (!isRouteRequest || isRSC) return

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

export default function Page() {
return <Link href="/foo">Trigger RSC request</Link>
}
11 changes: 11 additions & 0 deletions test/e2e/app-dir/logging/fetch-logging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ describe('app-dir - logging', () => {
expect(output).toContain('Cache skipped reason: (cache: no-store)')
})
})

it('should exlucde Middleware invoked and _rsc requests', async () => {
const outputIndex = next.cliOutput.length
await next.fetch('/')
const browser = await next.browser('/link')
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')
})
}
} else {
// No fetches logging enabled
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/app-dir/logging/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// used to test that if Middleware is present, non-route requests are still not logged
export function middleware() {}

0 comments on commit add2e6e

Please sign in to comment.