Skip to content

Commit

Permalink
perf(githubstats): Minify SVGs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Jan 20, 2025
1 parent 03066eb commit b71e842
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/pocketbase/pocketbase v0.24.4
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/tdewolff/minify/v2 v2.21.3
)

require (
Expand Down Expand Up @@ -53,6 +54,7 @@ require (
github.com/pocketbase/dbx v1.11.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/tdewolff/parse/v2 v2.7.19 // indirect
go.opencensus.io v0.24.0 // indirect
gocloud.dev v0.40.0 // indirect
golang.org/x/crypto v0.32.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tdewolff/minify/v2 v2.21.3 h1:KmhKNGrN/dGcvb2WDdB5yA49bo37s+hcD8RiF+lioV8=
github.com/tdewolff/minify/v2 v2.21.3/go.mod h1:iGxHaGiONAnsYuo8CRyf8iPUcqRJVB/RhtEcTpqS7xw=
github.com/tdewolff/parse/v2 v2.7.19 h1:7Ljh26yj+gdLFEq/7q9LT4SYyKtwQX4ocNrj45UCePg=
github.com/tdewolff/parse/v2 v2.7.19/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA=
github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE=
github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03u/dMQK9g+Iw9ewps4mCl1nB8Sscbo=
github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0=
go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo=
Expand Down
17 changes: 13 additions & 4 deletions internal/handlers/githubstats/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (
"time"

"github.com/pocketbase/pocketbase/core"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/svg"
)

func NewCache(sourceURL string) *Cache {
Expand Down Expand Up @@ -92,15 +95,21 @@ func (c *Cache) Update(ctx context.Context) error {
}
}

b, err := io.ReadAll(resp.Body)
if err != nil {
m := minify.New()
m.AddFunc("image/svg+xml", svg.Minify)
m.AddFunc("text/css", css.Minify)

c.mu.RLock()
buf := bytes.NewBuffer(make([]byte, 0, max(len(c.data), 2048)))
c.mu.RUnlock()

if err := m.Minify("image/svg+xml", buf, resp.Body); err != nil {
return err
}
b = slices.Clip(b)

c.mu.Lock()
defer c.mu.Unlock()
c.data = b
c.data = slices.Clip(buf.Bytes())
c.lastModified = lastModified
c.etag = etag
return nil
Expand Down

0 comments on commit b71e842

Please sign in to comment.