From 53fb93b232208de7144e13990bb2ce0cb5467899 Mon Sep 17 00:00:00 2001 From: silverwind Date: Mon, 6 Sep 2021 18:37:22 +0200 Subject: [PATCH 1/2] 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 https://github.com/go-gitea/gitea/issues/16964. --- routers/web/user/avatar.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/routers/web/user/avatar.go b/routers/web/user/avatar.go index 8a5a36e070a9..baa73f890418 100644 --- a/routers/web/user/avatar.go +++ b/routers/web/user/avatar.go @@ -13,10 +13,16 @@ import ( "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" ) +func doRedirect(ctx *context.Context, location string) { + ctx.Resp.Header().Set("Cache-Control", httpcache.GetCacheControl()) + ctx.Redirect(location) +} + // Avatar redirect browser to user avatar of requested size func Avatar(ctx *context.Context) { userName := ctx.Params(":username") @@ -43,7 +49,7 @@ func Avatar(ctx *context.Context) { user = models.NewGhostUser() } - ctx.Redirect(user.RealSizedAvatarLink(size)) + doRedirect(ctx, user.RealSizedAvatarLink(size)) } // AvatarByEmailHash redirects the browser to the appropriate Avatar link @@ -63,7 +69,7 @@ func AvatarByEmailHash(ctx *context.Context) { return } if len(email) == 0 { - ctx.Redirect(models.DefaultAvatarLink()) + doRedirect(ctx, models.DefaultAvatarLink()) return } size := ctx.FormInt("size") @@ -94,5 +100,5 @@ func AvatarByEmailHash(ctx *context.Context) { } } - ctx.Redirect(models.MakeFinalAvatarURL(avatarURL, size)) + doRedirect(ctx, models.MakeFinalAvatarURL(avatarURL, size)) } From 86ef36e002c0c07bc53c334101ed58b5b997066d Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 6 Sep 2021 21:33:14 +0100 Subject: [PATCH 2/2] Apply suggestions from code review --- routers/web/user/avatar.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/routers/web/user/avatar.go b/routers/web/user/avatar.go index baa73f890418..2df5c148f790 100644 --- a/routers/web/user/avatar.go +++ b/routers/web/user/avatar.go @@ -18,7 +18,7 @@ import ( "code.gitea.io/gitea/modules/setting" ) -func doRedirect(ctx *context.Context, location string) { +func cacheableRedirect(ctx *context.Context, location string) { ctx.Resp.Header().Set("Cache-Control", httpcache.GetCacheControl()) ctx.Redirect(location) } @@ -49,7 +49,7 @@ func Avatar(ctx *context.Context) { user = models.NewGhostUser() } - doRedirect(ctx, user.RealSizedAvatarLink(size)) + cacheableRedirect(ctx, user.RealSizedAvatarLink(size)) } // AvatarByEmailHash redirects the browser to the appropriate Avatar link @@ -69,7 +69,7 @@ func AvatarByEmailHash(ctx *context.Context) { return } if len(email) == 0 { - doRedirect(ctx, models.DefaultAvatarLink()) + cacheableRedirect(ctx, models.DefaultAvatarLink()) return } size := ctx.FormInt("size") @@ -100,5 +100,5 @@ func AvatarByEmailHash(ctx *context.Context) { } } - doRedirect(ctx, models.MakeFinalAvatarURL(avatarURL, size)) + cacheableRedirect(ctx, models.MakeFinalAvatarURL(avatarURL, size)) }