From f01b28aad934e21dcfa2e1081e2b2104550cf8ef Mon Sep 17 00:00:00 2001 From: Bryan Frimin Date: Wed, 18 Mar 2026 17:21:33 +0100 Subject: [PATCH] Buffer renderer output before sending HTTP 200 Render dynamic file content into a bytes.Buffer first so that renderer errors return 500 instead of a partial 200 response. Signed-off-by: Bryan Frimin --- pkg/server/statichandler/statichandler.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pkg/server/statichandler/statichandler.go b/pkg/server/statichandler/statichandler.go index 830cfa895..39422abc4 100644 --- a/pkg/server/statichandler/statichandler.go +++ b/pkg/server/statichandler/statichandler.go @@ -16,6 +16,7 @@ package statichandler import ( + "bytes" "compress/gzip" "crypto/md5" "encoding/hex" @@ -144,8 +145,14 @@ func (s *Server) serveIndex(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") if renderer, ok := s.fileRenderers["/index.html"]; ok { + var buf bytes.Buffer + if err := renderer(&buf, r); err != nil { + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusOK) - _ = renderer(w, r) + _, _ = w.Write(buf.Bytes()) return } @@ -183,10 +190,16 @@ func (s *Server) ServeSPA(w http.ResponseWriter, r *http.Request) { } if renderer, ok := s.fileRenderers[path]; ok { + var buf bytes.Buffer + if err := renderer(&buf, r); err != nil { + http.Error(w, "Internal Server Error", http.StatusInternalServerError) + return + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate") w.WriteHeader(http.StatusOK) - _ = renderer(w, r) + _, _ = w.Write(buf.Bytes()) return }