Skip to content

Instantly share code, notes, and snippets.

@peterroelants
Created October 11, 2023 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peterroelants/792e62172e086dfab19edb3ab63f594a to your computer and use it in GitHub Desktop.
Save peterroelants/792e62172e086dfab19edb3ab63f594a to your computer and use it in GitHub Desktop.
FastAPI Cache-Control : no-cache
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
class StaticNoCache(StaticFiles):
"""Disable caching of static files serverd"""
def is_not_modified(self, *args, **kwargs) -> bool:
return False
def file_response(self, *args, **kwargs):
resp = super().file_response(*args, **kwargs)
resp.headers["Cache-Control"] = "no-cache"
return resp
app = FastAPI()
app.mount("/static", StaticNoCache(directory="./static_files"), name="static")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment