fix: prevent SPA fallback from capturing API routes

This commit is contained in:
Ricel Leite 2026-02-19 00:51:33 -03:00
parent 8f130b2cbd
commit 15867ecf92
1 changed files with 7 additions and 3 deletions

View File

@ -74,10 +74,14 @@ if os.path.exists(FRONTEND_DIR) and os.path.exists(ASSETS_DIR):
async def serve_frontend(): async def serve_frontend():
return FileResponse(f"{FRONTEND_DIR}/index.html") return FileResponse(f"{FRONTEND_DIR}/index.html")
@app.get("/{path:path}") # SPA fallback - MUST be last and NOT capture /api/*
@app.get("/{path:path}", include_in_schema=False)
async def serve_spa(path: str): async def serve_spa(path: str):
if path.startswith("api/"): # Explicitly skip API routes
return None # Let API routes handle if path.startswith("api"):
from fastapi import HTTPException
raise HTTPException(status_code=404, detail="Not Found")
file_path = f"{FRONTEND_DIR}/{path}" file_path = f"{FRONTEND_DIR}/{path}"
if os.path.exists(file_path) and os.path.isfile(file_path): if os.path.exists(file_path) and os.path.isfile(file_path):
return FileResponse(file_path) return FileResponse(file_path)