-
Notifications
You must be signed in to change notification settings - Fork 7.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to cache static files? #1222
Comments
The status code But you are wrong. This is enough to enable HTTP Conditional Get: r := gin.Default()
r.Static("/assets", "./assets") A file in |
@lutzhorn This is my code: r := gin.Default()
r.Static("/assets", "web/public/") But every refurbish browser,the css,js file will return 200 ( NOT from disk cache.) |
+1 |
See the following example for gin Handler: // ViewHandler support dist handler from UI
func ViewHandler() gin.HandlerFunc {
fileServer := http.FileServer(dist.HTTP)
data := []byte(time.Now().String())
etag := fmt.Sprintf("%x", md5.Sum(data))
return func(c *gin.Context) {
c.Header("Cache-Control", "public, max-age=31536000")
c.Header("ETag", etag)
if match := c.GetHeader("If-None-Match"); match != "" {
if strings.Contains(match, etag) {
c.Status(http.StatusNotModified)
return
}
}
fileServer.ServeHTTP(c.Writer, c.Request)
}
} |
@appleboy's example is slightly inconsistent with HTTP standard.
MD5 sum is a weak validator:
Therefore, the ETag should be |
Hello, |
Every static file http state code is 200
how to cache static files?
change satat code 200 to 304
The text was updated successfully, but these errors were encountered: