Add 2nd cloned body to FinalizationRegistry. Only used RuntimeStyles component for webpack (#88577)
### What?
Reduces memory retention in two areas: response stream cleanup in
`cloneResponse` and unnecessary runtime style initialization in
Turbopack mode. Also adds a test fixture for standalone mode fetch
memory leak detection.
### Why?
Based on the reproduction provided in #85914, investigating potential
sources of memory retention. When a `ReadableStream` is tee'd, both
branches must be cancelled to release connection resources — previously
only one branch was registered for GC cancellation. Additionally,
`RuntimeStyles` and `_N_E_STYLE_LOAD` were always initialized even in
Turbopack mode where they are unused.
### How?
**`clone-response.ts`**: Register both `cloned1` and `cloned2` bodies
with the `FinalizationRegistry` so that when either response is garbage
collected, its stream branch is properly cancelled.
**`app-router.tsx`**: Guard `_N_E_STYLE_LOAD` and style tracking `Set`
initialization behind `!process.env.TURBOPACK && typeof window !==
'undefined'`. Rename the component to `RuntimeStylesForWebpack` and skip
rendering it entirely when using Turbopack.
Fixes #85914
---------
Co-authored-by:
Unghee Lee <unghee66@gmail.com>
Showing
Please
register
or
sign in
to comment