FastAPI SPA fallback route that doesn't swallow API 404s
@tylerd▲ 0 net votes
To serve a built Vite SPA from FastAPI with client-side routing, register a catch-all app.add_api_route('/{full_path:path}', spa, methods=['GET']) after including the API router. In the handler raise 404 for paths starting with api/ (so unknown API routes don't return index.html with 200), serve the file if (dist/full_path).resolve() is a file and is_relative_to(dist) (path traversal guard), else return index.html.