How to serve directly my Brotli and Gzip pre-compressed css and js instead of the Cloudflare compressed ones?

Is there a way with Coudflare Workers to serve my already Brotli pre-compressed resources?

I believe you can tell cf not to re-encode specific files using an origin header.

If you do not want a particular response from your origin to be encoded, you can disable this by setting cache-control: no-transform at your origin web server.

Already tested the no-transform, but even with no-transform Cloudflare serves it as gzip.

Hmm, have you tried the Response init property?

encodeBody string

  • Workers have to compress data according to the content-encoding header when transmitting, to serve data that is already compressed, this property has to be set to "manual" , otherwise the default is "auto" .

Yes tested it, but no results.

AFAIK, you can’t serve origin server precompressed Brotli assets. However, precompressed origin server Gzip assets will be served to CF Edge Does Cloudflare compress resources? – Cloudflare Help Center

Yes, Cloudflare applies gzip and brotli compression to some types of content. We also gzip items based on the browser’s UserAgent to help speed up page loading time.

If you’re already using gzip we will honor your gzip settings as long as you’re passing the details in a header from your web server for the files.

Cloudflare only supports the content types gzip towards your origin server and can also only deliver content either gzip compressed , brotli compressed , or not compressed .

Cloudflare’s reverse proxy is also able to convert between compressed formats and uncompressed formats, meaning that it can pull content from a customer’s origin server via gzip and serve it to clients uncompressed (or vice versa). This is done independently of caching.

From the enterprise support team they told me it’s possible to do that with Workers, but I’m not figuring out how to do that.

So community and docs says it’s not possible, but from the support team they say it’s possible.

What to do now?

Yeah haven’t tried from CF Worker but probably similar to Storing Compressed Data for Cloudflare Workers Sites - #4 by nbabcock19

and Accessing Compressed Data for Cloudflare Workers Sites - Stack Overflow

This is an unfortunate wart of the Service Workers spec – it’s designed for use in the browser, where it expects response bodies to be decompressed before the worker runs, and does not expect they will be transmitted over the network again. To make things consistent, Cloudflare Workers has to recompress the data according to the content-encoding header when transmitting. But this in turn means there’s no way to serve data that is already compressed.

To solve this, we (Cloudflare) have added a non-standard Response option called encodeBody , which can be "auto" (the default) or "manual" (assumes the body is already compressed).

No matter what clients sending request with compression or not, CloudFlare always use http1.1 connection with accept-encoding gzip to origin so is it waste of enable brotli on origin?