Skip to content

Commit

Permalink
refactor: adjust http cache for static file
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Oct 31, 2023
1 parent 9abdd25 commit 03cd7b7
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions middleware/static_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,6 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
for k, v := range config.Header {
c.AddHeader(k, v)
}
// 未设置cache control
// 或文件符合正则
if cacheControl == "" ||
(noCacheRegexp != nil && noCacheRegexp.MatchString(file)) {
c.NoCache()
} else {
c.SetHeader(elton.HeaderCacheControl, cacheControl)
}
// 如果有设置before response
if config.BeforeResponse != nil && fileBuf != nil {
buf, err := config.BeforeResponse(file, fileBuf)
Expand All @@ -295,6 +287,14 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
}
fileBuf = buf
}
// 未设置cache control
// 或文件符合正则
if cacheControl == "" ||
(noCacheRegexp != nil && noCacheRegexp.MatchString(file)) {
c.NoCache()
} else {
c.SetHeader(elton.HeaderCacheControl, cacheControl)
}
if fileBuf != nil {
c.StatusCode = http.StatusOK
c.BodyBuffer = bytes.NewBuffer(fileBuf)
Expand Down

0 comments on commit 03cd7b7

Please sign in to comment.