Relisora
Relisora
Explore posts from servers
CDCloudflare Developers
Created by Relisora on 4/26/2024 in #workers-help
Worers outcome: canceled
Hello, I am struggling a bit with my new API made with hono using CF workers. At some random times, for a few minutes, all requests time out and from the worker logs I see that their outcome is cancelled. Once those few minutes are over, everything works perfectly again. It's worth to note that while I'm getting myself the error, I see some successful requests in the logs too, so it may be only the workers in my area or something like that? The API is really simple, all it does is connect to postgres (First I connected to hyperdrive and got the error, and now I connect to the DB directly and still get the error) then returns the value. Any ideas about how to solve it? It seems to occur very regularly across different locations, according to my logs
9 replies
CDCloudflare Developers
Created by Relisora on 3/13/2024 in #workers-help
Caching a brotli encoded response in KV from a Worker
Hello o/ I am trying to create a proxy with my worker which would just fetch the original url on first call, then cache the response. Until the cache gets hit, that worker would just send a reponse which is the cached value. I am blocking on the "saving in cache" part as I noticed that the reponse I'm getting from the database API (supabase) is encoded in brotli. Here is my code:
// Forward the request to Supabase and return the response
const data = await fetch(modifiedRequest);
const dataClone = data.clone()
const dataText = await dataClone.text()
console.log(dataText)

// Set the cache if response is successful
if (data.status === 200) await env.SUPABASE_PREVIEW.put(cacheKey, JSON.stringify(dataText), { expirationTtl: 60 })

return new Response(data.body, {
status: data.status,
statusText: data.statusText,
headers: data.headers
});
// Forward the request to Supabase and return the response
const data = await fetch(modifiedRequest);
const dataClone = data.clone()
const dataText = await dataClone.text()
console.log(dataText)

// Set the cache if response is successful
if (data.status === 200) await env.SUPABASE_PREVIEW.put(cacheKey, JSON.stringify(dataText), { expirationTtl: 60 })

return new Response(data.body, {
status: data.status,
statusText: data.statusText,
headers: data.headers
});
I think that the problem is that this response is encoded and therefore dataText is unreadable. I tried .json() instead as the response has json type but it fails even harder. Does anyone have an idea of what I'm doing wrong, what I'm not understanding or just any thing you'd like me to test and see if it helps? Thanks a lot ❤️
1 replies