From 3128c17fab94d4aaa98a02114674315e39cb026a Mon Sep 17 00:00:00 2001 From: Mike Coutermarsh Date: Mon, 23 Dec 2019 09:22:38 -0800 Subject: [PATCH] Improve twitter card caching (#5206) [deploy] * update test so it fails from badge randomness * Better caching This removes the randomness from the views. Which means the cache keys won't get regenerated every request. The badges will still be in random positions, but it will change per user/article rather than every render. --- app/views/social_previews/article.html.erb | 4 +++- app/views/social_previews/comment.html.erb | 4 +++- app/views/social_previews/user.html.erb | 4 +++- spec/requests/social_previews_spec.rb | 6 ++++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/views/social_previews/article.html.erb b/app/views/social_previews/article.html.erb index 7e66edf1f..d1f48ea06 100644 --- a/app/views/social_previews/article.html.erb +++ b/app/views/social_previews/article.html.erb @@ -132,7 +132,9 @@
<% @tag_badges.each do |badge| %> - + <%# Use a seed in Random so we don't break cache on every render %> + <% not_so_rand = Random.new(badge.id + @article.id) %> + <% end %>
diff --git a/app/views/social_previews/comment.html.erb b/app/views/social_previews/comment.html.erb index 0460983ba..ff2ca7670 100644 --- a/app/views/social_previews/comment.html.erb +++ b/app/views/social_previews/comment.html.erb @@ -125,7 +125,9 @@
<% @tag_badges.each do |badge| %> - + <%# Use a seed in Random so we don't break cache on every render %> + <% not_so_rand = Random.new(badge.id + @comment.id) %> + <% end %>
diff --git a/app/views/social_previews/user.html.erb b/app/views/social_previews/user.html.erb index ad08f51d7..08d3cad34 100644 --- a/app/views/social_previews/user.html.erb +++ b/app/views/social_previews/user.html.erb @@ -118,7 +118,9 @@
<% @tag_badges.each do |badge| %> - + <%# Use a seed in Random so we don't break cache on every render %> + <% not_so_rand = Random.new(badge.id + @user.id) %> + <% end %>
diff --git a/spec/requests/social_previews_spec.rb b/spec/requests/social_previews_spec.rb index 210dd8326..b7164e94b 100644 --- a/spec/requests/social_previews_spec.rb +++ b/spec/requests/social_previews_spec.rb @@ -2,9 +2,9 @@ require "rails_helper" RSpec.describe "SocialPreviews", type: :request do let(:user) { create(:user) } - let(:tag) { create(:tag) } + let(:tag) { create(:tag, badge: create(:badge)) } let(:organization) { create(:organization) } - let(:article) { create(:article, user_id: user.id) } + let(:article) { create(:article, user_id: user.id, tags: tag.name) } let(:comment) { create(:comment, user_id: user.id, commentable_id: article.id) } let(:image_url) { "https://hcti.io/v1/image/6c52de9d-4d37-4008-80f8-67155589e1a1" } let(:listing) { create(:classified_listing, user_id: user.id, category: "cfp") } @@ -55,6 +55,8 @@ RSpec.describe "SocialPreviews", type: :request do end it "renders consistent HTML between requests" do + create(:badge_achievement, user: user) + # We use the HTML for caching. It needs to be deterministic (if data is unchanged, the HTML should be the same) get "/social_previews/user/#{user.id}" first_request_body = response.body