diff --git a/app/controllers/favicon_controller.rb b/app/controllers/favicon_controller.rb new file mode 100644 index 0000000000..a19448dcf8 --- /dev/null +++ b/app/controllers/favicon_controller.rb @@ -0,0 +1,12 @@ +# Because favicon has to be present at root, we need a controller +# to redirect it to the asset. When there's a better solution to this +# problem we can remove this controller and routes. +class FaviconController < ApplicationController + before_action { expires_in(1.day, public: true) } + + def redirect_to_asset + redirect_to(view_context.asset_path("favicon.ico"), + status: :moved_permanently, + allow_other_host: true) + end +end diff --git a/config/routes.rb b/config/routes.rb index 413826a98f..f13c08aea9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,6 +15,9 @@ # http://stackoverflow.com/a/3443678 get "*path.gif", to: proc { |_env| [404, {}, ["Not Found"]] } + # Favicon redirect + get "/favicon.ico", to: "favicon#redirect_to_asset" + unless Rails.env.production? get "/development", to: "development#index" end diff --git a/public/favicon.ico b/public/favicon.ico deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/spec/requests/favicon_spec.rb b/spec/requests/favicon_spec.rb new file mode 100644 index 0000000000..3dbcf1b95d --- /dev/null +++ b/spec/requests/favicon_spec.rb @@ -0,0 +1,9 @@ +RSpec.describe "Favicon" do + it "redirects permanently to an asset with a 1 day expiry" do + get "/favicon.ico" + + expect(response.headers["Cache-Control"]).to eq("max-age=86400, public") + expect(response.status).to eq(301) + expect(response.location).to match(/http:\/\/www.example.com\/assets\/frontend\/favicon-(.+).ico/) + end +end