Skip to content

Commit

Permalink
Do not have a cache version if there's no version present
Browse files Browse the repository at this point in the history
We should not cache empty responses.
  • Loading branch information
mamhoff committed May 14, 2024
1 parent 7383a02 commit 74dfcab
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
4 changes: 2 additions & 2 deletions app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def layout_partial_name
# Returns the string version of the +last_modified_at+ timestamp.
#
def cache_version
last_modified_at.to_s
last_modified_at&.to_s
end

# Returns the timestamp that the page was last modified at, regardless of through.
Expand All @@ -117,7 +117,7 @@ def cache_version
#
def last_modified_at
relevant_page_version = (Current.preview_page == self) ? draft_version : public_version
relevant_page_version&.updated_at || updated_at
relevant_page_version&.updated_at
end

# Returns true if the page cache control headers should be set.
Expand Down
32 changes: 22 additions & 10 deletions spec/models/alchemy/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -706,18 +706,30 @@ module Alchemy
describe "#cache_version" do
let(:page) { build(:alchemy_page) }

before do
allow(page).to receive(:last_modified_at).and_return(1.day.ago)
end

around do |example|
travel_to(Time.parse("2019-01-01 12:00:00 UTC")) do
example.run
end
end

it "returns a cache version string" do
expect(page.cache_version).to eq("2018-12-31 12:00:00 UTC")
context "last modified is a time object" do
before do
allow(page).to receive(:last_modified_at).and_return(1.day.ago)
end

it "returns a cache version string" do
expect(page.cache_version).to eq("2018-12-31 12:00:00 UTC")
end
end

context "last modified at is nil" do
before do
allow(page).to receive(:last_modified_at).and_return(nil)
end

it "returns a cache version string" do
expect(page.cache_version).to be(nil)
end
end
end

Expand Down Expand Up @@ -768,17 +780,17 @@ module Alchemy
context "if page has no draft version" do
let(:draft_version) { nil }

it "uses page's updated_at" do
is_expected.to be_within(1.second).of(yesterday)
it "is nil" do
is_expected.to be(nil)
end
end
end

context "not in preview mode" do
let(:preview) { nil }

it "uses page's updated_at" do
is_expected.to be_within(1.second).of(yesterday)
it "is nil" do
is_expected.to be(nil)
end
end
end
Expand Down

0 comments on commit 74dfcab

Please sign in to comment.