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 ❤️
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server