Last active
May 2, 2020 08:48
-
-
Save ashinzekene/5123c6896ae15f47d53b871b934d3e65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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