mirror of
https://github.com/gofiber/fiber.git
synced 2025-05-31 11:52:41 +00:00
🔥set a Cache Control
header for static ressources
Right now, each static request returns the file without any cache control header. If you are using `app.Static` to serve your assets, the browser shouldn't cache it. This PR introduces a way to set the cache control header (if the file was found) on the file response to set a custom max-age to e.g. cache it 1 year.
This commit is contained in:
parent
dcf7b35c1c
commit
3e6dfb313b
6
app.go
6
app.go
@ -261,6 +261,12 @@ type Static struct {
|
||||
// The name of the index file for serving a directory.
|
||||
// Optional. Default value "index.html".
|
||||
Index string `json:"index"`
|
||||
|
||||
// The value for the Cache-Control HTTP-header
|
||||
// that is set on the file response. MaxAge is defined in seconds.
|
||||
//
|
||||
// Optional. Default value 0.
|
||||
MaxAge int `json:"max_age"`
|
||||
}
|
||||
|
||||
// Default Config values
|
||||
|
20
app_test.go
20
app_test.go
@ -562,12 +562,30 @@ func Test_App_Static_Direct(t *testing.T) {
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "")
|
||||
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get(HeaderContentType))
|
||||
utils.AssertEqual(t, "text/plain; charset=utf-8", resp.Header.Get("Content-Type"))
|
||||
utils.AssertEqual(t, "", resp.Header.Get(HeaderCacheControl), "CacheControl Control")
|
||||
|
||||
|
||||
body, err = ioutil.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, strings.Contains(string(body), "gofiber.io/support"))
|
||||
}
|
||||
|
||||
// go test -run Test_App_Static_MaxAge
|
||||
func Test_App_Static_MaxAge(t *testing.T) {
|
||||
app := New()
|
||||
|
||||
app.Static("/", "./.github", Static{MaxAge: 100})
|
||||
|
||||
resp, err := app.Test(httptest.NewRequest("GET", "/index.html", nil))
|
||||
utils.AssertEqual(t, nil, err, "app.Test(req)")
|
||||
utils.AssertEqual(t, 200, resp.StatusCode, "Status code")
|
||||
utils.AssertEqual(t, false, resp.Header.Get(HeaderContentLength) == "")
|
||||
utils.AssertEqual(t, "text/html; charset=utf-8", resp.Header.Get(HeaderContentType))
|
||||
utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(HeaderCacheControl), "CacheControl Control")
|
||||
}
|
||||
|
||||
// go test -run Test_App_Static_Group
|
||||
func Test_App_Static_Group(t *testing.T) {
|
||||
app := New()
|
||||
|
||||
|
11
router.go
11
router.go
@ -7,6 +7,7 @@ package fiber
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -338,8 +339,15 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
|
||||
fctx.Response.SetStatusCode(StatusNotFound)
|
||||
},
|
||||
}
|
||||
|
||||
// Set config if provided
|
||||
var cacheControlValue string
|
||||
if len(config) > 0 {
|
||||
maxAge := config[0].MaxAge
|
||||
if maxAge > 0 {
|
||||
cacheControlValue = "public, max-age=" + strconv.Itoa(maxAge)
|
||||
}
|
||||
|
||||
fs.Compress = config[0].Compress
|
||||
fs.AcceptByteRange = config[0].ByteRange
|
||||
fs.GenerateIndexPages = config[0].Browse
|
||||
@ -354,6 +362,9 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
|
||||
// Return request if found and not forbidden
|
||||
status := c.fasthttp.Response.StatusCode()
|
||||
if status != StatusNotFound && status != StatusForbidden {
|
||||
if len(cacheControlValue) > 0 {
|
||||
c.fasthttp.Response.Header.Set(HeaderCacheControl, cacheControlValue)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Reset response to default
|
||||
|
Loading…
x
Reference in New Issue
Block a user