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
M pkg/apps/pgs/web_asset_handler.go
+6, -1
 1@@ -282,7 +282,12 @@ func (h *ApiAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 2 	w.Header().Set("cache-control", cc)
 3 
 4 	for _, hdr := range userHeaders {
 5-		w.Header().Add(hdr.Name, hdr.Value)
 6+		// Use Set() for cache-control to override the middleware default
 7+		if strings.EqualFold(hdr.Name, "cache-control") {
 8+			w.Header().Set(hdr.Name, hdr.Value)
 9+		} else {
10+			w.Header().Add(hdr.Name, hdr.Value)
11+		}
12 	}
13 	if w.Header().Get("content-type") == "" {
14 		w.Header().Set("content-type", contentType)
M pkg/apps/pgs/web_test.go
+30, -7
 1@@ -106,13 +106,14 @@ func (t *testStorage) GetObject(bucket storage.Bucket, fpath string) (utils.Read
 2 }
 3 
 4 type ApiExample struct {
 5-	name        string
 6-	path        string
 7-	reqHeaders  map[string]string
 8-	want        string
 9-	wantUrl     string
10-	status      int
11-	contentType string
12+	name          string
13+	path          string
14+	reqHeaders    map[string]string
15+	want          string
16+	wantUrl       string
17+	wantCacheCtrl string
18+	status        int
19+	contentType   string
20 
21 	storage map[string]map[string]string
22 }
23@@ -396,6 +397,21 @@ func TestApiBasic(t *testing.T) {
24 				},
25 			},
26 		},
27+		{
28+			name:          "headers-cache-control-override",
29+			path:          "/test.html",
30+			want:          "hello world!",
31+			status:        http.StatusOK,
32+			contentType:   "text/html",
33+			wantCacheCtrl: "public, max-age=31536000, immutable",
34+
35+			storage: map[string]map[string]string{
36+				bucketName: {
37+					"/test/test.html": "hello world!",
38+					"/test/_headers":  "/*\n\tcache-control: public, max-age=31536000, immutable",
39+				},
40+			},
41+		},
42 	}
43 
44 	for _, tc := range tt {
45@@ -447,6 +463,13 @@ func TestApiBasic(t *testing.T) {
46 					t.Errorf("Want '%s', got '%s'", tc.wantUrl, location.String())
47 				}
48 			}
49+
50+			if tc.wantCacheCtrl != "" {
51+				cc := responseRecorder.Header().Get("cache-control")
52+				if cc != tc.wantCacheCtrl {
53+					t.Errorf("Want cache-control '%s', got '%s'", tc.wantCacheCtrl, cc)
54+				}
55+			}
56 		})
57 	}
58 }