相关疑难解决方法(0)

大猩猩/ mux golang缓存静态文件

我有一个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 …
Run Code Online (Sandbox Code Playgroud)

go gorilla

2
推荐指数
1
解决办法
1315
查看次数

标签 统计

go ×1

gorilla ×1