Since Plug.Static can serve both gzip and brotli, can phx.digest be tweaked to compressed into both?

Right now it can only compress into gzip with the Erlang :zlib.gzip function.

2 Likes

As Plug.Static does not actually do brotli (de)compression, it would require pulling in a brotli library as an dependency … but otherwise, yes it could be implemented fairly easily with some adjustments to Phoenix.Digester.write_to_disk/3.

The real question is whether that dependency would be OK to the Phoenix team. It could be written to produce brotli files only if you pull in brotli as a local dependency with something like:

diff --git a/lib/phoenix/digester.ex b/lib/phoenix/digester.ex
index 9c8a59c0..ba86feeb 100644
--- a/lib/phoenix/digester.ex
+++ b/lib/phoenix/digester.ex
@@ -179,6 +179,11 @@ defmodule Phoenix.Digester do
     if compress_file?(file) do
       File.write!(Path.join(path, file.digested_filename <> ".gz"), :zlib.gzip(digested_file_contents))
       File.write!(Path.join(path, file.filename <> ".gz"), :zlib.gzip(file.content))
+
+      if Code.ensure_loaded(:brotli) == {:module, :brotli} do
+        File.write!(Path.join(path, file.digested_filename <> ".br"), :brotli.encode(digested_file_contents))
+        File.write!(Path.join(path, file.filename <> ".br"), :brotli.encode(file.content))
+      end
     end
 
     # uncompressed files

You could always place an issue on the issue tracker, or maybe @chrismccord will notice this posting :slight_smile:

3 Likes

Thanks for your detailed reply, which almost makes a good PR in my humble opinion. :smile:

I do understand Plug.Static does no compressing but only serves the appropriate files. But the Plug.Static doc already mentions brotli and even says " FILE.br is checked first and dominates FILE.gz due to the better compression ratio". Even if phx.digest does not produce brotli files, I think it might worth mentioning something in the doc of phx.digest to avoid confusion.