saap-plattform / backend /spa_static_files.py
Hwandji's picture
fix(backend): Correct SPA static files mounting order
582752d
"""
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"))