Use render lifecycle callback for fetch metrics cleanup (#100323)
# Human This is part of factoring out `request` and `response` access from the rendering path as we don't have real values for them when e.g. pre-rendering at build time. # AI below ## Summary Use the existing `renderOpts.onClose` lifecycle callback to stop tracking fetch metrics when the response closes. This removes a direct response dependency and the request type check from App Router render preparation while preserving production runtime cleanup. Retain the Edge guard and exclude build-time prerenders from cleanup registration. Their lifecycle closes before `after()` callbacks run, and fetch metrics must remain enabled until export collects them. For Node runtime requests, both the old and new callbacks subscribe to the same response `close` event. Add end-to-end coverage for build-time metrics and runtime cleanup. Dev Cache Components can render again while preparing background validation after the response closes, so those metric logs can arrive in either order; this also occurs with canary's implementation. Only that dev case permits the extra render record. Production assertions remain strict for every record. ## Verification - Fetch logging tests: 53 passed with Turbopack and 53 with Webpack in dev. - Lifecycle tests with the failing CI cache-components dev environment: 4 passed. - Production lifecycle comparison: all 6 checks passed with both canary's implementation and this PR. Temporarily restoring the dev-only cleanup guard correctly failed the production runtime-prerender check. <!-- NEXT_JS_LLM -->
Showing
Please
register
or
sign in
to comment