Unverified Commit 7569c808 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

feat(log): improve dev/build logs (#62946)

Co-authored-by: 's avatarJiachi Liu <inbox@huozhi.im>
parent 740d1032
......@@ -68,19 +68,16 @@ export const createProgress = (total: number, label: string) => {
}
const isFinished = curProgress === total
const message = `${label} (${curProgress}/${total})`
if (progressSpinner && !isFinished) {
progressSpinner.setText(`${label} (${curProgress}/${total})`)
progressSpinner.setText(message)
} else {
if (progressSpinner) {
progressSpinner.stop()
progressSpinner?.stop()
if (isFinished) {
Log.event(message)
} else {
Log.info(`${message} ${process.stdout.isTTY ? '\n' : '\r'}`)
}
console.log(
` ${
isFinished ? Log.prefixes.event : Log.prefixes.info
} ${label} (${curProgress}/${total}) ${
isFinished ? '' : process.stdout.isTTY ? '\n' : '\r'
}`
)
}
}
}
......@@ -47,18 +47,18 @@ export default function createSpinner(
console.warn = origWarn
console.error = origError
}
spinner.setText = (newText: string) => {
spinner.setText = (newText) => {
text = newText
prefixText = ` ${Log.prefixes.info} ${newText} `
spinner!.prefixText = prefixText
return spinner!
}
spinner.stop = (): ora.Ora => {
spinner.stop = () => {
origStop()
resetLog()
return spinner!
}
spinner.stopAndPersist = (): ora.Ora => {
spinner.stopAndPersist = () => {
// Add \r at beginning to reset the current line of loading status text
const suffixText = `\r ${Log.prefixes.event} ${text} `
if (spinner) {
......
......@@ -481,7 +481,7 @@ export async function printTreeView(
if (item === '/_app' || item === '/_app.server') {
symbol = ' '
} else if (isEdgeRuntime(pageInfo?.runtime)) {
symbol = 'ℇ'
symbol = 'ƒ'
} else if (pageInfo?.isPPR) {
if (
// If the page has an empty prelude, then it's equivalent to a dynamic page
......@@ -490,7 +490,7 @@ export async function printTreeView(
// since in this case we're able to partially prerender it
(pageInfo.isDynamicAppRoute && !pageInfo.hasPostponed)
) {
symbol = 'λ'
symbol = 'ƒ'
} else if (!pageInfo?.hasPostponed) {
symbol = '○'
} else {
......@@ -501,7 +501,7 @@ export async function printTreeView(
} else if (pageInfo?.isSSG) {
symbol = '●'
} else {
symbol = 'λ'
symbol = 'ƒ'
}
usedSymbols.add(symbol)
......@@ -749,16 +749,7 @@ export async function printTreeView(
'(Partial Prerender)',
'prerendered as static HTML with dynamic server-streamed content',
],
usedSymbols.has('λ') && [
'λ',
'(Dynamic)',
`server-rendered on demand using Node.js`,
],
usedSymbols.has('ℇ') && [
'ℇ',
'(Edge Runtime)',
`server-rendered on demand using the Edge Runtime`,
],
usedSymbols.has('ƒ') && ['ƒ', '(Dynamic)', `server-rendered on demand`],
].filter((x) => x) as [string, string, string][],
{
align: ['l', 'l', 'l'],
......
......@@ -18,6 +18,7 @@ import { normalizeAppPath } from '../../../../shared/lib/router/utils/app-paths'
import type { SizeLimit } from '../../../../../types'
import { internal_getCurrentFunctionWaitUntil } from '../../../../server/web/internal-edge-wait-until'
import type { PAGE_TYPES } from '../../../../lib/page-types'
import type { NextRequestHint } from '../../../../server/web/adapter'
export function getRender({
dev,
......@@ -151,23 +152,23 @@ export function getRender({
const handler = server.getRequestHandler()
return async function render(request: Request, event: NextFetchEvent) {
return async function render(
request: NextRequestHint,
event?: NextFetchEvent
) {
const extendedReq = new WebNextRequest(request)
const extendedRes = new WebNextResponse()
handler(extendedReq, extendedRes)
const result = await extendedRes.toResponse()
if (event && event.waitUntil) {
if (event?.waitUntil) {
const waitUntilPromise = internal_getCurrentFunctionWaitUntil()
if (waitUntilPromise) {
event.waitUntil(waitUntilPromise)
}
}
// fetchMetrics is attached to the web request that going through the server,
// wait for the handler result is ready and attach it back to the original request.
;(request as any).fetchMetrics = extendedReq.fetchMetrics
return result
}
}
......@@ -4,6 +4,7 @@ import type { AsyncLocalStorage } from 'async_hooks'
import type { IncrementalCache } from '../lib/incremental-cache'
import { createPrerenderState } from '../../server/app-render/dynamic-rendering'
import type { FetchMetric } from '../base-http'
export type StaticGenerationContext = {
urlPathname: string
......@@ -31,6 +32,8 @@ export type StaticGenerationContext = {
*/
// TODO: remove this when we resolve accessing the store outside the execution context
store?: StaticGenerationStore
/** Fetch metrics attached in patch-fetch.ts */
fetchMetrics?: FetchMetric[]
}
}
......
......@@ -11,7 +11,7 @@ export interface BaseNextRequestConfig {
trailingSlash?: boolean | undefined
}
export type FetchMetrics = Array<{
export type FetchMetric = {
url: string
idx: number
end: number
......@@ -20,11 +20,14 @@ export type FetchMetrics = Array<{
status: number
cacheReason: string
cacheStatus: 'hit' | 'miss' | 'skip'
}>
}
export type FetchMetrics = Array<FetchMetric>
export abstract class BaseNextRequest<Body = any> {
protected _cookies: NextApiRequestCookies | undefined
public abstract headers: IncomingHttpHeaders
public abstract fetchMetrics?: FetchMetric[]
constructor(public method: string, public url: string, public body: Body) {}
......
......@@ -7,16 +7,18 @@ import type { NextApiRequestCookies } from '../api-utils'
import { NEXT_REQUEST_META } from '../request-meta'
import type { RequestMeta } from '../request-meta'
import { BaseNextRequest, BaseNextResponse } from './index'
import { BaseNextRequest, BaseNextResponse, type FetchMetric } from './index'
import type { OutgoingHttpHeaders } from 'node:http'
type Req = IncomingMessage & {
[NEXT_REQUEST_META]?: RequestMeta
cookies?: NextApiRequestCookies
fetchMetrics?: FetchMetric[]
}
export class NodeNextRequest extends BaseNextRequest<Readable> {
public headers = this._req.headers;
public headers = this._req.headers
public fetchMetrics?: FetchMetric[] = this._req?.fetchMetrics;
[NEXT_REQUEST_META]: RequestMeta = this._req[NEXT_REQUEST_META] || {}
......
......@@ -4,13 +4,14 @@ import type { FetchMetrics } from './index'
import { toNodeOutgoingHttpHeaders } from '../web/utils'
import { BaseNextRequest, BaseNextResponse } from './index'
import { DetachedPromise } from '../../lib/detached-promise'
import type { NextRequestHint } from '../web/adapter'
export class WebNextRequest extends BaseNextRequest<ReadableStream | null> {
public request: Request
public headers: IncomingHttpHeaders
public fetchMetrics?: FetchMetrics
constructor(request: Request) {
constructor(request: NextRequestHint) {
const url = new URL(request.url)
super(
......@@ -19,6 +20,7 @@ export class WebNextRequest extends BaseNextRequest<ReadableStream | null> {
request.clone().body
)
this.request = request
this.fetchMetrics = request.fetchMetrics
this.headers = {}
for (const [name, value] of request.headers.entries()) {
......
......@@ -391,7 +391,7 @@ export class AppRouteRouteModule extends RouteModule<
`No response is returned from route handler '${this.resolvedPagePath}'. Ensure you return a \`Response\` or a \`NextResponse\` in all branches of your handler.`
)
}
;(context.renderOpts as any).fetchMetrics =
context.renderOpts.fetchMetrics =
staticGenerationStore.fetchMetrics
context.renderOpts.waitUntil = Promise.all(
......
......@@ -12,7 +12,7 @@ export function logStartInfo({
appUrl,
envInfo,
expFeatureInfo,
maxExperimentalFeatures,
maxExperimentalFeatures = Infinity,
}: {
networkUrl: string | null
appUrl: string | null
......@@ -21,31 +21,27 @@ export function logStartInfo({
maxExperimentalFeatures?: number
}) {
Log.bootstrap(
bold(
purple(
` ${Log.prefixes.ready} Next.js ${process.env.__NEXT_VERSION}${
process.env.TURBOPACK ? ' (turbo)' : ''
}`
)
)
`${bold(
purple(`${Log.prefixes.ready} Next.js ${process.env.__NEXT_VERSION}`)
)}${process.env.TURBOPACK ? ' (turbo)' : ''}`
)
if (appUrl) {
Log.bootstrap(` - Local: ${appUrl}`)
Log.bootstrap(`- Local: ${appUrl}`)
}
if (networkUrl) {
Log.bootstrap(` - Network: ${networkUrl}`)
Log.bootstrap(`- Network: ${networkUrl}`)
}
if (envInfo?.length) Log.bootstrap(` - Environments: ${envInfo.join(', ')}`)
if (envInfo?.length) Log.bootstrap(`- Environments: ${envInfo.join(', ')}`)
if (expFeatureInfo?.length) {
Log.bootstrap(` - Experiments (use with caution):`)
// only show maximum 3 flags
Log.bootstrap(`- Experiments (use with caution):`)
// only show a maximum number of flags
for (const exp of expFeatureInfo.slice(0, maxExperimentalFeatures)) {
Log.bootstrap(` · ${exp}`)
Log.bootstrap(` · ${exp}`)
}
/* ${expFeatureInfo.length - 3} more */
if (expFeatureInfo.length > 3 && maxExperimentalFeatures) {
Log.bootstrap(` · ...`)
/* indicate if there are more than the maximum shown no. flags */
if (expFeatureInfo.length > maxExperimentalFeatures) {
Log.bootstrap(` · ...`)
}
}
......
......@@ -14,6 +14,7 @@ import {
} from '../../lib/constants'
import * as Log from '../../build/output/log'
import { trackDynamicFetch } from '../app-render/dynamic-rendering'
import type { FetchMetric } from '../base-http'
const isEdgeRuntime = process.env.NEXT_RUNTIME === 'edge'
......@@ -146,38 +147,23 @@ export function addImplicitTags(staticGenerationStore: StaticGenerationStore) {
function trackFetchMetric(
staticGenerationStore: StaticGenerationStore,
ctx: {
url: string
status: number
method: string
cacheReason: string
cacheStatus: 'hit' | 'miss' | 'skip'
start: number
}
ctx: Omit<FetchMetric, 'end' | 'idx'>
) {
if (!staticGenerationStore) return
if (!staticGenerationStore.fetchMetrics) {
staticGenerationStore.fetchMetrics = []
}
const dedupeFields = ['url', 'status', 'method']
staticGenerationStore.fetchMetrics ??= []
const dedupeFields = ['url', 'status', 'method'] as const
// don't add metric if one already exists for the fetch
if (
staticGenerationStore.fetchMetrics.some((metric) => {
return dedupeFields.every(
(field) => (metric as any)[field] === (ctx as any)[field]
)
})
staticGenerationStore.fetchMetrics.some((metric) =>
dedupeFields.every((field) => metric[field] === ctx[field])
)
) {
return
}
staticGenerationStore.fetchMetrics.push({
url: ctx.url,
cacheStatus: ctx.cacheStatus,
cacheReason: ctx.cacheReason,
status: ctx.status,
method: ctx.method,
start: ctx.start,
...ctx,
end: Date.now(),
idx: staticGenerationStore.nextFetchId || 0,
})
......
......@@ -259,6 +259,8 @@ export async function startServer(
maxExperimentalFeatures: 3,
})
Log.event(`Starting...`)
try {
const cleanup = () => {
debug('start-server process cleanup')
......
......@@ -12,7 +12,11 @@ import type { MiddlewareManifest } from '../build/webpack/plugins/middleware-plu
import type RenderResult from './render-result'
import type { FetchEventResult } from './web/types'
import type { PrerenderManifest } from '../build'
import type { BaseNextRequest, BaseNextResponse } from './base-http'
import type {
BaseNextRequest,
BaseNextResponse,
FetchMetric,
} from './base-http'
import type { PagesManifest } from '../build/webpack/plugins/pages-manifest-plugin'
import type { NextParsedUrlQuery, NextUrlWithParsedQuery } from './request-meta'
import type { Params } from '../shared/lib/router/utils/route-matcher'
......@@ -1091,7 +1095,7 @@ export default class NextNodeServer extends BaseServer {
const shouldTruncateUrl = !loggingFetchesConfig?.fullUrl
if (this.renderOpts.dev) {
const { bold, green, yellow, red, gray, white } =
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
......@@ -1112,33 +1116,28 @@ export default class NextNodeServer extends BaseServer {
return
}
const reqEnd = Date.now()
const fetchMetrics = (normalizedReq as any).fetchMetrics || []
const fetchMetrics = normalizedReq.fetchMetrics || []
const reqDuration = reqEnd - reqStart
const getDurationStr = (duration: number) => {
let durationStr = duration.toString()
if (duration < 500) {
durationStr = green(duration + 'ms')
} else if (duration < 2000) {
durationStr = yellow(duration + 'ms')
} else {
durationStr = red(duration + 'ms')
}
return durationStr
const statusColor = (status?: number) => {
if (!status || status < 200) return white
else if (status < 300) return green
else if (status < 400) return blue
else if (status < 500) return yellow
return red
}
if (Array.isArray(fetchMetrics) && fetchMetrics.length) {
if (enabledVerboseLogging) {
writeStdoutLine(
`${white(bold(req.method || 'GET'))} ${req.url} ${
res.statusCode
} in ${getDurationStr(reqDuration)}`
)
}
const color = statusColor(res.statusCode)
const method = req.method || 'GET'
writeStdoutLine(
`${color(method)} ${color(req.url ?? '')} ${
res.statusCode
} in ${reqDuration}ms`
)
if (fetchMetrics.length && enabledVerboseLogging) {
const calcNestedLevel = (
prevMetrics: any[],
prevMetrics: FetchMetric[],
start: number
): string => {
let nestedLevel = 0
......@@ -1154,7 +1153,7 @@ export default class NextNodeServer extends BaseServer {
nestedLevel += 1
}
}
return nestedLevel === 0 ? ' ' : ' │ '.repeat(nestedLevel)
return nestedLevel === 0 ? ' ' : ' │ '.repeat(nestedLevel)
}
for (let i = 0; i < fetchMetrics.length; i++) {
......@@ -1162,17 +1161,16 @@ export default class NextNodeServer extends BaseServer {
let { cacheStatus, cacheReason } = metric
let cacheReasonStr = ''
let cacheColor
const duration = metric.end - metric.start
if (cacheStatus === 'hit') {
cacheStatus = green('HIT')
} else if (cacheStatus === 'skip') {
cacheStatus = yellow('SKIP')
cacheColor = green
} else {
cacheColor = yellow
const status = cacheStatus === 'skip' ? 'skipped' : 'missed'
cacheReasonStr = gray(
`Cache missed reason: (${white(cacheReason)})`
`Cache ${status} reason: (${white(cacheReason)})`
)
} else {
cacheStatus = yellow('MISS')
}
let url = metric.url
......@@ -1199,45 +1197,29 @@ export default class NextNodeServer extends BaseServer {
truncatedSearch
}
if (enabledVerboseLogging) {
const newLineLeadingChar = '│'
const nestedIndent = calcNestedLevel(
const status = cacheColor(`(cache ${cacheStatus})`)
const newLineLeadingChar = '│'
const nestedIndent = calcNestedLevel(
fetchMetrics.slice(0, i + 1),
metric.start
)
writeStdoutLine(
`${newLineLeadingChar}${nestedIndent}${white(
metric.method
)} ${white(url)} ${metric.status} in ${duration}ms ${status}`
)
if (cacheReasonStr) {
const nextNestedIndent = calcNestedLevel(
fetchMetrics.slice(0, i + 1),
metric.start
)
writeStdoutLine(
` ${`${newLineLeadingChar}${nestedIndent}${white(
bold(metric.method)
)} ${gray(url)} ${metric.status} in ${getDurationStr(
duration
)} (cache: ${cacheStatus})`}`
`${newLineLeadingChar}${nextNestedIndent}${newLineLeadingChar} ${cacheReasonStr}`
)
if (cacheReasonStr) {
const nextNestedIndent = calcNestedLevel(
fetchMetrics.slice(0, i + 1),
metric.start
)
writeStdoutLine(
' ' +
newLineLeadingChar +
nextNestedIndent +
' ' +
newLineLeadingChar +
' ' +
cacheReasonStr
)
}
}
}
} else {
if (enabledVerboseLogging) {
writeStdoutLine(
`${white(bold(req.method || 'GET'))} ${req.url} ${
res.statusCode
} in ${getDurationStr(reqDuration)}`
)
}
}
origRes.off('close', reqCallback)
}
......@@ -1910,7 +1892,7 @@ export default class NextNodeServer extends BaseServer {
})
if (result.fetchMetrics) {
;(params.req as any).fetchMetrics = result.fetchMetrics
params.req.fetchMetrics = result.fetchMetrics
}
if (!params.res.statusCode || params.res.statusCode < 400) {
......
import type { NextMiddleware, RequestData, FetchEventResult } from './types'
import type { RequestData, FetchEventResult } from './types'
import type { RequestInit } from './spec-extension/request'
import type { PrerenderManifest } from '../../build'
import { PageSignatureError } from './error'
......@@ -20,7 +20,7 @@ import { getTracer } from '../lib/trace/tracer'
import type { TextMapGetter } from 'next/dist/compiled/@opentelemetry/api'
import { MiddlewareSpan } from '../lib/trace/constants'
class NextRequestHint extends NextRequest {
export class NextRequestHint extends NextRequest {
sourcePage: string
fetchMetrics?: FetchEventResult['fetchMetrics']
......@@ -52,7 +52,7 @@ const headersGetter: TextMapGetter<Headers> = {
}
export type AdapterOptions = {
handler: NextMiddleware
handler: (req: NextRequestHint, event: NextFetchEvent) => Promise<Response>
page: string
request: RequestData
IncrementalCache?: typeof import('../lib/incremental-cache').IncrementalCache
......
import { findPort, waitFor } from 'next-test-utils'
import http from 'http'
import { outdent } from 'outdent'
import { FileRef, createNext } from 'e2e-utils'
import { isNextDev, isNextStart, nextTestSetup } from 'e2e-utils'
describe('app-fetch-deduping', () => {
if ((global as any).isNextStart) {
if (isNextStart) {
describe('during static generation', () => {
const { next } = nextTestSetup({ files: __dirname, skipStart: true })
let externalServerPort: number
let externalServer: http.Server
let requests = []
......@@ -35,28 +36,27 @@ describe('app-fetch-deduping', () => {
afterAll(() => externalServer.close())
it('dedupes requests amongst static workers when experimental.staticWorkerRequestDeduping is enabled', async () => {
const next = await createNext({
files: new FileRef(__dirname),
env: { TEST_SERVER_PORT: `${externalServerPort}` },
nextConfig: {
await next.patchFileFast(
'next.config.js',
`module.exports = {
env: { TEST_SERVER_PORT: "${externalServerPort}" },
experimental: {
staticWorkerRequestDeduping: true,
},
},
})
staticWorkerRequestDeduping: true
}
}`
)
await next.build()
expect(requests.length).toBe(1)
await next.destroy()
})
})
} else if ((global as any).isNextDev) {
} else if (isNextDev) {
describe('during next dev', () => {
it('should dedupe requests called from the same component', async () => {
const next = await createNext({
files: new FileRef(__dirname),
})
const { next } = nextTestSetup({ files: __dirname })
function invocation(cliOutput: string): number {
return cliOutput.match(/Route Handler invoked/g).length
}
it('should dedupe requests called from the same component', async () => {
await next.patchFile(
'app/test/page.tsx',
outdent`
......@@ -71,28 +71,23 @@ describe('app-fetch-deduping', () => {
const time = await getTime()
return <h1>{time}</h1>
}
`
}`
)
await next.render('/test')
let count = next.cliOutput.split('Starting...').length - 1
expect(count).toBe(1)
await next.destroy()
expect(invocation(next.cliOutput)).toBe(1)
await next.stop()
})
it('should dedupe pending revalidation requests', async () => {
const next = await createNext({
files: new FileRef(__dirname),
})
await next.start()
const revalidate = 5
await next.patchFile(
'app/test/page.tsx',
outdent`
async function getTime() {
const res = await fetch("http://localhost:${next.appPort}/api/time", { next: { revalidate: 5 } })
const res = await fetch("http://localhost:${next.appPort}/api/time", { next: { revalidate: ${revalidate} } })
return res.text()
}
......@@ -102,27 +97,19 @@ describe('app-fetch-deduping', () => {
const time = await getTime()
return <h1>{time}</h1>
}
`
}`
)
await next.render('/test')
let count = next.cliOutput.split('Starting...').length - 1
expect(count).toBe(1)
const outputIndex = next.cliOutput.length
expect(invocation(next.cliOutput)).toBe(1)
// wait for the revalidation to finish
await waitFor(6000)
await waitFor(revalidate * 1000 + 1000)
await next.render('/test')
count =
next.cliOutput.slice(outputIndex).split('Starting...').length - 1
expect(count).toBe(1)
await next.destroy()
expect(invocation(next.cliOutput)).toBe(2)
})
})
} else {
......
import { NextResponse } from 'next/server'
export async function GET() {
console.log('Starting...')
console.log('Route Handler invoked')
return NextResponse.json({ time: Date.now() })
}
......@@ -8,8 +8,8 @@ createNextDescribe(
({ next, isNextStart }) => {
if (isNextStart) {
it('should correctly mark a route handler that uses revalidateTag as dynamic', async () => {
expect(next.cliOutput).toContain('λ /api/revalidate-path')
expect(next.cliOutput).toContain('λ /api/revalidate-tag')
expect(next.cliOutput).toContain('ƒ /api/revalidate-path')
expect(next.cliOutput).toContain('ƒ /api/revalidate-tag')
})
}
......
......@@ -585,17 +585,17 @@ describe('Switchable runtime', () => {
const expectedOutputLines = splitLines(`
┌ /_app
├ ○ /404
├ ℇ /api/hello
├ λ /api/node
├ ℇ /edge
├ ℇ /edge-rsc
├ ƒ /api/hello
├ ƒ /api/node
├ ƒ /edge
├ ƒ /edge-rsc
├ ○ /node
├ ● /node-rsc
├ ● /node-rsc-isr
├ ● /node-rsc-ssg
├ λ /node-rsc-ssr
├ ƒ /node-rsc-ssr
├ ● /node-ssg
├ λ /node-ssr
├ ƒ /node-ssr
└ ○ /static
`)
......
......@@ -267,7 +267,7 @@ describe('Build Output', () => {
})
expect(stdout).toMatch(/\/ (.* )?\d{1,} B/)
expect(stdout).toMatch(/λ \/404 (.* )?\d{1,} B/)
expect(stdout).toMatch(/ƒ \/404 (.* )?\d{1,} B/)
expect(stdout).toMatch(/\+ First Load JS shared by all [ 0-9.]* kB/)
expect(stdout).toMatch(/ chunks\/main-[0-9a-z]{16}\.js [ 0-9.]* kB/)
expect(stdout).toMatch(
......@@ -295,7 +295,7 @@ describe('Build Output', () => {
stdout: true,
})
expect(stdout).toContain('○ /404')
expect(stdout).not.toContain('λ /_error')
expect(stdout).not.toContain('ƒ /_error')
expect(stdout).not.toContain('<buildId>')
})
})
......
......@@ -96,8 +96,8 @@ describe('Custom _error', () => {
afterAll(() => killApp(app))
it('should not contain /_error in build output', async () => {
expect(buildOutput).toMatch(/λ .*?\/404/)
expect(buildOutput).not.toMatch(/λ .*?\/_error/)
expect(buildOutput).toMatch(/ƒ .*?\/404/)
expect(buildOutput).not.toMatch(/ƒ .*?\/_error/)
})
runTests()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment