Add HTTP caching for web dashboard resources

Implement cache-control middleware to optimize browser caching and reduce
bandwidth usage. Static files are cached for 1 year when accessed with
version parameters, while dynamic content is never cached.

Changes:
- Add CacheControlMiddleware with path-based caching logic
- Register middleware in web app after ProxyHeadersMiddleware
- Add version query parameters to CSS, JS, and app.js references
- Create comprehensive test suite (20 tests) for all cache behaviors

Cache strategy:
- Static files with ?v=X.Y.Z: 1 year (immutable)
- Static files without version: 1 hour (fallback)
- SPA shell HTML: no-cache (dynamic config)
- Health endpoints: no-cache, no-store (always fresh)
- Map data: 5 minutes (location updates)
- Custom pages: 1 hour (stable markdown)
- API proxy: pass-through (backend controls)

All 458 tests passing, 95% middleware coverage.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Louis King
2026-02-14 00:01:08 +00:00
parent 96ca6190db
commit 189eb3a139
4 changed files with 312 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
from meshcore_hub import __version__
from meshcore_hub.common.i18n import load_locale, t
from meshcore_hub.common.schemas import RadioConfig
from meshcore_hub.web.middleware import CacheControlMiddleware
from meshcore_hub.web.pages import PageLoader
logger = logging.getLogger(__name__)
@@ -176,6 +177,9 @@ def create_app(
# Trust proxy headers (X-Forwarded-Proto, X-Forwarded-For) for HTTPS detection
app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
# Add cache control headers based on resource type
app.add_middleware(CacheControlMiddleware)
# Load i18n translations
app.state.web_locale = settings.web_locale or "en"
load_locale(app.state.web_locale)

View File

@@ -0,0 +1,85 @@
"""HTTP caching middleware for the web component."""
from collections.abc import Awaitable, Callable
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
from starlette.responses import Response
from starlette.types import ASGIApp
class CacheControlMiddleware(BaseHTTPMiddleware):
"""Middleware to set appropriate Cache-Control headers based on resource type."""
def __init__(self, app: ASGIApp) -> None:
"""Initialize the middleware.
Args:
app: The ASGI application to wrap.
"""
super().__init__(app)
async def dispatch(
self,
request: Request,
call_next: Callable[[Request], Awaitable[Response]],
) -> Response:
"""Process the request and add appropriate caching headers.
Args:
request: The incoming HTTP request.
call_next: The next middleware or route handler.
Returns:
The response with cache headers added.
"""
response: Response = await call_next(request)
# Skip if Cache-Control already set (explicit override)
if "cache-control" in response.headers:
return response
path = request.url.path
query_params = request.url.query
# Health endpoints - never cache
if path.startswith("/health"):
response.headers["cache-control"] = "no-cache, no-store, must-revalidate"
# Static files with version parameter - long-term cache
elif path.startswith("/static/") and "v=" in query_params:
response.headers["cache-control"] = "public, max-age=31536000, immutable"
# Static files without version - short cache as fallback
elif path.startswith("/static/"):
response.headers["cache-control"] = "public, max-age=3600"
# Media files with version parameter - long-term cache
elif path.startswith("/media/") and "v=" in query_params:
response.headers["cache-control"] = "public, max-age=31536000, immutable"
# Media files without version - short cache (user may update)
elif path.startswith("/media/"):
response.headers["cache-control"] = "public, max-age=3600"
# Map data - short cache (5 minutes)
elif path == "/map/data":
response.headers["cache-control"] = "public, max-age=300"
# Custom pages - moderate cache (1 hour)
elif path.startswith("/spa/pages/"):
response.headers["cache-control"] = "public, max-age=3600"
# SEO files - moderate cache (1 hour)
elif path in ("/robots.txt", "/sitemap.xml"):
response.headers["cache-control"] = "public, max-age=3600"
# API proxy - don't add headers (pass through backend)
elif path.startswith("/api/"):
pass
# SPA shell HTML (catch-all for client-side routes) - no cache
elif response.headers.get("content-type", "").startswith("text/html"):
response.headers["cache-control"] = "no-cache, public"
return response

View File

@@ -39,7 +39,7 @@
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
<!-- Custom application styles -->
<link rel="stylesheet" href="/static/css/app.css">
<link rel="stylesheet" href="/static/css/app.css?v={{ version }}">
<!-- Import map for ES module dependencies -->
<script type="importmap">
@@ -175,7 +175,7 @@
<script src="https://cdn.jsdelivr.net/npm/qrcodejs@1.0.0/qrcode.min.js"></script>
<!-- Chart helper functions -->
<script src="/static/js/charts.js"></script>
<script src="/static/js/charts.js?v={{ version }}"></script>
<!-- Embedded app configuration -->
<script>
@@ -199,6 +199,6 @@
</script>
<!-- SPA Application (ES Module) -->
<script type="module" src="/static/js/spa/app.js"></script>
<script type="module" src="/static/js/spa/app.js?v={{ version }}"></script>
</body>
</html>