Incorrect caching of requests using URLSearchParams as body
Nobody has claimed this yet.
- Dominant language
- JavaScript
- Stars
- 166
- Forks
- 21
- PR merge metrics
- No merged PRs in 30d
Description
Example (adapted from https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body):
Fetch("https://example.com", {
fetchOptions: {
method: "POST",
body: new URLSearchParams({ query: 'abc' }),
},
})
Any change in the URLSearchParams will hit the same cache entry, which should not happen.
This happens because JSON.stringify (used here) serializes any URLSearchParams object to {}, because URLSearchParams does not implement a toJSON method. A solution could be to implement a replacer for JSON.stringify.
As a workaround, one can manually convert it to its string representation and set the Content-Type header (as undici's implementation would do automatically in the first example):
let params = new URLSearchParams({ query: 'abc' });
Fetch("https://example.com", {
fetchOptions: {
method: "POST",
body: params.toString(),
headers: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
},
})
Note: as mentioned in the MDN article, URLSearchParams is not the only kind of object one can pass as body, apart from a plain string. I only tested URLSearchParams.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in src/RemoteAssetCache.js at the JSON.stringify call linked in the report. Reproduce the behavior with two different URLSearchParams bodies, then inspect how the cache key is formed and add coverage for distinct entries. Done means changed URLSearchParams bodies no longer share a cache entry.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100