Skip to content

Commit

Permalink
Fix clone response backport (#71115)
Browse files Browse the repository at this point in the history
Follow-up to #70649 this tweaks
the backport to properly clone and return original response so that
pending revalidates aren't sharing an already used response.
  • Loading branch information
ijjk authored Oct 10, 2024
1 parent 737c29e commit 4f395f5
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions packages/next/src/server/lib/patch-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,16 +737,28 @@ function createPatchedFetcher(
const res: Response = await pendingRevalidate
return res.clone()
}
return (staticGenerationStore.pendingRevalidates[cacheKey] =
doOriginalFetch(true, cacheReasonOverride)
.then((res) => {
return res.clone()
})
.finally(async () => {
staticGenerationStore.pendingRevalidates ??= {}
delete staticGenerationStore.pendingRevalidates[cacheKey || '']
await handleUnlock()
}))
const pendingResponse = doOriginalFetch(true, cacheReasonOverride)
const nextRevalidate = pendingResponse
.then((res) => res.clone())
.finally(() => {
if (cacheKey) {
// If the pending revalidate is not present in the store, then
// we have nothing to delete.
if (!staticGenerationStore.pendingRevalidates?.[cacheKey]) {
return
}

delete staticGenerationStore.pendingRevalidates[cacheKey]
}
})

// Attach the empty catch here so we don't get a "unhandled promise
// rejection" warning
nextRevalidate.catch(() => {})

staticGenerationStore.pendingRevalidates[cacheKey] = nextRevalidate

return pendingResponse
} else {
return doOriginalFetch(false, cacheReasonOverride).finally(
handleUnlock
Expand Down

0 comments on commit 4f395f5

Please sign in to comment.