Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use page version's updated_at timestamp as cache key #2862

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Use last-modified-at in PagesController
mamhoff committed May 15, 2024
commit 6787e9683506931dcf379309e910355d6ab1efec
2 changes: 1 addition & 1 deletion app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
@@ -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: [email protected],
template: "pages/show"
)
5 changes: 2 additions & 3 deletions app/models/alchemy/page/page_natures.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 7 additions & 9 deletions spec/requests/alchemy/page_request_caching_spec.rb
Original file line number Diff line number Diff line change
@@ -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
@@ -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