Skip to content

Commit cf61bf4

Browse files
committed
Configure Fast.ly
- Controls headers for Fastly and server caching - Adds FastlyService singleton used for purging api requests - Adds Surrogate-Key response header for website/event scoped cache purging - Adds ability to change caching setting to off/automatic/manual - Adds purging callbacks to website to purge automatically when updated or touched through pages or sponsors - Adds manual purge button on the Website edit page - Adds when_fresh call to add Last-Modified header based on website#purged_at - Adds cache-control for s3 uploads [Configure Fast.ly](https://miro.com/app/board/uXjVO6C1LxA=/?moveToWidget=3458764524414700733&cot=14)
1 parent 243ecd1 commit cf61bf4

19 files changed

+157
-7
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ gem 'coderay', '~> 1.0'
3939
gem 'country_select', '~> 1.3'
4040
gem 'draper', '~> 4.0'
4141
gem 'faker'
42+
gem 'fastly'
4243
gem 'groupdate'
4344
gem 'nokogiri'
4445
gem 'pundit'

Gemfile.lock

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ GEM
200200
faraday-net_http_persistent (1.2.0)
201201
faraday-patron (1.0.0)
202202
faraday-rack (1.0.0)
203+
fastly (3.0.2)
203204
ffi (1.15.4)
204205
foreman (0.87.2)
205206
formatador (0.3.0)
@@ -525,6 +526,7 @@ DEPENDENCIES
525526
draper (~> 4.0)
526527
factory_bot_rails
527528
faker
529+
fastly
528530
foreman
529531
groupdate
530532
growl

app/controllers/application_controller.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,20 @@ def program_tracks
198198
end
199199

200200
def set_cache_headers
201-
expires_in 1.hour, public: true
201+
return unless Rails.configuration.action_controller.perform_caching
202+
203+
server_cache_age =
204+
current_website.caching_off? ? 0 : ENV.fetch('CACHE_CONTROL_S_MAXAGE', 1.week)
205+
206+
expires_in(
207+
ENV.fetch('CACHE_CONTROL_MAX_AGE', 0).to_i,
208+
public: !current_website.caching_off?,
209+
's-maxage': server_cache_age.to_i
210+
)
211+
response.headers['Surrogate-Key'] = current_website.event.slug if FastlyService.service
212+
fresh_when(
213+
current_website,
214+
last_modified: current_website.purged_at || current_website.updated_at
215+
) unless current_website.caching_off?
202216
end
203217
end

app/controllers/staff/websites_controller.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ def update
2727
end
2828
end
2929

30+
def purge
31+
@website.manual_purge
32+
33+
flash[:success] = "Website was successfully purged."
34+
redirect_to edit_event_staff_website_path(current_event)
35+
end
36+
3037
private
3138

3239
def set_website
@@ -55,6 +62,7 @@ def website_params
5562
:facebook_url,
5663
:instagram_url,
5764
:head_content,
65+
:caching,
5866
footer_categories: [],
5967
navigation_links: [],
6068
session_format_configs_attributes: [

app/models/page.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Page < ApplicationRecord
44
'home' => { },
55
}
66

7-
belongs_to :website
7+
belongs_to :website, touch: :purged_at
88

99
scope :published, -> { where.not(published_body: nil).where(hide_page: false) }
1010
scope :in_footer, -> { published.where.not(footer_category: [nil, ""]) }

app/models/sponsor.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class Sponsor < ApplicationRecord
22
belongs_to :event
3+
has_one :website, through: :event, touch: :purged_at
34

45
has_one_attached :primary_logo
56
has_one_attached :footer_logo

app/models/website.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class Website < ApplicationRecord
2+
enum caching: { off: 'off', automatic: 'automatic', manual: 'manual' }, _prefix: true
3+
24
belongs_to :event
35
has_many :pages, dependent: :destroy
46
has_many :fonts, class_name: 'Website::Font', dependent: :destroy
@@ -19,11 +21,26 @@ class Website < ApplicationRecord
1921
has_one_attached :background
2022
has_one_attached :favicon
2123

24+
after_touch :purge_cache, if: :caching_automatic?
25+
around_update :purge_cache, if: :caching_automatic?
26+
2227
DEFAULT = 'default'.freeze
2328

2429
def self.domain_match(domain)
2530
where(arel_table[:domains].matches("%#{(domain)}"))
2631
end
32+
33+
def manual_purge
34+
purge_cache { save }
35+
end
36+
37+
private
38+
39+
def purge_cache
40+
self.purged_at = Time.current
41+
yield if block_given?
42+
FastlyService.service&.purge_by_key(event.slug)
43+
end
2744
end
2845

2946
# == Schema Information

app/policies/website_policy.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ def edit?
1818
def update?
1919
show?
2020
end
21+
22+
def purge?
23+
show?
24+
end
2125
end

app/views/staff/websites/_form.html.haml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
= ff.input :author
7373
= ff.input :description
7474
= image_input(ff, :image)
75+
76+
%legend.fieldset-legent Caching
77+
= f.input :caching, collection: Website.cachings, include_blank: false
7578
.row
7679
.col-sm-12
7780
= submit_tag("Save", class: "pull-right btn btn-success", type: "submit")
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
.row
22
.col-md-12
33
.page-header.clearfix
4-
.btn-navbar.pull-right
4+
.btn-navbar
55
%h1 Edit Website
6+
- if FastlyService.service
7+
.pull-right
8+
= button_to "Purge",
9+
purge_event_staff_website_path(current_event),
10+
class: 'btn btn-success',
11+
data: { confirm: "Are you sure you want to purge all website cached content in Fastly?" }
612

713
= render 'form', website: @website

0 commit comments

Comments
 (0)