From 40fd1a7c86c0c2b7964441b01274a39c344ee8d1 Mon Sep 17 00:00:00 2001 From: Seth Hoenig Date: Sun, 29 Nov 2026 00:19:11 +0000 Subject: [PATCH] headers: helper to disable cache-control --- README.md | 4 ++++ headers.go | 6 ++++++ headers_test.go | 8 ++++++++ 3 files changed, 18 insertions(+) diff --git a/README.md b/README.md index 3b03b64..a915af1 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ webtools.SetRobotsTag(w, webtools.RobotsYesIndex) webtools.SetCacheControl(w, 24 * time.Hour) ``` +```go +webtools.DisableCacheControl(w) +``` + - `Authorization` ```go diff --git a/headers.go b/headers.go index ce42ce8..bd9b9c6 100644 --- a/headers.go +++ b/headers.go @@ -61,6 +61,12 @@ func SetCacheControl(w http.ResponseWriter, ttl time.Duration) { w.Header().Add("Cache-Control", s) } +// DisableCacheControl sets the Cache-Control header on w to no-store, +// instructing intermediate caches to not store the response. +func DisableCacheControl(w http.ResponseWriter) { + w.Header().Add("Cache-Control", "no-store") +} + // SetBasicAuth sets the Authorization header on r, using the given username // and password. // diff --git a/headers_test.go b/headers_test.go index 7aef8cb..60df48f 100644 --- a/headers_test.go +++ b/headers_test.go @@ -18,6 +18,14 @@ func Test_SetCacheControl(t *testing.T) { must.Eq(t, "public, max-age=240", w.Header().Get("Cache-Control")) } +func Test_DisableCacheControl(t *testing.T) { + t.Parallel() + + w := httptest.NewRecorder() + DisableCacheControl(w) + must.Eq(t, "no-store", w.Header().Get("Cache-Control")) +} + func Test_SetContentType(t *testing.T) { t.Parallel()