From 872ae5db32c605ef385588a33673ce5fef29ff21 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Mon, 3 Jun 2024 22:24:39 +0200 Subject: feat: use stale-while-revalidate instead of immutable asset paths --- frontend/assets.go | 30 ++++++++++++------------------ 1 file changed, 12 insertions(+), 18 deletions(-) (limited to 'frontend') diff --git a/frontend/assets.go b/frontend/assets.go index c04e706..46369fa 100644 --- a/frontend/assets.go +++ b/frontend/assets.go @@ -3,32 +3,31 @@ package frontend import ( "crypto/sha256" "encoding/base64" - "encoding/hex" + "fmt" "hash/fnv" "io" "io/fs" - "path/filepath" - "strings" "github.com/pkg/errors" ) var Assets = &AssetCollection{ - Scripts: make(map[string]*Asset), - Stylesheets: make(map[string]*Asset), - ByImmutablePath: make(map[string]*Asset), + Scripts: make(map[string]*Asset), + Stylesheets: make(map[string]*Asset), + ByPath: make(map[string]*Asset), } type Asset struct { URL string + ETag string Filename string Base64SHA256 string } type AssetCollection struct { - Scripts map[string]*Asset - Stylesheets map[string]*Asset - ByImmutablePath map[string]*Asset + Scripts map[string]*Asset + Stylesheets map[string]*Asset + ByPath map[string]*Asset } func newAsset(filename string) (*Asset, error) { @@ -45,18 +44,13 @@ func newAsset(filename string) (*Asset, error) { } return &Asset{ - URL: makeImmutablePath(filename, hex.EncodeToString(hash.Sum(nil))), + URL: "/" + filename, + ETag: fmt.Sprintf(`W/"%x"`, hash.Sum(nil)), Filename: filename, Base64SHA256: base64.StdEncoding.EncodeToString(shasum.Sum(nil)), }, nil } -func makeImmutablePath(filename string, hash string) string { - ext := filepath.Ext(filename) - - return "/" + strings.Replace(filename, ext, "."+hash+ext, 1) -} - func hashScripts() error { scripts, err := fs.Glob(Files, "static/**.js") if err != nil { @@ -68,7 +62,7 @@ func hashScripts() error { return err } Assets.Scripts[filename] = asset - Assets.ByImmutablePath[asset.URL] = asset + Assets.ByPath[asset.URL] = asset } return nil @@ -85,7 +79,7 @@ func hashStyles() error { return err } Assets.Stylesheets[filename] = asset - Assets.ByImmutablePath[asset.URL] = asset + Assets.ByPath[asset.URL] = asset } return nil -- cgit 1.4.1