diff --git a/lib/mastodon/middleware/public_file_server.rb b/lib/mastodon/middleware/public_file_server.rb index ac2f8c52c4..eb06184d51 100644 --- a/lib/mastodon/middleware/public_file_server.rb +++ b/lib/mastodon/middleware/public_file_server.rb @@ -5,8 +5,9 @@ require 'action_dispatch/middleware/static' module Mastodon module Middleware class PublicFileServer + CACHE_TTL = 28.days.to_i + SERVICE_WORKER_PATH = '/sw.js' SERVICE_WORKER_TTL = 7.days.to_i - CACHE_TTL = 28.days.to_i def initialize(app) @app = app @@ -24,7 +25,7 @@ module Mastodon # Set cache headers on static files. Some paths require different cache headers request = Rack::Request.new env headers['cache-control'] = begin - if request.path.start_with?('/sw.js') + if request.path.start_with?(SERVICE_WORKER_PATH) "public, max-age=#{SERVICE_WORKER_TTL}, must-revalidate" elsif request.path.start_with?(paperclip_root_url) "public, max-age=#{CACHE_TTL}, immutable" diff --git a/spec/requests/public_files_spec.rb b/spec/requests/public_files_spec.rb index 512e069cf7..c8d6b176af 100644 --- a/spec/requests/public_files_spec.rb +++ b/spec/requests/public_files_spec.rb @@ -6,8 +6,10 @@ RSpec.describe 'Public files' do include RoutingHelper context 'when requesting service worker file' do + before { stub_const 'Mastodon::Middleware::PublicFileServer::SERVICE_WORKER_PATH', '/robots.txt' } + it 'returns the file with the expected headers' do - get '/sw.js' + get Mastodon::Middleware::PublicFileServer::SERVICE_WORKER_PATH expect(response) .to have_http_status(200)