How do I do gzip and / or brotli compression for data in CloudFlare Workers?

How do I do gzip and / or brotli compression for data in CloudFlare Workers?

Would like to compress and output the data in gzip or brotli.

How do I get it done?

It does that automatically, but it should only compress when it is more efficient. Something below 1kb there is no gain. What is your output size?

it doesnt do that automatically.

are you sure it does?

i stored response.text() inside a kv, and use the below to return and it doesnt seem to automatically compress the output

return new Response(clonedResponse.body, response)

not really sure how to double confirm this but some of the output generated is actually text/html raw without compression. 65kb to 150kb

@harris reply at Cloudflare 503 Status Maintenance Page worker compressed HTML response? helped me

HTH

@user2952 I faced the same issue recently I was able to solve by setting a content-type header on the response. I found this info from these docs. And you get will get brotli by default but you can configure it using content-encoding header as mentioned in @eva2000 message above.