2024-01-16 06:50:30 +00:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from starlette.responses import Response
|
|
|
|
from starlette.staticfiles import StaticFiles
|
|
|
|
|
|
|
|
|
|
|
|
class NoCacheStaticFiles(StaticFiles):
|
|
|
|
"""
|
|
|
|
This class is used to override the default caching behavior of starlette for static files,
|
|
|
|
ensuring we *never* cache static files. It modifies the file response headers to strictly
|
|
|
|
never cache the files.
|
|
|
|
|
2024-01-22 05:01:26 +00:00
|
|
|
Static files include the javascript bundles, fonts, locales, and some images. Generated
|
2024-01-22 05:02:00 +00:00
|
|
|
images are not included, as they are served by a router.
|
2024-01-16 06:50:30 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args: Any, **kwargs: Any):
|
|
|
|
self.cachecontrol = "max-age=0, no-cache, no-store, , must-revalidate"
|
|
|
|
self.pragma = "no-cache"
|
|
|
|
self.expires = "0"
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def file_response(self, *args: Any, **kwargs: Any) -> Response:
|
|
|
|
resp = super().file_response(*args, **kwargs)
|
|
|
|
resp.headers.setdefault("Cache-Control", self.cachecontrol)
|
|
|
|
resp.headers.setdefault("Pragma", self.pragma)
|
|
|
|
resp.headers.setdefault("Expires", self.expires)
|
|
|
|
return resp
|