From fbf1eeb4c55bafd106649595f0b0802a9196d028 Mon Sep 17 00:00:00 2001 From: Willy Kloucek Date: Thu, 13 Oct 2022 11:09:26 +0200 Subject: [PATCH 1/4] don't cache the web index.html --- services/web/pkg/service/v0/service.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/services/web/pkg/service/v0/service.go b/services/web/pkg/service/v0/service.go index d3aef6e42..c03ccef34 100644 --- a/services/web/pkg/service/v0/service.go +++ b/services/web/pkg/service/v0/service.go @@ -152,6 +152,13 @@ func (p Web) Static(ttl int) http.HandlerFunc { 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) + + if r.URL.Path == rootWithSlash || r.URL.Path == rootWithSlash+"index.html" { + w.Header().Set("Cache-Control", "no-cache") + + } else { + w.Header().Set("Cache-Control", "must-revalidate") + } w.Header().Set("SameSite", "Strict") static.ServeHTTP(w, r) From ff4f160fdc29ed83538f93e82b3f8713e6b0ed63 Mon Sep 17 00:00:00 2001 From: Florian Schade Date: Thu, 13 Oct 2022 14:18:12 +0200 Subject: [PATCH 2/4] disable cache for certain resources and respect try_files --- services/web/pkg/assets/server.go | 15 +++++++++++---- services/web/pkg/service/v0/service.go | 12 ++---------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/services/web/pkg/assets/server.go b/services/web/pkg/assets/server.go index 527134330..af97f498d 100644 --- a/services/web/pkg/assets/server.go +++ b/services/web/pkg/assets/server.go @@ -22,18 +22,24 @@ func (f *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { upath := path.Clean(path.Join("/", r.URL.Path)) r.URL.Path = upath - asset, err := f.root.Open(upath) - if err != nil { + disableCache := func() { + w.Header().Set("Cache-Control", "no-cache") + } + handleIndex := func() { + disableCache() r.URL.Path = "/index.html" f.ServeHTTP(w, r) + } + asset, err := f.root.Open(upath) + if err != nil { + handleIndex() return } defer asset.Close() s, _ := asset.Stat() if s.IsDir() { - r.URL.Path = "/index.html" - f.ServeHTTP(w, r) + handleIndex() return } @@ -43,6 +49,7 @@ func (f *fileServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { switch s.Name() { case "index.html", "oidc-callback.html", "oidc-silent-redirect.html": + disableCache() _ = withBase(buf, asset, "/") default: _, _ = buf.ReadFrom(asset) diff --git a/services/web/pkg/service/v0/service.go b/services/web/pkg/service/v0/service.go index c03ccef34..a00e28bcd 100644 --- a/services/web/pkg/service/v0/service.go +++ b/services/web/pkg/service/v0/service.go @@ -3,7 +3,6 @@ package svc import ( "encoding/json" "fmt" - "io/ioutil" "net/http" "net/url" "os" @@ -90,7 +89,7 @@ func (p Web) getPayload() (payload []byte, err error) { Msg("web config doesn't exist") } - payload, err = ioutil.ReadFile(p.config.Web.Path) + payload, err = os.ReadFile(p.config.Web.Path) if err != nil { p.logger.Fatal(). @@ -102,7 +101,7 @@ func (p Web) getPayload() (payload []byte, err error) { } // Config implements the Service interface. -func (p Web) Config(w http.ResponseWriter, r *http.Request) { +func (p Web) Config(w http.ResponseWriter, _ *http.Request) { payload, err := p.getPayload() if err != nil { @@ -152,13 +151,6 @@ func (p Web) Static(ttl int) http.HandlerFunc { 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) - - if r.URL.Path == rootWithSlash || r.URL.Path == rootWithSlash+"index.html" { - w.Header().Set("Cache-Control", "no-cache") - - } else { - w.Header().Set("Cache-Control", "must-revalidate") - } w.Header().Set("SameSite", "Strict") static.ServeHTTP(w, r) From 3d84e8fc36c8e81144d02329a5f3868f068e68c0 Mon Sep 17 00:00:00 2001 From: Florian Schade Date: Thu, 13 Oct 2022 14:44:59 +0200 Subject: [PATCH 3/4] add changelog --- changelog/unreleased/fix-static-asset-caching.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/unreleased/fix-static-asset-caching.md diff --git a/changelog/unreleased/fix-static-asset-caching.md b/changelog/unreleased/fix-static-asset-caching.md new file mode 100644 index 000000000..8d15fc0b0 --- /dev/null +++ b/changelog/unreleased/fix-static-asset-caching.md @@ -0,0 +1,6 @@ +Bugfix: Disable cache for selected static web assets. + +We've disabled caching for some static web assets. +Files like the web index.html, oidc-callback.html or similar contain paths to timestamped resources and should not be cached. + +https://github.com/owncloud/ocis/pull/4809 From 2bc0f717f131583e79fc54107abf2341a094efe5 Mon Sep 17 00:00:00 2001 From: Florian Schade Date: Thu, 13 Oct 2022 16:09:09 +0200 Subject: [PATCH 4/4] fix changelog --- changelog/unreleased/fix-static-asset-caching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/unreleased/fix-static-asset-caching.md b/changelog/unreleased/fix-static-asset-caching.md index 8d15fc0b0..e27df2da0 100644 --- a/changelog/unreleased/fix-static-asset-caching.md +++ b/changelog/unreleased/fix-static-asset-caching.md @@ -1,4 +1,4 @@ -Bugfix: Disable cache for selected static web assets. +Bugfix: Disable cache for selected static web assets We've disabled caching for some static web assets. Files like the web index.html, oidc-callback.html or similar contain paths to timestamped resources and should not be cached.