cloudflare / cloudflare/workerd
🐛 Bug Report — Runtime APIs -- Using Cache API in HtmlRewriter while caching the response causes a deadlock
- Dominant language
- C++
- Stars
- 8.7k
- Forks
- 739
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 174
Description
Hey! I've ran into an interesting edge case it seems. Async Handlers are supported with HtmlRewriter, however if you try to use the cache api inside of an async handler while caching the response itself, it just loads forever/seems to deadlock itself. No errors in console. This is reproducible both in local dev and deployed on Cloudflare Workers.
Min. Reproducible Example:
```js
export default {
async fetch(request, env, ctx) {
let myResponse = new Response("x
test
", {headers: {
"content-type": "text/html",
}
})
myResponse = new HTMLRewriter()
.on('h1', {
async element(e) {
await caches.default.put("https://example.com/dummy", new Response("hey!")) // I know this wouldn't get cached, just min to reproduce issue
e.setInnerContent('Rewritten');
}
})
.transform(myResponse);
await caches.default.put("https://example.com/raw", myResponse.clone());
return myResponse;
},
};
```
(of course in a larger example I was fetching, changing response headers and caching/using body/using ctx.waitUntil)
The .clone itself doesn't block, I tried doing it outside of it. If I force the rewriter to run and pull the response into memory that works around it:
```js
let body = await myResponse.arrayBuffer()
let cacheResponse = new Response(body, myResponse);
cacheResponse.headers.append("Cache-Control", "s-maxage=120");
await caches.default.put("https://example.com/raw", cacheResponse);
return new Response(body, myResponse);
```
Contributor guide
Assessment
This issue has not been assessed yet.