Merge pull request #934 from owncloud/set-expires-headers

Set expires headers and force asset revalidation
This commit is contained in:
Benedikt Kulmann
2020-11-24 11:09:16 +01:00
committed by GitHub
3 changed files with 7 additions and 4 deletions
@@ -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
+3 -2
View File
@@ -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 {
@@ -168,9 +169,9 @@ 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)
w.Header().Del("Expires")
static.ServeHTTP(w, r)
}
+3 -2
View File
@@ -24,15 +24,16 @@ 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) {
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)
w.Header().Del("Expires")
static.ServeHTTP(w, r)
}
})