If a particular response for an origin URL is not found in cache, in order to stop Cloudflare from making multiple requests to my origin server if two or more parallel requests come in at the same time for the same uncached URL, I “share” the fetch() Promise across multiple requests using a global variable.
This seems to work perfectly fine in production (only one request is made to the origin server while the other requests wait for the shared fetch() Promise to resolve), but if I’m running wrangler dev on my local machine and make multiple parallel requests to uncached URLs (so that the sharing of the Promise kicks in), I get this error:
[ERROR] Uncaught (in promise) Error: Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance.
Again, this is only on my local machine running wrangler dev (not in “local” mode, so from my understanding the requests are proxied by wrangler to an actual Cloudflare data center nearby).
I’m happy it’s working fine in production, but the above error worries me that it could stop working at any moment if the production environment suddenly decides to switch over to a more restrictive mode.
Can someone from Cloudflare please chime in and let me know which of the above outcomes can be relied upon (i.e. always erroring or always working)?
Thanks,
Nick