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