My cloudflare workers site houses binary data that a react app fetches. This binary data is stored as gzip compressed as it compresses extremely well (we're talking about a 20-25x reduction and uncompressed it is too large to fit in the 10MB KV limit). The problem I'm running into is that either the worker returns the data without the appropriate header:
Content-Encoding: gzip
or if I have the worker add the header, cloudflare will doubly compress the response. So how do I store gzip compressed data within cloudflare KV such that I can return it with the proper content encoding and without cloudflare doubly compressing the response?
Reference
For a minimal reproduction: here are the two worker scripts I'm using.
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
addEventListener('fetch', event => {
try {
event.respondWith(handleEvent(event))
} catch (e) {
event.respondWith(new Response('Internal Error', { status: 500 }))
}
})
async function handleEvent(event) {
const cacheControl = { browserTTL: 60 * 60 * 6 };
return await getAssetFromKV(event, { mapRequestToAsset, cacheControl });
}
The above worker script returns the binary data without content encoding header, so the browser does not automatically inflate the response.
So then I tried adding the header manually via
import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler'
addEventListener('fetch', event => {
try {
event.respondWith(handleEvent(event))
} catch (e) {
event.respondWith(new Response('Internal Error', { status: 500 }))
}
})
async function handleEvent(event) {
const cacheControl = { browserTTL: 60 * 60 * 6 };
const resp = await getAssetFromKV(event, { mapRequestToAsset, cacheControl });
resp.headers.set("Content-Encoding", "gzip");
return resp;
}
The response has the correct content-encoding but cloudflare compresses the response so now it is serving gzip within gzip.
Is there any way to get a middle ground where I can serve compressed data from within cloudflare KV with the correct header and without doubly compressing the response?