mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
Merge pull request #602 from prymitive/fix-404-expires
fix(backend): don't set cache headers for 404s
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user