Serves static files without allowing client-side caching. Overrides the default Starlette StaticFiles to add 'Cache-Control' headers that prevent browsers from caching static assets. Useful for development.
| 74 | # Custom no-cache StaticFiles |
| 75 | # -------------------------------------------------------------------- |
| 76 | class NoCacheStaticFiles(StaticFiles): |
| 77 | """ |
| 78 | Serves static files without allowing client-side caching. |
| 79 | |
| 80 | Overrides the default Starlette StaticFiles to add 'Cache-Control' headers |
| 81 | that prevent browsers from caching static assets. Useful for development. |
| 82 | """ |
| 83 | async def get_response(self, path: str, scope: Dict[str, Any]) -> Response: |
| 84 | """ |
| 85 | Gets the response for a requested path, adding no-cache headers. |
| 86 | |
| 87 | Args: |
| 88 | path: The path to the static file requested. |
| 89 | scope: The ASGI scope dictionary for the request. |
| 90 | |
| 91 | Returns: |
| 92 | A Starlette Response object with cache-control headers modified. |
| 93 | """ |
| 94 | response: Response = await super().get_response(path, scope) |
| 95 | response.headers["Cache-Control"] = "no-store, no-cache, must-revalidate, max-age=0" |
| 96 | # These might not be strictly necessary with no-store, but belt and suspenders |
| 97 | if "etag" in response.headers: |
| 98 | response.headers.__delitem__("etag") |
| 99 | if "last-modified" in response.headers: |
| 100 | response.headers.__delitem__("last-modified") |
| 101 | return response |
| 102 | |
| 103 | # -------------------------------------------------------------------- |
| 104 | # Lifespan management |