Răsfoiți Sursa

Send Cache-Control: no-cache for web UI files

Static web UI files, theme.css and custom-webui/ were served with
Last-Modified but no Cache-Control, so browsers cached them
heuristically and could keep an old UI after an upgrade. no-cache makes
them revalidate; unchanged files still return 304.

Fixes #1164

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Damian Kujtkowski 23 ore în urmă
părinte
comite
2a7efa6cd9

+ 16 - 1
service/internal/httpservers/webuiServer.go

@@ -34,7 +34,16 @@ func NewWebUIServer(cfg *config.Config) *webUIServer {
 	return s
 }
 
+// Web UI files keep their names between releases, and some replacement UIs
+// (webUIDir) do not hash their asset names at all. Without Cache-Control,
+// browsers cache responses that only carry Last-Modified heuristically, and
+// can keep an old UI after an upgrade. no-cache makes them revalidate every
+// time; unchanged files still come back as a cheap 304.
+const webuiCacheControl = "no-cache"
+
 func (s *webUIServer) handleWebui(w http.ResponseWriter, r *http.Request) {
+	w.Header().Set("Cache-Control", webuiCacheControl)
+
 	// Mangle requests for any path like /logs or /config to load the webui index.html
 	if path.Ext(r.URL.Path) == "" && r.URL.Path != "/" {
 		log.Debugf("Mangling request for %s to /index.html", r.URL.Path)
@@ -119,6 +128,7 @@ func (s *webUIServer) generateThemeCss(w http.ResponseWriter, r *http.Request) {
 	}
 
 	w.Header().Add("Content-Type", "text/css")
+	w.Header().Set("Cache-Control", webuiCacheControl)
 	_, err := w.Write(customThemeCss)
 	if err != nil {
 		log.WithFields(log.Fields{
@@ -128,5 +138,10 @@ func (s *webUIServer) generateThemeCss(w http.ResponseWriter, r *http.Request) {
 }
 
 func (s *webUIServer) handleCustomWebui() http.Handler {
-	return http.StripPrefix("/custom-webui/", http.FileServer(http.Dir(s.findCustomWebuiDir())))
+	fs := http.StripPrefix("/custom-webui/", http.FileServer(http.Dir(s.findCustomWebuiDir())))
+
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		w.Header().Set("Cache-Control", webuiCacheControl)
+		fs.ServeHTTP(w, r)
+	})
 }

+ 29 - 1
service/internal/httpservers/webuiServer_test.go

@@ -1,3 +1,31 @@
 package httpservers
 
-import ()
+import (
+	"net/http"
+	"net/http/httptest"
+	"os"
+	"path/filepath"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
+)
+
+func TestHandleWebuiSetsCacheControl(t *testing.T) {
+	t.Parallel()
+
+	dir := t.TempDir()
+	require.NoError(t, os.WriteFile(filepath.Join(dir, "index.html"), []byte("<!doctype html>"), 0o600))
+	require.NoError(t, os.WriteFile(filepath.Join(dir, "app.js"), []byte("// app"), 0o600))
+
+	s := &webUIServer{webuiDir: dir}
+
+	for _, p := range []string{"/", "/app.js", "/logs"} {
+		rec := httptest.NewRecorder()
+		s.handleWebui(rec, httptest.NewRequest(http.MethodGet, p, nil))
+
+		assert.Equal(t, http.StatusOK, rec.Code, p)
+		assert.Equal(t, "no-cache", rec.Header().Get("Cache-Control"), p)
+		assert.NotEmpty(t, rec.Header().Get("Last-Modified"), p)
+	}
+}