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":
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
}

View File

@@ -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)
}

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -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 } }]