Skip to content

Commit

Permalink
404 on Refresh Fix (#830)
Browse files Browse the repository at this point in the history
* Added Generic UI Mapping

The following solution is adaption of https://stackoverflow.com/a/64687181 to provide default route to ui app if the file server does not contain given path. The file server returns 404 path if requested path does not correspond to any files in its filesystem. Because of that while routing request to our single page app (e.g. path "/instances/{id}") the request was handled by file server and not by the app itself (which corresponds to `index.html`). The solution routes existing paths, like "/static/*", to the file server and non existing, like "/instances/{id}", to the app frontend.

* Linter
  • Loading branch information
ralikio authored and kyma-gopher-bot committed Aug 22, 2024
1 parent a3ffa82 commit 057e88e
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"log/slog"
"net/http"
"os"
"path"
"strings"
"time"

Expand Down Expand Up @@ -74,9 +76,20 @@ func (a *API) AttachRoutes(router *http.ServeMux) {
router.HandleFunc("GET /api/service-bindings/{id}", a.GetServiceBinding)
router.HandleFunc("POST /api/service-bindings", a.CreateServiceBinding)
router.HandleFunc("DELETE /api/service-bindings/{id}", a.DeleteServiceBinding)
router.Handle("GET /offerings", http.StripPrefix("/offerings", http.FileServer(a.frontendFS)))
router.Handle("GET /instances", http.StripPrefix("/instances", http.FileServer(a.frontendFS)))
router.Handle("GET /", http.FileServer(a.frontendFS))

router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
fullPath := strings.TrimPrefix(path.Clean(r.URL.Path), "/")
_, err := a.frontendFS.Open(fullPath)
if err != nil {
if !os.IsNotExist(err) {
panic(err)
}
r.URL.Path = "/"
}
}
http.FileServer(a.frontendFS).ServeHTTP(w, r)
})
}

func (a *API) Address() string {
Expand Down

0 comments on commit 057e88e

Please sign in to comment.