diff --git a/chrome/app.py b/chrome/app.py index e17cf82..3b41f3f 100644 --- a/chrome/app.py +++ b/chrome/app.py @@ -14,6 +14,7 @@ from pathlib import Path from fastapi import FastAPI, Request from fastapi.responses import HTMLResponse, RedirectResponse, Response +from fastapi.responses import FileResponse from fastapi.staticfiles import StaticFiles from fastapi.templating import Jinja2Templates from jinja2 import ChoiceLoader, FileSystemLoader @@ -124,11 +125,18 @@ class Chrome: app.mount(self.static_path, StaticFiles(directory=static_dir), name="static") + # The service's icon set, or chrome's generic one for whatever is + # missing. Browsers differ in what they fetch: Chromium and Firefox + # take the SVG the page links, Safari and bookmark bars want a PNG, + # Android home screens want 192/512, and many clients blindly ask + # for /favicon.ico - so every one of those has an answer. # One token over chrome's tree and the service's, so any change to # either invalidates both and a page can never pair old CSS with new JS. self.asset_version = assets.digest( *(p for p in (STATIC_DIR, static_dir) if p is not None) ) + self.icons = self._find_icons(static_dir) + self._install_icon_routes(prefix) loaders = [FileSystemLoader(templates_dir)] if templates_dir else [] loaders.append(FileSystemLoader(TEMPLATES_DIR)) @@ -137,6 +145,7 @@ class Chrome: self.templates.env.globals.update( asset=self.asset, chrome_asset=self.chrome_asset, + icon=self.icon, wa_version=WEBAWESOME_VERSION, wa_base=self.wa_path, ) @@ -154,6 +163,57 @@ class Chrome: self._install_language_route() self._install_error_pages() + # --- icons ---------------------------------------------------------------- + + #: Logical name -> candidate paths under the service's static dir, in + #: order. The first that exists wins; chrome's own file is the fallback. + ICON_FILES = { + "svg": ("icons/favicon.svg", "favicon.svg"), + "png32": ("icons/favicon-32.png", "favicon-32.png", "icons/favicon.png"), + "apple": ("icons/apple-touch-icon.png", "apple-touch-icon.png", "icons/icon-192.png", "icon-192.png"), + "192": ("icons/icon-192.png", "icon-192.png"), + "512": ("icons/icon-512.png", "icon-512.png"), + } + + def _find_icons(self, static_dir: Path | None) -> dict[str, tuple[str, Path]]: + """{name: (url, file)} for each icon kind.""" + found: dict[str, tuple[str, Path]] = {} + for kind, candidates in self.ICON_FILES.items(): + hit = None + if static_dir: + for rel in candidates: + f = static_dir / rel + if f.is_file(): + hit = (self.asset(rel), f) + break + if hit is None: + rel = candidates[0] + hit = (self.chrome_asset(rel), STATIC_DIR / rel) + found[kind] = hit + return found + + def icon(self, kind: str = "svg") -> str: + """URL of the service's icon of that kind (svg, png32, apple, 192, 512).""" + return self.icons[kind][0] + + def _install_icon_routes(self, prefix: str) -> None: + """/favicon.ico and /apple-touch-icon.png, where clients look without + reading the page. Under a mount prefix both the prefixed and the root + paths are answered; a proxy that forwards only the prefix simply + never delivers the root ones.""" + png32, apple, svg = self.icons["png32"][1], self.icons["apple"][1], self.icons["svg"][1] + + async def favicon() -> FileResponse: + # A PNG at /favicon.ico is accepted by every current browser. + return FileResponse(png32 if png32.is_file() else svg, headers={"Cache-Control": "public, max-age=86400"}) + + async def touch_icon() -> FileResponse: + return FileResponse(apple, headers={"Cache-Control": "public, max-age=86400"}) + + for base in {prefix, ""}: + self.app.add_api_route(f"{base}/favicon.ico", favicon, methods=["GET"], include_in_schema=False) + self.app.add_api_route(f"{base}/apple-touch-icon.png", touch_icon, methods=["GET"], include_in_schema=False) + # --- urls --------------------------------------------------------------- def asset(self, path: str) -> str: diff --git a/chrome/static/icons/apple-touch-icon.png b/chrome/static/icons/apple-touch-icon.png new file mode 100644 index 0000000..a7ad047 Binary files /dev/null and b/chrome/static/icons/apple-touch-icon.png differ diff --git a/chrome/static/icons/favicon-32.png b/chrome/static/icons/favicon-32.png new file mode 100644 index 0000000..b9e2a71 Binary files /dev/null and b/chrome/static/icons/favicon-32.png differ diff --git a/chrome/static/icons/favicon.svg b/chrome/static/icons/favicon.svg new file mode 100644 index 0000000..20feb2f --- /dev/null +++ b/chrome/static/icons/favicon.svg @@ -0,0 +1,4 @@ + diff --git a/chrome/static/icons/icon-192.png b/chrome/static/icons/icon-192.png new file mode 100644 index 0000000..5e1422f Binary files /dev/null and b/chrome/static/icons/icon-192.png differ diff --git a/chrome/static/icons/icon-512.png b/chrome/static/icons/icon-512.png new file mode 100644 index 0000000..f32904f Binary files /dev/null and b/chrome/static/icons/icon-512.png differ diff --git a/chrome/templates/base.html b/chrome/templates/base.html index 7ca4892..7a8c99e 100644 --- a/chrome/templates/base.html +++ b/chrome/templates/base.html @@ -21,7 +21,12 @@