From f76d35c192a153b7f27dbe2a3d67b0a67d8e85d9 Mon Sep 17 00:00:00 2001 From: Jeff Pittelkow <6314033+jpittelkow@users.noreply.github.com> Date: Thu, 26 Nov 2026 12:24:32 -0600 Subject: [PATCH] Serve the page with relative addresses so it works under a path prefix The page and its scripts asked for /static and /api by absolute path, so behind a proxy that serves WhatsOn from a sub-path (a Home Assistant ingress panel at /api/ingress/whatson/) every request went to the proxy's root instead. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 5 +++++ docs/configuration.md | 3 +++ docs/testing.md | 1 + tests/test_sources.py | 2 +- tests/test_subpath.py | 25 +++++++++++++++++++++++++ whatson/sources/jellyfin.py | 2 +- whatson/static/index.html | 4 ++-- whatson/static/js/core.js | 7 +++++-- whatson/static/js/main.js | 4 ++-- 9 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 tests/test_subpath.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 50ff95b..0180a48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ What changed in each version of WhatsOn, newest first. ## Unreleased +### Added +- **WhatsOn works under a path prefix.** The page now asks for its files and the API with relative addresses, so it + can sit behind a proxy that serves it from a sub-path, such as a Home Assistant ingress panel at + `/api/ingress/whatson/`. Opening it directly at its own address works as before. + ## 0.2.8 - 2026-11-10 ### Added diff --git a/docs/configuration.md b/docs/configuration.md index 1dfadb9..eedc99a 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -63,3 +63,6 @@ A key from the environment only goes to the provider's own address, never to `AI - WhatsOn listens on port 8430. The image sets this, not an environment variable. - `compose.yaml` uses host networking so the container can reach TVs on ports 6466 and 6467. +- WhatsOn can be served under a path prefix, for example as a Home Assistant ingress panel at `/api/ingress/whatson/`. + The page uses relative addresses for its files and the API, so nothing needs setting for it. Open it with a trailing + slash. The proxy must keep WhatsOn's own address in the `Host` header, or that name must be in `ALLOWED_HOSTS`. diff --git a/docs/testing.md b/docs/testing.md index e2c8e1f..57afd7b 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -27,6 +27,7 @@ scripts when Node.js is installed. CI also runs gitleaks over the whole history, | `tests/test_trakt.py` | Trakt token refresh and its redirect URI | | `tests/test_settings_layout.py` | Settings tabs and links into them | | `tests/test_progress_ui.py` | Wiring for search and AI progress | +| `tests/test_subpath.py` | The page uses relative addresses, so it works under a path prefix | | `tests/test_release.py` | Release notes and version checks | | `tests/test_unraid.py` | The Unraid template matches how the app runs | | `tests/test_version.py` | The status route reports the version | diff --git a/tests/test_sources.py b/tests/test_sources.py index 6029cc5..17d3b7d 100644 --- a/tests/test_sources.py +++ b/tests/test_sources.py @@ -72,7 +72,7 @@ def test_guide_channels_and_listings(): channels, airings = run(jf.load_guide(0, 1)) assert channels[0].vcn == "12" and channels[1].vcn == "13" and channels[1].thumb == "" # Channel art is used as an address directly, so the browser must only ever be pointed at WhatsOn. - assert channels[0].thumb.startswith("/api/thumb?path=%2FItems%2F") and "jf.example.com" not in channels[0].thumb + assert channels[0].thumb.startswith("api/thumb?path=%2FItems%2F") and "jf.example.com" not in channels[0].thumb assert [(a.kind, a.subtitle) for a in airings] == [("episode", "S01E22 The Pool"), ("movie", ""), ("sports", "")] assert airings[0].ends - airings[0].begins == 1800 and airings[1].year == 1995 diff --git a/tests/test_subpath.py b/tests/test_subpath.py new file mode 100644 index 0000000..ea85a73 --- /dev/null +++ b/tests/test_subpath.py @@ -0,0 +1,25 @@ +"""WhatsOn can be served under a path prefix, such as a Home Assistant ingress panel at /api/ingress/whatson/. +That only works while the page asks for its files and the API with relative URLs.""" +from __future__ import annotations + +import re +from pathlib import Path + +STATIC = Path(__file__).resolve().parent.parent / "whatson" / "static" +JS = STATIC / "js" + + +def test_the_page_loads_its_files_relatively(): + html = (STATIC / "index.html").read_text(encoding="utf-8") + assert not re.search(r'(?:src|href)="/(?!/)', html) + + +def test_direct_fetches_use_relative_urls(): + for f in JS.glob("*.js"): + assert not re.search(r"fetch\(\s*[\"'`]/(?!/)", f.read_text(encoding="utf-8")), f.name + + +def test_api_calls_and_artwork_resolve_under_a_prefix(): + core = (JS / "core.js").read_text(encoding="utf-8") + assert "fetch(rel(path)" in core + assert "`api/thumb?path=" in core diff --git a/whatson/sources/jellyfin.py b/whatson/sources/jellyfin.py index 48d0d00..1a3456a 100644 --- a/whatson/sources/jellyfin.py +++ b/whatson/sources/jellyfin.py @@ -179,7 +179,7 @@ async def load_guide(self, start: int, end: int) -> tuple[list[Channel], list[Ai art = _poster(c) channels.append(Channel(id=c["Id"], vcn=str(c.get("ChannelNumber") or c.get("Number") or c["Id"]), title=c.get("Name", ""), - thumb=f"/api/thumb?path={quote(art, safe='')}&width=120" if art else "")) + thumb=f"api/thumb?path={quote(art, safe='')}&width=120" if art else "")) programs = await self._pages("/LiveTv/Programs", MinEndDate=_iso(start), MaxStartDate=_iso(end), Fields="Overview,Genres", EnableImages="false", EnableUserData="false", **self._user()) diff --git a/whatson/static/index.html b/whatson/static/index.html index 1805a2e..9cd50e7 100644 --- a/whatson/static/index.html +++ b/whatson/static/index.html @@ -8,7 +8,7 @@ - +
@@ -271,6 +271,6 @@

Your teams

- + diff --git a/whatson/static/js/core.js b/whatson/static/js/core.js index 22d61f8..e6dec92 100644 --- a/whatson/static/js/core.js +++ b/whatson/static/js/core.js @@ -32,8 +32,11 @@ export function applyTheme(theme) { } } +/** Turns "/api/x" into "api/x", so the page works under a path prefix such as a Home Assistant ingress panel. */ +export const rel = (url) => (url.startsWith("/") && !url.startsWith("//") ? url.slice(1) : url); + export async function api(path, opts = {}) { - const res = await fetch(path, { + const res = await fetch(rel(path), { ...opts, headers: { "X-WhatsOn": "1", ...(opts.body ? { "Content-Type": "application/json" } : {}) }, body: opts.body ? JSON.stringify(opts.body) : undefined, @@ -92,7 +95,7 @@ export const fmtMins = (ms) => { return m >= 60 ? `${Math.floor(m / 60)} h ${String(m % 60).padStart(2, "0")}` : `${m} min`; }; export const thumbUrl = (path, width = 300) => - path ? `/api/thumb?path=${encodeURIComponent(path)}&width=${width}` : ""; + path ? `api/thumb?path=${encodeURIComponent(path)}&width=${width}` : ""; export const isPhone = () => window.matchMedia("(max-width: 700px)").matches; diff --git a/whatson/static/js/main.js b/whatson/static/js/main.js index 28b7e4d..41ba757 100644 --- a/whatson/static/js/main.js +++ b/whatson/static/js/main.js @@ -83,7 +83,7 @@ document.addEventListener("click", (ev) => { $("#open-settings").addEventListener("click", () => openSettings("tvs")); $("#settings").addEventListener("close", async () => { - const st = await fetch("/api/status").then((r) => r.json()).catch(() => ({})); + const st = await fetch("api/status").then((r) => r.json()).catch(() => ({})); const wasOff = !$("#connect-source").hidden; $("#connect-source").hidden = st.connected !== false; ai.on = Boolean(st.ai); @@ -111,7 +111,7 @@ initSettings(); await loadDevices().catch(() => {}); refreshNow(); // Before the first screen, so it knows whether to show that AI is working. - const st = await fetch("/api/status").then((r) => r.json()).catch(() => ({})); + const st = await fetch("api/status").then((r) => r.json()).catch(() => ({})); ai.on = Boolean(st.ai); route(); // First run: nothing works until a server is connected.