From 937aadc5d332c713f1324ead7620b66180f2e10f Mon Sep 17 00:00:00 2001 From: David Leadbeater Date: Mon, 15 Dec 2025 21:02:25 +1100 Subject: [PATCH] Allow setting custom Cache-Control headers via _headers Before this change Cache-Control header would always be overridden, this change allows custom Cache-Control, provided Cache-Control is added to the header allow list. --- src/pages.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/pages.go b/src/pages.go index ab50282..2394dce 100644 --- a/src/pages.go +++ b/src/pages.go @@ -417,13 +417,15 @@ func getPage(w http.ResponseWriter, r *http.Request) error { io.Copy(w, reader) } } else { - // consider content fresh for 60 seconds (the same as the freshness interval of - // manifests in the S3 backend), and use stale content anyway as long as it's not - // older than a hour; while it is cheap to handle If-Modified-Since queries - // server-side, on the client `max-age=0, must-revalidate` causes every resource - // to block the page load every time - w.Header().Set("Cache-Control", "max-age=60, stale-while-revalidate=3600") - // see https://web.dev/articles/stale-while-revalidate for details + if _, hasCacheControl := w.Header()["Cache-Control"]; !hasCacheControl { + // consider content fresh for 60 seconds (the same as the freshness interval of + // manifests in the S3 backend), and use stale content anyway as long as it's not + // older than a hour; while it is cheap to handle If-Modified-Since queries + // server-side, on the client `max-age=0, must-revalidate` causes every resource + // to block the page load every time + w.Header().Set("Cache-Control", "max-age=60, stale-while-revalidate=3600") + // see https://web.dev/articles/stale-while-revalidate for details + } // http.ServeContent handles conditional requests and range requests http.ServeContent(w, r, entryPath, mtime, reader)