From fe909d6dc55502f396fdb33dcd3119b2ec9ab5b3 Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Fri, 21 Nov 2025 19:18:19 +0100 Subject: [PATCH] Disable stale while revalidate for own avatar Locally, having stale_while_revalidate works great, but in production when we are behind CloudFlare, this results in an old image being shown after you upload a new one See: https://app.fizzy.do/5986089/cards/2978 --- app/controllers/users/avatars_controller.rb | 10 +++++++++- test/controllers/users/avatars_controller_test.rb | 3 ++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/controllers/users/avatars_controller.rb b/app/controllers/users/avatars_controller.rb index 425c194b1..76b2825ed 100644 --- a/app/controllers/users/avatars_controller.rb +++ b/app/controllers/users/avatars_controller.rb @@ -7,7 +7,7 @@ class Users::AvatarsController < ApplicationController before_action :ensure_permission_to_administer_user, only: :destroy def show - if stale? @user, cache_control: { max_age: (30.minutes unless Current.user == @user), stale_while_revalidate: 1.week }.compact + if stale? @user, cache_control: cache_control render_avatar_or_initials end end @@ -26,6 +26,14 @@ class Users::AvatarsController < ApplicationController head :forbidden unless Current.user.can_change?(@user) end + def cache_control + if @user == Current.user + {} + else + { max_age: 30.minutes, stale_while_revalidate: 1.week } + end + end + def render_avatar_or_initials if @user.avatar.attached? send_blob_stream @user.avatar diff --git a/test/controllers/users/avatars_controller_test.rb b/test/controllers/users/avatars_controller_test.rb index 4fa45c2c9..f23044ef3 100644 --- a/test/controllers/users/avatars_controller_test.rb +++ b/test/controllers/users/avatars_controller_test.rb @@ -8,7 +8,8 @@ class Users::AvatarsControllerTest < ActionDispatch::IntegrationTest test "show self without caching" do get user_avatar_path(users(:david)) assert_match "image/svg+xml", @response.content_type - assert_nil @response.cache_control[:max_age] + assert @response.cache_control[:private] + assert_equal "0", @response.cache_control[:max_age] end test "show other with caching" do