Besjon Cifliku commited on
Commit
bf9e9a4
·
1 Parent(s): db764ae

fix: build frontend serve files fails 404

Browse files
Files changed (2) hide show
  1. frontend/vite.config.ts +1 -0
  2. server.py +15 -12
frontend/vite.config.ts CHANGED
@@ -3,6 +3,7 @@ import react from "@vitejs/plugin-react";
3
 
4
  export default defineConfig({
5
  plugins: [react()],
 
6
  server: {
7
  proxy: {
8
  "/api/logs/stream": {
 
3
 
4
  export default defineConfig({
5
  plugins: [react()],
6
+ base: "./",
7
  server: {
8
  proxy: {
9
  "/api/logs/stream": {
server.py CHANGED
@@ -768,18 +768,21 @@ def _serialize_analysis(analysis):
768
  _FRONTEND_DIR = BASE_DIR / "frontend" / "dist"
769
 
770
  if _FRONTEND_DIR.is_dir():
771
- # Serve static assets (JS, CSS, images)
772
- app.mount("/assets", StaticFiles(directory=_FRONTEND_DIR / "assets"), name="static-assets")
773
-
774
- @app.get("/{full_path:path}")
775
- async def serve_frontend(full_path: str):
776
- """Serve the React SPA for any non-API route."""
777
- # Try to serve the exact file first (e.g. favicon.ico)
778
- file_path = _FRONTEND_DIR / full_path
779
- if full_path and file_path.is_file() and file_path.is_relative_to(_FRONTEND_DIR):
780
- return FileResponse(file_path)
781
- # Otherwise serve index.html (SPA client-side routing)
782
- return FileResponse(_FRONTEND_DIR / "index.html")
 
 
 
783
 
784
 
785
  if __name__ == "__main__":
 
768
  _FRONTEND_DIR = BASE_DIR / "frontend" / "dist"
769
 
770
  if _FRONTEND_DIR.is_dir():
771
+ from starlette.exceptions import HTTPException as _StarletteHTTPException
772
+
773
+ class _SPAStaticFiles(StaticFiles):
774
+ """StaticFiles with SPA fallback: unknown paths serve index.html."""
775
+
776
+ async def get_response(self, path, scope):
777
+ try:
778
+ return await super().get_response(path, scope)
779
+ except _StarletteHTTPException as e:
780
+ if e.status_code == 404:
781
+ return await super().get_response(".", scope)
782
+ raise
783
+
784
+ app.mount("/", _SPAStaticFiles(directory=str(_FRONTEND_DIR), html=True), name="frontend")
785
+ logger.info("Mounted frontend from %s", _FRONTEND_DIR)
786
 
787
 
788
  if __name__ == "__main__":