serve pre-compressed files according to accept-encoding
1 file changed, 12 insertions(+), 4 deletions(-)
changed files
M internal/website/mux.go → internal/website/mux.go
@@ -9,6 +9,7 @@ "website/internal/config" "website/internal/log" "github.com/benpate/digit" + "github.com/kevinpollet/nego" "github.com/pkg/errors" )@@ -22,7 +23,7 @@ func canonicalisePath(path string) (cPath string, differs bool) { cPath = path if strings.HasSuffix(path, "/index.html") { cPath, differs = strings.CutSuffix(path, "index.html") - } else if !strings.HasSuffix(path, "/") && files[path+"/"] != (File{}) { + } else if !strings.HasSuffix(path, "/") && files[path+"/"] != nil { cPath, differs = path+"/", true }@@ -67,7 +68,7 @@ return nil } file := GetFile(urlPath) - if file == (File{}) { + if file == nil { return &HTTPError{ Message: "File not found", Code: http.StatusNotFound,@@ -79,8 +80,15 @@ w.Header().Add("Content-Security-Policy", cfg.CSP.String()) for k, v := range cfg.Extra.Headers { w.Header().Add(k, v) } - - http.ServeFile(w, r, files[urlPath].filename) + enc := nego.NegotiateContentEncoding(r, "br", "gzip") + switch enc { + case "", "identity": + http.ServeFile(w, r, files[urlPath].filename) + case "br", "gzip": + w.Header().Add("Content-Encoding", enc) + w.Header().Add("Content-Type", file.contentType) + http.ServeFile(w, r, files[urlPath].alternatives[enc]) + } return nil }))