""" Hub web application — serves the MeshBay SPA and static assets. The SPA (Preact + htm) handles: - Authentication (login, register, token refresh) - Group discovery and browsing - WebRTC connection to nodes for P2P file transfer - Dark/light theme with system preference detection Static files are served from meshbay_hub/static/ via Starlette StaticFiles. The root route (/) returns the SPA HTML shell. """ import hashlib from pathlib import Path from fastapi import APIRouter from fastapi.responses import HTMLResponse STATIC_DIR = Path(__file__).parent.parent / "static" router = APIRouter(tags=["webapp"]) # The shell loads a few assets by name; everything else is imported by app.js # from a relative path, which inherits the `/a//` prefix the shell loaded # app.js under — so the whole module graph moves together. def _asset_version() -> str: """A fingerprint of what we are actually serving. `Cache-Control: no-cache` only binds a browser that asks. One that cached app.js *before* that header existed applies heuristic freshness — a fraction of the file's age, which for a file dated weeks ago is days — and never asks at all. It then runs last week's player against this week's node for as long as that lasts, which is indistinguishable from the fix not working. Changing the URL is the only thing that reaches such a browser, and a content hash changes it exactly when the content changes. **Every file under the static directory**, because that is what `/a//` serves — with a year's `immutable`, so a file outside the hash is a file a browser never fetches again. The fingerprint used to cover a hand-kept list of top-level modules, and the translations and `vendor/` were not on it: a change confined to the catalogues kept the hash, and a phone went on showing a heading that had been rewritten and deployed. The path is hashed with the content, so a rename or a new file moves the version too. """ h = hashlib.sha256() for path in sorted(p for p in STATIC_DIR.rglob("*") if p.is_file()): h.update(path.relative_to(STATIC_DIR).as_posix().encode() + b"\0") h.update(path.read_bytes()) return h.hexdigest()[:12] ASSET_V = _asset_version() # No Cache-Control here meant no explicit signal either way, and a browser # left to its own heuristics can decide this is fresh enough without asking # — which nothing about a subsequent reload, pull-to-refresh included, # is guaranteed to override. `{v}` only reaches the browser at all if this # shell itself is refetched; a heuristically-cached copy of it re-serves the # OLD hash and therefore the old JS forever, indistinguishable from a fix not # working. `no-store` forces every navigation here to hit the network, which # is the only way `{v}` can ever change what a browser holding an old page # actually asks for next. _NO_STORE = {"Cache-Control": "no-store"} # Content-Security-Policy for the whole hub, applied by a middleware in app.py. # # This is the desktop client's policy for these exact UI files # (`meshbay-client/src/main.js`), which serves the same interface, and the two # have to be changed together — a directive added here and not there breaks the # app, and the reverse leaves the browser behind. They differ in two places, on # purpose: `frame-ancestors` is `'self'` here and `'none'` there (nothing frames # an `app://` page), and `frame-src` carries `'self'` here for the streamed # download's hidden iframe, which the client has no service worker for. # `'unsafe-inline'` is style-only — htm/preact set inline `style=` attributes # everywhere; nothing inline executes, and the shell below carries no inline # ` """.replace("{v}", ASSET_V)