Skip to content

Commit ff6a2ce

Browse files
committed
CP-990: add stale-while-revalidate headers to the supplier table
1 parent bb2572f commit ff6a2ce

4 files changed

Lines changed: 30 additions & 10 deletions

File tree

app/controllers/concerns/.keep

Whitespace-only changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
module Cacheable
4+
extend ActiveSupport::Concern
5+
6+
included do
7+
after_action :cache_control_header
8+
9+
protected
10+
11+
def cache_control_header
12+
# Allow clients to cache as we do not have log-in in this app, and cache at the CDN level
13+
# via s-maxage, allowing stale content to be served if there are upstream errors
14+
expires_in(
15+
5.minutes,
16+
public: true,
17+
"s-maxage": 5.minutes,
18+
stale_while_revalidate: 1.minute,
19+
stale_if_error: 1.day
20+
)
21+
end
22+
end
23+
end

app/controllers/csr_table/suppliers_controller.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class SuppliersController < ApplicationController
55
class SupplierNotFoundError < StandardError; end
66

77
include SwiftypeMeta
8+
include Cacheable
89

910
rescue_from SupplierNotFoundError, with: :not_found
1011
rescue_from Contentful::GraphqlAdapter::QueryError, with: :internal_server_error
@@ -13,7 +14,6 @@ class SupplierNotFoundError < StandardError; end
1314
before_action :set_suppliers, :set_unranked_supplier, :set_next_quarter_release, only: :index
1415
before_action :set_quarter_date, only: %i[index show]
1516
before_action :set_page_meta_tags, :set_swiftype_meta_tags
16-
after_action :cache_control_header
1717

1818
attr_accessor :supplier, :unranked_supplier, :quarter_date, :next_quarter_release
1919

@@ -126,14 +126,5 @@ def custom_data_layer_properties
126126
}
127127
end
128128
end
129-
130-
def cache_control_header
131-
expires_in(
132-
5.minutes,
133-
public: true,
134-
"s-maxage": 5.minutes,
135-
must_revalidate: true
136-
)
137-
end
138129
end
139130
end

spec/requests/csr_table/suppliers_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@
3636
it "contains the correct canonical url" do
3737
expect(response.body).to include("<link href='https://www.citizensadvice.org.uk#{CSR_APP_PATH}' rel='canonical'>")
3838
end
39+
40+
it "returns headers to allow the CDN to cache it" do
41+
expect(response.headers["Cache-Control"]).to eq(
42+
"max-age=300, public, stale-while-revalidate=60, stale-if-error=86400, s-maxage=300"
43+
)
44+
end
3945
end
4046

4147
context "when visiting the supplier index" do

0 commit comments

Comments
 (0)