all repos — homestead @ 37a07480b19013b92c8cd851d507e622968fbb0b

Code for my website

serve pre-compressed files according to accept-encoding

Alan Pearce
commit

37a07480b19013b92c8cd851d507e622968fbb0b

parent

1f86478ad670bdecdd2fa26d17ec23ce902dd8a0

1 file changed, 12 insertions(+), 4 deletions(-)

changed files
M internal/website/mux.gointernal/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 }))