fix: respect Accept-Encoding for pre-compressed JS (#5750)
This commit is contained in:
@@ -5,6 +5,8 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"io"
|
||||||
|
"compress/gzip"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@@ -144,16 +146,32 @@ func getStaticHandlers(store *storage.Storage, server *settings.Server, assetsFs
|
|||||||
return 0, nil
|
return 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
fileContents, err := fs.ReadFile(assetsFs, r.URL.Path+".gz")
|
f, err := assetsFs.Open(r.URL.Path + ".gz")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return http.StatusNotFound, err
|
return http.StatusNotFound, err
|
||||||
}
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
w.Header().Set("Content-Encoding", "gzip")
|
acceptEncoding := r.Header.Get("Accept-Encoding")
|
||||||
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
if strings.Contains(acceptEncoding, "gzip") {
|
||||||
|
w.Header().Set("Content-Encoding", "gzip")
|
||||||
|
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
||||||
|
|
||||||
if _, err := w.Write(fileContents); err != nil {
|
if _, err := io.Copy(w, f); err != nil {
|
||||||
return http.StatusInternalServerError, err
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
gzReader, err := gzip.NewReader(f)
|
||||||
|
if err != nil {
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
defer gzReader.Close()
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/javascript; charset=utf-8")
|
||||||
|
|
||||||
|
if _, err := io.Copy(w, gzReader); err != nil {
|
||||||
|
return http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0, nil
|
return 0, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user