From 273ddcd3a1be250cfbc3f495f59c6ca0cc19274b Mon Sep 17 00:00:00 2001 From: Thomas Leese Date: Thu, 14 Jan 2021 12:36:42 +0000 Subject: [PATCH] Ensure asset-manager redirects are cached At the moment these redirects aren't cached which means that they won't be cached by Fastly and will always hit our origin. This PR should prevent this from happening and instead they'll be cached by Fastly for 1 day. --- app/controllers/asset_manager_redirect_controller.rb | 1 + test/controllers/asset_manager_redirect_controller_test.rb | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/app/controllers/asset_manager_redirect_controller.rb b/app/controllers/asset_manager_redirect_controller.rb index fc877b585..942e762d8 100644 --- a/app/controllers/asset_manager_redirect_controller.rb +++ b/app/controllers/asset_manager_redirect_controller.rb @@ -5,6 +5,7 @@ def show asset_url = Plek.new.external_url_for("draft-assets") end + expires_in 1.day, public: true redirect_to host: URI.parse(asset_url).host, status: :moved_permanently end end diff --git a/test/controllers/asset_manager_redirect_controller_test.rb b/test/controllers/asset_manager_redirect_controller_test.rb index a013a984a..61b87e5f3 100644 --- a/test/controllers/asset_manager_redirect_controller_test.rb +++ b/test/controllers/asset_manager_redirect_controller_test.rb @@ -6,6 +6,13 @@ class AssetManagerRedirectControllerTest < ActionController::TestCase Plek.any_instance.stubs(:external_url_for).returns("http://draft-asset-host.com") end + test "sets the cache-control max-age to 1 day" do + request.host = "some-host.com" + get :show, params: { path: "asset.txt" } + + assert_equal @response.headers["Cache-Control"], "max-age=86400, public" + end + test "redirects asset requests made via public host to the public asset host" do request.host = "some-host.com" get :show, params: { path: "asset.txt" }