cmd/packserver: update libs, add expiry404 option

The default Cache-Control header now becomes "no-cache". When the
--expiry option is used, it only applies to HTTP 200 responses. There is
a new --expiry404 option dessigned to allow for a short caching period
of invalid paths, but allowing regular re-checking in case the asset
becomes avaoilable in the future.
This commit is contained in:
Laurence Withers 2026-08-28 10:56:48 +01:00
commit 7d96b5bdcb
3 changed files with 34 additions and 52 deletions

View file

@ -55,6 +55,8 @@ func main() {
"Name of index file (index.html or similar)")
rootCmd.Flags().Duration("expiry", 0,
"Tell client how long it can cache data for; 0 means no caching")
rootCmd.Flags().Duration("expiry404", 0,
"Tell client how long it can cache 404 responses for; 0 means no caching")
rootCmd.Flags().String("fallback-404", "",
"Name of file to return if response would be 404 (spa.html or similar)")
rootCmd.Flags().String("frames", "sameorigin",
@ -135,15 +137,20 @@ func run(c *cobra.Command, args []string) error {
// parse expiry time
// NB: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
cacheControl200, cacheControl404 := "no-cache", "no-cache"
expiry, err := c.Flags().GetDuration("expiry")
if err != nil {
return err
}
if expiry <= 0 {
extraHeaders.Set("Cache-Control", "no-cache")
} else {
extraHeaders.Set("Cache-Control",
fmt.Sprintf("public, max-age=%d", expiry/1e9))
if expiry > 0 {
cacheControl200 = fmt.Sprintf("public, max-age=%d", expiry/1e9)
}
expiry, err = c.Flags().GetDuration("expiry404")
if err != nil {
return err
}
if expiry > 0 {
cacheControl404 = fmt.Sprintf("public, max-age=%d", expiry/1e9)
}
// optional index file
@ -199,6 +206,7 @@ func run(c *cobra.Command, args []string) error {
if err != nil {
return err
}
packHandler.SetCacheControl(cacheControl200, cacheControl404)
if indexFile != "" {
packHandler.SetIndex(indexFile)
}