fix(ui/ux): make the browser revalidate the static files
This commit is contained in:
+13
-1
@@ -23,5 +23,17 @@ app.include_router(imports.router)
|
||||
app.include_router(fs.router)
|
||||
|
||||
|
||||
class RevalidatingStatics(StaticFiles):
|
||||
"""Without a Cache-Control header the browser caches heuristically and does
|
||||
not ask again, so a redeployed app.js kept rendering the old UI. no-cache
|
||||
still allows the cached copy, it just has to be revalidated first - one 304
|
||||
per file per load."""
|
||||
|
||||
def file_response(self, *args, **kwargs): # starlette passes these positionally
|
||||
resp = super().file_response(*args, **kwargs)
|
||||
resp.headers["Cache-Control"] = "no-cache"
|
||||
return resp
|
||||
|
||||
|
||||
static_dir = Path(__file__).parent.parent / "static"
|
||||
app.mount("/", StaticFiles(directory=static_dir, html=True), name="static")
|
||||
app.mount("/", RevalidatingStatics(directory=static_dir, html=True), name="static")
|
||||
|
||||
Reference in New Issue
Block a user