From 005825bbc9a3fcfa90ea7bed9b1e79241261617e Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Mon, 23 Nov 2020 13:06:05 +0100 Subject: [PATCH 1/3] Set proper expires header --- ocis-phoenix/pkg/service/v0/service.go | 3 ++- ocis-pkg/middleware/static.go | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ocis-phoenix/pkg/service/v0/service.go b/ocis-phoenix/pkg/service/v0/service.go index 4bb9942b2..1f426af33 100644 --- a/ocis-phoenix/pkg/service/v0/service.go +++ b/ocis-phoenix/pkg/service/v0/service.go @@ -146,6 +146,7 @@ func (p Phoenix) Static(ttl int) http.HandlerFunc { // we don't have a last modification date of the static assets, so we use the service start date lastModified := time.Now().UTC().Format(http.TimeFormat) + expires := time.Now().Add(time.Second * time.Duration(ttl)).UTC().Format(http.TimeFormat) return func(w http.ResponseWriter, r *http.Request) { if rootWithSlash != "/" && r.URL.Path == p.config.HTTP.Root { @@ -169,8 +170,8 @@ func (p Phoenix) Static(ttl int) http.HandlerFunc { } w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s", strconv.Itoa(ttl))) + w.Header().Set("Expires", expires) w.Header().Set("Last-Modified", lastModified) - w.Header().Del("Expires") static.ServeHTTP(w, r) } diff --git a/ocis-pkg/middleware/static.go b/ocis-pkg/middleware/static.go index 57f2e6957..03d61ac0f 100644 --- a/ocis-pkg/middleware/static.go +++ b/ocis-pkg/middleware/static.go @@ -24,6 +24,7 @@ func Static(root string, fs http.FileSystem, ttl int) func(http.Handler) http.Ha // we don't have a last modification date of the static assets, so we use the service start date lastModified := time.Now().UTC().Format(http.TimeFormat) + expires := time.Now().Add(time.Second * time.Duration(ttl)).UTC().Format(http.TimeFormat) return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { @@ -31,8 +32,8 @@ func Static(root string, fs http.FileSystem, ttl int) func(http.Handler) http.Ha next.ServeHTTP(w, r) } else { w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s", strconv.Itoa(ttl))) + w.Header().Set("Expires", expires) w.Header().Set("Last-Modified", lastModified) - w.Header().Del("Expires") static.ServeHTTP(w, r) } }) From 5de15f4d126ca71a86e513eb670a6d3457497bdb Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Mon, 23 Nov 2020 13:16:49 +0100 Subject: [PATCH 2/3] Force web asset re-validation (on last modification date) --- ocis-phoenix/pkg/service/v0/service.go | 2 +- ocis-pkg/middleware/static.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ocis-phoenix/pkg/service/v0/service.go b/ocis-phoenix/pkg/service/v0/service.go index 1f426af33..eea57c84a 100644 --- a/ocis-phoenix/pkg/service/v0/service.go +++ b/ocis-phoenix/pkg/service/v0/service.go @@ -169,7 +169,7 @@ func (p Phoenix) Static(ttl int) http.HandlerFunc { return } - w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s", strconv.Itoa(ttl))) + w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s, must-revalidate", strconv.Itoa(ttl))) w.Header().Set("Expires", expires) w.Header().Set("Last-Modified", lastModified) diff --git a/ocis-pkg/middleware/static.go b/ocis-pkg/middleware/static.go index 03d61ac0f..d601a3eb7 100644 --- a/ocis-pkg/middleware/static.go +++ b/ocis-pkg/middleware/static.go @@ -31,7 +31,7 @@ func Static(root string, fs http.FileSystem, ttl int) func(http.Handler) http.Ha if strings.HasPrefix(r.URL.Path, path.Join(root, "api")) { next.ServeHTTP(w, r) } else { - w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s", strconv.Itoa(ttl))) + w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%s, must-revalidate", strconv.Itoa(ttl))) w.Header().Set("Expires", expires) w.Header().Set("Last-Modified", lastModified) static.ServeHTTP(w, r) From 48169de239706dd8c381b9123cdcc3b221266c70 Mon Sep 17 00:00:00 2001 From: Benedikt Kulmann Date: Mon, 23 Nov 2020 13:18:24 +0100 Subject: [PATCH 3/3] Changelog --- changelog/unreleased/web-assets-caching.md | 1 + 1 file changed, 1 insertion(+) diff --git a/changelog/unreleased/web-assets-caching.md b/changelog/unreleased/web-assets-caching.md index e6fc2547f..7db566fc8 100644 --- a/changelog/unreleased/web-assets-caching.md +++ b/changelog/unreleased/web-assets-caching.md @@ -5,3 +5,4 @@ Tags: accounts, settings, web We now set http caching headers for static web assets, so that they don't get force-reloaded on each request. The max-age for the caching is configurable and defaults to 7 days. The last modified date of the assets is set to the service start date, so that a service restart results in cache invalidation. https://github.com/owncloud/ocis/pull/866 +https://github.com/owncloud/ocis/pull/934