improve (speed): support for brotli compression

This commit is contained in:
Mickael Kerjean
2019-05-09 00:43:05 +10:00
parent 5c0cd37f77
commit 7f34aaea2c
2 changed files with 23 additions and 1 deletions

View File

@ -78,21 +78,36 @@ func AboutHandler(ctx App, res http.ResponseWriter, req *http.Request) {
func ServeFile(res http.ResponseWriter, req *http.Request, filePath string) {
zFilePath := filePath + ".gz"
bFilePath := filePath + ".br"
etagNormal := hashFile(filePath, 10)
etagGzip := hashFile(zFilePath, 10)
etagBr := hashFile(bFilePath, 10)
if req.Header.Get("If-None-Match") != "" {
browserTag := req.Header.Get("If-None-Match")
if browserTag == etagNormal {
res.WriteHeader(http.StatusNotModified)
return
} else if browserTag == etagBr {
res.WriteHeader(http.StatusNotModified)
return
} else if browserTag == etagGzip {
res.WriteHeader(http.StatusNotModified)
return
}
}
head := res.Header()
if strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
acceptEncoding := req.Header.Get("Accept-Encoding")
if strings.Contains(acceptEncoding, "br") {
if file, err := os.OpenFile(bFilePath, os.O_RDONLY, os.ModePerm); err == nil {
head.Set("Content-Encoding", "br")
head.Set("Etag", etagBr)
io.Copy(res, file)
file.Close()
return
}
} else if strings.Contains(acceptEncoding, "gzip") {
if file, err := os.OpenFile(zFilePath, os.O_RDONLY, os.ModePerm); err == nil {
head.Set("Content-Encoding", "gzip")
head.Set("Etag", etagGzip)

View File

@ -82,6 +82,13 @@ if (process.env.NODE_ENV === 'production') {
threshold: 0,
minRatio: 0.8
}));
config.plugins.push(new CompressionPlugin({
asset: "[path].br[query]",
algorithm: "brotliCompress",
test: /\.js$|\.json$|\.html$|\.svg|\.ico$/,
threshold: 0,
minRatio: 0.8
}));
} else {
config.devtool = '#inline-source-map';
}