mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 00:09:23 +02:00
[PR #425] [MERGED] fix(isr): KVCacheHandler.set() now awaits KV put to prevent perpetual STALE #563
Labels
No labels
enhancement
enhancement
good first issue
help wanted
nextjs-tracking
nextjs-tracking
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/vinext#563
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/cloudflare/vinext/pull/425
Author: @james-elicx
Created: 3/10/2026
Status: ✅ Merged
Merged: 3/10/2026
Merged by: @james-elicx
Base:
main← Head:fix/isr-kv-stale-regen📝 Commits (2)
b260f7ffix(isr): KVCacheHandler.set() now awaits KV put to prevent perpetual STALEf25a25efix: remove unused variable flagged by lint📊 Changes
2 files changed (+148 additions, -11 deletions)
View changed files
📝
packages/vinext/src/cloudflare/kv-cache-handler.ts(+6 -11)📝
tests/kv-cache-handler.test.ts(+142 -0)📄 Description
Problem
After a cache entry's TTL expires, every subsequent request returned
X-Vinext-Cache: STALEand never transitioned toHIT. Background regeneration appeared to trigger but the cache was never updated.Root cause:
KVCacheHandler.set()called_putInBackground()and immediately returnedPromise.resolve(). In the background regen path inapp-rsc-entry.ts:```js
await Promise.all([
__isrSet(htmlKey, ...), // resolves immediately — KV put not done
__isrSet(rscKey, ...),
]);
// renderFn() resolves here, before KV write completes
```
ctx.waitUntil(renderFn())expired as soon asrenderFn()resolved. The Cloudflare Workers runtime tore down the isolate while thekv.put()network operation was still in flight, silently killing the write. The entry stayed STALE forever.The MISS path was not affected — it tees the HTML stream and builds an explicit
__cachePromisethat fully awaits the write beforectx.waitUntilcan expire.Fix
Replace the fire-and-forget
_putInBackground()with_put()which returns the actualkv.put()promise.set()now returns that promise instead ofPromise.resolve().```ts
// Before
this._putInBackground(kvKey, json, opts);
return Promise.resolve(); // resolves before KV write
// After
return this._put(kvKey, json, opts); // resolves when KV write completes
```
_put()also registers withctx.waitUntil()as belt-and-suspenders (same as before), so the Workers runtime keeps the isolate alive even if a caller doesn'tawaitthe returned promise.The full regen chain is now properly awaited end-to-end:
ctx.waitUntil(renderFn())→await Promise.all([__isrSet(...)])→await handler.set(...)→return this._put(...)→return kv.put(...)Tests
Adds three regression tests in
tests/kv-cache-handler.test.tsunder "STALE → regen → HIT lifecycle":set()resolves only after the KV put completesset()returned promise is the actualkv.putpromise, not an immediately-resolved stub (uses a latch-controlled mock to prove this)waitUntilcovers the actual KV write with a delayed put — regen promise does not settle until the write completes🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.