Better HTTP caching

This commit is contained in:
NI
2019-08-27 15:43:38 +08:00
parent 526fe55d91
commit 6cfc5b0e07
5 changed files with 23 additions and 49 deletions

View File

@@ -76,20 +76,19 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
case "/socket": case "/socket":
err = serveController(h.socketCtl, w, r, clientLogger) err = serveController(h.socketCtl, w, r, clientLogger)
case "/robots.txt": case "/index.html":
fallthrough fallthrough
case "/favicon.ico": case "/error.html":
fallthrough err = ErrNotFound
case "/README.md":
fallthrough
case "/LICENSE.md":
fallthrough
case "/DEPENDENCIES.md":
err = serveStaticData(r.URL.Path[1:], w, r, clientLogger)
default: default:
if strings.HasPrefix(r.URL.Path, "/assets/") { if len(r.URL.Path) > 0 {
err = serveStaticData(r.URL.Path[8:], w, r, clientLogger) err = serveStaticData(
r.URL.Path[1:],
staticFileExt(r.URL.Path[1:]),
w,
r,
clientLogger)
} else { } else {
err = ErrNotFound err = ErrNotFound
} }

View File

@@ -31,5 +31,5 @@ func serveFailure(
) error { ) error {
w.WriteHeader(err.Code()) w.WriteHeader(err.Code())
return serveStaticPage("error.html", w, r, l) return serveStaticPage("error.html", ".html", w, r, l)
} }

View File

@@ -29,5 +29,5 @@ type home struct {
} }
func (h home) Get(w http.ResponseWriter, r *http.Request, l log.Logger) error { 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)
} }

View File

@@ -48,42 +48,23 @@ func staticFileExt(fileName string) string {
return strings.ToLower(fileName[extIdx:]) return strings.ToLower(fileName[extIdx:])
} }
func serveStaticPage( func serveStaticData(
dataName string, dataName string,
fileExt string,
w http.ResponseWriter, w http.ResponseWriter,
r *http.Request, r *http.Request,
l log.Logger, l log.Logger,
) error { ) error {
d, dFound := staticPages[dataName] if fileExt == ".html" || fileExt == ".htm" {
if !dFound {
return ErrNotFound return ErrNotFound
} }
selectedData := d.data return serveStaticPage(dataName, fileExt, w, r, l)
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)) func serveStaticPage(
if len(mimeType) > 0 {
w.Header().Add("Content-Type", mimeType)
} else {
w.Header().Add("Content-Type", "application/binary")
}
_, wErr := w.Write(selectedData)
return wErr
}
func serveStaticData(
dataName string, dataName string,
fileExt string,
w http.ResponseWriter, w http.ResponseWriter,
r *http.Request, r *http.Request,
l log.Logger, l log.Logger,
@@ -92,12 +73,6 @@ func serveStaticData(
return ErrControllerNotImplemented return ErrControllerNotImplemented
} }
fileExt := staticFileExt(dataName)
if fileExt == ".html" || fileExt == ".htm" {
return ErrNotFound
}
d, dFound := staticPages[dataName] d, dFound := staticPages[dataName]
if !dFound { if !dFound {

View File

@@ -132,9 +132,9 @@ module.exports = {
devtool: devtool:
process.env.NODE_ENV === "development" ? "inline-source-map" : "source-map", process.env.NODE_ENV === "development" ? "inline-source-map" : "source-map",
output: { output: {
publicPath: "/assets/", publicPath: "/",
path: path.join(__dirname, ".tmp", "dist"), path: path.join(__dirname, ".tmp", "dist"),
filename: "[hash]d.js" filename: "_[hash].js"
}, },
resolve: { resolve: {
alias: { alias: {
@@ -330,11 +330,11 @@ module.exports = {
} }
}), }),
new MiniCssExtractPlugin({ new MiniCssExtractPlugin({
filename: "[hash]__.css", filename: "_[hash].css",
chunkFilename: "[chunkhash]__.css" chunkFilename: "_[chunkhash].css"
}), }),
new OptimizeCssAssetsPlugin({ new OptimizeCssAssetsPlugin({
assetNameRegExp: /\_\_\.css$/g, assetNameRegExp: /^_.*\.css$/g,
cssProcessor: require("cssnano"), cssProcessor: require("cssnano"),
cssProcessorPluginOptions: { cssProcessorPluginOptions: {
preset: ["default", { discardComments: { removeAll: true } }] preset: ["default", { discardComments: { removeAll: true } }]