Why caddy 2 is not able to serve static brotli files?

header gets executed before handle and rewrite, so theoretically we can easily manually re-specify the Content-Type based on the request without even messing with the existing Brotli sidecar detection/rewrite. The file server should leave the manually-set Content-Type header alone, I think?

Try something like this.

@brotli {
  file {
    try_files {path}.br
  }
}
handle @brotli {
  header Content-Encoding br
  rewrite {http.matchers.file.relative}
}

@svg {
  file
  path *.svg
}
header @svg Content-Type image/svg+xml

@css {
  file
  path *.css
}
header @css Content-Type text/css

@js {
  file
  path *.js
}
header @js Content-Type text/javascript

If you wanted to be fast and dirty but risk setting the wrong Content-Type on a 404, you could skip all of the matcher declarations and just go with:

@brotli {
  file {
    try_files {path}.br
  }
}
handle @brotli {
  header Content-Encoding br
  rewrite {http.matchers.file.relative}
}

header *.svg Content-Type image/svg+xml
header *.css Content-Type text/css
header *.js  Content-Type text/javascript
1 Like