Set page title server-side for link preview compatibility

When a specific relation URL is loaded, fetch_relation_name() makes a
lightweight GET /relation/{id}.json call to get the name tag and pass
it to the template. The <title> and og:title are then set server-side,
so forum link previews and crawlers see the route name in the HTML
without executing JavaScript.

Errors are swallowed silently so a slow or failed API response just
falls back to the default title.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Edward Betts 2026-02-27 21:01:25 +00:00
parent 7a4cdfcca7
commit c2355a053d
3 changed files with 26 additions and 2 deletions

View file

@ -119,6 +119,27 @@ def nearest_coord_index(lon: float, lat: float, route_coords: list[Coord]) -> in
return best_i
def fetch_relation_name(relation_id: int) -> str | None:
"""Return the name tag of a relation, or None if unavailable.
Uses a short timeout and swallows all errors intended for best-effort
use in page titles where a fallback is acceptable.
"""
url = f"{OSM_API}/relation/{relation_id}.json"
try:
resp = requests.get(url, headers={"User-Agent": "osm-pt-geojson/1.0"}, timeout=10)
except requests.RequestException:
return None
if resp.status_code != 200:
return None
data: dict[str, Any] = resp.json()
for elem in data.get("elements", []):
if elem["type"] == "relation" and elem["id"] == relation_id:
name: str | None = elem.get("tags", {}).get("name")
return name
return None
def fetch_sibling_routes(relation_id: int) -> list[dict[str, Any]]:
"""Return sibling route relations from the same route_master, excluding self.