Skip to content

Commit 9fa6bdb

Browse files
silverwindzeripath
andauthored
Add Cache-Control to avatar redirects (#16973)
* Add Cache-Control to avatar redirects This does seem to do the trick to make the Avatar redirects cachable in Chrome. In Firefox, it does not seem to work, thought and I found no way to suppress the requests to the original URLs, I even tried setting an Etag to no avail. Related discussion in #16964. Co-authored-by: zeripath <[email protected]>
1 parent cf6d398 commit 9fa6bdb

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

routers/web/user/avatar.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ import (
1313

1414
"code.gitea.io/gitea/models"
1515
"code.gitea.io/gitea/modules/context"
16+
"code.gitea.io/gitea/modules/httpcache"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/setting"
1819
)
1920

21+
func cacheableRedirect(ctx *context.Context, location string) {
22+
ctx.Resp.Header().Set("Cache-Control", httpcache.GetCacheControl())
23+
ctx.Redirect(location)
24+
}
25+
2026
// Avatar redirect browser to user avatar of requested size
2127
func Avatar(ctx *context.Context) {
2228
userName := ctx.Params(":username")
@@ -43,7 +49,7 @@ func Avatar(ctx *context.Context) {
4349
user = models.NewGhostUser()
4450
}
4551

46-
ctx.Redirect(user.RealSizedAvatarLink(size))
52+
cacheableRedirect(ctx, user.RealSizedAvatarLink(size))
4753
}
4854

4955
// AvatarByEmailHash redirects the browser to the appropriate Avatar link
@@ -63,7 +69,7 @@ func AvatarByEmailHash(ctx *context.Context) {
6369
return
6470
}
6571
if len(email) == 0 {
66-
ctx.Redirect(models.DefaultAvatarLink())
72+
cacheableRedirect(ctx, models.DefaultAvatarLink())
6773
return
6874
}
6975
size := ctx.FormInt("size")
@@ -94,5 +100,5 @@ func AvatarByEmailHash(ctx *context.Context) {
94100
}
95101
}
96102

97-
ctx.Redirect(models.MakeFinalAvatarURL(avatarURL, size))
103+
cacheableRedirect(ctx, models.MakeFinalAvatarURL(avatarURL, size))
98104
}

0 commit comments

Comments
 (0)