Skip to content

Commit 96ceee6

Browse files
lidelacejam
andauthored
fix(gateway): Cache-Control header for UnixFS directories on /ipfs (#643)
* fix(gateway): Cache-Control header for UnixFS directories * fix: limit static cache-control to /ipfs just a precaution, since we still have TTL=0 for many DNSLink responses due to #329 (comment) --------- Co-authored-by: Joshua Noble <jnoble@filebase.com>
1 parent 77bd1ef commit 96ceee6

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ The following emojis are used to highlight certain changes:
2020

2121
- `bitswap/server` minor memory use and performance improvements
2222
- `bitswap` unify logger names to use uniform format bitswap/path/pkgname
23+
- `gateway` now always returns meaningful cache-control headers for generated HTML listings of UnixFS directories
2324

2425
### Removed
2526

gateway/gateway_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ func TestHeaders(t *testing.T) {
130130
path string
131131
cacheControl string
132132
}{
133-
{"/ipns/example.net/", "public, max-age=30"}, // As generated directory listing
134-
{"/ipns/example.com/", "public, max-age=55"}, // As generated directory listing (different)
135-
{"/ipns/unknown.com/", ""}, // As generated directory listing (unknown)
133+
{"/ipns/example.net/", "public, max-age=30, stale-while-revalidate=2678400"}, // As generated directory listing
134+
{"/ipns/example.com/", "public, max-age=55, stale-while-revalidate=2678400"}, // As generated directory listing (different)
135+
{"/ipns/unknown.com/", ""}, // As generated directory listing (unknown TTL)
136136
{"/ipns/example.net/foo/", "public, max-age=30"}, // As index.html directory listing
137137
{"/ipns/example.net/foo/index.html", "public, max-age=30"}, // As deserialized UnixFS file
138138
{"/ipns/example.net/?format=raw", "public, max-age=30"}, // As Raw block

gateway/handler_unixfs_dir.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,14 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
136136
dirEtag := getDirListingEtag(resolvedPath.RootCid())
137137
w.Header().Set("Etag", dirEtag)
138138

139-
// Add TTL if known.
139+
// Set Cache-Control
140140
if rq.ttl > 0 {
141-
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d", int(rq.ttl.Seconds())))
141+
// Use known TTL from IPNS Record or DNSLink TXT Record
142+
w.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%d, stale-while-revalidate=2678400", int(rq.ttl.Seconds())))
143+
} else if !rq.contentPath.Mutable() {
144+
// Cache for 1 week, serve stale cache for up to a month
145+
// (style of generated HTML may change, should not be cached forever)
146+
w.Header().Set("Cache-Control", "public, max-age=604800, stale-while-revalidate=2678400")
142147
}
143148

144149
if r.Method == http.MethodHead {

0 commit comments

Comments
 (0)