A platform for community discussion. Free, open, simple.
FEATURE: Add bfcache-compatible cache-control headers option (#38763)
Rafael dos Santos Silva14dd3ba0c743
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 66e9c992b49..780ee38bebf 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -101,8 +101,12 @@ class ApplicationController < ActionController::Basedef dont_cache_pageif !response.headers["Cache-Control"] && response.cache_control.blank?- response.cache_control[:no_cache] = true- response.cache_control[:extras] = ["no-store"]+ if SiteSetting.cache_control_bfcache_compatibility+ response.cache_control[:no_cache] = true+ else+ response.cache_control[:no_cache] = true+ response.cache_control[:extras] = ["no-store"]+ endendresponse.headers["Discourse-No-Onebox"] = "1" if SiteSetting.login_requiredenddiff --git a/config/site_settings.yml b/config/site_settings.ymlindex b95d2ebfebd..7d0f41ffc74 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -2808,6 +2808,9 @@ security:refresh: truehidden: truearea: "group_permissions"+ cache_control_bfcache_compatibility:+ default: false+ hidden: trueonebox:post_onebox_maxlength:diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex 70304dbc66a..a613130f595 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -24,6 +24,31 @@ RSpec.describe ApplicationController doexpect(response.status).to eq(403)expect(response.headers["Cache-Control"]).to eq("no-cache, no-store")end++ context "when cache_control_bfcache_compatibility is enabled" do+ before { SiteSetting.cache_control_bfcache_compatibility = true }++ it "sets bfcache-compatible cache control headers" do+ get "/latest"++ expect(response.status).to eq(200)+ expect(response.headers["Cache-Control"]).to eq("no-cache")+ end++ it "sets bfcache-compatible cache control headers on 404" do+ get "/invalid-urlllllllllll"++ expect(response.status).to eq(404)+ expect(response.headers["Cache-Control"]).to eq("no-cache")+ end++ it "sets bfcache-compatible cache control headers on 403" do+ get "/latest.json", headers: { HTTP_API_KEY: "invalid-api-key" }++ expect(response.status).to eq(403)+ expect(response.headers["Cache-Control"]).to eq("no-cache")+ end+ endendcontext "when visiting an invalid URL" do