A platform for community discussion. Free, open, simple.
DEV: Skip srcset for onebox thumbnails (#22621)
Blake Erickson90f395a11895
diff --git a/lib/cooked_post_processor.rb b/lib/cooked_post_processor.rbindex 0d3a6d78367..910d5068d1e 100644--- a/lib/cooked_post_processor.rb+++ b/lib/cooked_post_processor.rb@@ -228,6 +228,7 @@ class CookedPostProcessordef optimize_image!(img, upload, cropped: false)w, h = img["width"].to_i, img["height"].to_i+ onebox = img.ancestors(".onebox, .onebox-body").first# note: optimize_urls cooks the src further after thisthumbnail = upload.thumbnail(w, h)@@ -236,21 +237,27 @@ class CookedPostProcessorsrcset = +""- each_responsive_ratio do |ratio|- resized_w = (w * ratio).to_i- resized_h = (h * ratio).to_i-- if !cropped && upload.width && resized_w > upload.width- cooked_url = UrlHelper.cook_url(upload.url, secure: @post.with_secure_uploads?)- srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0\z/, "")}x"- elsif t = upload.thumbnail(resized_w, resized_h)- cooked_url = UrlHelper.cook_url(t.url, secure: @post.with_secure_uploads?)- srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0\z/, "")}x"- end+ # Skip srcset for onebox images. Because onebox thumbnails by default+ # are fairly small the width/height of the smallest thumbnail is likely larger+ # than what the onebox thumbnail size will be displayed at, so we shouldn't+ # need to upscale for retina devices+ if !onebox+ each_responsive_ratio do |ratio|+ resized_w = (w * ratio).to_i+ resized_h = (h * ratio).to_i++ if !cropped && upload.width && resized_w > upload.width+ cooked_url = UrlHelper.cook_url(upload.url, secure: @post.with_secure_uploads?)+ srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0\z/, "")}x"+ elsif t = upload.thumbnail(resized_w, resized_h)+ cooked_url = UrlHelper.cook_url(t.url, secure: @post.with_secure_uploads?)+ srcset << ", #{cooked_url} #{ratio.to_s.sub(/\.0\z/, "")}x"+ end- img[- "srcset"- ] = "#{UrlHelper.cook_url(img["src"], secure: @post.with_secure_uploads?)}#{srcset}" if srcset.present?+ img[+ "srcset"+ ] = "#{UrlHelper.cook_url(img["src"], secure: @post.with_secure_uploads?)}#{srcset}" if srcset.present?+ endendelseimg["src"] = upload.urldiff --git a/spec/lib/cooked_post_processor_spec.rb b/spec/lib/cooked_post_processor_spec.rbindex b3ec4b6d151..b95882b2614 100644--- a/spec/lib/cooked_post_processor_spec.rb+++ b/spec/lib/cooked_post_processor_spec.rb@@ -1042,7 +1042,9 @@ RSpec.describe CookedPostProcessor dodoc = Nokogiri::HTML5.fragment(cpp.html)expect(doc.css(".lightbox-wrapper").size).to eq(0)- expect(doc.css("img").first["srcset"]).to_not eq(nil)+ expect(doc.css("img").first["srcset"]).to eq(nil)+ expect(doc.css("img").first["src"]).to include("optimized")+ expect(doc.css("img").first["src"]).to include("512x384")endend