FastAPI's StaticFiles at '/' silently blocks all API routes when dist folder exists
A developer discovered that mounting StaticFiles with html=True at the root path '/' in FastAPI causes all API routes to become unreachable once the static directory is populated. The issue stems from Starlette's routing priority, where mounts at '/' are matched before route handlers, and the html=True option serves index.html for any unrecognized path instead of passing the request along. The bug is particularly deceptive because it does not trigger any errors or 404 responses, and only surfaces in production when the dist folder actually contains files. The recommended fix is to mount static files only at a specific sub-path like '/assets', then add an explicit catch-all route at the end of the router to serve index.html for frontend paths. This approach makes routing priority transparent in the code and ensures API endpoints are always resolved before the SPA fallback.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in