Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gateway): Cache-Control header for UnixFS directories on /ipfs #643

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ The following emojis are used to highlight certain changes:

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

### Removed

Expand Down
6 changes: 3 additions & 3 deletions gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ func TestHeaders(t *testing.T) {
path string
cacheControl string
}{
{"/ipns/example.net/", "public, max-age=30"}, // As generated directory listing
{"/ipns/example.com/", "public, max-age=55"}, // As generated directory listing (different)
{"/ipns/unknown.com/", ""}, // As generated directory listing (unknown)
{"/ipns/example.net/", "public, max-age=30, stale-while-revalidate=2678400"}, // As generated directory listing
{"/ipns/example.com/", "public, max-age=55, stale-while-revalidate=2678400"}, // As generated directory listing (different)
{"/ipns/unknown.com/", ""}, // As generated directory listing (unknown TTL)
{"/ipns/example.net/foo/", "public, max-age=30"}, // As index.html directory listing
{"/ipns/example.net/foo/index.html", "public, max-age=30"}, // As deserialized UnixFS file
{"/ipns/example.net/?format=raw", "public, max-age=30"}, // As Raw block
Expand Down
9 changes: 7 additions & 2 deletions gateway/handler_unixfs_dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,14 @@ func (i *handler) serveDirectory(ctx context.Context, w http.ResponseWriter, r *
dirEtag := getDirListingEtag(resolvedPath.RootCid())
w.Header().Set("Etag", dirEtag)

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

if r.Method == http.MethodHead {
Expand Down
Loading