This commit is contained in:
@@ -1,18 +1,38 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"vctp/version"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"vctp/version"
|
||||
)
|
||||
|
||||
// CacheMiddleware sets the Cache-Control header based on the version.
|
||||
func CacheMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if version.Value == "dev" {
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
|
||||
w.Header().Set("Pragma", "no-cache")
|
||||
w.Header().Set("Expires", "0")
|
||||
} else {
|
||||
w.Header().Set("Cache-Control", "public, max-age=31536000")
|
||||
cacheControl := "public, max-age=31536000"
|
||||
if isVersionedAssetRequest(r) {
|
||||
cacheControl += ", immutable"
|
||||
}
|
||||
w.Header().Set("Cache-Control", cacheControl)
|
||||
w.Header().Set("Expires", time.Now().UTC().Add(365*24*time.Hour).Format(http.TimeFormat))
|
||||
}
|
||||
w.Header().Set("Vary", "Accept-Encoding")
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func isVersionedAssetRequest(r *http.Request) bool {
|
||||
if r == nil {
|
||||
return false
|
||||
}
|
||||
if r.URL.Query().Get("v") != "" {
|
||||
return true
|
||||
}
|
||||
return strings.Contains(r.URL.Path, "@")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user