""" SPA Static Files Handler for FastAPI Serves Vue.js SPA while preserving API routes """ from fastapi.staticfiles import StaticFiles from starlette.responses import FileResponse from starlette.types import Scope import os class SPAStaticFiles(StaticFiles): """ Custom StaticFiles that serves index.html for non-API routes (SPA fallback) while allowing API routes to pass through """ async def get_response(self, path: str, scope: Scope): """ Handle requests: - Static files (exist) → serve file - Everything else → index.html (SPA routing) NOTE: This should be mounted AFTER API routes in main.py so API routes are handled first """ try: # Try to serve the file return await super().get_response(path, scope) except Exception: # File not found → serve index.html for SPA routing return FileResponse(os.path.join(self.directory, "index.html"))