Skip to content

Commit

Permalink
Merge pull request #2365 from tvdeyen/fix-page-cache-etag
Browse files Browse the repository at this point in the history
Use cache_key_with_version in page_etag
  • Loading branch information
tvdeyen authored Sep 2, 2022
2 parents 92b92d3 + 45ec14f commit feb5e6f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,12 @@ 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, current_alchemy_user]
end

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

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

it "returns the etag for response headers" do
expect(subject).to eq("aaa")
expect(subject).to include(page)
end

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
expect(subject).to eq("aaabbb")
expect(subject).to include(an_instance_of(Alchemy.user_class))
end
end
end
Expand Down

0 comments on commit feb5e6f

Please sign in to comment.