From 6925f404bf85229bb8cac20223c658f94987edd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drek=20Doma=C5=84ski?= Date: Tue, 18 Dec 2018 18:29:26 +0100 Subject: [PATCH] Render fresh page when there is a flash message Never render a cached version of a page if there is a flash message. --- app/controllers/alchemy/pages_controller.rb | 13 ++++++---- .../alchemy/page_request_caching_spec.rb | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/controllers/alchemy/pages_controller.rb b/app/controllers/alchemy/pages_controller.rb index 92a31c192d..67f8de23e7 100644 --- a/app/controllers/alchemy/pages_controller.rb +++ b/app/controllers/alchemy/pages_controller.rb @@ -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 @@ -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 diff --git a/spec/requests/alchemy/page_request_caching_spec.rb b/spec/requests/alchemy/page_request_caching_spec.rb index aff86ec4e9..018e629720 100644 --- a/spec/requests/alchemy/page_request_caching_spec.rb +++ b/spec/requests/alchemy/page_request_caching_spec.rb @@ -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