@@ -940,8 +940,33 @@ def doc():
940940 return FileResponse (f'{ TEMPLATE_DIR } /rest-api-doc.html' )
941941
942942
943+ class CachelessFiles (StaticFiles ):
944+ """Filters what files should or shouldn't be cached by a user's browser"""
945+
946+ async def get_response (self , path , scope ):
947+ response = await super ().get_response (path , scope )
948+
949+ content_type = response .headers .get ("content-type" , "" )
950+
951+ # Vite wraps all javascript and CSS files into bundles called index_[hash].js index_[hash].css
952+ # That causes this check to only let images into the cache
953+
954+ # The fact that those files are hashed _should_ make it so that old files are replaced with new files with new hashes post-update
955+ # but the endpoint that provides the files has a cached response that contains the old files
956+ if content_type .startswith ("text/html" ) or "index" in path :
957+ # index.html (root + SPA fallback)
958+ response .headers ["Cache-Control" ] = "no-cache, no-store, must-revalidate"
959+ response .headers ["Pragma" ] = "no-cache"
960+ response .headers ["Expires" ] = "0"
961+ else :
962+ # all versioned/static assets
963+ response .headers ["Cache-Control" ] = "public, max-age=31536000, immutable"
964+
965+ return response
966+
967+
943968# Website
944- app .mount ('/' , StaticFiles (directory = WEB_DIR , html = True ), name = 'web' )
969+ app .mount ('/' , CachelessFiles (directory = WEB_DIR , html = True ), name = 'web' )
945970
946971
947972def create_app (mock_ctrl = None , mock_streams = None , config_file = None , delay_saves = None , settings : models .AppSettings = models .AppSettings ()) -> FastAPI :
0 commit comments