Unverified Commit 70c43864 authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

log FetchMetrics during SSG to diagnostics dir (#68078)

### What
We don't currently provide any observability into fetch metrics during
SSG, which can be useful to diagnose slow builds or SSG errors related
to data dependencies.

### Why
`trackFetchMetrics` is disabled outside of `development`, and we do not
currently surface that information anywhere during build.

### How
This removes the `trackFetchMetrics` bailout during static generation.
Additionally, it updates the export worker to track the fetch metrics it
collected during rendering as part of the export result. The build step
then uses these results to write the results (keyed by page) to
`.next/diagnostics/fetch-metrics.json`.

**Note: this is currently gated by the presence of env var:
`NEXT_DEBUG_BUILD`**

<details>

<summary>Example File</summary>

```json
{
  "/": [
    {
      "start": 1721760882192,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?page",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882466,
      "idx": 2
    }
  ],
  "/hooks/use-search-params/with-suspense": [],
  "/strip-header-traceparent": [
    {
      "start": 1721760882191,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882402,
      "idx": 2
    },
    {
      "start": 1721760882408,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "revalidate: 50",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882408,
      "idx": 3
    },
    {
      "start": 1721760882408,
      "url": "https://next-data-api-endpoint.vercel.app/api/echo-headers",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882702,
      "idx": 4
    }
  ],
  "/unstable-cache/fetch/no-cache": [
    {
      "start": 1721760882215,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "fetchCache = force-no-store",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882426,
      "idx": 3
    }
  ],
  "/unstable-cache/fetch/no-store": [
    {
      "start": 1721760882229,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "fetchCache = force-no-store",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882432,
      "idx": 3
    }
  ],
  "/variable-revalidate/encoding": [
    {
      "start": 1721760882199,
      "url": "https://next-data-api-endpoint.vercel.app/api/utf8-encoding",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882440,
      "idx": 3
    },
    {
      "start": 1721760882201,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882442,
      "idx": 3
    },
    {
      "start": 1721760882449,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882449,
      "idx": 4
    }
  ],
  "/variable-revalidate/headers-instance": [
    {
      "start": 1721760882193,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882426,
      "idx": 3
    },
    {
      "start": 1721760882192,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882431,
      "idx": 3
    },
    {
      "start": 1721760882435,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882435,
      "idx": 5
    },
    {
      "start": 1721760882434,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882553,
      "idx": 5
    }
  ],
  "/variable-revalidate/post-method": [
    {
      "start": 1721760882295,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882494,
      "idx": 3
    },
    {
      "start": 1721760882294,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "POST",
      "end": 1721760882497,
      "idx": 3
    },
    {
      "start": 1721760882502,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882502,
      "idx": 5
    },
    {
      "start": 1721760882501,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "POST",
      "end": 1721760882617,
      "idx": 5
    },
    {
      "start": 1721760882619,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "POST",
      "end": 1721760882739,
      "idx": 6
    },
    {
      "start": 1721760882741,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "POST",
      "end": 1721760882848,
      "idx": 7
    },
    {
      "start": 1721760882850,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "POST",
      "end": 1721760883028,
      "idx": 8
    }
  ],
  "/no-config-fetch": [
    {
      "start": 1721760882299,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882499,
      "idx": 2
    }
  ],
  "/variable-revalidate/revalidate-3": [
    {
      "start": 1721760882347,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?page",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882534,
      "idx": 3
    },
    {
      "start": 1721760882537,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?page",
      "cacheReason": "revalidate: 3",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882537,
      "idx": 4
    },
    {
      "start": 1721760882347,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882541,
      "idx": 4
    },
    {
      "start": 1721760882544,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882544,
      "idx": 5
    }
  ],
  "/variable-revalidate/revalidate-360-isr": [
    {
      "start": 1721760882358,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?page",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882484,
      "idx": 3
    },
    {
      "start": 1721760882358,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882532,
      "idx": 4
    },
    {
      "start": 1721760882535,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882535,
      "idx": 5
    },
    {
      "start": 1721760882485,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?a=b",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882607,
      "idx": 5
    },
    {
      "start": 1721760882610,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "fetchCache = force-no-store",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882748,
      "idx": 7
    }
  ],
  "/_not-found": [],
  "/variable-revalidate/authorization": [
    {
      "start": 1721760882460,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882473,
      "idx": 3
    },
    {
      "start": 1721760882475,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882475,
      "idx": 4
    },
    {
      "start": 1721760882460,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882583,
      "idx": 4
    }
  ],
  "/articles/works": [],
  "/blog/tim/first-post": [],
  "/blog/seb/second-post": [],
  "/blog/styfle/first-post": [],
  "/blog/styfle/second-post": [],
  "/blog/tim": [
    {
      "start": 1721760882785,
      "url": "https://example.vercel.sh/",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882882,
      "idx": 2
    }
  ],
  "/blog/seb": [
    {
      "start": 1721760882783,
      "url": "https://example.vercel.sh/",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882831,
      "idx": 2
    }
  ],
  "/blog/styfle": [
    {
      "start": 1721760882785,
      "url": "https://example.vercel.sh/",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882873,
      "idx": 2
    }
  ],
  "/force-cache": [
    {
      "start": 1721760882781,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?a1",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882889,
      "idx": 2
    },
    {
      "start": 1721760882890,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?b2",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760883000,
      "idx": 3
    },
    {
      "start": 1721760883001,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?c3",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760883106,
      "idx": 4
    },
    {
      "start": 1721760883107,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?d4",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "cacheWarning": "Specified \"cache: force-cache\" and \"revalidate: 3\", only one should be specified.",
      "status": 200,
      "method": "GET",
      "end": 1721760883212,
      "idx": 5
    },
    {
      "start": 1721760883213,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?d4",
      "cacheReason": "cache-control: no-cache (hard refresh)",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760883213,
      "idx": 6
    }
  ],
  "/no-store/static": [],
  "/force-static/first": [],
  "/force-static/second": [],
  "/hooks/use-pathname/slug": [],
  "/hooks/use-search-params/force-static": [],
  "/force-static-fetch-no-store": [
    {
      "start": 1721760882844,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?page",
      "cacheReason": "cache: no-store",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882994,
      "idx": 2
    },
    {
      "start": 1721760882995,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?page",
      "cacheReason": "revalidate: 0",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882996,
      "idx": 3
    },
    {
      "start": 1721760882996,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?page",
      "cacheReason": "cache: no-cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760882996,
      "idx": 4
    }
  ],
  "/gen-params-dynamic-revalidate/one": [],
  "/isr-error-handling": [],
  "/ssg-draft-mode": [
    {
      "start": 1721760882919,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760883020,
      "idx": 2
    }
  ],
  "/ssg-draft-mode/test": [
    {
      "start": 1721760882918,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760883072,
      "idx": 2
    }
  ],
  "/ssg-draft-mode/test-2": [
    {
      "start": 1721760882919,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760883030,
      "idx": 2
    }
  ],
  "/variable-config-revalidate/revalidate-3": [
    {
      "start": 1721760882917,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "revalidate: 9",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882917,
      "idx": 2
    }
  ],
  "/variable-revalidate-stable/revalidate-3": [],
  "/variable-revalidate/cookie": [
    {
      "start": 1721760882919,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882921,
      "idx": 3
    },
    {
      "start": 1721760882923,
      "url": "https://next-data-api-endpoint.vercel.app/api/random?layout",
      "cacheReason": "revalidate: 10",
      "cacheStatus": "hit",
      "status": 200,
      "method": "GET",
      "end": 1721760882923,
      "idx": 4
    },
    {
      "start": 1721760882919,
      "url": "https://next-data-api-endpoint.vercel.app/api/random",
      "cacheReason": "auto no cache",
      "cacheStatus": "skip",
      "status": 200,
      "method": "GET",
      "end": 1721760883068,
      "idx": 4
    }
  ],
  "/partial-gen-params-no-additional-lang/en/first": [],
  "/partial-gen-params-no-additional-lang/en/second": [],
  "/partial-gen-params-no-additional-lang/en/3": [],
  "/partial-gen-params-no-additional-lang/fr/first": [],
  "/partial-gen-params-no-additional-lang/fr/second": [],
  "/partial-gen-params-no-additional-lang/fr/3": [],
  "/partial-gen-params-no-additional-slug/en/first": [],
  "/partial-gen-params-no-additional-slug/en/second": [],
  "/partial-gen-params-no-additional-slug/en/85": [],
  "/partial-gen-params-no-additional-slug/fr/first": [],
  "/partial-gen-params-no-additional-slug/fr/second": [],
  "/partial-gen-params-no-additional-slug/fr/85": []
}
```

</details>

Closes NDX-96
parent 027a407b
......@@ -162,6 +162,7 @@ import { formatManifest } from './manifests/formatter/format-manifest'
import {
recordFrameworkVersion,
updateBuildDiagnostics,
recordFetchMetrics,
} from '../diagnostics/build-diagnostics'
import { getStartServerInfo, logStartInfo } from '../server/lib/app-info-log'
import type { NextEnabledDirectories } from '../server/base-server'
......@@ -2735,6 +2736,10 @@ export default async function build(
// If there was no result, there's nothing more to do.
if (!exportResult) return
if (debugOutput) {
recordFetchMetrics(exportResult)
}
writeTurborepoAccessTraceResult({
distDir: config.distDir,
traces: [
......
import { mkdir, readFile, writeFile } from 'fs/promises'
import { join } from 'path'
import { traceGlobals } from '../trace/shared'
import type { ExportAppResult } from '../export/types'
import type { FetchMetrics } from '../server/base-http'
const DIAGNOSTICS_DIR = 'diagnostics'
const DIAGNOSTICS_FILE = 'build-diagnostics.json'
const FETCH_METRICS_FILE = 'fetch-metrics.json'
const FRAMEWORK_VERSION_FILE = 'framework.json'
interface BuildDiagnostics {
// The current stage of the build process. This should be updated as the
......@@ -26,7 +30,7 @@ async function getDiagnosticsDir(): Promise<string> {
*/
export async function recordFrameworkVersion(version: string): Promise<void> {
const diagnosticsDir = await getDiagnosticsDir()
const frameworkVersionFile = join(diagnosticsDir, 'framework.json')
const frameworkVersionFile = join(diagnosticsDir, FRAMEWORK_VERSION_FILE)
await writeFile(
frameworkVersionFile,
JSON.stringify({ name: 'Next.js', version })
......@@ -60,3 +64,22 @@ export async function updateBuildDiagnostics(
}
await writeFile(diagnosticsFile, JSON.stringify(updatedDiagnostics, null, 2))
}
/**
* Writes fetch metrics collected during static generation to a file.
*/
export async function recordFetchMetrics(
exportResult: ExportAppResult
): Promise<void> {
const diagnosticsDir = await getDiagnosticsDir()
const diagnosticsFile = join(diagnosticsDir, FETCH_METRICS_FILE)
const fetchMetricsByPath: Record<string, FetchMetrics> = {}
for (const [appPath, { fetchMetrics }] of exportResult.byPath) {
if (fetchMetrics) {
fetchMetricsByPath[appPath] = fetchMetrics
}
}
return writeFile(diagnosticsFile, JSON.stringify(fetchMetricsByPath, null, 2))
}
......@@ -674,6 +674,10 @@ export async function exportAppImpl(
info.hasPostponed = result.hasPostponed
}
if (typeof result.fetchMetrics !== 'undefined') {
info.fetchMetrics = result.fetchMetrics
}
collector.byPath.set(path, info)
// Update not found.
......
......@@ -20,6 +20,8 @@ import { lazyRenderAppPage } from '../../server/route-modules/app-page/module.re
import { isBailoutToCSRError } from '../../shared/lib/lazy-dynamic/bailout-to-csr'
import { NodeNextRequest, NodeNextResponse } from '../../server/base-http/node'
import { NEXT_IS_PRERENDER_HEADER } from '../../client/components/app-router-headers'
import type { FetchMetrics } from '../../server/base-http'
import type { StaticGenerationStore } from '../../client/components/static-generation-async-storage.external'
export const enum ExportedAppPageFiles {
HTML = 'HTML',
......@@ -62,7 +64,13 @@ export async function exportAppPage(
const html = result.toUnchunkedString()
const { metadata } = result
const { flightData, revalidate = false, postponed, fetchTags } = metadata
const {
flightData,
revalidate = false,
postponed,
fetchTags,
fetchMetrics,
} = metadata
// Ensure we don't postpone without having PPR enabled.
if (postponed && !renderOpts.experimental.isRoutePPREnabled) {
......@@ -85,7 +93,7 @@ export async function exportAppPage(
})
}
return { revalidate: 0 }
return { revalidate: 0, fetchMetrics }
}
// If page data isn't available, it means that the page couldn't be rendered
// properly.
......@@ -167,6 +175,7 @@ export async function exportAppPage(
hasEmptyPrelude: Boolean(postponed) && html === '',
hasPostponed: Boolean(postponed),
revalidate,
fetchMetrics,
}
} catch (err) {
if (!isDynamicUsageError(err)) {
......@@ -179,18 +188,21 @@ export async function exportAppPage(
throw err
}
let fetchMetrics: FetchMetrics | undefined
if (debugOutput) {
const { dynamicUsageDescription, dynamicUsageStack } = (renderOpts as any)
.store
const store = (renderOpts as any).store as StaticGenerationStore
const { dynamicUsageDescription, dynamicUsageStack } = store
fetchMetrics = store.fetchMetrics
logDynamicUsageWarning({
path,
description: dynamicUsageDescription,
description: dynamicUsageDescription ?? '',
stack: dynamicUsageStack,
})
}
return { revalidate: 0 }
return { revalidate: 0, fetchMetrics }
}
}
......
......@@ -6,7 +6,6 @@ import type { OutgoingHttpHeaders } from 'http'
import type AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import type { FontConfig } from '../server/font-utils'
import type { ExportPathMap, NextConfigComplete } from '../server/config-shared'
import type { Span } from '../trace'
import type { Revalidate } from '../server/lib/revalidate'
import type {
NextEnabledDirectories,
......@@ -16,6 +15,7 @@ import type {
SerializableTurborepoAccessTraceResult,
TurborepoAccessTraceResult,
} from '../build/turborepo-access-trace'
import type { FetchMetrics } from '../server/base-http'
export interface AmpValidation {
page: string
......@@ -85,6 +85,7 @@ export type ExportRouteResult =
ssgNotFound?: boolean
hasEmptyPrelude?: boolean
hasPostponed?: boolean
fetchMetrics?: FetchMetrics
}
| {
error: boolean
......@@ -156,6 +157,8 @@ export type ExportAppResult = {
* If the page has postponed when using PPR.
*/
hasPostponed?: boolean
fetchMetrics?: FetchMetrics
}
>
......@@ -174,9 +177,3 @@ export type ExportAppResult = {
*/
turborepoAccessTraceResults: Map<string, TurborepoAccessTraceResult>
}
export type ExportAppWorker = (
dir: string,
options: ExportAppOptions,
span: Span
) => Promise<ExportAppResult | null>
......@@ -386,6 +386,7 @@ export default async function exportPage(
hasEmptyPrelude: result.hasEmptyPrelude,
hasPostponed: result.hasPostponed,
turborepoAccessTraceResult: turborepoAccessTraceResult.serialize(),
fetchMetrics: result.fetchMetrics,
}
}
......
......@@ -167,13 +167,23 @@ function trackFetchMetric(
staticGenerationStore: StaticGenerationStore,
ctx: Omit<FetchMetric, 'end' | 'idx'>
) {
// If the static generation store is not available, we can't track the fetch
if (!staticGenerationStore) return
if (staticGenerationStore.requestEndedState?.ended) return
const isDebugBuild =
!!process.env.NEXT_DEBUG_BUILD && staticGenerationStore.isStaticGeneration
const isDevelopment = process.env.NODE_ENV === 'development'
if (
!staticGenerationStore ||
staticGenerationStore.requestEndedState?.ended ||
process.env.NODE_ENV !== 'development'
// The only time we want to track fetch metrics outside of development is when
// we are performing a static generation & we are in debug mode.
!isDebugBuild &&
!isDevelopment
) {
return
}
staticGenerationStore.fetchMetrics ??= []
staticGenerationStore.fetchMetrics.push({
......
......@@ -2091,6 +2091,41 @@ describe('app-dir static/dynamic handling', () => {
)
})
it('should log fetch metrics to the diagnostics directory', async () => {
const fetchMetrics = JSON.parse(
await next.readFile('.next/diagnostics/fetch-metrics.json')
)
const indexFetchMetrics = fetchMetrics['/']
expect(indexFetchMetrics).toHaveLength(1)
expect(indexFetchMetrics[0]).toMatchObject({
url: 'https://next-data-api-endpoint.vercel.app/api/random?page',
status: 200,
cacheStatus: 'skip',
start: expect.any(Number),
end: expect.any(Number),
cacheReason: 'auto no cache',
})
const otherPageMetrics =
fetchMetrics['/variable-revalidate/headers-instance']
expect(otherPageMetrics).toHaveLength(4)
expect(otherPageMetrics).toEqual(
expect.arrayContaining([
expect.objectContaining({
url: 'https://next-data-api-endpoint.vercel.app/api/random?layout',
status: 200,
cacheStatus: 'hit',
start: expect.any(Number),
end: expect.any(Number),
cacheReason: 'revalidate: 10',
}),
])
)
})
// build cache not leveraged for custom cache handler so not seeded
if (!process.env.CUSTOM_CACHE_HANDLER) {
it('should correctly error and not update cache for ISR', async () => {
......
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