Commit 2ccfad9

Daniel Axtens <dja@axtens.net>
2026-10-27 23:40:08
support sha-tagged css
1 parent 14ff84f
assets/common.css
@@ -198,10 +198,6 @@ footer {
             border-top-left-radius: 0;
             border-top-right-radius: 0;
         }
-
-        .goto-top {
-            display: flex;
-        }
     }
 }
 
pkg/templates/layout.gohtml
@@ -5,7 +5,7 @@
     <meta charset="UTF-8">
     <meta name="viewport" content="width=device-width, initial-scale=1">
     <title>{{ .Title }}</title>
-    <link rel="stylesheet" href="{{ .RootHref }}assets/common.css">
+    <link rel="stylesheet" href="{{ .RootHref }}assets/{{ index .AssetNames "common.css" }}">
     {{ template "head" . }}
 </head>
 <body>
pkg/templates/templates.go
@@ -65,6 +65,7 @@ type LayoutParams struct {
 	RootHref      string
 	CurrentRefDir string
 	Selected      string
+	AssetNames    map[string]string
 }
 
 type HeaderParams struct {
assets.go
@@ -1,32 +1,75 @@
 package main
 
 import (
+	"bytes"
+	"crypto/sha256"
 	_ "embed"
+	"fmt"
 	"os"
 	"path/filepath"
+
+	"github.com/tdewolff/minify/v2"
+	"github.com/tdewolff/minify/v2/css"
 )
 
 //go:embed assets/common.css
 var CommonCSS []byte
 
-func createAssets(params Params) error {
+func createAssets(params Params, do_minify bool) (map[string]string, error) {
+	// First, collect it
+	var cssBuffer bytes.Buffer
+
+	if _, err := cssBuffer.Write(CommonCSS); err != nil {
+		return nil, err
+	}
+
+	if err := generateCommitsCSS(&cssBuffer, params); err != nil {
+		return nil, err
+	}
+
+	// minify if desired
+	var outBuffer bytes.Buffer
+	if do_minify {
+		var m *minify.M
+		m = minify.New()
+		m.AddFunc("text/css", css.Minify)
+
+		mini, err := m.Bytes("text/css", cssBuffer.Bytes())
+		if err != nil {
+			return nil, err
+		}
+
+		if _, err := outBuffer.Write(mini); err != nil {
+			return nil, err
+		}
+	} else {
+		outBuffer = cssBuffer
+	}
+
+	// get the sha256
+	sha := sha256.Sum256(outBuffer.Bytes())
+	filename := fmt.Sprintf("common.%x.css", sha)
+
+	// write it out
 	outDir := filepath.Join(params.OutputDir, "assets")
 	if err := os.MkdirAll(outDir, 0o755); err != nil {
-		return err
+		return nil, err
 	}
 
-	path := filepath.Join(outDir, "common.css")
+	path := filepath.Join(outDir, filename)
 	f, err := os.Create(path)
 	if err != nil {
-		return err
+		return nil, err
 	}
 	defer f.Close()
 
-	if _, err := f.Write(CommonCSS); err != nil {
-		return err
+	if _, err := f.Write(outBuffer.Bytes()); err != nil {
+		return nil, err
 	}
 
-	generateCommitsCSS(f, params)
+	result := map[string]string{
+		"common.css": filename,
+	}
 
-	return nil
+	return result, nil
 }
blob.go
@@ -149,6 +149,7 @@ func generateBlobs(files []git.Blob, params Params) error {
 								RootHref:      rootHref,
 								CurrentRefDir: params.Ref.DirName(),
 								Selected:      "code",
+								AssetNames:    params.AssetNames,
 							},
 							HeaderParams: templates.HeaderParams{
 								Ref:         params.Ref,
@@ -190,6 +191,7 @@ func generateBlobs(files []git.Blob, params Params) error {
 								RootHref:      rootHref,
 								CurrentRefDir: params.Ref.DirName(),
 								Selected:      "code",
+								AssetNames:    params.AssetNames,
 							},
 							HeaderParams: templates.HeaderParams{
 								Ref:         params.Ref,
branches.go
@@ -53,6 +53,7 @@ func generateBranches(branches []git.Ref, defaultBranch string, params Params) e
 			RootHref:      rootHref,
 			CurrentRefDir: params.DefaultRef.DirName(),
 			Selected:      "branches",
+			AssetNames:    params.AssetNames,
 		},
 		Branches: entries,
 	})
commit.go
@@ -286,6 +286,7 @@ func generateCommitPage(commit git.Commit, params Params) error {
 			RootHref:      rootHref,
 			CurrentRefDir: currentRef.DirName(),
 			Selected:      "commits",
+			AssetNames:    params.AssetNames,
 		},
 		Commit:    commit,
 		FileTree:  fileTree,
commits_list.go
@@ -67,6 +67,7 @@ func generateLogForBranch(allCommits []git.Commit, params Params) error {
 				RootHref:      rootHref,
 				CurrentRefDir: params.Ref.DirName(),
 				Selected:      "commits",
+				AssetNames:    params.AssetNames,
 			},
 			HeaderParams: templates.HeaderParams{
 				Header: "Commits",
index.go
@@ -105,6 +105,7 @@ func generateIndex(files []git.Blob, params Params) error {
 			RootHref:      rootHref,
 			CurrentRefDir: params.Ref.DirName(),
 			Selected:      "code",
+			AssetNames:    params.AssetNames,
 		},
 		HeaderParams: templates.HeaderParams{
 			Ref:         params.Ref,
list.go
@@ -183,6 +183,7 @@ func generateLists(files []git.Blob, params Params) error {
 							RootHref:      rootHref,
 							CurrentRefDir: params.Ref.DirName(),
 							Selected:      "code",
+							AssetNames:    params.AssetNames,
 						},
 						HeaderParams: templates.HeaderParams{
 							Ref:         params.Ref,
main.go
@@ -35,6 +35,7 @@ type Params struct {
 	LightStyle string
 	DarkStyle  string
 	DefaultRef git.Ref
+	AssetNames map[string]string
 }
 
 func main() {
@@ -166,10 +167,11 @@ func main() {
 		LightStyle: flagLightTheme,
 		DarkStyle:  flagDarkTheme,
 		DefaultRef: git.NewRef(flagDefaultBranch),
+		AssetNames: nil,
 	}
 
 	// first, get our assets together as we refer to them in the templates
-	if err = createAssets(params); err != nil {
+	if params.AssetNames, err = createAssets(params, flagMinify); err != nil {
 		panic(err)
 	}
 
post_process.go
@@ -12,7 +12,6 @@ import (
 	"sync"
 
 	"github.com/tdewolff/minify/v2"
-	"github.com/tdewolff/minify/v2/css"
 	"github.com/tdewolff/minify/v2/html"
 	"github.com/tdewolff/minify/v2/svg"
 
@@ -20,9 +19,9 @@ import (
 )
 
 func postProcess(root string, doMinify bool, doGzip bool) error {
-	// 1) Collect all HTML and CSS files first
+	// 1) Collect all HTML files first
+	// (no need to collect css, it's pre-minified)
 	var html_files []string
-	var css_files []string
 	if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
 		if err != nil {
 			return err
@@ -33,15 +32,12 @@ func postProcess(root string, doMinify bool, doGzip bool) error {
 		if strings.HasSuffix(d.Name(), ".html") {
 			html_files = append(html_files, path)
 		}
-		if strings.HasSuffix(d.Name(), ".css") {
-			css_files = append(css_files, path)
-		}
 		return nil
 	}); err != nil {
 		return err
 	}
 
-	if len(html_files) == 0 && len(css_files) == 0 {
+	if len(html_files) == 0 {
 		return nil
 	}
 
@@ -53,7 +49,7 @@ func postProcess(root string, doMinify bool, doGzip bool) error {
 	if doGzip {
 		labels = append(labels, "gzip")
 	}
-	pb := progress_bar.NewProgressBar(strings.Join(labels, " + "), len(html_files)+len(css_files))
+	pb := progress_bar.NewProgressBar(strings.Join(labels, " + "), len(html_files))
 	defer pb.Done()
 
 	// 3) Worker pool
@@ -62,7 +58,6 @@ func postProcess(root string, doMinify bool, doGzip bool) error {
 		workers = 1
 	}
 	html_jobs := make(chan string, workers*2)
-	css_jobs := make(chan string, workers*2)
 	var wg sync.WaitGroup
 	var mu sync.Mutex
 	var firstErr error
@@ -73,7 +68,6 @@ func postProcess(root string, doMinify bool, doGzip bool) error {
 		if doMinify {
 			m = minify.New()
 			m.AddFunc("text/html", html.Minify)
-			m.AddFunc("text/css", css.Minify)
 			m.AddFunc("image/svg+xml", svg.Minify)
 		}
 		for path := range queue {
@@ -122,16 +116,6 @@ func postProcess(root string, doMinify bool, doGzip bool) error {
 	close(html_jobs)
 	wg.Wait()
 
-	wg.Add(workers)
-	for i := 0; i < workers; i++ {
-		go workerFn("text/css", css_jobs)
-	}
-	for _, f := range css_files {
-		css_jobs <- f
-	}
-	close(css_jobs)
-	wg.Wait()
-
 	return firstErr
 }
 
tags.go
@@ -30,6 +30,7 @@ func generateTags(entries []git.Tag, params Params) error {
 			RootHref:      rootHref,
 			CurrentRefDir: params.DefaultRef.DirName(),
 			Selected:      "tags",
+			AssetNames:    params.AssetNames,
 		},
 		Tags: entries,
 	})