-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added cache busting for static files
- Loading branch information
1 parent
141438c
commit b0e0997
Showing
15 changed files
with
293 additions
and
119 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package handler | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
|
||
s "github.com/Pineapple217/mb/pkg/static" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type Icon struct { | ||
Src string `json:"src"` | ||
Type string `json:"type"` | ||
Sizes string `json:"sizes"` | ||
} | ||
|
||
type IconSet struct { | ||
Icons []Icon `json:"icons"` | ||
} | ||
|
||
func (h *Handler) Manifest(c echo.Context) error { | ||
// TODO: could cache the man | ||
var manifest = IconSet{ | ||
Icons: []Icon{ | ||
{Src: s.StaticMap["/static/icon-192.png"], Type: "image/png", Sizes: "192x192"}, | ||
{Src: s.StaticMap["/static/icon-512.png"], Type: "image/png", Sizes: "512x512"}, | ||
}, | ||
} | ||
jsonData, err := json.Marshal(manifest) | ||
if err != nil { | ||
return err | ||
} | ||
c.Response().Header().Set("Content-Type", "application/manifest+json") | ||
return c.String(http.StatusOK, string(jsonData)) | ||
} |
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package static | ||
|
||
import ( | ||
"crypto/sha256" | ||
"embed" | ||
"encoding/hex" | ||
"fmt" | ||
"io" | ||
"io/fs" | ||
"log/slog" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var ( | ||
//go:embed public/* | ||
PublicFS embed.FS | ||
excludeExts = []string{".woff2"} | ||
StaticMap map[string]string | ||
) | ||
|
||
func hashFile(file fs.File) (string, error) { | ||
hash := sha256.New() | ||
if _, err := io.Copy(hash, file); err != nil { | ||
return "", err | ||
} | ||
sum := hash.Sum(nil) | ||
|
||
return hex.EncodeToString(sum)[:12], nil | ||
} | ||
|
||
func hashFiles(f embed.FS) (map[string]string, error) { | ||
filesDetails := map[string]string{} | ||
|
||
err := fs.WalkDir(f, ".", func(path string, d fs.DirEntry, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !d.IsDir() { | ||
ext := filepath.Ext(path) | ||
for _, e := range excludeExts { | ||
if ext == e { | ||
return nil | ||
} | ||
} | ||
|
||
file, err := f.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
hash, err := hashFile(file) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fileName := strings.TrimSuffix(d.Name(), ext) | ||
|
||
newFilename := fmt.Sprintf("%s-%s%s", fileName, hash, ext) | ||
basePath := strings.TrimSuffix(path, d.Name()) | ||
basePath = strings.ReplaceAll(basePath, "public", "/static") | ||
|
||
p := strings.ReplaceAll(path, "public", "/static") | ||
filesDetails[p] = basePath + newFilename | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return filesDetails, nil | ||
} | ||
|
||
func HashPublicFS() map[string]string { | ||
slog.Info("Hashing static files") | ||
files, err := hashFiles(PublicFS) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for k, v := range files { | ||
slog.Info("file hashed", "old", k, "new", v) | ||
} | ||
StaticMap = files | ||
swappedMap := make(map[string]string) | ||
for k, v := range files { | ||
swappedMap[v] = k | ||
} | ||
return swappedMap | ||
} |
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
This file contains 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
Oops, something went wrong.