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.
This commit is contained in:
Mike Coutermarsh 2019-12-23 09:22:38 -08:00 committed by Ben Halpern
parent ca71cb2c34
commit 3128c17fab
4 changed files with 13 additions and 5 deletions

View file

@ -132,7 +132,9 @@
</div>
<div class="badge-images">
<% @tag_badges.each do |badge| %>
<img src="<%= badge.badge_image_url %>" style="transform: rotate(<%= rand(-6..6) %>deg);" />
<%# Use a seed in Random so we don't break cache on every render %>
<% not_so_rand = Random.new(badge.id + @article.id) %>
<img src="<%= badge.badge_image_url %>" style="transform: rotate(<%= not_so_rand.rand(-6..6) %>deg);" />
<% end %>
<img src="https://thepracticaldev.s3.amazonaws.com/i/bh2wmpcltaybu1xsnico.png" />
</div>

View file

@ -125,7 +125,9 @@
</div>
<div class="badge-images">
<% @tag_badges.each do |badge| %>
<img src="<%= badge.badge_image_url %>" style="transform: rotate(<%= rand(-6..6) %>deg);" />
<%# Use a seed in Random so we don't break cache on every render %>
<% not_so_rand = Random.new(badge.id + @comment.id) %>
<img src="<%= badge.badge_image_url %>" style="transform: rotate(<%= not_so_rand.rand(-6..6) %>deg);" />
<% end %>
<img src="https://thepracticaldev.s3.amazonaws.com/i/bh2wmpcltaybu1xsnico.png" />
</div>

View file

@ -118,7 +118,9 @@
</div>
<div class="badge-images">
<% @tag_badges.each do |badge| %>
<img src="<%= badge.badge_image_url %>" style="transform: rotate(<%= rand(-6..6) %>deg);" />
<%# Use a seed in Random so we don't break cache on every render %>
<% not_so_rand = Random.new(badge.id + @user.id) %>
<img src="<%= badge.badge_image_url %>" style="transform: rotate(<%= not_so_rand.rand(-6..6) %>deg);" />
<% end %>
<img src="https://thepracticaldev.s3.amazonaws.com/i/bh2wmpcltaybu1xsnico.png" />
</div>

View file

@ -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