Commit b0db3e1

Eric Bower  ·  2026-05-11 09:50:51 -0400 EDT
parent 1864102
fix(pgs): _headers should override default cache-control

Closes: https://github.com/picosh/pico/issues/215
2 files changed,  +36, -8
+6, -1
......@@ -282,7 +282,12 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
282282 w.Header().Set("cache-control", cc)
283283
284284 for _, hdr := range userHeaders {
285- w.Header().Add(hdr.Name, hdr.Value)
285+ // Use Set() for cache-control to override the middleware default
286+ if strings.EqualFold(hdr.Name, "cache-control") {
287+ w.Header().Set(hdr.Name, hdr.Value)
288+ } else {
289+ w.Header().Add(hdr.Name, hdr.Value)
290+ }
286291 }
287292 if w.Header().Get("content-type") == "" {
288293 w.Header().Set("content-type", contentType)
+30, -7
......@@ -106,13 +106,14 @@ func (t *testStorage) GetObject(bucket storage.Bucket, fpath string) (utils.Read
106106 }
107107
108108 type ApiExample struct {
109- name string
110- path string
111- reqHeaders map[string]string
112- want string
113- wantUrl string
114- status int
115- contentType string
109+ name string
110+ path string
111+ reqHeaders map[string]string
112+ want string
113+ wantUrl string
114+ wantCacheCtrl string
115+ status int
116+ contentType string
116117
117118 storage map[string]map[string]string
118119 }
......@@ -396,6 +397,21 @@ func TestApiBasic(t *testing.T) {
396397 },
397398 },
398399 },
400+ {
401+ name: "headers-cache-control-override",
402+ path: "/test.html",
403+ want: "hello world!",
404+ status: http.StatusOK,
405+ contentType: "text/html",
406+ wantCacheCtrl: "public, max-age=31536000, immutable",
407+
408+ storage: map[string]map[string]string{
409+ bucketName: {
410+ "/test/test.html": "hello world!",
411+ "/test/_headers": "/*\n\tcache-control: public, max-age=31536000, immutable",
412+ },
413+ },
414+ },
399415 }
400416
401417 for _, tc := range tt {
......@@ -447,6 +463,13 @@ func TestApiBasic(t *testing.T) {
447463 t.Errorf("Want '%s', got '%s'", tc.wantUrl, location.String())
448464 }
449465 }
466+
467+ if tc.wantCacheCtrl != "" {
468+ cc := responseRecorder.Header().Get("cache-control")
469+ if cc != tc.wantCacheCtrl {
470+ t.Errorf("Want cache-control '%s', got '%s'", tc.wantCacheCtrl, cc)
471+ }
472+ }
450473 })
451474 }
452475 }