Skip to content

Commit d6fd8d5

Browse files
fix: make sure that the patched fetch cache set promise is properly awaited (vercel#75971)
The [promise created for updating the incremental cache on successful fetches](https://github.com/vercel/next.js/blob/106e17ca8f093899d300885938eab863ffa478a7/packages/next/src/server/lib/patch-fetch.ts#L687-L728) is not used, returned nor awaited in any way. I imagine that this can be fine in environments such as Node.js servers where the dangling promise will eventually run, in other environments however the promise might be cancelled causing the cache update not to happen, so it would be appropriate, when possible that the promise is awaited using `waitUntil` (for more context, I've encountered this issue in the [OpenNext project](https://github.com/opennextjs)). So in this PR I'm adding the promise to the `pendingRevalidates` map so that it will eventually be properly awaited. cc. @vicb, @conico974 <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # --> Co-authored-by: JJ Kasper <jj@jjsweb.site>
1 parent 23d2441 commit d6fd8d5

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ export function createPatchedFetcher(
676676
// We are dynamically rendering including dev mode. We want to return
677677
// the response to the caller as soon as possible because it might stream
678678
// over a very long time.
679-
cloned1
679+
const cacheSetPromise = cloned1
680680
.arrayBuffer()
681681
.then(async (arrayBuffer) => {
682682
const bodyBuffer = Buffer.from(arrayBuffer)
@@ -710,6 +710,26 @@ export function createPatchedFetcher(
710710
)
711711
.finally(handleUnlock)
712712

713+
const pendingRevalidateKey = `cache-set-${cacheKey}`
714+
workStore.pendingRevalidates ??= {}
715+
if (pendingRevalidateKey in workStore.pendingRevalidates) {
716+
// there is already a pending revalidate entry that
717+
// we need to await to avoid race conditions
718+
await workStore.pendingRevalidates[pendingRevalidateKey]
719+
}
720+
workStore.pendingRevalidates[pendingRevalidateKey] =
721+
cacheSetPromise.finally(() => {
722+
// If the pending revalidate is not present in the store, then
723+
// we have nothing to delete.
724+
if (
725+
!workStore.pendingRevalidates?.[pendingRevalidateKey]
726+
) {
727+
return
728+
}
729+
730+
delete workStore.pendingRevalidates[pendingRevalidateKey]
731+
})
732+
713733
return cloned2
714734
}
715735
}

0 commit comments

Comments
 (0)