Icons: a service's own set or chrome's generic one, and /favicon.ico answered
The head linked `icons/favicon.svg` under the service's static files and nothing else, so a service that kept its icon elsewhere (scribe) or had none (agent-bridge) showed no icon at all, and clients that ignore SVG (Safari, bookmark bars) or ask /favicon.ico without reading the page got a 404 everywhere. Chrome now resolves five icon kinds - svg, 32 px png, the Apple touch icon, 192 and 512 - from the service's static dir with a few accepted paths each, falling back per kind to a generic set shipped here, links svg + png + touch icon in the head, and answers /favicon.ico and /apple-touch-icon.png at the root and under the mount prefix. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
parent
039b6f3711
commit
16852fffa8
9 changed files with 99 additions and 2 deletions
|
|
@ -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:
|
||||
|
|
|
|||
BIN
chrome/static/icons/apple-touch-icon.png
Normal file
BIN
chrome/static/icons/apple-touch-icon.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.4 KiB |
BIN
chrome/static/icons/favicon-32.png
Normal file
BIN
chrome/static/icons/favicon-32.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 640 B |
4
chrome/static/icons/favicon.svg
Normal file
4
chrome/static/icons/favicon.svg
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
|
||||
<rect width="64" height="64" rx="14" fill="#475569"/>
|
||||
<path d="M20 40a9 9 0 0 1 1.6-17.9A12 12 0 0 1 44.5 25a7.5 7.5 0 0 1 .5 15z" fill="#fff"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 217 B |
BIN
chrome/static/icons/icon-192.png
Normal file
BIN
chrome/static/icons/icon-192.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
BIN
chrome/static/icons/icon-512.png
Normal file
BIN
chrome/static/icons/icon-512.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
|
|
@ -21,7 +21,12 @@
|
|||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{% block title %}{{ title }}{% endblock %}</title>
|
||||
<link rel="icon" href="{{ asset('icons/favicon.svg') }}" type="image/svg+xml">
|
||||
{# The service's icons, or chrome's generic set for what it lacks (see
|
||||
Chrome._find_icons). SVG for Chromium and Firefox tabs, PNG for Safari
|
||||
and bookmark bars, the Apple touch icon for home screens. #}
|
||||
<link rel="icon" href="{{ icon('svg') }}" type="image/svg+xml">
|
||||
<link rel="icon" href="{{ icon('png32') }}" type="image/png" sizes="32x32">
|
||||
<link rel="apple-touch-icon" href="{{ icon('apple') }}" sizes="180x180">
|
||||
|
||||
<script>
|
||||
// Inline and first: anything that defers this reintroduces the flash.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
[project]
|
||||
name = "chrome"
|
||||
version = "0.6.0"
|
||||
version = "0.7.0"
|
||||
description = "Shared page shell, design tokens and service helpers for the internal services."
|
||||
requires-python = ">=3.13"
|
||||
dependencies = [
|
||||
|
|
|
|||
|
|
@ -392,3 +392,31 @@ def test_a_service_can_keep_its_own_health_probe(tmp_path):
|
|||
return {"ok": True, "engine": "reachable"}
|
||||
|
||||
assert TestClient(app).get("/healthz").json() == {"ok": True, "engine": "reachable"}
|
||||
|
||||
|
||||
def test_icons_fall_back_to_chromes_and_are_served(tmp_path: Path) -> None:
|
||||
from fastapi import FastAPI
|
||||
# a service with no icons of its own
|
||||
app = FastAPI()
|
||||
ui = Chrome(app, service="bare", version="0", title="Bare", mount_prefix="/ui")
|
||||
c = TestClient(app)
|
||||
assert ui.icon("svg").startswith("/ui/chrome-static/icons/favicon.svg") or "chrome" in ui.icon("svg")
|
||||
for path in ("/favicon.ico", "/ui/favicon.ico"):
|
||||
r = c.get(path); assert r.status_code == 200 and r.headers["content-type"] == "image/png"
|
||||
r = c.get("/ui/apple-touch-icon.png"); assert r.status_code == 200 and len(r.content) > 100
|
||||
# a service with icons under static/icons/ gets its own, and chrome's for what it lacks
|
||||
static = tmp_path / "static"; (static / "icons").mkdir(parents=True)
|
||||
(static / "icons" / "favicon.svg").write_text("<svg xmlns='http://www.w3.org/2000/svg'/>")
|
||||
(static / "icons" / "favicon-32.png").write_bytes(b"\x89PNG\r\n\x1a\n" + b"\0" * 40)
|
||||
app2 = FastAPI()
|
||||
ui2 = Chrome(app2, service="own", version="0", title="Own", static_dir=static, templates_dir=None)
|
||||
c2 = TestClient(app2)
|
||||
assert ui2.icon("svg").startswith("/static/icons/favicon.svg?v=") and ui2.icon("png32").startswith("/static/icons/favicon-32.png")
|
||||
assert ui2.icon("apple").startswith("/chrome/icons/apple-touch-icon.png") # not shipped by the service: chrome's
|
||||
assert c2.get("/favicon.ico").content.startswith(b"\x89PNG")
|
||||
# the page links all three
|
||||
@app2.get("/")
|
||||
def index(request: Request):
|
||||
return ui2.render(request, "base.html")
|
||||
html = c2.get("/").text
|
||||
assert 'rel="icon" href="/static/icons/favicon.svg?v=' in html and 'type="image/png" sizes="32x32"' in html and 'rel="apple-touch-icon"' in html
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue