Fix the feedback URL and stop serving a UI when WEB_STATIC=0

VITE_GATEWAY_URL names a WebSocket origin, and FEEDBACK_URL was derived
from it verbatim - so with VITE_GATEWAY_URL=wss://gateway.example the
form posted to wss://gateway.example/api/feedback, which fetch() refuses
outright ("TypeError: unknown scheme"). Every submission from a
separately hosted frontend failed before it left the browser. Map the
scheme back to http(s); an already-http VITE_GATEWAY_URL is untouched.

While there: WEB_STATIC=0 answered every path with the dev hint page,
which talks about localhost:5173 and npm run dev. That page is wrong for
a gateway that is reachable publicly, and an SPA-shaped answer on every
path makes the host look like a second, stale copy of the app. Serve a
short deployment-neutral notice at "/" and 404 everywhere else; the dev
hint stays for the case it is actually about, a missing web/dist.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
MoepchiandClaude Opus 5 committed 2026-09-11 12:39:50 +00:00
1 parent 2ee6d1f0b5
commit cc715e24b2
2 files changed
+28 -4

No files matched your search

+23 -2
View File
@@ -245,12 +245,25 @@ async function handleFeedback(req: IncomingMessage, res: ServerResponse) {
const DEV_HINT = `<!doctype html><html><body style="font:14px system-ui;padding:2rem;max-width:40rem">
<h1>WebSpeak3 gateway</h1>
<p>WebSocket: <code>/ws</code></p>
<p>Static UI is disabled (<code>WEB_STATIC=0</code>) or <code>web/dist</code> is missing.</p>
<p><code>web/dist</code> is missing — the UI has not been built yet.</p>
<p>For local development open the Vite app at <a href="http://localhost:5173/">http://localhost:5173/</a>
(run <code>npm run dev</code> in <code>web/</code>). Rebuild with <code>npm run build</code> in <code>web/</code>
to serve the UI from this port again.</p>
</body></html>`;
/**
* Shown at "/" when WEB_STATIC=0, i.e. when this process deliberately is not
* the app server. Deployment-neutral on purpose: where the UI actually lives
* is the operator's business, and this host may well be reachable publicly.
*/
const NO_UI_NOTICE = `<!doctype html><html lang="en"><head><meta charset="utf-8">
<title>WebSpeak3 gateway</title></head>
<body style="font:14px system-ui;padding:2rem;max-width:40rem">
<h1>WebSpeak3 gateway</h1>
<p>WebSocket endpoint: <code>/ws</code></p>
<p>No web UI is served from this host.</p>
</body></html>`;
/**
* TLS material, when configured.
*
@@ -280,8 +293,16 @@ const requestHandler = (req: IncomingMessage, res: ServerResponse) => {
}
if (!SERVE_STATIC) {
// No SPA fallback here: with the UI hosted elsewhere, answering every
// path with a page would put a second, stale-looking "app" on this
// hostname. Only "/" gets a notice, everything else is simply absent.
if (requestUrl.pathname !== "/") {
res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
res.end("Not found\n");
return;
}
res.writeHead(200, { "Content-Type": "text/html; charset=utf-8" });
res.end(DEV_HINT);
res.end(NO_UI_NOTICE);
return;
}
let filePath = path.join(WEB_DIST, decodeURIComponent(requestUrl.pathname));
+5 -2
View File
@@ -86,11 +86,14 @@ const GATEWAY_URL = import.meta.env.DEV
// Same derivation as GATEWAY_URL above, but for a plain HTTP POST (the
// feedback form) instead of the WebSocket - see gateway/src/index.ts's
// /api/feedback handler.
// /api/feedback handler. VITE_GATEWAY_URL names a WebSocket origin, so its
// scheme has to be mapped back to http(s) here: fetch() refuses anything
// else outright ("TypeError: unknown scheme"), so with VITE_GATEWAY_URL set
// the form could not submit at all.
const FEEDBACK_URL = import.meta.env.DEV
? "http://localhost:8080/api/feedback"
: import.meta.env.VITE_GATEWAY_URL
? `${import.meta.env.VITE_GATEWAY_URL.replace(/\/$/, "")}/api/feedback`
? `${import.meta.env.VITE_GATEWAY_URL.replace(/\/$/, "").replace(/^ws(s?):\/\//, "http$1://")}/api/feedback`
: `${window.location.protocol}//${window.location.host}/api/feedback`;
// This file's frontend is deployed as multiple Cloudflare Pages projects