diff --git a/services/files/README.md b/services/files/README.md index 64ad379cd16..05ec972ecc4 100644 --- a/services/files/README.md +++ b/services/files/README.md @@ -55,7 +55,8 @@ Set HTTP metadata when uploading: `application/gzip`. Missing types default to `application/octet-stream`. - `Content-Disposition`: use `attachment; filename="..."` when a file should download rather than display inline. -- `Cache-Control`: defaults to `public, max-age=3600` (one hour). Use +- `Cache-Control`: defaults to `public, max-age=3600` (one hour), or a year + (`immutable`) under `site/`, whose files never change in place. Use `public, max-age=31536000, immutable` for versioned/content-addressed files, and a short TTL or `no-store` for mutable pointers such as `latest.json`. diff --git a/services/files/src/index.ts b/services/files/src/index.ts index c9061479456..fe34d3f3ce8 100644 --- a/services/files/src/index.ts +++ b/services/files/src/index.ts @@ -77,7 +77,9 @@ export default { headers.set("Last-Modified", object.uploaded.toUTCString()) headers.set("Accept-Ranges", "bytes") if (!headers.has("Content-Type")) headers.set("Content-Type", "application/octet-stream") - if (!headers.has("Cache-Control")) headers.set("Cache-Control", "public, max-age=3600") + // The site's media never changes in place: a changed file gets a new dated folder (services/site/MEDIA.md). + if (!headers.has("Cache-Control")) + headers.set("Cache-Control", key.startsWith("site/") ? "public, max-age=31536000, immutable" : "public, max-age=3600") const modified = Math.floor(object.uploaded.getTime() / 1000) * 1000 const ifMatch = request.headers.get("If-Match")