Skip to content

Commit

Permalink
Merge pull request #1525 from jedrekdomanski/Render-new-page-only-whe…
Browse files Browse the repository at this point in the history
…n-there-is-a-flash-message

Render new page when there is a flash message
  • Loading branch information
tvdeyen authored Apr 26, 2019
2 parents 3cfff1e + 6925f40 commit 218fad2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
13 changes: 9 additions & 4 deletions app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ def render_page
end

def set_expiration_headers
if @page.cache_page?
expires_in @page.expiration_time, public: !@page.restricted, must_revalidate: true
else
if must_not_cache?
expires_now
else
expires_in @page.expiration_time, public: !@page.restricted, must_revalidate: true
end
end

Expand Down Expand Up @@ -190,12 +190,17 @@ def page_etag
# or the cache is stale, because it's been republished by the user.
#
def render_fresh_page?
!@page.cache_page? || stale?(etag: page_etag,
must_not_cache? || stale?(etag: page_etag,
last_modified: @page.published_at,
public: !@page.restricted,
template: 'pages/show')
end

# don't cache pages if we have flash message to display or the page has caching disabled
def must_not_cache?
flash.present? || !@page.cache_page?
end

def page_not_found!
not_found_error!("Alchemy::Page not found \"#{request.fullpath}\"")
end
Expand Down
24 changes: 24 additions & 0 deletions spec/requests/alchemy/page_request_caching_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,30 @@
end
end

context "but a flash message is present" do
before do
allow_any_instance_of(ActionDispatch::Flash::FlashHash).to receive(:present?) do
true
end
end

it "sets no-cache header" do
get "/#{page.urlname}"
expect(response.headers).to have_key('Cache-Control')
expect(response.headers['Cache-Control']).to eq('no-cache')
end

it "does not set etag header" do
get "/#{page.urlname}"
expect(response.headers).to_not 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
end

after do
Rails.application.config.action_controller.perform_caching = false
end
Expand Down

0 comments on commit 218fad2

Please sign in to comment.