From 6cfc5b0e0781252021e51bd711e5a7b729024a0b Mon Sep 17 00:00:00 2001 From: NI Date: Tue, 27 Aug 2019 15:43:38 +0800 Subject: [PATCH] Better HTTP caching --- application/controller/controller.go | 21 ++++++++-------- application/controller/failure.go | 2 +- application/controller/home.go | 2 +- application/controller/static.go | 37 +++++----------------------- webpack.config.js | 10 ++++---- 5 files changed, 23 insertions(+), 49 deletions(-) diff --git a/application/controller/controller.go b/application/controller/controller.go index 7e7a1ba..fa40045 100644 --- a/application/controller/controller.go +++ b/application/controller/controller.go @@ -76,20 +76,19 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case "/socket": err = serveController(h.socketCtl, w, r, clientLogger) - case "/robots.txt": + case "/index.html": fallthrough - case "/favicon.ico": - fallthrough - case "/README.md": - fallthrough - case "/LICENSE.md": - fallthrough - case "/DEPENDENCIES.md": - err = serveStaticData(r.URL.Path[1:], w, r, clientLogger) + case "/error.html": + err = ErrNotFound default: - if strings.HasPrefix(r.URL.Path, "/assets/") { - err = serveStaticData(r.URL.Path[8:], w, r, clientLogger) + if len(r.URL.Path) > 0 { + err = serveStaticData( + r.URL.Path[1:], + staticFileExt(r.URL.Path[1:]), + w, + r, + clientLogger) } else { err = ErrNotFound } diff --git a/application/controller/failure.go b/application/controller/failure.go index b9d1927..0a1231d 100644 --- a/application/controller/failure.go +++ b/application/controller/failure.go @@ -31,5 +31,5 @@ func serveFailure( ) error { w.WriteHeader(err.Code()) - return serveStaticPage("error.html", w, r, l) + return serveStaticPage("error.html", ".html", w, r, l) } diff --git a/application/controller/home.go b/application/controller/home.go index 6cd7bd0..00daebd 100644 --- a/application/controller/home.go +++ b/application/controller/home.go @@ -29,5 +29,5 @@ type home struct { } func (h home) Get(w http.ResponseWriter, r *http.Request, l log.Logger) error { - return serveStaticPage("index.html", w, r, l) + return serveStaticPage("index.html", ".html", w, r, l) } diff --git a/application/controller/static.go b/application/controller/static.go index 2e0b4ef..8087fea 100644 --- a/application/controller/static.go +++ b/application/controller/static.go @@ -48,42 +48,23 @@ func staticFileExt(fileName string) string { return strings.ToLower(fileName[extIdx:]) } -func serveStaticPage( +func serveStaticData( dataName string, + fileExt string, w http.ResponseWriter, r *http.Request, l log.Logger, ) error { - d, dFound := staticPages[dataName] - - if !dFound { + if fileExt == ".html" || fileExt == ".htm" { return ErrNotFound } - selectedData := d.data - - if clientSupportGZIP(r) && d.hasCompressed() { - selectedData = d.compressd - - w.Header().Add("Vary", "Accept-Encoding") - w.Header().Add("Content-Encoding", "gzip") - } - - mimeType := mime.TypeByExtension(staticFileExt(dataName)) - - if len(mimeType) > 0 { - w.Header().Add("Content-Type", mimeType) - } else { - w.Header().Add("Content-Type", "application/binary") - } - - _, wErr := w.Write(selectedData) - - return wErr + return serveStaticPage(dataName, fileExt, w, r, l) } -func serveStaticData( +func serveStaticPage( dataName string, + fileExt string, w http.ResponseWriter, r *http.Request, l log.Logger, @@ -92,12 +73,6 @@ func serveStaticData( return ErrControllerNotImplemented } - fileExt := staticFileExt(dataName) - - if fileExt == ".html" || fileExt == ".htm" { - return ErrNotFound - } - d, dFound := staticPages[dataName] if !dFound { diff --git a/webpack.config.js b/webpack.config.js index eb3e42d..8096e7c 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -132,9 +132,9 @@ module.exports = { devtool: process.env.NODE_ENV === "development" ? "inline-source-map" : "source-map", output: { - publicPath: "/assets/", + publicPath: "/", path: path.join(__dirname, ".tmp", "dist"), - filename: "[hash]d.js" + filename: "_[hash].js" }, resolve: { alias: { @@ -330,11 +330,11 @@ module.exports = { } }), new MiniCssExtractPlugin({ - filename: "[hash]__.css", - chunkFilename: "[chunkhash]__.css" + filename: "_[hash].css", + chunkFilename: "_[chunkhash].css" }), new OptimizeCssAssetsPlugin({ - assetNameRegExp: /\_\_\.css$/g, + assetNameRegExp: /^_.*\.css$/g, cssProcessor: require("cssnano"), cssProcessorPluginOptions: { preset: ["default", { discardComments: { removeAll: true } }]