FIX: always serve new avatar for previous version

FIX: always serve new avatar for previous version

Previously we killed caching on old avatars cause we kept serving blank this meant we would front many more avatar requests after a version change

This change ensures all old avatars do not cause a flood of requests on the server

diff --git a/app/controllers/user_avatars_controller.rb b/app/controllers/user_avatars_controller.rb
index a7f32b1..79c3715 100644
--- a/app/controllers/user_avatars_controller.rb
+++ b/app/controllers/user_avatars_controller.rb
@@ -99,7 +99,11 @@ class UserAvatarsController < ApplicationController
     upload_id, version = params[:version].split("_")
 
     version = (version || OptimizedImage::VERSION).to_i
-    return render_blank if version != OptimizedImage::VERSION
+
+    # old versions simply get new avatar
+    if version > OptimizedImage::VERSION
+      return render_blank
+    end
 
     upload_id = upload_id.to_i
     return render_blank unless upload_id > 0
diff --git a/spec/requests/user_avatars_controller_spec.rb b/spec/requests/user_avatars_controller_spec.rb
index 7341cd6..030959c 100644
--- a/spec/requests/user_avatars_controller_spec.rb
+++ b/spec/requests/user_avatars_controller_spec.rb
@@ -102,6 +102,29 @@ describe UserAvatarsController do
       expect(response.headers["Last-Modified"]).to eq(optimized_image.upload.created_at.httpdate)
     end
 
+    it 'serves new version for old urls' do
+      user = Fabricate(:user)
+      SiteSetting.avatar_sizes = "45"
+
+      image = file_from_fixtures("cropped.png")
+      upload = UploadCreator.new(image, "image.png").create_for(user.id)
+
+      user.update_columns(uploaded_avatar_id: upload.id)
+
+      get "/user_avatar/default/#{user.username}/45/#{upload.id}_1.png"
+
+      expect(response.status).to eq(200)
+
+      image = response.body
+      optimized = upload.get_optimized_image(45, 45, {})
+
+      expect(optimized.filesize).to eq(body.length)
+
+      # clean up images
+      upload.destroy
+
+    end
+
     it 'serves a correct last modified for render blank' do
       freeze_time

GitHub sha: f947e3c6

2 Likes

FYI @davidtaylorhq this is why you were noticing old quotes having “broken” avatars. This corrects it and reduces load on the server at the same time.

2 Likes