Skip to content

Commit

Permalink
add CacheControl to Static config (#2140)
Browse files Browse the repository at this point in the history
* add CacheControl to Static config

* change config name to ModifyResponse

Co-authored-by: Nathan Faucett <nfaucett@utility.com>
  • Loading branch information
nathanfaucett and Nathan Faucett committed Oct 5, 2022
1 parent 00ebb21 commit 6a5fc64
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app.go
Expand Up @@ -421,6 +421,11 @@ type Static struct {
// Optional. Default value 0.
MaxAge int `json:"max_age"`

// ModifyResponse defines a function that allows you to alter the response.
//
// Optional. Default: nil
ModifyResponse Handler

// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Expand Down
20 changes: 20 additions & 0 deletions app_test.go
Expand Up @@ -767,6 +767,26 @@ func Test_App_Static_MaxAge(t *testing.T) {
utils.AssertEqual(t, "public, max-age=100", resp.Header.Get(HeaderCacheControl), "CacheControl Control")
}

// go test -run Test_App_Static_Custom_CacheControl
func Test_App_Static_Custom_CacheControl(t *testing.T) {
app := New()

app.Static("/", "./.github", Static{ModifyResponse: func(c *Ctx) error {
if strings.Contains(string(c.GetRespHeader("Content-Type")), "text/html") {
c.Response().Header.Set("Cache-Control", "no-cache, no-store, must-revalidate")
}
return nil
}})

resp, err := app.Test(httptest.NewRequest("GET", "/index.html", nil))
utils.AssertEqual(t, nil, err, "app.Test(req)")
utils.AssertEqual(t, "no-cache, no-store, must-revalidate", resp.Header.Get(HeaderCacheControl), "CacheControl Control")

normal_resp, normal_err := app.Test(httptest.NewRequest("GET", "/config.yml", nil))
utils.AssertEqual(t, nil, normal_err, "app.Test(req)")
utils.AssertEqual(t, "", normal_resp.Header.Get(HeaderCacheControl), "CacheControl Control")
}

// go test -run Test_App_Static_Download
func Test_App_Static_Download(t *testing.T) {
app := New()
Expand Down
5 changes: 5 additions & 0 deletions router.go
Expand Up @@ -357,6 +357,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {

// Set config if provided
var cacheControlValue string
var modifyResponse Handler
if len(config) > 0 {
maxAge := config[0].MaxAge
if maxAge > 0 {
Expand All @@ -369,6 +370,7 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
if config[0].Index != "" {
fs.IndexNames = []string{config[0].Index}
}
modifyResponse = config[0].ModifyResponse
}
fileHandler := fs.NewRequestHandler()
handler := func(c *Ctx) error {
Expand All @@ -388,6 +390,9 @@ func (app *App) registerStatic(prefix, root string, config ...Static) Router {
if len(cacheControlValue) > 0 {
c.fasthttp.Response.Header.Set(HeaderCacheControl, cacheControlValue)
}
if modifyResponse != nil {
return modifyResponse(c)
}
return nil
}
// Reset response to default
Expand Down

0 comments on commit 6a5fc64

Please sign in to comment.