Skip to content

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.rb
index 66e9c992b49..780ee38bebf 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -101,8 +101,12 @@ class ApplicationController < ActionController::Base
def dont_cache_page
if !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"]
+ end
end
response.headers["Discourse-No-Onebox"] = "1" if SiteSetting.login_required
end
diff --git a/config/site_settings.yml b/config/site_settings.yml
index b95d2ebfebd..7d0f41ffc74 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -2808,6 +2808,9 @@ security:
refresh: true
hidden: true
area: "group_permissions"
+ cache_control_bfcache_compatibility:
+ default: false
+ hidden: true
onebox:
post_onebox_maxlength:
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index 70304dbc66a..a613130f595 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -24,6 +24,31 @@ RSpec.describe ApplicationController do
expect(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
+ end
end
context "when visiting an invalid URL" do