diff --git a/clients/ui/bff/internal/api/app.go b/clients/ui/bff/internal/api/app.go index bc8fe0fa..d9210a1c 100644 --- a/clients/ui/bff/internal/api/app.go +++ b/clients/ui/bff/internal/api/app.go @@ -8,6 +8,7 @@ import ( "github.com/kubeflow/model-registry/ui/bff/internal/repositories" "log/slog" "net/http" + "path" "github.com/julienschmidt/httprouter" "github.com/kubeflow/model-registry/ui/bff/internal/mocks" @@ -117,13 +118,24 @@ func (app *App) Routes() http.Handler { // App Router appMux := http.NewServeMux() + // handler for api calls appMux.Handle("/api/v1/", apiRouter) - // file server for the frontend - staticAccessDir := http.Dir(app.config.StaticAssetsDir) - fileServer := http.FileServer(staticAccessDir) - appMux.Handle("/", fileServer) + // file server for the frontend file and SPA routes + staticDir := http.Dir(app.config.StaticAssetsDir) + fileServer := http.FileServer(staticDir) + appMux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + // Check if the requested file exists + if _, err := staticDir.Open(r.URL.Path); err == nil { + // Serve the file if it exists + fileServer.ServeHTTP(w, r) + return + } + + // Fallback to index.html for SPA routes + http.ServeFile(w, r, path.Join(app.config.StaticAssetsDir, "index.html")) + }) return app.RecoverPanic(app.enableCORS(app.InjectUserHeaders(appMux))) } diff --git a/clients/ui/bff/internal/api/app_test.go b/clients/ui/bff/internal/api/app_test.go index c03afb0b..bce813dd 100644 --- a/clients/ui/bff/internal/api/app_test.go +++ b/clients/ui/bff/internal/api/app_test.go @@ -62,12 +62,17 @@ var _ = Describe("Static File serving Test", func() { Expect(string(body)).To(ContainSubstring("BFF Stub Subfolder Page")) }) - It("should return 404 for a non-existent static file", func() { + It("should return index.html for a non-existent static file", func() { resp, err := client.Get(server.URL + "/non-existent.html") Expect(err).NotTo(HaveOccurred()) defer resp.Body.Close() - Expect(resp.StatusCode).To(Equal(http.StatusNotFound)) + Expect(resp.StatusCode).To(Equal(http.StatusOK)) + + body, err := io.ReadAll(resp.Body) + Expect(err).NotTo(HaveOccurred()) + //BFF Stub page is the context of index.html + Expect(string(body)).To(ContainSubstring("BFF Stub Page")) }) })