Unverified Commit a449e4a1 authored by Wyatt Johnson's avatar Wyatt Johnson Committed by GitHub

fix: clone response in first handler to prevent race (#70082)

This fixes a race where if the body was resolved before the clone
operation, it would clone later, resulting in an error being thrown due
to the body already being consumed.
parent ce9c28fa
...@@ -859,34 +859,55 @@ export function createPatchedFetcher( ...@@ -859,34 +859,55 @@ export function createPatchedFetcher(
}) })
} }
/** // We used to just resolve the Response and clone it however for
* We used to just resolve the Response and clone it however for static generation // static generation with dynamicIO we need the response to be able to
* with dynamicIO we need the response to be able to be resolved in a microtask // be resolved in a microtask and Response#clone() will never have a
* and Response#clone() will never have a body that can resolve in a microtask in node (as observed through experimentation) // body that can resolve in a microtask in node (as observed through
* So instead we await the body and then when it is available we construct manually // experimentation) So instead we await the body and then when it is
* cloned Response objects with the body as an ArrayBuffer. This will be resolvable in // available we construct manually cloned Response objects with the
* a microtask making it compatiable with dynamicIO // body as an ArrayBuffer. This will be resolvable in a microtask
*/ // making it compatible with dynamicIO.
const pendingResponse = doOriginalFetch(true, cacheReasonOverride) const pendingResponse = doOriginalFetch(true, cacheReasonOverride)
const nextRevalidate = pendingResponse const nextRevalidate = pendingResponse
.then(async (response) => { .then(async (response) => {
// Clone the response here. It'll run first because we attached
// the resolve before we returned below. We have to clone it
// because the original response is going to be consumed by
// at a later point in time.
const clonedResponse = response.clone()
return { return {
body: await response.arrayBuffer(), body: await clonedResponse.arrayBuffer(),
headers: response.headers, headers: clonedResponse.headers,
status: response.status, status: clonedResponse.status,
statusText: response.statusText, statusText: clonedResponse.statusText,
} }
}) })
.finally(() => { .finally(() => {
staticGenerationStore.pendingRevalidates ??= {} // If the pending revalidate is not present in the store, then
// we have nothing to delete.
if (
!staticGenerationStore.pendingRevalidates?.[
pendingRevalidateKey
]
) {
return
}
delete staticGenerationStore.pendingRevalidates[ delete staticGenerationStore.pendingRevalidates[
pendingRevalidateKey pendingRevalidateKey
] ]
}) })
// Attach the empty catch here so we don't get a "unhandled promise
// rejection" warning
nextRevalidate.catch(() => {}) nextRevalidate.catch(() => {})
staticGenerationStore.pendingRevalidates[pendingRevalidateKey] = staticGenerationStore.pendingRevalidates[pendingRevalidateKey] =
nextRevalidate nextRevalidate
return (await pendingResponse).clone()
return pendingResponse
} else { } else {
return doOriginalFetch(false, cacheReasonOverride) return doOriginalFetch(false, cacheReasonOverride)
} }
......
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