[deploy] Update manual Fastly header assignments (#7119)

This commit is contained in:
Alex 2020-04-07 13:22:32 -04:00 committed by GitHub
parent eef227113c
commit 441cd4f7db
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 5 deletions

View file

@ -24,7 +24,10 @@ class ArticlesController < ApplicationController
end
set_surrogate_key_header "feed"
response.headers["Surrogate-Control"] = "max-age=600, stale-while-revalidate=30, stale-if-error=86400"
set_cache_control_headers(600,
stale_while_revalidate: 30,
stale_if_error: 86_400)
render layout: false
end

View file

@ -13,7 +13,9 @@ class SitemapsController < ApplicationController
end
@articles = Article.published.where("published_at > ? AND published_at < ? AND score > ?", date, date.end_of_month, 3).pluck(:path, :last_comment_at)
set_surrogate_controls(date)
response.headers["Surrogate-Control"] = "max-age=#{@max_age}, stale-while-revalidate=#{@stale_while_revalidate}, stale-if-error=#{@stale_if_error}"
set_cache_control_headers(@max_age,
stale_while_revalidate: @stale_while_revalidate,
stale_if_error: @stale_if_error)
render layout: false
end

View file

@ -60,7 +60,7 @@ class StoriesController < ApplicationController
def assign_hero_html
return if SiteConfig.campaign_hero_html_variant_name.blank?
@hero_area = HtmlVariant.relevant.select(:name, :html).
@hero_area = HtmlVariant.relevant.select(:name, :html).
find_by(group: "campaign", name: SiteConfig.campaign_hero_html_variant_name)
@hero_html = @hero_area&.html
end
@ -138,7 +138,9 @@ class StoriesController < ApplicationController
@stories = @stories.decorate
set_surrogate_key_header "articles-#{@tag}"
response.headers["Surrogate-Control"] = "max-age=600, stale-while-revalidate=30, stale-if-error=86400"
set_cache_control_headers(600,
stale_while_revalidate: 30,
stale_if_error: 86_400)
render template: "articles/tag_index"
end
@ -159,7 +161,9 @@ class StoriesController < ApplicationController
@featured_story = (featured_story || Article.new)&.decorate
@stories = ArticleDecorator.decorate_collection(@stories)
set_surrogate_key_header "main_app_home_page"
response.headers["Surrogate-Control"] = "max-age=600, stale-while-revalidate=30, stale-if-error=86400"
set_cache_control_headers(600,
stale_while_revalidate: 30,
stale_if_error: 86_400)
render template: "articles/index"
end

View file

@ -18,6 +18,33 @@ RSpec.describe "Articles", type: :request do
expect { get "/feed/#{tag.name}" }.to raise_error(ActiveRecord::RecordNotFound)
end
it "sets Fastly Cache-Control headers" do
create(:article, featured: true)
get "/feed"
expect(response.status).to eq(200)
expected_cache_control_headers = %w[public no-cache]
expect(response.headers["Cache-Control"].split(", ")).to match_array(expected_cache_control_headers)
end
it "sets Fastly Surrogate-Control headers" do
create(:article, featured: true)
get "/feed"
expect(response.status).to eq(200)
expected_surrogate_control_headers = %w[max-age=600 stale-while-revalidate=30 stale-if-error=86400]
expect(response.headers["Surrogate-Control"].split(", ")).to match_array(expected_surrogate_control_headers)
end
it "sets Fastly Surrogate-Key headers" do
create(:article, featured: true)
get "/feed"
expect(response.status).to eq(200)
expected_surrogate_key_headers = %w[feed]
expect(response.headers["Surrogate-Key"].split(", ")).to match_array(expected_surrogate_key_headers)
end
context "when :username param is not given" do
let!(:featured_article) { create(:article, featured: true) }
let!(:not_featured_article) { create(:article, featured: false) }

View file

@ -81,6 +81,14 @@ RSpec.describe "StoriesIndex", type: :request do
expect(response.body).to include(CGI.escapeHTML(listing.title))
end
it "sets Fastly Surrogate-Key headers" do
get "/"
expect(response.status).to eq(200)
expected_surrogate_key_headers = %w[main_app_home_page]
expect(response.headers["Surrogate-Key"].split(", ")).to match_array(expected_surrogate_key_headers)
end
context "with campaign hero" do
let_it_be_readonly(:hero_html) do
create(
@ -183,6 +191,30 @@ RSpec.describe "StoriesIndex", type: :request do
expect(response.body).to include(tag.name)
end
it "sets Fastly Cache-Control headers" do
get "/t/#{tag.name}"
expect(response.status).to eq(200)
expected_cache_control_headers = %w[public no-cache]
expect(response.headers["Cache-Control"].split(", ")).to match_array(expected_cache_control_headers)
end
it "sets Fastly Surrogate-Control headers" do
get "/t/#{tag.name}"
expect(response.status).to eq(200)
expected_surrogate_control_headers = %w[max-age=600 stale-while-revalidate=30 stale-if-error=86400]
expect(response.headers["Surrogate-Control"].split(", ")).to match_array(expected_surrogate_control_headers)
end
it "sets Fastly Surrogate-Key headers" do
get "/t/#{tag.name}"
expect(response.status).to eq(200)
expected_surrogate_key_headers = %W[articles-#{tag}]
expect(response.headers["Surrogate-Key"].split(", ")).to match_array(expected_surrogate_key_headers)
end
it "renders page with top/week etc." do
get "/t/#{tag.name}/top/week"
expect(response.body).to include(tag.name)