Skip to content

Commit ea75af3

Browse files
committed
cache busting
1 parent c14d165 commit ea75af3

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/where_the_plow/main.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# src/where_the_plow/main.py
22
import asyncio
3+
import hashlib
34
import logging
45
from contextlib import asynccontextmanager
56
from pathlib import Path
67

78
from fastapi import FastAPI
8-
from fastapi.responses import FileResponse
9+
from fastapi.responses import FileResponse, HTMLResponse
910
from fastapi.staticfiles import StaticFiles
1011

1112
from where_the_plow import collector
@@ -56,9 +57,28 @@ async def lifespan(app: FastAPI):
5657
app.mount("/static", StaticFiles(directory=str(STATIC_DIR)), name="static")
5758

5859

60+
def _file_hash(path: Path) -> str:
61+
"""Return first 12 chars of the MD5 hex digest of a file's contents."""
62+
return hashlib.md5(path.read_bytes()).hexdigest()[:12]
63+
64+
65+
def _build_index_html() -> str:
66+
"""Read index.html and append ?v=<hash> to local static asset references."""
67+
html = (STATIC_DIR / "index.html").read_text()
68+
for filename in ("style.css", "app.js"):
69+
asset_path = STATIC_DIR / filename
70+
if asset_path.exists():
71+
h = _file_hash(asset_path)
72+
html = html.replace(f"/static/{filename}", f"/static/{filename}?v={h}")
73+
return html
74+
75+
76+
_INDEX_HTML = _build_index_html()
77+
78+
5979
@app.get("/", include_in_schema=False)
6080
def root():
61-
return FileResponse(str(STATIC_DIR / "index.html"))
81+
return HTMLResponse(_INDEX_HTML)
6282

6383

6484
@app.get("/health", tags=["system"])

0 commit comments

Comments
 (0)