Spaces:
Running
Running
fix: cache headers so deploys are visible immediately
Browse filesindex.html (the pointer to the current bundle) is served no-cache, must-revalidate;
hashed /assets files are cached immutable for a year. Without this, mobile browsers
heuristically cached index.html and kept showing the previous UI after a deploy.
- src/case_zero/api/server.py +20 -4
src/case_zero/api/server.py
CHANGED
|
@@ -34,6 +34,16 @@ _INDEX = WEB_DIST / "index.html"
|
|
| 34 |
_AUDIO_DIR = ASSETS_DIR / "ui"
|
| 35 |
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
def build_server() -> Server:
|
| 38 |
"""Return a configured (not yet launched) ``gradio.Server``."""
|
| 39 |
server = Server(title="Case Zero", docs_url=None, redoc_url=None, openapi_url=None)
|
|
@@ -71,15 +81,21 @@ def build_server() -> Server:
|
|
| 71 |
|
| 72 |
if WEB_DIST.is_dir():
|
| 73 |
# Hashed JS/CSS live under /assets - a specific prefix that never collides with /api.
|
|
|
|
| 74 |
server.mount(
|
| 75 |
"/assets",
|
| 76 |
-
|
| 77 |
name="assets",
|
| 78 |
)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
@server.get("/")
|
| 81 |
def _index() -> FileResponse:
|
| 82 |
-
return FileResponse(_INDEX)
|
| 83 |
|
| 84 |
@server.get("/{filename}")
|
| 85 |
def _root_or_spa(filename: str) -> FileResponse:
|
|
@@ -88,9 +104,9 @@ def build_server() -> Server:
|
|
| 88 |
# this never shadows /gradio_api/* or /api/* (those have deeper paths).
|
| 89 |
candidate = (WEB_DIST / filename).resolve()
|
| 90 |
if candidate.is_file() and candidate.is_relative_to(WEB_DIST):
|
| 91 |
-
return FileResponse(candidate)
|
| 92 |
if _INDEX.is_file():
|
| 93 |
-
return FileResponse(_INDEX)
|
| 94 |
raise HTTPException(status_code=404)
|
| 95 |
|
| 96 |
return server
|
|
|
|
| 34 |
_AUDIO_DIR = ASSETS_DIR / "ui"
|
| 35 |
|
| 36 |
|
| 37 |
+
class _ImmutableStaticFiles(StaticFiles):
|
| 38 |
+
"""Static files with a far-future cache policy - only safe because every file under
|
| 39 |
+
/assets carries a content hash in its name, so an update is always a new URL."""
|
| 40 |
+
|
| 41 |
+
def file_response(self, *args, **kwargs): # type: ignore[no-untyped-def]
|
| 42 |
+
response = super().file_response(*args, **kwargs)
|
| 43 |
+
response.headers["Cache-Control"] = "public, max-age=31536000, immutable"
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
|
| 47 |
def build_server() -> Server:
|
| 48 |
"""Return a configured (not yet launched) ``gradio.Server``."""
|
| 49 |
server = Server(title="Case Zero", docs_url=None, redoc_url=None, openapi_url=None)
|
|
|
|
| 81 |
|
| 82 |
if WEB_DIST.is_dir():
|
| 83 |
# Hashed JS/CSS live under /assets - a specific prefix that never collides with /api.
|
| 84 |
+
# Content-hashed filenames are safe to cache forever; new deploys ship new names.
|
| 85 |
server.mount(
|
| 86 |
"/assets",
|
| 87 |
+
_ImmutableStaticFiles(directory=str(WEB_DIST / "assets")),
|
| 88 |
name="assets",
|
| 89 |
)
|
| 90 |
|
| 91 |
+
# index.html must NEVER be cached blindly: it is the pointer to the current bundle.
|
| 92 |
+
# Without this header, mobile browsers heuristically cache it and keep loading a
|
| 93 |
+
# stale bundle after a deploy (players and judges would see the previous UI).
|
| 94 |
+
_NO_CACHE = {"Cache-Control": "no-cache, must-revalidate"}
|
| 95 |
+
|
| 96 |
@server.get("/")
|
| 97 |
def _index() -> FileResponse:
|
| 98 |
+
return FileResponse(_INDEX, headers=_NO_CACHE)
|
| 99 |
|
| 100 |
@server.get("/{filename}")
|
| 101 |
def _root_or_spa(filename: str) -> FileResponse:
|
|
|
|
| 104 |
# this never shadows /gradio_api/* or /api/* (those have deeper paths).
|
| 105 |
candidate = (WEB_DIST / filename).resolve()
|
| 106 |
if candidate.is_file() and candidate.is_relative_to(WEB_DIST):
|
| 107 |
+
return FileResponse(candidate, headers=_NO_CACHE)
|
| 108 |
if _INDEX.is_file():
|
| 109 |
+
return FileResponse(_INDEX, headers=_NO_CACHE)
|
| 110 |
raise HTTPException(status_code=404)
|
| 111 |
|
| 112 |
return server
|