web/satellite: use brotli instead of gzip

WHAT:
we'll use brotli instead of gzip from now on

WHY:
better compression

Change-Id: Ibeadd6bfc783e9c15cf3f62f719af692071a7721
This commit is contained in:
VitaliiShpital 2020-12-15 21:06:26 +02:00 committed by Stefan Benten
parent 50dd9fb11a
commit f4bbd0f5df
2 changed files with 12 additions and 11 deletions

View File

@ -207,7 +207,7 @@ func NewServer(logger *zap.Logger, config Config, service *console.Service, mail
router.HandleFunc("/password-recovery/", server.passwordRecoveryHandler)
router.HandleFunc("/cancel-password-recovery/", server.cancelPasswordRecoveryHandler)
router.HandleFunc("/usage-report", server.bucketUsageReportHandler)
router.PathPrefix("/static/").Handler(server.gzipMiddleware(http.StripPrefix("/static", fs)))
router.PathPrefix("/static/").Handler(server.brotliMiddleware(http.StripPrefix("/static", fs)))
router.PathPrefix("/").Handler(http.HandlerFunc(server.appHandler))
}
@ -792,19 +792,19 @@ func (server *Server) seoHandler(w http.ResponseWriter, req *http.Request) {
}
}
// gzipMiddleware is used to gzip static content to minify resources if browser support such decoding.
func (server *Server) gzipMiddleware(fn http.Handler) http.Handler {
// brotliMiddleware is used to compress static content using brotli to minify resources if browser support such decoding.
func (server *Server) brotliMiddleware(fn http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age=31536000")
w.Header().Set("X-Content-Type-Options", "nosniff")
isGzipSupported := strings.Contains(r.Header.Get("Accept-Encoding"), "gzip")
if !isGzipSupported {
isBrotliSupported := strings.Contains(r.Header.Get("Accept-Encoding"), "br")
if !isBrotliSupported {
fn.ServeHTTP(w, r)
return
}
info, err := os.Stat(server.config.StaticDir + strings.TrimPrefix(r.URL.Path, "/static") + ".gz")
info, err := os.Stat(server.config.StaticDir + strings.TrimPrefix(r.URL.Path, "/static") + ".br")
if err != nil {
fn.ServeHTTP(w, r)
return
@ -812,13 +812,13 @@ func (server *Server) gzipMiddleware(fn http.Handler) http.Handler {
extension := filepath.Ext(info.Name()[:len(info.Name())-3])
w.Header().Set(contentType, mime.TypeByExtension(extension))
w.Header().Set("Content-Encoding", "gzip")
w.Header().Set("Content-Encoding", "br")
newRequest := new(http.Request)
*newRequest = *r
newRequest.URL = new(url.URL)
*newRequest.URL = *r.URL
newRequest.URL.Path += ".gz"
newRequest.URL.Path += ".br"
fn.ServeHTTP(w, newRequest)
})

View File

@ -5,7 +5,7 @@ const path = require('path');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
const StyleLintPlugin = require('stylelint-webpack-plugin');
const WorkerPlugin = require('worker-plugin');
const productionGzipExtensions = ['js', 'css', 'ttf'];
const productionBrotliExtensions = ['js', 'css', 'ttf'];
module.exports = {
publicPath: "/static/dist",
@ -14,8 +14,9 @@ module.exports = {
configureWebpack: {
plugins: [
new CompressionWebpackPlugin({
algorithm: 'gzip',
test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
algorithm: 'brotliCompress',
filename: '[path][name].br',
test: new RegExp('\\.(' + productionBrotliExtensions.join('|') + ')$'),
threshold: 1024,
minRatio: 0.8
}),