* Rename FastlyHeaders to CachingHeaders, conditionally experiment with removing no-cache We suspect that it is safe to remove `no-cache` entirely from the Cache-Control headers that are sent along to Fastly, but there is no great way to confirm this hypothesis. We're testing this by experimenting on one single article. We will conditionally default to the same headers that we send to Nginx, and send those headers to Fastly (by removing `no-cache` and adding in `max-age`, which should be ignored since Fastly also has Surrogate-Control headers added to it, which include a `max-age`, which Fastly should prioritize over any other `max-age`. Once we've confirmed that this doesn't cause issues on the Fastly side, we can remove this check and use the same Cache-Control headers for both Fastly and Nginx. * Add check for request.env[REQUEST_PATH].present?
53 lines
2.2 KiB
Ruby
53 lines
2.2 KiB
Ruby
module CachingHeaders
|
|
extend ActiveSupport::Concern
|
|
|
|
# The following is from the now deprecated fastly-rails gem
|
|
# https://github.com/fastly/fastly-rails/tree/master/lib/fastly-rails/action_controller
|
|
|
|
# Sets Cache-Control and Surrogate-Control HTTP headers
|
|
# Surrogate-Control is stripped at the cache, Cache-Control persists (in case of other caches in front of fastly)
|
|
# Defaults are:
|
|
# Cache-Control: 'public, no-cache'
|
|
# Surrogate-Control: 'max-age: 86400' - 1 day in seconds
|
|
# custom config example:
|
|
# {cache_control: 'public, no-cache, maxage=xyz', surrogate_control: 'max-age: 100'}
|
|
def set_cache_control_headers(
|
|
max_age = 1.day.to_i,
|
|
surrogate_control: nil, stale_while_revalidate: nil, stale_if_error: 26_400
|
|
)
|
|
|
|
# TODO: [@forem/delightful]: Once we've tested that removing `no-cache` from the Cache-Control headers
|
|
# does not adversely affect fastly, we should remove this specific check entirely.
|
|
article_requested = request.env["REQUEST_PATH"].present? &&
|
|
request.env["REQUEST_PATH"].include?("the-3-biggest-misconceptions-about-diversity-3hkm")
|
|
cache_control = if article_requested
|
|
"public, max-age=86400"
|
|
else
|
|
"public, no-cache"
|
|
end
|
|
|
|
request.session_options[:skip] = true # no cookies
|
|
response.headers["Cache-Control"] = cache_control
|
|
|
|
response.headers["Surrogate-Control"] = surrogate_control.presence || build_surrogate_control(
|
|
max_age, stale_while_revalidate: stale_while_revalidate, stale_if_error: stale_if_error
|
|
)
|
|
end
|
|
|
|
# Sets Surrogate-Key HTTP header with one or more keys strips session data
|
|
# from the request
|
|
def set_surrogate_key_header(*surrogate_keys)
|
|
request.session_options[:skip] = true # No Set-Cookie
|
|
response.headers["Surrogate-Key"] = surrogate_keys.join(" ")
|
|
end
|
|
|
|
private
|
|
|
|
def build_surrogate_control(max_age, stale_while_revalidate: nil, stale_if_error: 26_400)
|
|
surrogate_control = "max-age=#{max_age}"
|
|
|
|
surrogate_control += ", stale-while-revalidate=#{stale_while_revalidate}" if stale_while_revalidate
|
|
surrogate_control += ", stale-if-error=#{stale_if_error}" if stale_if_error
|
|
surrogate_control
|
|
end
|
|
end
|