我有一个Go Web应用程序,该应用程序提供静态HTML / JS / CSS文件以及一些API端点。我注意到我的HTML / JS / CSS没有被缓存在浏览器中。例如,每次我重新加载页面时,它们都会被完全重新下载。
这是我需要设置的服务器端配置更改吗?如何使用Go和Gorilla Mux完成此操作?
我正在使用Google App Engine,因此Nginx是不可能的。
这是我的main.go代码:
package main
import (
"associations"
"html/template"
"net/http"
"log"
"io/ioutil"
"github.com/gorilla/mux"
"github.com/rs/cors"
"google.golang.org/appengine"
"google.golang.org/appengine/mail"
)
var index = template.Must(template.ParseFiles(
"dist/index.html",
))
func init() {
r := mux.NewRouter()
r.HandleFunc("/", homeHandler)
r.HandleFunc("/api/{tenant}/certificates", associations.GetCertificate).Methods("GET")
r.HandleFunc("/api/{tenant}/requests", associations.PostRequest).Methods("POST")
// handle static files
r.PathPrefix("/static/").Handler(
http.StripPrefix("/static/", http.FileServer(http.Dir("dist/static/"))))
r.NotFoundHandler = http.HandlerFunc(homeHandler) // work around for SPA serving index.html
handler := cors.Default().Handler(r)
http.Handle("/", handler)
}
Run Code Online (Sandbox Code Playgroud)
编辑:这是@Topo建议的解决方案:
// handle static files
r.PathPrefix("/static/").Handler(http.StripPrefix("/static/",
CacheControlWrapper(http.FileServer(http.Dir("dist/static/")))))
....
func CacheControlWrapper(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "max-age=2592000") // 30 days
h.ServeHTTP(w, r)
})
}
Run Code Online (Sandbox Code Playgroud)
要告诉浏览器缓存文件,您需要告诉它多长时间(如果没有的话),用户将永远看不到文件的较新版本。
为此,只需Cache-Control在处理函数中设置标头即可:
w.Header().Set("Cache-Control", "max-age=3600")
Run Code Online (Sandbox Code Playgroud)
使用Etag标头让浏览器知道文件的新版本也是一个好主意。这样,您可以缓存文件很长时间,并在新内容可用时仍将其提供给用户:
etag := "some unique value"
w.Header().Set("Etag", etag)
Run Code Online (Sandbox Code Playgroud)
每次文件更改时,您都需要使用不同的etag值。浏览器将存储它,并且仅当etag与存储的值不同时才重新加载文件。您可以为etag使用类似文件名+修改日期的名称:
var modTime time.Time
fi, err := fh.Stat()
if err != nil {
modTime = fi.ModTime()
} else {
modTime = time.Now()
}
etag := "\"" + file + modTime.String() + "\""
w.Header().Set("Etag", etag)
Run Code Online (Sandbox Code Playgroud)
您可以阅读有关Cache-Control和Etag附加程序的mozilla文档。
如果要避免为静态文件编写自己的处理程序函数,而是继续使用http.FileServer,则需要在编写响应之前将其包装在设置标头的处理程序中。这是有关包装Web处理程序的博客文章。
| 归档时间: |
|
| 查看次数: |
1315 次 |
| 最近记录: |