-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_cache.go
More file actions
36 lines (31 loc) · 1023 Bytes
/
Copy pathclient_cache.go
File metadata and controls
36 lines (31 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package middlewares
import (
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func ClientCacheMiddleware(isDev bool) gin.HandlerFunc {
return func(c *gin.Context) {
if isDev {
c.Writer.Header().Set("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0")
c.Writer.Header().Set("Pragma", "no-cache")
c.Writer.Header().Set("Expires", "0")
} else {
if strings.HasPrefix(c.Request.URL.Path, "/assets") ||
strings.HasPrefix(c.Request.URL.Path, "/js") ||
strings.HasPrefix(c.Request.URL.Path, "/style") {
// 31536000 = 1 year
c.Writer.Header().Set("Cache-Control", "public, max-age=31536000")
} else if c.Request.Method == "GET" || c.Request.Method == "HEAD" {
c.Writer.Header().Set("Cache-Control", "no-cache")
c.Writer.Header().Set("Last-Modified", time.Now().UTC().Format(http.TimeFormat))
} else {
c.Writer.Header().Set("Cache-Control", "no-store")
}
}
// Cache-Control Vary header
c.Writer.Header().Set("Vary", "Origin")
c.Next()
}
}