Skip to content

Commit

Permalink
Use last-modified-at in PagesController
Browse files Browse the repository at this point in the history
  • Loading branch information
mamhoff committed May 15, 2024
1 parent 74dfcab commit 6787e96
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def page_etag
def render_fresh_page?
must_not_cache? || stale?(
etag: page_etag,
last_modified: @page.published_at,
last_modified: @page.last_modified_at,
public: !@page.restricted,
template: "pages/show"
)
Expand Down
5 changes: 2 additions & 3 deletions app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,18 @@ def layout_partial_name
page_layout.parameterize.underscore
end

# Returns the string version of the +last_modified_at+ timestamp.
# Returns the version string that's taken for Rails' recycable cache key.
#
def cache_version
last_modified_at&.to_s
end

# Returns the timestamp that the page was last modified at, regardless of through.
# Returns the timestamp that the page was last modified at, regardless of through
# publishing or editing page, or through a change of related objects through ingredients.
# Respects the public version not changing if editing a preview.
#
# In preview mode, it will take the draft version's updated_at timestamp.
# In public mode, it will take the public version's updated_at timestamp.
# If no version is available, it will take the page's updated_at timestamp.
#
def last_modified_at
relevant_page_version = (Current.preview_page == self) ? draft_version : public_version
Expand Down
16 changes: 7 additions & 9 deletions spec/requests/alchemy/page_request_caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
require "rails_helper"

RSpec.describe "Page request caching" do
let!(:page) { create(:alchemy_page, :public, published_at: published_at) }
let(:published_at) { nil }
let!(:page) { create(:alchemy_page, :public) }

context "when caching is disabled in app" do
before do
Expand Down Expand Up @@ -81,18 +80,17 @@
expect(response.headers).to have_key("ETag")
end

it "does not set last-modified header" do
get "/#{page.urlname}"
expect(response.headers).to_not have_key("Last-Modified")
end
context "and public version is present" do
let(:jan_first) { Time.new(2020, 1, 1) }

context "and published_at is set" do
let(:published_at) { DateTime.yesterday }
before do
allow_any_instance_of(Alchemy::Page).to receive(:last_modified_at) { jan_first }
end

it "sets last-modified header" do
get "/#{page.urlname}"
expect(response.headers).to have_key("Last-Modified")
expect(response.headers["Last-Modified"]).to eq(published_at.httpdate)
expect(response.headers["Last-Modified"]).to eq(jan_first.httpdate)
end
end
end
Expand Down

0 comments on commit 6787e96

Please sign in to comment.