Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ export interface StaticGenerationStore {

isDraftMode?: boolean
isUnstableNoStore?: boolean

requestEndedState?: { ended?: boolean }
}

export type StaticGenerationAsyncStorage =
Expand Down
24 changes: 18 additions & 6 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ async function renderToHTMLOrFlightImpl(
pagePath: string,
query: NextParsedUrlQuery,
renderOpts: RenderOpts,
baseCtx: AppRenderBaseContext
baseCtx: AppRenderBaseContext,
requestEndedState: { ended?: boolean }
) {
const isNotFoundPath = pagePath === '/404'

Expand Down Expand Up @@ -631,6 +632,8 @@ async function renderToHTMLOrFlightImpl(
isNodeNextRequest(req)
) {
req.originalRequest.on('end', () => {
requestEndedState.ended = true

if ('performance' in globalThis) {
const metrics = getClientComponentLoaderMetrics({ reset: true })
if (metrics) {
Expand Down Expand Up @@ -1448,14 +1451,23 @@ export const renderToHTMLOrFlight: AppPageRender = (
{
urlPathname: pathname,
renderOpts,
requestEndedState: { ended: false },
},
(staticGenerationStore) =>
renderToHTMLOrFlightImpl(req, res, pagePath, query, renderOpts, {
requestStore,
staticGenerationStore,
componentMod: renderOpts.ComponentMod,
renderToHTMLOrFlightImpl(
req,
res,
pagePath,
query,
renderOpts,
})
{
requestStore,
staticGenerationStore,
componentMod: renderOpts.ComponentMod,
renderOpts,
},
staticGenerationStore.requestEndedState || {}
)
)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { FetchMetric } from '../base-http'

export type StaticGenerationContext = {
urlPathname: string
requestEndedState?: { ended?: boolean }
renderOpts: {
incrementalCache?: IncrementalCache
isOnDemandRevalidate?: boolean
Expand Down Expand Up @@ -50,7 +51,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
> = {
wrap<Result>(
storage: AsyncLocalStorage<StaticGenerationStore>,
{ urlPathname, renderOpts }: StaticGenerationContext,
{ urlPathname, renderOpts, requestEndedState }: StaticGenerationContext,
callback: (store: StaticGenerationStore) => Result
): Result {
/**
Expand Down Expand Up @@ -96,6 +97,7 @@ export const StaticGenerationAsyncStorageWrapper: AsyncStorageWrapper<
isDraftMode: renderOpts.isDraftMode,

prerenderState,
requestEndedState,
}

// TODO: remove this when we resolve accessing the store outside the execution context
Expand Down
27 changes: 26 additions & 1 deletion packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,13 @@ function trackFetchMetric(
staticGenerationStore: StaticGenerationStore,
ctx: Omit<FetchMetric, 'end' | 'idx'>
) {
if (!staticGenerationStore) return
if (
!staticGenerationStore ||
staticGenerationStore.requestEndedState?.ended ||
process.env.NODE_ENV !== 'development'
) {
return
}
staticGenerationStore.fetchMetrics ??= []

const dedupeFields = ['url', 'status', 'method'] as const
Expand All @@ -181,6 +187,25 @@ function trackFetchMetric(
end: Date.now(),
idx: staticGenerationStore.nextFetchId || 0,
})

// only store top 10 metrics to avoid storing too many
if (staticGenerationStore.fetchMetrics.length > 10) {
// sort slowest first as these should be highlighted
staticGenerationStore.fetchMetrics.sort((a, b) => {
const aDur = a.end - a.start
const bDur = b.end - b.start

if (aDur < bDur) {
return 1
} else if (aDur > bDur) {
return -1
}
return 0
})
// now grab top 10
staticGenerationStore.fetchMetrics =
staticGenerationStore.fetchMetrics.slice(0, 10)
}
}

interface PatchableModule {
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1232,6 +1232,7 @@ export default class NextNodeServer extends BaseServer<
}
}
}
delete normalizedReq.fetchMetrics
originalResponse.off('close', reqCallback)
}
originalResponse.on('close', reqCallback)
Expand Down