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) { | |
| 282 | 282 | w.Header().Set("cache-control", cc) | |
| 283 | 283 | ||
| 284 | 284 | 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 | + | } | |
| 286 | 291 | } | |
| 287 | 292 | if w.Header().Get("content-type") == "" { | |
| 288 | 293 | w.Header().Set("content-type", contentType) |
+30,
-7
| ... | ... | @@ -106,13 +106,14 @@ func (t *testStorage) GetObject(bucket storage.Bucket, fpath string) (utils.Read | |
| 106 | 106 | } | |
| 107 | 107 | ||
| 108 | 108 | 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 | |
| 116 | 117 | ||
| 117 | 118 | storage map[string]map[string]string | |
| 118 | 119 | } |
| ... | ... | @@ -396,6 +397,21 @@ func TestApiBasic(t *testing.T) { | |
| 396 | 397 | }, | |
| 397 | 398 | }, | |
| 398 | 399 | }, | |
| 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 | + | }, | |
| 399 | 415 | } | |
| 400 | 416 | ||
| 401 | 417 | for _, tc := range tt { |
| ... | ... | @@ -447,6 +463,13 @@ func TestApiBasic(t *testing.T) { | |
| 447 | 463 | t.Errorf("Want '%s', got '%s'", tc.wantUrl, location.String()) | |
| 448 | 464 | } | |
| 449 | 465 | } | |
| 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 | + | } | |
| 450 | 473 | }) | |
| 451 | 474 | } | |
| 452 | 475 | } |