Prevent caching problem of HTML files for web UI

- Add NoCacheStaticFiles class
- Set Cache-Control headers for HTML files
This commit is contained in:
yangdx
2025-03-12 18:54:35 +08:00
parent 215dd76a46
commit 5c1cf6a8ff
2 changed files with 14 additions and 1 deletions

View File

@@ -423,12 +423,22 @@ def create_app(args):
"update_status": update_status,
}
# Custom StaticFiles class to prevent caching of HTML files
class NoCacheStaticFiles(StaticFiles):
async def get_response(self, path: str, scope):
response = await super().get_response(path, scope)
if path.endswith('.html'):
response.headers['Cache-Control'] = 'no-cache, no-store, must-revalidate'
response.headers['Pragma'] = 'no-cache'
response.headers['Expires'] = '0'
return response
# Webui mount webui/index.html
static_dir = Path(__file__).parent / "webui"
static_dir.mkdir(exist_ok=True)
app.mount(
"/webui",
StaticFiles(directory=static_dir, html=True, check_dir=True),
NoCacheStaticFiles(directory=static_dir, html=True, check_dir=True),
name="webui",
)