Skip to content

The agent that grows with you

perf(dashboard): serve hashed /assets bundles with immutable cache headers

kshitijdecf12eda017
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.py
index 2fc8f629a47..a6678d04f29 100644
--- a/hermes_cli/web_server.py
+++ b/hermes_cli/web_server.py
@@ -15927,6 +15927,14 @@ def _render_active_theme_bootstrap_css() -> str:
return ""
+# Hashed bundle assets (``/assets/<name>-<contenthash>.<ext>``) are immutable
+# by construction: any content change produces a new filename, and the entry
+# point (index.html) is served ``no-store`` so it always references the
+# current hashes. A year-long immutable cache lets browsers skip even the
+# revalidation round-trip on every dashboard load.
+_IMMUTABLE_ASSET_CACHE_CONTROL = "public, max-age=31536000, immutable"
+
+
def mount_spa(application: FastAPI):
"""Mount the built SPA. Falls back to index.html for client-side routing.
@@ -16049,9 +16057,32 @@ def mount_spa(application: FastAPI):
css = css.replace(f"url({asset_dir}", f"url({prefix}{asset_dir}")
css = css.replace(f"url(\"{asset_dir}", f"url(\"{prefix}{asset_dir}")
css = css.replace(f"url('{asset_dir}", f"url('{prefix}{asset_dir}")
- return Response(content=css, media_type="text/css")
+ return Response(
+ content=css,
+ media_type="text/css",
+ headers={"Cache-Control": _IMMUTABLE_ASSET_CACHE_CONTROL},
+ )
+
+ class _ImmutableAssetFiles(StaticFiles):
+ """StaticFiles that marks hashed bundle assets immutable.
- application.mount("/assets", StaticFiles(directory=WEB_DIST / "assets"), name="assets")
+ Everything under ``/assets/`` carries a Vite content hash in its
+ filename, so a given URL's bytes can never change — a rebuild
+ produces a NEW filename referenced by a fresh (``no-store``)
+ index.html. Without this header every dashboard load re-validated
+ each chunk; with it the browser serves reloads straight from its
+ HTTP cache.
+ """
+
+ async def get_response(self, path: str, scope):
+ response = await super().get_response(path, scope)
+ if response.status_code == 200:
+ response.headers["Cache-Control"] = _IMMUTABLE_ASSET_CACHE_CONTROL
+ return response
+
+ application.mount(
+ "/assets", _ImmutableAssetFiles(directory=WEB_DIST / "assets"), name="assets"
+ )
@application.get("/{full_path:path}")
async def serve_spa(full_path: str, request: Request):
diff --git a/tests/hermes_cli/test_web_server.py b/tests/hermes_cli/test_web_server.py
index 67a6ae24dd6..fc4a41c4c0c 100644
--- a/tests/hermes_cli/test_web_server.py
+++ b/tests/hermes_cli/test_web_server.py
@@ -3948,6 +3948,81 @@ class TestServeIndexMissingIndex:
assert "SPA-rebuilt" in resp.text
+class TestHashedAssetCacheHeaders:
+ """Hashed /assets/* responses must be immutable-cacheable; index.html
+ must stay no-store so it always references the current hashes
+ (salvaged from PR #28543)."""
+
+ _IMMUTABLE = "public, max-age=31536000, immutable"
+
+ @staticmethod
+ def _client(tmp_path, monkeypatch):
+ from fastapi import FastAPI
+ from starlette.testclient import TestClient
+ import hermes_cli.web_server as ws
+
+ dist = tmp_path / "web_dist"
+ (dist / "assets").mkdir(parents=True)
+ (dist / "index.html").write_text(
+ "<html><head></head><body>SPA</body></html>", encoding="utf-8"
+ )
+ (dist / "assets" / "index-abc123.js").write_text(
+ "console.log('bundle');", encoding="utf-8"
+ )
+ (dist / "assets" / "index-abc123.css").write_text(
+ "body{background:url(/ds-assets/bg.png);"
+ "font-family:url(/fonts-terminal/x.woff2)}",
+ encoding="utf-8",
+ )
+ monkeypatch.setattr(ws, "WEB_DIST", dist)
+ monkeypatch.delenv("HERMES_SERVE_HEADLESS", raising=False)
+ spa_app = FastAPI()
+ ws.mount_spa(spa_app)
+ return TestClient(spa_app)
+
+ def test_hashed_js_asset_is_immutable(self, tmp_path, monkeypatch):
+ client = self._client(tmp_path, monkeypatch)
+ resp = client.get("/assets/index-abc123.js")
+ assert resp.status_code == 200
+ assert resp.headers["cache-control"] == self._IMMUTABLE
+
+ def test_serve_css_is_immutable_and_keeps_prefix_rewrites(
+ self, tmp_path, monkeypatch
+ ):
+ client = self._client(tmp_path, monkeypatch)
+ resp = client.get("/assets/index-abc123.css")
+ assert resp.status_code == 200
+ assert resp.headers["cache-control"] == self._IMMUTABLE
+
+ # The proxy-prefix rewrite path (main's ds-assets/fonts-terminal
+ # handling) must survive the header change.
+ prefixed = client.get(
+ "/assets/index-abc123.css",
+ headers={"X-Forwarded-Prefix": "/hermes"},
+ )
+ assert prefixed.status_code == 200
+ assert prefixed.headers["cache-control"] == self._IMMUTABLE
+ assert "url(/hermes/ds-assets/bg.png)" in prefixed.text
+ assert "url(/hermes/fonts-terminal/x.woff2)" in prefixed.text
+
+ def test_index_html_stays_no_store(self, tmp_path, monkeypatch):
+ client = self._client(tmp_path, monkeypatch)
+ for route in ("/", "/chat"):
+ resp = client.get(route)
+ assert resp.status_code == 200
+ cache_control = resp.headers["cache-control"]
+ assert "no-store" in cache_control
+ assert "immutable" not in cache_control
+
+ def test_missing_asset_is_not_marked_immutable(self, tmp_path, monkeypatch):
+ """A 404 must never be cached for a year — a later rebuild can
+ legitimately create the file."""
+ client = self._client(tmp_path, monkeypatch)
+ resp = client.get("/assets/nope-000000.js")
+ assert resp.status_code == 404
+ assert "immutable" not in resp.headers.get("cache-control", "")
+
+
class TestDashboardComponentHealth:
"""Component-health rollup: error middleware, /api/status components, self-test."""