mirror of
https://github.com/cloudflare/vinext.git
synced 2026-05-09 08:25:34 +02:00
[PR #332] [MERGED] Fix fetch cache key collisions for Request and FormData bodies #484
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#484
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/332
Author: @JaredStowell
Created: 3/7/2026
Status: ✅ Merged
Merged: 3/8/2026
Merged by: @james-elicx
Base:
main← Head:jstowell/fix-cache-key-collisions📝 Commits (7)
0ac97abFix fetch cache key collisions for Request and FormData bodies1373156Add additional tests7887a36Address codex feedback by reading incrementally with early exit on content-length check + regression test44210a8Add regression test for already-consumed bodyd659ed1Harden fetch cache keying for Request form bodies + testse2241aaPreserve FormData insertion order + add file name/type into cache key payload + tests0951604Fix PR feedback + additional regression tests📊 Changes
2 files changed (+532 additions, -32 deletions)
View changed files
📝
packages/vinext/src/shims/fetch-cache.ts(+161 -29)📝
tests/fetch-cache.test.ts(+371 -3)📄 Description
Summary
Fix fetch cache key generation so cached requests are keyed by the actual effective request body, including when the body is provided on a
Requestobject.This also fixes ambiguous
FormDataserialization that could cause distinct payloads to collapse into the same cache entry.Problem
Vinext’s fetch cache key generation did not fully account for the request body in all supported call shapes.
Requestbodies were ignoredThe cache key logic correctly merged headers from both
inputandinit, but it only serializedinit.body. If the body lived on aRequestobject, vinext effectively treated the request as body-less for cache-key purposes.Example:
Before this change, both requests could produce the same cache key even though the payloads were different.
That makes cached POST-style fetches unsafe: a response generated for one payload can be reused for another payload.
Concrete failure modes include:
Multi-value
FormDatacould collideFormDatavalues were serialized by joining them with commas, which is ambiguous.Example:
These are different payloads, but they produced the same cache-key fragment.
Root Cause
The issue was in the fetch cache key builder, not in cache storage itself.
collectHeaders()already handledRequestinputs correctlybuildFetchCacheKey()usedserializeBody(init)serializeBody()only looked atinit.bodyRequestbodies were therefore omitted unless duplicated ininitFormDataentries were flattened with comma-joining, which is not injectiveIn other words, the cache key did not always represent the true effective request.
What Changed
Fetch cache keying
Requestobject bodies in cache key generationRequestinputs without mutating the original fetch behaviorFormDataserializationTests
Add regression coverage for:
Requestbodies producing distinct cache entriesRequestbodies reusing the same cache entryFormDatapayloads not collidingRequestbodies still being forwarded intact after cache-key generationExamples
Example 1: POST search requests
Before:
These could collide and reuse the wrong cached response.
After:
Example 2: Multi-value form submissions
Before:
These serialized to the same cache-key fragment.
After:
FormDatavalues are serialized in a structured formatWhy This Approach
This change fixes the root cause while keeping the existing caching model intact.
The principle is simple:
The patch stays within the current fetch cache architecture and only changes key generation and regression coverage.
Files Changed
packages/vinext/src/shims/fetch-cache.tstests/fetch-cache.test.tsTest Plan
Ran targeted regression coverage for the affected surface:
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.