Worker doesn't return gzip/brotli compressed data

I have setup a worker which returns some JSON data, but I have noticed that the response is not compressed in any way.

I searched in this forum for similar topics and found a few threads that stated that the “content-type” header should be set to a value that would match CF’s “whitelist” in order for the response to get compressed. The “Content-type” header in my response is set to “application/json; charset=utf-8”, but it’s still uncompressed.

What should I do to get my responses gzip-ed / brotli-ed?

Haven’t used gzip for JSON, but maybe if you add header like ("Content-Encoding", "gzip")?
Probbably in combination with ("Content-length"), if so.

Neither sure if Workers should do it, or rather your origin host / server?

Furthermore, may I ask is Brotli option enabled at Cloudflare dashboard for your domain name? For Brotli, it should be ("Content-Encoding", "br").

Below post could maybe help too:

Just in case, kindly consider lookin into the posts from below topic:

Please, wait for another reply from someone more experienced with Cloudflare Workers. Tagging @WalshyMVP :wink:

If you return a response with the header Content-Encoding: gzip, Workers will automatically gzip-compress it for you. Something like this:

return new Response(JSON.stringify(result), {
    headers: {
        "Content-Type": "application/json",
        "Content-Encoding": "gzip"
    }})

Unfortunately, only gzip is supported in this way, not brotli at this time.

3 Likes