Skip to content

Commit ead5493

Browse files
committed
Fix caching of social meta tags
Just use the url and the updated_at timestamp of the site to generate the cache key for the head elements. Whilst this will mean that we add cached head elements for pages that will be only viewed once such as the signature validation page the LRU nature of Memcached means they will be evicted first. Also if the @Petition variable is set then use it for setting the meta tags so that even if someone shares a thank you page or a signature validation page then the URL shared by Facebook will be the main petition url and not the url of the page they last visited.
1 parent 36382a8 commit ead5493

4 files changed

Lines changed: 25 additions & 16 deletions

File tree

app/helpers/cache_helper.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Keys
3131
delegate :last_signature_at, to: :template
3232
delegate :petition_page?, to: :template
3333
delegate :page_title, to: :template
34+
delegate :request, to: :template
3435

3536
def initialize(template)
3637
@template = template
@@ -68,6 +69,10 @@ def site_updated_at
6869
Site.updated_at
6970
end
7071

72+
def url
73+
request.original_url
74+
end
75+
7176
def for(keys)
7277
keys.map{ |key| [key, value_for(key)] }.uniq
7378
end

app/views/application/_social_meta.html.erb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@
22
<%= open_graph_tag 'site_name', :site_name %>
33
<%= open_graph_tag 'locale', 'en_GB' %>
44
<%= open_graph_tag 'image', 'os-social/opengraph-image.png' %>
5-
<% if petition_page? %>
6-
<%= open_graph_tag 'url', petition_url(@petition) %>
7-
<%= open_graph_tag 'type', 'article' %>
8-
<%= open_graph_tag 'title', :title, petition: @petition.action %>
9-
<%= open_graph_tag 'description', @petition.background %>
10-
<% elsif archived_petition_page? %>
5+
<% if archived_petition_page? %>
116
<%= open_graph_tag 'url', archived_petition_url(@petition) %>
127
<%= open_graph_tag 'type', 'article' %>
138
<%= open_graph_tag 'title', :archived_title, petition: @petition.title %>
149
<%= open_graph_tag 'description', @petition.description %>
10+
<% elsif defined?(@petition) %>
11+
<%= open_graph_tag 'url', petition_url(@petition) %>
12+
<%= open_graph_tag 'type', 'article' %>
13+
<%= open_graph_tag 'title', :title, petition: @petition.action %>
14+
<%= open_graph_tag 'description', @petition.background %>
1515
<% else %>
1616
<%= open_graph_tag 'url', request.original_url %>
1717
<%= open_graph_tag 'type', 'website' %>
1818
<%= open_graph_tag 'title', page_title %>
1919
<% end %>
20-
2120
<!-- Twitter -->
2221
<%= twitter_card_tag 'card', 'summary' %>
2322
<%= twitter_card_tag 'site', '@hocpetitions' %>
2423
<%= twitter_card_tag 'image', 'os-social/opengraph-image.png' %>
25-
<% if petition_page? %>
26-
<%= twitter_card_tag 'title', :title, petition: @petition.action %>
27-
<%= twitter_card_tag 'description', @petition.background %>
28-
<% elsif archived_petition_page? %>
24+
<% if archived_petition_page? %>
2925
<%= twitter_card_tag 'title', :title, petition: @petition.title %>
3026
<%= twitter_card_tag 'description', @petition.description %>
27+
<% elsif defined?(@petition) %>
28+
<%= twitter_card_tag 'title', :title, petition: @petition.action %>
29+
<%= twitter_card_tag 'description', @petition.background %>
3130
<% else %>
3231
<%= twitter_card_tag 'title', page_title %>
3332
<% end %>

config/fragments.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
head:
22
keys:
33
- :site_updated_at
4-
- :last_signature_at
5-
- :petition_page
6-
- :archived_petition_page
7-
- :page_title
8-
- :petition
4+
- :url
95
options:
106
expires_in: 300
117

spec/helpers/cache_helper_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@
156156
end
157157
end
158158

159+
describe "#url" do
160+
let(:request) { double(:request, original_url: "/petitions/123") }
161+
162+
it "delegates to the request's original_url method" do
163+
expect(helper).to receive(:request).and_return(request)
164+
expect(keys.url).to eq("/petitions/123")
165+
end
166+
end
167+
159168
describe "#method_missing" do
160169
it "returns an assigned variable in the template context" do
161170
assign('signature_count', 32)

0 commit comments

Comments
 (0)