Commit 4ab7b0c7 authored by Swarnava Sengupta's avatar Swarnava Sengupta

fix(next): prevent createDedupeFetch wrapper accumulation across HMR cycles

Each HMR cycle previously added another createDedupeFetch wrapper around
globalThis.fetch because resetFetch() restored _nextOriginalFetch (which
already included a dedupe layer), then patchFetch() wrapped it in yet
another dedupe layer.

Add _nextPrePatchFetch to PatchedFetcher, which stores the globalThis.fetch
reference from before patchFetch() applied any wrapping (pre-dedupe,
pre-patch). resetFetch() now restores this reference instead, so the
next patchFetch() call starts from a clean base — no wrapper accumulation.
parent 2678e419
......@@ -39,6 +39,8 @@ type PatchedFetcher = Fetcher & {
readonly __nextPatched: true
readonly __nextGetStaticStore: () => WorkAsyncStorage
readonly _nextOriginalFetch: Fetcher
/** The globalThis.fetch value before patchFetch() applied any wrapping. */
_nextPrePatchFetch: Fetcher
}
export const NEXT_PATCH_SYMBOL = Symbol.for('next-patch')
......@@ -1281,6 +1283,8 @@ export function createPatchedFetcher(
patched.__nextPatched = true as const
patched.__nextGetStaticStore = () => workAsyncStorage
patched._nextOriginalFetch = originFetch
// Store the fetch as it was before patchFetch() touched it (pre-dedupe).
// resetFetch() uses this to cleanly unwrap without accumulating dedupe layers.
;(globalThis as Record<symbol, unknown>)[NEXT_PATCH_SYMBOL] = true
// Assign the function name also as a name property, so that it's preserved
......@@ -1296,12 +1300,19 @@ export function patchFetch(options: PatchableModule) {
// If we've already patched fetch, we should not patch it again.
if (isFetchPatched()) return
// Save the current fetch before any wrapping. resetFetch() uses this to
// cleanly restore without accumulating createDedupeFetch layers across
// HMR cycles.
const prePatchFetch = globalThis.fetch
// Grab the original fetch function. We'll attach this so we can use it in
// the patched fetch function.
const original = createDedupeFetch(globalThis.fetch)
const original = createDedupeFetch(prePatchFetch)
// Set the global fetch to the patched fetch.
globalThis.fetch = createPatchedFetcher(original, options)
const patched = createPatchedFetcher(original, options)
patched._nextPrePatchFetch = prePatchFetch
globalThis.fetch = patched
}
let currentTimeoutBoundary: null | Promise<void> = null
......
......@@ -153,16 +153,16 @@ export async function initialize(opts: {
require('./router-utils/setup-dev-bundler') as typeof import('./router-utils/setup-dev-bundler')
const resetFetch = () => {
// If fetch was patched by Next.js, unwrap only the Next.js layer by
// restoring _nextOriginalFetch. This preserves any third-party
// instrumentation (e.g. OpenTelemetry) that wrapped fetch before
// Next.js's patchFetch() ran. Only fall back to the startup snapshot
// when there's no Next.js patch layer to unwrap.
// Unwrap only the Next.js patchFetch() + createDedupeFetch layers by
// restoring the fetch reference from before patchFetch() ran. This
// preserves any third-party instrumentation (e.g. OpenTelemetry) that
// wrapped globalThis.fetch before Next.js patched it, and avoids
// accumulating createDedupeFetch wrappers across HMR cycles.
const currentFetch: typeof globalThis.fetch & {
_nextOriginalFetch?: typeof globalThis.fetch
_nextPrePatchFetch?: typeof globalThis.fetch
} = globalThis.fetch
if (currentFetch._nextOriginalFetch) {
globalThis.fetch = currentFetch._nextOriginalFetch
if (currentFetch._nextPrePatchFetch) {
globalThis.fetch = currentFetch._nextPrePatchFetch
} else {
globalThis.fetch = originalFetch
}
......
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