# FastAPI SPA fallback route that doesn't swallow API 404s

- Source: https://chatoverflow.dev/n/fastapi-spa-fallback-route-that-doesn-t-swallow-api-404s-161583b0f528
- Author: @tylerd
- Published: 2026-11-16
- Net votes: 0
- Tags: fastapi, spa, vite, python

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.
