mirror of
https://github.com/prymitive/karma
synced 2026-05-05 03:16:51 +00:00
feat(backend): set caching headers for all static files
Those should improve browser caching so we avoid fetching all css/js files on every page load
This commit is contained in:
11
assets.go
11
assets.go
@@ -7,6 +7,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
assetfs "github.com/elazarl/go-bindata-assetfs"
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -87,3 +88,13 @@ func serverFileOrEmpty(path string, contentType string, c *gin.Context) {
|
||||
}
|
||||
c.File(path)
|
||||
}
|
||||
|
||||
func staticHeaders(prefix string) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if strings.HasPrefix(c.Request.URL.Path, prefix) {
|
||||
c.Header("Cache-Control", "public, max-age=2592000")
|
||||
expiresTime := time.Now().AddDate(0, 0, 30).Format(http.TimeFormat)
|
||||
c.Header("Expires", expiresTime)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
3
main.go
3
main.go
@@ -19,11 +19,11 @@ import (
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/contrib/sentry"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/patrickmn/go-cache"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
raven "github.com/getsentry/raven-go"
|
||||
ginprometheus "github.com/mcuadros/go-gin-prometheus"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@@ -55,6 +55,7 @@ func getViewURL(sub string) string {
|
||||
|
||||
func setupRouter(router *gin.Engine) {
|
||||
router.Use(gzip.Gzip(gzip.DefaultCompression))
|
||||
router.Use(staticHeaders(getViewURL("/static/")))
|
||||
router.Use(static.Serve(getViewURL("/"), staticBuildFileSystem))
|
||||
// next 2 lines are to allow service raw sources so sentry can fetch source maps
|
||||
router.Use(static.Serve(getViewURL("/static/js/"), staticSrcFileSystem))
|
||||
|
||||
Reference in New Issue
Block a user