Skip to content

Commit

Permalink
Output CloudFront-friendly headers for diffs (#1098)
Browse files Browse the repository at this point in the history
This is part of web-monitoring#168 — the goal here is to make our diff cache less important so we can shrink it or even remove it (in favor of just having CloudFront do the job).

CloudFront needs a `Date` header to go with the `Last-Modified` and `ETag` headers from the `stale?` method, and works better still if we give it an actual cache age. This adds the `expires_in` helper which sets all the headers. I've also taken the strategy of setting a *relatively* short cache time but a much longer revalidation window when a stale response can be used, to balance concerns about updating the diff algorithm and effective long-term caching.
  • Loading branch information
Mr0grog authored Mar 30, 2023
1 parent 4c1c0f6 commit e85d7f6
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions app/controllers/api/v0/diff_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ class Api::V0::DiffController < Api::V0::ApiController
def show
ensure_diffable

# Some front-end caches, like CloudFront, need the headers from *both*
# expires_in and stale? to cache most effectively.
unless Rails.env.development?
cache_time = params[:diff_version] ? 100.years : 1.day
expires_in(cache_time, public: true, stale_while_revalidate: 7.days, stale_if_error: 7.days)
end
if stale?(etag: diff_etag, last_modified: Differ.cache_date, public: true)
render json: {
links: {
Expand Down

0 comments on commit e85d7f6

Please sign in to comment.