Skip to content

Commit 2fe0dab

Browse files
silverwindwxiaoguangzeripath
authored
Add Cache-Control header to html and api responses, add no-transform (#20432) (#20459)
`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. Co-authored-by: wxiaoguang <[email protected]> Co-authored-by: Andrew Thornton <[email protected]>
1 parent e930d66 commit 2fe0dab

File tree

5 files changed

+19
-5
lines changed

5 files changed

+19
-5
lines changed

Diff for: modules/context/api.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
repo_model "code.gitea.io/gitea/models/repo"
1717
"code.gitea.io/gitea/modules/cache"
1818
"code.gitea.io/gitea/modules/git"
19+
"code.gitea.io/gitea/modules/httpcache"
1920
"code.gitea.io/gitea/modules/log"
2021
"code.gitea.io/gitea/modules/setting"
2122
"code.gitea.io/gitea/modules/web/middleware"
@@ -268,6 +269,7 @@ func APIContexter() func(http.Handler) http.Handler {
268269
}
269270
}
270271

272+
httpcache.AddCacheControlToHeader(ctx.Resp.Header(), 0, "no-transform")
271273
ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
272274

273275
ctx.Data["Context"] = &ctx

Diff for: modules/context/context.go

+2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"code.gitea.io/gitea/modules/base"
2929
mc "code.gitea.io/gitea/modules/cache"
3030
"code.gitea.io/gitea/modules/git"
31+
"code.gitea.io/gitea/modules/httpcache"
3132
"code.gitea.io/gitea/modules/json"
3233
"code.gitea.io/gitea/modules/log"
3334
"code.gitea.io/gitea/modules/setting"
@@ -767,6 +768,7 @@ func Contexter() func(next http.Handler) http.Handler {
767768
}
768769
}
769770

771+
httpcache.AddCacheControlToHeader(ctx.Resp.Header(), 0, "no-transform")
770772
ctx.Resp.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
771773

772774
ctx.Data["CsrfToken"] = ctx.csrf.GetToken()

Diff for: modules/httpcache/httpcache.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,23 @@ import (
1717
)
1818

1919
// AddCacheControlToHeader adds suitable cache-control headers to response
20-
func AddCacheControlToHeader(h http.Header, d time.Duration) {
20+
func AddCacheControlToHeader(h http.Header, maxAge time.Duration, additionalDirectives ...string) {
21+
directives := make([]string, 0, 2+len(additionalDirectives))
22+
2123
if setting.IsProd {
22-
h.Set("Cache-Control", "private, max-age="+strconv.Itoa(int(d.Seconds())))
24+
if maxAge == 0 {
25+
directives = append(directives, "no-store")
26+
} else {
27+
directives = append(directives, "private", "max-age="+strconv.Itoa(int(maxAge.Seconds())))
28+
}
2329
} else {
24-
h.Set("Cache-Control", "no-store")
30+
directives = append(directives, "no-store")
31+
2532
// to remind users they are using non-prod setting.
26-
// some users may be confused by "Cache-Control: no-store" in their setup if they did wrong to `RUN_MODE` in `app.ini`.
2733
h.Add("X-Gitea-Debug", "RUN_MODE="+setting.RunMode)
28-
h.Add("X-Gitea-Debug", "CacheControl=no-store")
2934
}
35+
36+
h.Set("Cache-Control", strings.Join(append(directives, additionalDirectives...), ", "))
3037
}
3138

3239
// generateETag generates an ETag based on size, filename and file modification time

Diff for: routers/install/routes.go

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"net/http"
1010
"path"
1111

12+
"code.gitea.io/gitea/modules/httpcache"
1213
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/public"
1415
"code.gitea.io/gitea/modules/setting"
@@ -62,6 +63,7 @@ func installRecovery() func(next http.Handler) http.Handler {
6263
"SignedUserName": "",
6364
}
6465

66+
httpcache.AddCacheControlToHeader(w.Header(), 0, "no-transform")
6567
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
6668

6769
if !setting.IsProd {

Diff for: routers/web/base.go

+1
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ func Recovery() func(next http.Handler) http.Handler {
158158
store["SignedUserName"] = ""
159159
}
160160

161+
httpcache.AddCacheControlToHeader(w.Header(), 0, "no-transform")
161162
w.Header().Set(`X-Frame-Options`, setting.CORSConfig.XFrameOptions)
162163

163164
if !setting.IsProd {

0 commit comments

Comments
 (0)