diff --git a/app/models/alchemy/page/page_natures.rb b/app/models/alchemy/page/page_natures.rb index 728d2c54af..cf39fa3dcc 100644 --- a/app/models/alchemy/page/page_natures.rb +++ b/app/models/alchemy/page/page_natures.rb @@ -103,16 +103,11 @@ def layout_partial_name # Returns the version that's taken for Rails' recycable cache key. # - # Uses the +published_at+ value that's updated when the user publishes the page. - # - # If the page is the current preview it uses the +updated_at+ value as cache key. + # Always uses the +updated_at+ value. # def cache_version - if Current.preview_page == self - updated_at.to_s - else - published_at.to_s - end + relevant_page_version = (Current.preview_page == self) ? draft_version : public_version + (relevant_page_version&.updated_at || updated_at).to_s end # Returns true if the page cache control headers should be set. diff --git a/spec/models/alchemy/page_spec.rb b/spec/models/alchemy/page_spec.rb index 30e50c6c3f..7e1919a496 100644 --- a/spec/models/alchemy/page_spec.rb +++ b/spec/models/alchemy/page_spec.rb @@ -708,9 +708,12 @@ module Alchemy let(:last_week) { Time.current - 1.week } let(:page) do - build_stubbed(:alchemy_page, updated_at: now, published_at: last_week) + build_stubbed(:alchemy_page, public_version: public_version, draft_version: draft_version) end + let(:public_version) { build_stubbed(:alchemy_page_version, updated_at: last_week) } + let(:draft_version) { build_stubbed(:alchemy_page_version, updated_at: now) } + subject { page.cache_version } before do @@ -732,6 +735,26 @@ module Alchemy is_expected.to eq(last_week.to_s) end end + + context "if page has no public version" do + context "in preview mode" do + let(:page) { build_stubbed(:alchemy_page, public_version: nil, draft_version: draft_version, updated_at: now) } + let(:preview) { page } + + it "uses updated_at" do + is_expected.to eq(now.to_s) + end + end + + context "not in preview mode" do + let(:page) { build_stubbed(:alchemy_page, public_version: nil, draft_version: draft_version, updated_at: now) } + let(:preview) { nil } + + it "uses updated_at" do + is_expected.to eq(now.to_s) + end + end + end end describe "#public_version" do