It used to be a cool feature? I was just about to switch to caddy 2 but found out that my staticly compiled brotli files for styles and everything are not served. It uses dynamic gzip instead…
Docs say that it can do it, but in my case it doesn’t work
I feel like the docs aren’t wrong here because they don’t actually claim Caddy automatically does it - they advise that you do it. It is possible to do manually.
Very basically achieved like so:
# Find if the request has a .br variant (or .br index variant)
@brotli {
file {
try_files {uri}/index.html.br {uri}.br
}
}
# If it does exist, rewrite to it instead
rewrite @brotli {http.matchers.file.relative}
Brotli encoding support was included in Caddy’s core earlier, but it was stripped out for performance reasons. It was done in this commit:
This plugin did actual brotli compression on the fly, but it wasn’t ideal. The better approach is to serve pre-compressed like @Whitestrake and @matt already mentioned
It doesn’t work with my example because i already use try_files.
By some reason when i add your example or Matt’s it just sets encoding to all files to be “br”, but it doesn’t really serve encoded files.
Your Caddyfile above uses the directive, but the examples Matt and I gave show use of the matcher. You do not need to merge the contents of the file matcher with your existing try_files directive. You can have as many matchers as you want, all matching separate things.
I am on my way to sleep so I didn’t test it for you. (I would like the first syntax I gave you to work though, so I’ll submit a PR for that in the morning.)
Aye. Unlearning convention Caddy 1 invented is a pain point we see all over the place. Ultimately it’s important to remember that Caddy v1 and v2 are, essentially, completely different programs. There’s an absolutely massive amount of feature parity, but they’re just not the same underneath the hood at all.
Implementing automatic precompressed asset serving in Caddy v2 could be a good issue. Currently the closest one that mentions this is v2: Content negotiation · Issue #2665 · caddyserver/caddy · GitHub, but it’s not explicitly for this feature, only some functionality that would make this feature easier.