From 148062ec28b8de19e306bb717ee252a0f03d0d0a Mon Sep 17 00:00:00 2001 From: Bjoern Kalkbrenner Date: Thu, 29 Oct 2026 23:00:50 +0100 Subject: [PATCH] ApplyAppearance: cut BasePath only as a whole path segment Behind http.StripPrefix the request path is already relative, and a blind TrimPrefix turned /release-notes into "elease-notes" for BasePath /r, sending the menu back to the start page. Co-Authored-By: Claude Opus 5.5 --- appearance.go | 12 +++++++++++- appearance_test.go | 9 +++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/appearance.go b/appearance.go index b667865..5538506 100644 --- a/appearance.go +++ b/appearance.go @@ -67,7 +67,17 @@ func (h *PageHeader) ApplyAppearance(r *http.Request) { if c, err := r.Cookie(PaletteCookie); err == nil && known(Palettes, c.Value) { h.Palette = c.Value } - back := url.URL{Path: strings.TrimPrefix(r.URL.Path, h.BasePath), RawQuery: r.URL.RawQuery} + // Only a whole leading BasePath segment is cut: behind + // http.StripPrefix the path is already relative, and "/r" must not + // eat the start of "/release-notes". + path := r.URL.Path + if h.BasePath != "" && (path == h.BasePath || strings.HasPrefix(path, h.BasePath+"/")) { + path = strings.TrimPrefix(path, h.BasePath) + } + if path == "" { + path = "/" + } + back := url.URL{Path: path, RawQuery: r.URL.RawQuery} h.AppearanceBack = back.String() } diff --git a/appearance_test.go b/appearance_test.go index 94d6cd6..3d64fdf 100644 --- a/appearance_test.go +++ b/appearance_test.go @@ -142,6 +142,15 @@ func TestApplyAppearance(t *testing.T) { if h.AppearanceBack != "/tasks?f=1" { t.Errorf("AppearanceBack = %q, want %q", h.AppearanceBack, "/tasks?f=1") } + + // Behind http.StripPrefix the path is already relative to BasePath. + for path, want := range map[string]string{"/release-notes": "/release-notes", "/r": "/", "/r/x": "/x"} { + h := PageHeader{BasePath: "/r"} + h.ApplyAppearance(httptest.NewRequest(http.MethodGet, path, nil)) + if h.AppearanceBack != want { + t.Errorf("BasePath /r, path %s: AppearanceBack = %q, want %q", path, h.AppearanceBack, want) + } + } } func TestAppearanceHandler(t *testing.T) {