Return compressed response from worker

So I set up a static site with Workers Sites and I see the HTML and CSS files are being returned with br compression which is nice.

I tried doing the same returning some HTML from KV but the response is not being compressed.

const body = await NAMESPACE.get(key, 'arrayBuffer');
return new Response(body, {
	status: 200

If I use text instead of arrayBuffer the response is gzipped but obviously the browser is not interpreting the text correctly.

I skimmed over the source for kv-asset-handler which is what is being used for the static site and it seems the idea is to put the response in the CF cache which in turn will deliver the compressed response.

Is this the recommended approach to serve compressed responses?

That didn’t work. I’m fetching the response from the cache and getting a CF-Cache-Status: HIT header but still not getting gzip or brotli.


To be clear I’m using the Cache API with cache.match(...) and cache.put(...).

So I got it working by simply doing:

response.headers.set('Content-Encoding', 'gzip');

No cache shenanigans.

It didn’t work when using https because the brotli compression was enabled on the domain dashboard, but still CF was’t serving the HTML with brotli. Go figure…

So I disabled brotli in the dashboard and now I can serve with gzip.

The only problem now is that I’d actually prefer to serve with brotli but for some reason request.headers.get('Accept-Encoding') only gets gzip and not the full Accept-Encoding value the browser is sending gzip, deflate, br. Not sure if this is a bug or I’m missing something.