I’m getting an undocumented exception being thrown from Cache API, “Error: Unable to delete cached response.” from caches.default.delete(). Docs say Cache · Cloudflare Workers docs
returns a Promise for a Boolean response this is a promise rejection I believe, not a promise resolve with a true/false value. Actual code, only fails on real live runtime, not in Workers Preview Quick Edit. Are the docs wrong for delete() or this is a CF bug?
"use strict";
let v8start;
let routes;
addEventListener('fetch', event => {
if (!v8start) {
v8start = Date.now()
};
event.respondWith(handleRequest(event.request, event))
})
/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request, event) {
var url = new URL(request.url);
var pathname_callback = url.pathname;
//console.log(pathname_callback);
if (pathname_callback === "/routes.json") {
console.log('inr');
try {
return new Promise(async function (resolveCB) {
updateRoutes(event, resolveCB);
});
} catch (e) {
return new Response(e);
}
}
}
async function updateRoutes(event, resolveCB) {
var resp = fetch('https://httpbin.org/get');
resp = await resp;
if (resp.status == 200) {
resp = resp.text();
resp = await resp;
try {
console.log('del prom ' + await caches.default.delete('http://1.1.1.1/routes.json', {
ignoreMethod: true
}));
} catch (e) {
resolveCB(new Response(e))
}
if (resolveCB) {
resolveCB(new Response(resp, {
headers: {
'content-type': 'application/json',
'cache-control': 'no-cache, no-store',
"x-FRM-ORIGIN": 'true',
'x-v8st': v8start
}
}))
} else {
resolveCB(new Response('impossible 1'))
}
} else {
if (resolveCB)
resolveCB(resp);
}
return;
}
FetchEvent handler did not call respondWith() before returning, but initiated some asynchronous task. That task will be canceled and default handling will occur -- the request will be sent unmodified to your origin. Remember that you must call respondWith() *before* the event handler returns, if you don't want default handling. You cannot call it asynchronously later on. If you need to wait for I/O (e.g. a subrequest) before generating a Response, then call respondWith() with a Promise (for the eventual Response) as the argument.
The new Promise basically gets a handle to resolveCB native code from runtime, because I generate the Response() obj at many different places in my code, AND I do extra I/O or extra JS Statements (storing stuff in HS global vars) after delivering a HTTP response to the eyeball. I can’t write the code into a “return new Response(body, {headers: ‘X’:‘yz’});” since that stops execution. If I switch event.respondWith(handleRequest(event.request, event)) to handleRequest(event.request, event), I get the message fetch didn’t call respondWith message.
If I keep respondWith(handleRequest( but remove the generic promise you are talking about, I get
Uncaught (in response) TypeError: Incorrect type for Promise: the Promise did not resolve to 'Response'.
new Promise() seems to be a coding pattern for Workers
and alot of other posts with Promise.all() etc
This cache API throw is a heavily modded version of my code here
but in the example above, I never had to call cache.delete() before.