Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: base href does not work #11401 #10194 #2134 #11644

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions server/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,22 @@ type FilesServer struct {
hsts bool
xframeOpts string
corsAllowOrigin string
Hash func(string) string
}

func NewFilesServer(baseHRef string, hsts bool, xframeOpts string, corsAllowOrigin string) *FilesServer {
return &FilesServer{baseHRef, hsts, xframeOpts, corsAllowOrigin}
return &FilesServer{baseHRef, hsts, xframeOpts, corsAllowOrigin, Hash}
}

func (s *FilesServer) ServerFiles(w http.ResponseWriter, r *http.Request) {
if s.baseHRef != "/" {
p := strings.TrimPrefix(r.URL.Path, s.baseHRef)
if len(p) < len(r.URL.Path) {
r.URL.Path = p
}
}
// If there is no stored static file, we'll redirect to the js app
if Hash(strings.TrimLeft(r.URL.Path, "/")) == "" {
if s.Hash(strings.TrimLeft(r.URL.Path, "/")) == "" {
r.URL.Path = "index.html"
}

Expand Down
28 changes: 28 additions & 0 deletions server/static/static_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package static

import (
"net/http"
"net/http/httptest"
"testing"
)

func TestServeFile(t *testing.T) {
t.Run("should handle base href for static file", func(t *testing.T) {
filesServer := NewFilesServer("/argotest/", false, "", "")
filesServer.Hash = func(s string) string {
resources := map[string]string{
"main.js": "main.js",
}
return resources[s]
}

req := httptest.NewRequest(http.MethodGet, "/argotest/main.js", nil)
w := httptest.NewRecorder()

filesServer.ServerFiles(w, req)

if req.URL.Path != "main.js" {
t.Errorf("should handle base href, expect %v, got %v", "main.js", req.URL.Path)
}
})
}
Loading