Skip to content

Commit

Permalink
Use cache_key_with_version in page_etag
Browse files Browse the repository at this point in the history
The pages `cache_key` was including the `published_at` date before we
changed it to just the `cache_version` (used by Rails to form the `cache_key`)

Closes #2363
  • Loading branch information
tvdeyen committed Sep 2, 2022
1 parent 30f61f1 commit a1aa227
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
5 changes: 3 additions & 2 deletions app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,13 @@ def signup_required?
#
# IMPORTANT:
#
# If your user does not have a +cache_key+ method (i.e. it's not an ActiveRecord model),
# If your user does not have a +cache_key_with_version+ method (i.e. it's not an ActiveRecord model),
# you have to ensure to implement it and return a unique identifier for that particular user.
# Otherwise all users will see the same cached page, regardless of user's state.
#
def page_etag
@page.cache_key + current_alchemy_user.try(:cache_key).to_s
@page.cache_key_with_version +
current_alchemy_user.try(:cache_key_with_version).to_s
end

# We only render the page if either the cache is disabled for this page
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/alchemy/pages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ module Alchemy
subject { controller.send(:page_etag) }

before do
expect(page).to receive(:cache_key).and_return("aaa")
expect(page).to receive(:cache_key_with_version).and_return("aaa")
controller.instance_variable_set("@page", page)
end

Expand All @@ -283,7 +283,7 @@ module Alchemy

context "with user logged in" do
before do
authorize_user(mock_model(Alchemy.user_class, cache_key: "bbb"))
authorize_user(mock_model(Alchemy.user_class, cache_key_with_version: "bbb"))
end

it "returns another etag for response headers" do
Expand Down

0 comments on commit a1aa227

Please sign in to comment.