Skip to content

Commit

Permalink
fix: clone response in first handler to prevent race (vercel#70082)
Browse files Browse the repository at this point in the history
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.
# Conflicts:
#	packages/next/src/server/lib/patch-fetch.ts
  • Loading branch information
wyattjoh authored and ijjk committed Sep 30, 2024
1 parent 3020a11 commit b9f6e3e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,14 @@ function createPatchedFetcher(

if (pendingRevalidate) {
const res: Response = await pendingRevalidate
return res.clone()
const clonedResponse = res.clone()

return {
body: await clonedResponse.arrayBuffer(),
headers: clonedResponse.headers,
status: clonedResponse.status,
statusText: clonedResponse.statusText,
}
}
return (staticGenerationStore.pendingRevalidates[cacheKey] =
doOriginalFetch(true, cacheReasonOverride).finally(async () => {
Expand All @@ -757,7 +764,7 @@ function createPatchedFetcher(
patched.__nextGetStaticStore = () => staticGenerationAsyncStorage
patched._nextOriginalFetch = originFetch

return patched
return patched as PatchedFetcher
}

// we patch fetch to collect cache information used for
Expand Down

0 comments on commit b9f6e3e

Please sign in to comment.