Skip to content

Commit

Permalink
fix: clone response in first handler to prevent race (#70082) (#70649)
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.

backports #70082

---------

Co-authored-by: Wyatt Johnson <[email protected]>
  • Loading branch information
ijjk and wyattjoh authored Oct 1, 2024
1 parent 73f6b7d commit 887a419
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -738,11 +738,15 @@ function createPatchedFetcher(
return res.clone()
}
return (staticGenerationStore.pendingRevalidates[cacheKey] =
doOriginalFetch(true, cacheReasonOverride).finally(async () => {
staticGenerationStore.pendingRevalidates ??= {}
delete staticGenerationStore.pendingRevalidates[cacheKey || '']
await handleUnlock()
}))
doOriginalFetch(true, cacheReasonOverride)
.then((res) => {
return res.clone()
})
.finally(async () => {
staticGenerationStore.pendingRevalidates ??= {}
delete staticGenerationStore.pendingRevalidates[cacheKey || '']
await handleUnlock()
}))
} else {
return doOriginalFetch(false, cacheReasonOverride).finally(
handleUnlock
Expand All @@ -757,7 +761,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 887a419

Please sign in to comment.