Skip to content

Instantly share code, notes, and snippets.

@ashinzekene
Last active May 2, 2020 08:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashinzekene/5123c6896ae15f47d53b871b934d3e65 to your computer and use it in GitHub Desktop.
Save ashinzekene/5123c6896ae15f47d53b871b934d3e65 to your computer and use it in GitHub Desktop.
var (
// Match assets with hashes eg. main.347845.js, button.9029893.css
hashedPatternRegex = regexp.MustCompile(`[\w-/]\.[0-9,a-z]{20}\.(js|css)$`)
isHashedAsset = hashedPatternRegex.MatchString
)
func CacheHeaders(staticPath string) gin.HandlerFunc {
return func(c *gin.Context) {
r := c.Request.URL.Path
// Set a max age of 1-year for js and css with hashes
if isHashedAsset(r) {
c.Header("Cache-Control", "max-age=31536000, immutable")
}
c.Next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment