Skip to content

Commit 887a419

Browse files
ijjkwyattjoh
andauthored
fix: clone response in first handler to prevent race (#70082) (#70649)
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]>
1 parent 73f6b7d commit 887a419

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

packages/next/src/server/lib/patch-fetch.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,15 @@ function createPatchedFetcher(
738738
return res.clone()
739739
}
740740
return (staticGenerationStore.pendingRevalidates[cacheKey] =
741-
doOriginalFetch(true, cacheReasonOverride).finally(async () => {
742-
staticGenerationStore.pendingRevalidates ??= {}
743-
delete staticGenerationStore.pendingRevalidates[cacheKey || '']
744-
await handleUnlock()
745-
}))
741+
doOriginalFetch(true, cacheReasonOverride)
742+
.then((res) => {
743+
return res.clone()
744+
})
745+
.finally(async () => {
746+
staticGenerationStore.pendingRevalidates ??= {}
747+
delete staticGenerationStore.pendingRevalidates[cacheKey || '']
748+
await handleUnlock()
749+
}))
746750
} else {
747751
return doOriginalFetch(false, cacheReasonOverride).finally(
748752
handleUnlock
@@ -757,7 +761,7 @@ function createPatchedFetcher(
757761
patched.__nextGetStaticStore = () => staticGenerationAsyncStorage
758762
patched._nextOriginalFetch = originFetch
759763

760-
return patched
764+
return patched as PatchedFetcher
761765
}
762766

763767
// we patch fetch to collect cache information used for

0 commit comments

Comments
 (0)