Dynamically compute sw.js cache key based on static file hash
This commit is contained in:
18
src/main.py
18
src/main.py
@@ -1,3 +1,4 @@
|
||||
import hashlib
|
||||
import logging
|
||||
import logging.handlers
|
||||
import os
|
||||
@@ -56,6 +57,15 @@ from routers.admins import router as admins_router
|
||||
from routers.stats import router as stats_router
|
||||
from routers.telegram import router as telegram_router
|
||||
|
||||
# ── Cache-bust hash (SHA-1 of all static file contents) ──────────────────────
|
||||
def _static_hash(static_dir: str) -> str:
|
||||
h = hashlib.sha1()
|
||||
for root, _, files in sorted(os.walk(static_dir)):
|
||||
for name in sorted(files):
|
||||
with open(os.path.join(root, name), "rb") as f:
|
||||
h.update(f.read())
|
||||
return h.hexdigest()[:8]
|
||||
|
||||
# ── App ───────────────────────────────────────────────────────────────────────
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
@@ -132,10 +142,10 @@ async def _serve_admin() -> FileResponse:
|
||||
|
||||
|
||||
@app.get("/sw.js", include_in_schema=False)
|
||||
async def _serve_sw() -> FileResponse:
|
||||
return FileResponse(
|
||||
os.path.join(_STATIC_DIR, "sw.js"), media_type="application/javascript"
|
||||
)
|
||||
async def _serve_sw() -> Response:
|
||||
sw_path = os.path.join(_STATIC_DIR, "sw.js")
|
||||
content = open(sw_path, encoding="utf-8").read().replace("__STATIC_HASH__", _static_hash(os.path.join(os.path.dirname(os.path.abspath(__file__)), "static")))
|
||||
return Response(content=content, media_type="application/javascript")
|
||||
|
||||
|
||||
@app.get("/manifest.json", include_in_schema=False)
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
# routers package
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
from .avconnect import AVConnectAPI
|
||||
from .gates import call_open_gate
|
||||
|
||||
__all__ = [
|
||||
"AVConnectAPI",
|
||||
"call_open_gate",
|
||||
]
|
||||
@@ -136,9 +136,9 @@
|
||||
<nav class="tabs">
|
||||
<button class="tab-btn active" data-tab="keypasses">Keypasses</button>
|
||||
<button class="tab-btn" data-tab="gates">Gates</button>
|
||||
<button class="tab-btn admin-only" data-tab="credentials">API Credentials</button>
|
||||
<button class="tab-btn" data-tab="stats">Statistics</button>
|
||||
<button class="tab-btn admin-only" data-tab="admins">Admins</button>
|
||||
<button class="tab-btn admin-only" data-tab="credentials">API Credentials</button>
|
||||
<button class="tab-btn admin-only" data-tab="telegram">Notifications</button>
|
||||
</nav>
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/* Service worker - Lagomare Gates */
|
||||
const CACHE = "lagomare-gates-v1";
|
||||
const CACHE = "lagomare-gates-__STATIC_HASH__";
|
||||
const PRECACHE = ["/static/style.css", "/static/app.js", "/static/images/logo.svg", "/static/images/mobile_icon.png", "/manifest.json"];
|
||||
|
||||
self.addEventListener("install", event => {
|
||||
|
||||
Reference in New Issue
Block a user