Open platform to collect and prioritize feedback
fix: don't set cookies on cached content (#674)
Guilherme Oenningaf0ddd3b85f5
diff --git a/app/cmd/routes.go b/app/cmd/routes.goindex 5b67ba709..dc159398c 100644--- a/app/cmd/routes.go+++ b/app/cmd/routes.go@@ -62,8 +62,10 @@ func routes(r *web.Engine) *web.Engine {tenantAssets := r.Group(){- tenantAssets.Use(middlewares.ClientCache(30 * 24 * time.Hour))+ tenantAssets.Use(middlewares.ClientCache(5 * 24 * time.Hour))tenantAssets.Get("/avatars/:size/:id/:name", handlers.Avatar())++ tenantAssets.Use(middlewares.ClientCache(30 * 24 * time.Hour))tenantAssets.Get("/images/:size/:id", handlers.ViewUploadedImage())tenantAssets.Get("/custom/:md5.css", func(c web.Context) error {return c.Blob(http.StatusOK, "text/css", []byte(c.Tenant().CustomCSS))diff --git a/app/middlewares/cache.go b/app/middlewares/cache.goindex a7cf062e6..f0a6fa1d1 100644--- a/app/middlewares/cache.go+++ b/app/middlewares/cache.go@@ -11,7 +11,7 @@ import (func ClientCache(d time.Duration) web.MiddlewareFunc {return func(next web.HandlerFunc) web.HandlerFunc {return func(c web.Context) error {- c.Response.Header().Add("Cache-Control", fmt.Sprintf("public, max-age=%.f", d.Seconds()))+ c.Response.Header().Set("Cache-Control", fmt.Sprintf("public, max-age=%.f", d.Seconds()))return next(c)}}diff --git a/app/middlewares/session.go b/app/middlewares/session.goindex 0225a74f6..ff5716a6d 100644--- a/app/middlewares/session.go+++ b/app/middlewares/session.go@@ -1,6 +1,7 @@package middlewaresimport (+ "strings""time""github.com/getfider/fider/app/pkg/rand"@@ -17,7 +18,12 @@ func Session() web.MiddlewareFunc {cookie = c.AddCookie(web.CookieSessionName, rand.String(48), nextYear)}c.SetSessionID(cookie.Value)- return next(c)+ err = next(c)+ cc := c.Response.Header().Get("Cache-Control")+ if strings.Contains(cc, "max-age=") {+ c.Response.Header().Del("Set-Cookie")+ }+ return err}}}diff --git a/app/middlewares/session_test.go b/app/middlewares/session_test.goindex 814be6bf8..e39924e23 100644--- a/app/middlewares/session_test.go+++ b/app/middlewares/session_test.go@@ -47,3 +47,19 @@ func TestSession_ExistingSession(t *testing.T) {Expect(status).Equals(http.StatusOK)Expect(response.Header().Get("Set-Cookie")).Equals("")}++func TestSession_RemoveSessionIfResponseIsCached(t *testing.T) {+ RegisterT(t)++ server, _ := mock.NewServer()+ server.Use(middlewares.Session())+ server.Use(middlewares.ClientCache(30 * time.Hour))++ status, response := server.Execute(func(c web.Context) error {+ return c.NoContent(http.StatusOK)+ })++ Expect(status).Equals(http.StatusOK)+ Expect(response.Header().Get("Cache-Control")).Equals("public, max-age=108000")+ Expect(response.Header().Get("Set-Cookie")).Equals("")+}