Skip to content

Commit 0383467

Browse files
committed
fix(gateway): Cache-Control header for UnixFS directories
1 parent 2816b71 commit 0383467

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
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/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 {
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)