fix: use catch-all GET not mount for SPA, preserve WS routes

This commit is contained in:
Zac Gaetano 2026-04-05 11:07:44 -04:00
parent 67d738baf3
commit 54c1e9d92c

View file

@ -990,6 +990,10 @@ STATIC_DIR = Path("/app/static")
if not STATIC_DIR.exists():
STATIC_DIR = Path("/app/frontend/dist")
# Mount /assets for Vite-built JS/CSS bundles
if STATIC_DIR.exists() and (STATIC_DIR / "assets").exists():
app.mount("/assets", StaticFiles(directory=str(STATIC_DIR / "assets")), name="assets")
@app.get("/")
async def serve_index():
@ -999,7 +1003,17 @@ async def serve_index():
return {"message": "Claude Persistent Agent API v3.0", "docs": "/docs"}
# Mount static assets AFTER all API/WS routes are registered
# This ensures /api/* and WebSocket routes take priority
if STATIC_DIR.exists():
app.mount("/", StaticFiles(directory=str(STATIC_DIR), html=True), name="spa")
@app.get("/{full_path:path}")
async def serve_spa(full_path: str):
"""Serve React SPA for all non-API routes. API and WS are handled above."""
if full_path.startswith("api"):
raise HTTPException(404)
# Try serving the file directly (e.g. vite.svg, favicon)
file_path = STATIC_DIR / full_path
if file_path.is_file():
return FileResponse(str(file_path))
# Fall back to index.html for SPA routing
index = STATIC_DIR / "index.html"
if index.exists():
return FileResponse(str(index))
raise HTTPException(404)