add cache-busting query params to static assets

Generates a unique hash on app startup and appends it to all .js and
.css URLs in index.html. Forces browser to fetch fresh assets on redeploy.
This commit is contained in:
Viktor Barzin 2026-03-23 00:50:20 +02:00
parent aa6760cb0f
commit 745b6ceea8
No known key found for this signature in database
GPG key ID: 0EB088298288D958

View file

@ -1,8 +1,10 @@
"""Claude Memory API -- shared persistent memory with PostgreSQL full-text search.""" """Claude Memory API -- shared persistent memory with PostgreSQL full-text search."""
import hashlib
import json import json
import logging import logging
import pathlib import pathlib
import time
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from contextvars import ContextVar from contextvars import ContextVar
from datetime import datetime, timezone from datetime import datetime, timezone
@ -46,12 +48,17 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
app = FastAPI(title="Claude Memory API", lifespan=lifespan) app = FastAPI(title="Claude Memory API", lifespan=lifespan)
UI_DIR = pathlib.Path(__file__).parent.parent / "ui" / "static" UI_DIR = pathlib.Path(__file__).parent.parent / "ui" / "static"
_CACHE_BUST = hashlib.md5(str(time.time()).encode()).hexdigest()[:8]
@app.get("/") @app.get("/")
async def ui_root() -> FileResponse: async def ui_root() -> Response:
"""Serve the UI single-page app.""" """Serve the UI single-page app with cache-busted static assets."""
return FileResponse(UI_DIR / "index.html") html = (UI_DIR / "index.html").read_text()
html = html.replace("/static/js/", f"/static/js/").replace(
'.js"', f'.js?v={_CACHE_BUST}"'
).replace('.css"', f'.css?v={_CACHE_BUST}"')
return Response(content=html, media_type="text/html")
def _detect_sensitive(content: str) -> bool: def _detect_sensitive(content: str) -> bool: