staticfiles.py 571 B

123456789101112131415
  1. from starlette.responses import Response
  2. from starlette.staticfiles import StaticFiles
  3. from starlette.types import Scope
  4. class CacheControlledStaticFiles(StaticFiles):
  5. def __init__(self, *args, max_cache_age: int = 3600, **kwargs) -> None:
  6. self.max_cache_age = max_cache_age
  7. super().__init__(*args, **kwargs)
  8. async def get_response(self, path: str, scope: Scope) -> Response:
  9. response = await super().get_response(path, scope)
  10. response.headers['Cache-Control'] = f'public, max-age={self.max_cache_age}'
  11. return response