aboutsummaryrefslogtreecommitdiffstats
path: root/modules/httpcache
diff options
context:
space:
mode:
authorsilverwind <me@silverwind.io>2022-07-23 08:38:03 +0200
committerGitHub <noreply@github.com>2022-07-23 14:38:03 +0800
commit14178c56bb0fbc8b0b5820539e7681a7defeff47 (patch)
tree4da90c0d0761a1cf7a066738fc434b78ba02b33f /modules/httpcache
parentd9608c4e76401ffdd5958e1944ba0a17ded825b9 (diff)
downloadgitea-14178c56bb0fbc8b0b5820539e7681a7defeff47.tar.gz
gitea-14178c56bb0fbc8b0b5820539e7681a7defeff47.zip
Add Cache-Control header to html and api responses, add no-transform (#20432)
`no-transform` allegedly disables CloudFlare auto-minify and we did not set caching headers on html or api requests, which seems good to have regardless. Transformation is still allowed for asset requests. Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Andrew Thornton <art27@cantab.net>
Diffstat (limited to 'modules/httpcache')
-rw-r--r--modules/httpcache/httpcache.go17
1 files changed, 12 insertions, 5 deletions
diff --git a/modules/httpcache/httpcache.go b/modules/httpcache/httpcache.go
index 5797e981cf..750233d4a7 100644
--- a/modules/httpcache/httpcache.go
+++ b/modules/httpcache/httpcache.go
@@ -17,16 +17,23 @@ import (
)
// AddCacheControlToHeader adds suitable cache-control headers to response
-func AddCacheControlToHeader(h http.Header, d time.Duration) {
+func AddCacheControlToHeader(h http.Header, maxAge time.Duration, additionalDirectives ...string) {
+ directives := make([]string, 0, 2+len(additionalDirectives))
+
if setting.IsProd {
- h.Set("Cache-Control", "private, max-age="+strconv.Itoa(int(d.Seconds())))
+ if maxAge == 0 {
+ directives = append(directives, "no-store")
+ } else {
+ directives = append(directives, "private", "max-age="+strconv.Itoa(int(maxAge.Seconds())))
+ }
} else {
- h.Set("Cache-Control", "no-store")
+ directives = append(directives, "no-store")
+
// to remind users they are using non-prod setting.
- // some users may be confused by "Cache-Control: no-store" in their setup if they did wrong to `RUN_MODE` in `app.ini`.
h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
- h.Add("X-Gitea-Debug", "CacheControl=no-store")
}
+
+ h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", "))
}
// generateETag generates an ETag based on size, filename and file modification time