From 5a3edb9011e0210a674817562b198ff349f94d4a Mon Sep 17 00:00:00 2001 From: Roman Date: Mon, 5 Oct 2020 08:35:29 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A5add=20the=20option=20to=20set=20a?= =?UTF-8?q?=20custom=20Cache-Control=20value=20for=20a=20favicon=20respons?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Right now, each favicon response sets the `Cache-Control` header to `public, max-age=31536000` (1 year). In some cases this could be a little bit high if you are changing it etc. and the header is not replaced by a reverse proxy. With this change, the favicon middleware allows the configuration of the `Cache-Control` header. The default behavior is to keep the setting of `max-age=31536000 `, but you could fine-tune it. --- middleware/favicon/favicon.go | 16 ++++++++++++---- middleware/favicon/favicon_test.go | 16 ++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/middleware/favicon/favicon.go b/middleware/favicon/favicon.go index b4ae1e2d..6584fa25 100644 --- a/middleware/favicon/favicon.go +++ b/middleware/favicon/favicon.go @@ -18,18 +18,23 @@ type Config struct { // // Optional. Default: "" File string + + // CacheControl defines how the Cache-Control header in the response should be set + // + // Optional. Default: "public, max-age=31536000" + CacheControl string } // ConfigDefault is the default config var ConfigDefault = Config{ - Next: nil, - File: "", + Next: nil, + File: "", + CacheControl: "public, max-age=31536000", } const ( hType = "image/x-icon" hAllow = "GET, HEAD, OPTIONS" - hCache = "public, max-age=31536000" hZero = "0" ) @@ -49,6 +54,9 @@ func New(config ...Config) fiber.Handler { if cfg.File == "" { cfg.File = ConfigDefault.File } + if cfg.CacheControl == "" { + cfg.CacheControl = ConfigDefault.CacheControl + } } // Load icon if provided @@ -92,7 +100,7 @@ func New(config ...Config) fiber.Handler { if len(icon) > 0 { c.Set(fiber.HeaderContentLength, iconLen) c.Set(fiber.HeaderContentType, hType) - c.Set(fiber.HeaderCacheControl, hCache) + c.Set(fiber.HeaderCacheControl, cfg.CacheControl) return c.Status(fiber.StatusOK).Send(icon) } diff --git a/middleware/favicon/favicon_test.go b/middleware/favicon/favicon_test.go index 1de1fa15..5f7465ca 100644 --- a/middleware/favicon/favicon_test.go +++ b/middleware/favicon/favicon_test.go @@ -68,6 +68,22 @@ func Test_Middleware_Favicon_Found(t *testing.T) { utils.AssertEqual(t, nil, err, "app.Test(req)") utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code") utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType)) + utils.AssertEqual(t, "public, max-age=31536000", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control") +} + +// go test -run Test_Middleware_Favicon_CacheControl +func Test_Middleware_Favicon_CacheControl(t *testing.T) { + app := fiber.New() + + app.Use(New(Config{ + CacheControl: "public, max-age=100", + File: "../../.github/testdata/favicon.ico", + })) + resp, err := app.Test(httptest.NewRequest("GET", "/favicon.ico", nil)) + utils.AssertEqual(t, nil, err, "app.Test(req)") + utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode, "Status code") + utils.AssertEqual(t, "image/x-icon", resp.Header.Get(fiber.HeaderContentType)) + utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(fiber.HeaderCacheControl), "CacheControl Control") } // go test -v -run=^$ -bench=Benchmark_Middleware_Favicon -benchmem -count=4