mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #111] [MERGED] fix: fetch cache key generation — handle all body types, include all headers minus blocklist #319
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#319
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/111
Author: @threepointone
Created: 2/26/2026
Status: ✅ Merged
Merged: 2/26/2026
Merged by: @threepointone
Base:
main← Head:fix/fetch-cache-key-generation📝 Commits (3)
b420216fix: fetch cache key generation — handle all body types, include all headers minus blocklist84a5ca7Improve fetch cache key generation3d5c73dPR #111 approved. 52 tests pass, 3 fixes correct.📊 Changes
3 files changed (+871 additions, -266 deletions)
View changed files
📝
packages/vinext/src/shims/fetch-cache.ts(+163 -56)📝
tests/fetch-cache.test.ts(+414 -8)📝
tests/fixtures/pages-basic/dist/server/entry.js(+294 -202)📄 Description
Summary
Fixes two bugs in the fetch cache key generation and adds a third bug fix discovered during review.
Bug 1: Binary/FormData bodies ignored in cache key
buildFetchCacheKeyonly handledstringbodies. Requests withUint8Array,ReadableStream,FormData,Blob, orURLSearchParamsbodies all produced the same cache key regardless of body content — causing incorrect cache hits.Fix:
serializeBody()now handles allBodyInittypes.buildFetchCacheKeyis now async to support stream/blob consumption.Bug 2: Header inclusion strategy inverted vs Next.js
We used an allowlist of 3 auth headers (
authorization,cookie,x-api-key). Next.js uses a blocklist (traceparent,tracestate) and includes everything else. This meant headers likeAccept,Accept-Language, and custom headers were ignored — different requests could incorrectly share cache entries.Fix: Switched to blocklist approach matching Next.js. All headers are now included in the cache key except W3C trace context headers.
Bug 3: Consumed stream body sent to network (found during review)
After
serializeBodyconsumed aReadableStreambody for cache key generation, the spent stream was still passed tooriginalFetch(). The reconstructed body (_ogBody) was stashed but never restored.Fix:
stripNextFromInitnow restores_ogBodyas the body before passing tooriginalFetch.Additional improvements
mode,redirect,credentials,referrer,referrerPolicy,integrity,cachefromRequestInitCACHE_KEY_PREFIX = "v1"for future cache-bustingURLSearchParamshandled separately fromFormData(different APIs)Tests
19 new tests (52 total):
originalFetchgets usable body after key generation)Not breaking
All changes are internal. Public API unchanged. Only user-visible effect: one-time cold cache after upgrade (new key format).
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.