gorilla / mux golang缓存静态文件

时间:2018-02-20 00:46:09

标签: go gorilla

我有一个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)
}

编辑:这是解决@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)
    })
}

1 个答案:

答案 0 :(得分:3)

要告诉浏览器缓存您的文件,您需要告诉它多长时间,如果不是用户永远不会看到该文件的更新版本。

要执行此操作,只需在处理函数中设置Cache-Control标头:

w.Header().Set("Cache-Control", "max-age=3600")

使用Etag标头让浏览器知道何时有新版本的文件也是一个好主意。通过这种方式,您可以将文件缓存很长时间,并在用户可用时立即将新内容提供给用户:

etag := "some unique value"
w.Header().Set("Etag", etag)

每次文件更改时,您都需要使用不同的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)

您可以阅读Cache-ControlEtag标题的mozilla文档。

如果您想避免为静态文件编写自己的处理函数,而是继续使用http.FileServer,则需要将其包装在处理程序中,该处理程序在编写响应之前设置标头。 Here是关于包装Web处理程序的博客文章。

相关问题