Merge pull request #602 from prymitive/fix-404-expires

fix(backend): don't set cache headers for 404s
This commit is contained in:
Łukasz Mierzwa
2019-04-09 15:49:26 +01:00
committed by GitHub
2 changed files with 18 additions and 3 deletions

View File

@@ -93,9 +93,12 @@ func serveFileOr404(path string, contentType string, c *gin.Context) {
func staticHeaders(prefix string) gin.HandlerFunc {
return func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, prefix) {
c.Header("Cache-Control", "public, max-age=31536000")
expiresTime := time.Now().AddDate(0, 0, 365).Format(http.TimeFormat)
c.Header("Expires", expiresTime)
c.Next()
if c.IsAborted() {
c.Header("Cache-Control", "public, max-age=31536000")
expiresTime := time.Now().AddDate(0, 0, 365).Format(http.TimeFormat)
c.Header("Expires", expiresTime)
}
}
}
}

View File

@@ -80,3 +80,15 @@ func TestCustomizationAssets(t *testing.T) {
}
}
}
func TestStaticExpires404(t *testing.T) {
mockConfig()
r := ginTestEngine()
req := httptest.NewRequest("GET", "/static/foobar.js", nil)
resp := httptest.NewRecorder()
r.ServeHTTP(resp, req)
if resp.Result().Header.Get("Expires") != "" {
t.Errorf("Got Expires: '%s' header on a 404 /static/ response", resp.Result().Header.Get("Expires"))
}
}