From 12b6cdcd8ce52269be5a1ca8acaae006896808b5 Mon Sep 17 00:00:00 2001 From: tobi <31960611+tsmethurst@users.noreply.github.com> Date: Thu, 13 Jul 2023 21:27:25 +0200 Subject: [bugfix] Set Vary header correctly on cache-control (#1988) * [bugfix] Set Vary header correctly on cache-control * Prefer activitypub types on AP endpoints * use immutable on file server, vary by range * vary auth on Accept --- internal/api/fileserver.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'internal/api/fileserver.go') diff --git a/internal/api/fileserver.go b/internal/api/fileserver.go index 9a2fdd633..8f1e60b82 100644 --- a/internal/api/fileserver.go +++ b/internal/api/fileserver.go @@ -36,8 +36,8 @@ func (f *Fileserver) Route(r router.Router, m ...gin.HandlerFunc) { // Attach middlewares appropriate for this group. fileserverGroup.Use(m...) // If we're using local storage or proxying s3, we can set a - // long max-age on all file requests to reflect that we - // never host different files at the same URL (since + // long max-age + immutable on all file requests to reflect + // that we never host different files at the same URL (since // ULIDs are generated per piece of media), so we can // easily prevent clients having to fetch files repeatedly. // @@ -45,9 +45,18 @@ func (f *Fileserver) Route(r router.Router, m ...gin.HandlerFunc) { // must be set dynamically within the request handler, // based on how long the signed URL has left to live before // it expires. This ensures that clients won't cache expired - // links. This is done within fileserver/servefile.go. + // links. This is done within fileserver/servefile.go, so we + // should not set the middleware here in that case. + // + // See: + // + // - https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching#avoiding_revalidation + // - https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control#immutable if config.GetStorageBackend() == "local" || config.GetStorageS3Proxy() { - fileserverGroup.Use(middleware.CacheControl("private", "max-age=604800")) // 7d + fileserverGroup.Use(middleware.CacheControl(middleware.CacheControlConfig{ + Directives: []string{"private", "max-age=604800", "immutable"}, + Vary: []string{"Range"}, // Cache partial ranges separately. + })) } f.fileserver.Route(fileserverGroup.Handle) -- cgit v1.2.3