Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add CacheControl to Static config #2140

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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