Handle missing sw.js in test environment assets (#39459)

This commit is contained in:
Matt Jankowski 2026-06-17 05:01:45 -04:00 committed by Claire
parent eba056979f
commit 41ea9ad128
2 changed files with 6 additions and 3 deletions

View File

@ -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"

View File

@ -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)