Wasteful Response Cloning While Caching in Worker

Hi! Inside a Worker, I’m trying to cache a response using caches.default.put() inside a waitUntil(), and then immediately returning the original response. Here’s a brief snippet:

ctx.waitUntil(caches.default.put(cacheKey, response.clone()));

return response;

This looks pretty straightforward, but I’m getting this error:

Your worker called response.clone(), but did not read the body of both clones. This is wasteful, as it forces the system to buffer the entire response body in memory, rather than streaming it through. This may cause your worker to be unexpectedly terminated for going over the memory limit. If you only meant to copy the response headers and metadata (e.g. in order to be able to modify them), use new Response(response.body, response) instead.

Some version of this error appears to be happening no matter how I approach it. I’ve also attempted using .tee() on the response body to create two, brand-new responses for different uses, but no luck there either.

Is there some weird async thing going on? Maybe by the time the worker executes, it “thinks” the cached response was never read? How do I get around this?


1 Like