fix: exclude /api paths from tower SPA fallback route
Some checks failed
CI / Typecheck & Lint (pull_request) Failing after 0s

The /tower/*splat catch-all was serving index.html for all sub-paths
including /tower/api/ws, which prevented the WebSocket upgrade from
reaching the ws server. Now requests matching /tower/api/* are passed
through via next() so they can be handled by the API router or the
WebSocket server.

Fixes #36

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Alexander Whitestone
2026-03-22 22:09:48 -04:00
parent 0bdf9336bc
commit 00edc6006e

View File

@@ -79,7 +79,15 @@ const towerDist = (() => {
return path.join(process.cwd(), "the-matrix", "dist");
})();
app.use("/tower", express.static(towerDist));
app.get("/tower/*splat", (_req, res) => res.sendFile(path.join(towerDist, "index.html")));
app.get("/tower/*splat", (req, res, next) => {
// Never serve the SPA shell for requests that should hit the API or WS endpoint.
// The *splat wildcard would otherwise swallow paths like /tower/api/ws and return
// index.html, preventing the WebSocket upgrade from reaching the ws server.
const splatArr = (req.params as Record<string, string[]>)["splat"] ?? [];
const sub = splatArr.join("/");
if (sub === "api" || sub.startsWith("api/")) return next();
res.sendFile(path.join(towerDist, "index.html"));
});
// Vite builds asset references as absolute /assets/... paths.
// Mirror them at the root so the browser can load them from /tower.