aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2025-11-01 18:01:50 +0100
committerGabriel A. Giovanini <mail@gabrielgio.me>2025-11-01 18:03:45 +0100
commit49fc8733129028ff4a3537b9eb20f548f0e3e9e6 (patch)
tree71b579b2914ce97b38d52c29795c3e3ba6518329
parent5dd940eb52b40c78a2078ed0a02440e84bee0306 (diff)
downloadcerrado-49fc8733129028ff4a3537b9eb20f548f0e3e9e6.tar.gz
cerrado-49fc8733129028ff4a3537b9eb20f548f0e3e9e6.tar.bz2
cerrado-49fc8733129028ff4a3537b9eb20f548f0e3e9e6.zip
feat: Add slug to css so we can cache it forever
-rw-r--r--pkg/handler/router.go4
-rw-r--r--pkg/handler/static/handler.go22
-rw-r--r--templates/base.qtpl3
-rw-r--r--templates/base.qtpl.go58
4 files changed, 52 insertions, 35 deletions
diff --git a/pkg/handler/router.go b/pkg/handler/router.go
index bc81350..cb5d6f5 100644
--- a/pkg/handler/router.go
+++ b/pkg/handler/router.go
@@ -1,6 +1,7 @@
package handler
import (
+ "fmt"
"net/http"
serverconfig "git.gabrielgio.me/cerrado/pkg/config"
@@ -10,6 +11,7 @@ import (
"git.gabrielgio.me/cerrado/pkg/handler/git"
"git.gabrielgio.me/cerrado/pkg/handler/static"
"git.gabrielgio.me/cerrado/pkg/service"
+ "git.gabrielgio.me/cerrado/templates"
)
// Mount handler gets the requires service and repository to build the handlers
@@ -53,7 +55,7 @@ func MountHandler(
}
mux.HandleFunc("/static/{file}", staticHandler)
- mux.HandleFunc("/static/theme", cssStaticHandler)
+ mux.HandleFunc(fmt.Sprintf("/static/theme%s.css", templates.Slug), cssStaticHandler) // add slug so css file can be cached forever.
mux.HandleFunc("/{name}/about/{$}", gitHandler.About)
mux.HandleFunc("/{name}", gitHandler.Multiplex)
mux.HandleFunc("/{name}/{rest...}", gitHandler.Multiplex)
diff --git a/pkg/handler/static/handler.go b/pkg/handler/static/handler.go
index 779c786..6cc884e 100644
--- a/pkg/handler/static/handler.go
+++ b/pkg/handler/static/handler.go
@@ -1,6 +1,7 @@
package static
import (
+ "bytes"
"fmt"
"io"
"io/fs"
@@ -45,23 +46,32 @@ func ServeStaticCSSHandler(lightTheme, darkTheme string) (ext.ErrorRequestHandle
return func(w http.ResponseWriter, r *ext.Request) error {
ext.SetMIME(w, "text/css")
+ w.Header().Add("Cache-Control", "max-age=31536000")
+
+ // use buffer so this function can fail before writing to http.ResponseWriter
+ var buffer bytes.Buffer
var style *chroma.Style
style = darkStyle
- w.Write([]byte("[data-bs-theme=\"dark\"] {\n"))
- err := formatter.WriteCSS(&ws{w}, style)
+ buffer.Write([]byte("[data-bs-theme=\"dark\"] {\n"))
+ err := formatter.WriteCSS(&ws{&buffer}, style)
if err != nil {
return err
}
- w.Write([]byte("}\n"))
+ buffer.Write([]byte("}\n"))
style = lightStyle
- w.Write([]byte("[data-bs-theme=\"light\"] {\n"))
- err = formatter.WriteCSS(&ws{w}, style)
+ buffer.Write([]byte("[data-bs-theme=\"light\"] {\n"))
+ err = formatter.WriteCSS(&ws{&buffer}, style)
+ if err != nil {
+ return err
+ }
+ buffer.Write([]byte("}"))
+
+ _, err = io.Copy(w, &buffer)
if err != nil {
return err
}
- w.Write([]byte("\n}"))
return nil
}, nil
diff --git a/templates/base.qtpl b/templates/base.qtpl
index e43fb67..1dddb57 100644
--- a/templates/base.qtpl
+++ b/templates/base.qtpl
@@ -60,8 +60,7 @@ Page prints a page implementing Page interface.
<link rel="icon" href="data:,">
<title>{%= p.Title(ctx) %}</title>
<link rel="stylesheet" href="/static/main{%s Slug %}.css">
- <link rel="stylesheet" href="/static/themes/dark">
- <link rel="stylesheet" href="/static/themes/light">
+ <link rel="stylesheet" href="/static/theme{%s Slug %}.css">
<html data-bs-theme="dark">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1" />
diff --git a/templates/base.qtpl.go b/templates/base.qtpl.go
index 783de2c..3c4d8a9 100644
--- a/templates/base.qtpl.go
+++ b/templates/base.qtpl.go
@@ -8,14 +8,16 @@
package templates
//line templates/base.qtpl:3
-import (
- "context"
- "strconv"
- "time" //line templates/base.qtpl:4
+import "context"
+
+//line templates/base.qtpl:4
+import "strconv"
- //line templates/base.qtpl:5
- //line templates/base.qtpl:7
+//line templates/base.qtpl:5
+import "time"
+//line templates/base.qtpl:7
+import (
qtio422016 "io"
qt422016 "github.com/valyala/quicktemplate"
@@ -110,60 +112,64 @@ func StreamPageTemplate(qw422016 *qt422016.Writer, p Page, ctx context.Context)
qw422016.E().S(Slug)
//line templates/base.qtpl:62
qw422016.N().S(`.css">
- <link rel="stylesheet" href="/static/theme">
+ <link rel="stylesheet" href="/static/theme`)
+//line templates/base.qtpl:63
+ qw422016.E().S(Slug)
+//line templates/base.qtpl:63
+ qw422016.N().S(`.css">
<html data-bs-theme="dark">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
`)
-//line templates/base.qtpl:70
+//line templates/base.qtpl:69
p.StreamNavbar(qw422016, ctx)
-//line templates/base.qtpl:70
+//line templates/base.qtpl:69
qw422016.N().S(`
<div class="container">
`)
-//line templates/base.qtpl:72
+//line templates/base.qtpl:71
p.StreamContent(qw422016, ctx)
-//line templates/base.qtpl:72
+//line templates/base.qtpl:71
qw422016.N().S(`
</div>
</body>
`)
-//line templates/base.qtpl:75
+//line templates/base.qtpl:74
p.StreamScript(qw422016, ctx)
-//line templates/base.qtpl:75
+//line templates/base.qtpl:74
qw422016.N().S(`
<script>
function a(){const e=window.matchMedia("(prefers-color-scheme: dark)").matches;document.documentElement.setAttribute("data-bs-theme",e?"dark":"light")}a(),window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change",a);
</script>
</html>
`)
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
}
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
func WritePageTemplate(qq422016 qtio422016.Writer, p Page, ctx context.Context) {
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
qw422016 := qt422016.AcquireWriter(qq422016)
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
StreamPageTemplate(qw422016, p, ctx)
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
qt422016.ReleaseWriter(qw422016)
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
}
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
func PageTemplate(p Page, ctx context.Context) string {
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
qb422016 := qt422016.AcquireByteBuffer()
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
WritePageTemplate(qb422016, p, ctx)
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
qs422016 := string(qb422016.B)
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
qt422016.ReleaseByteBuffer(qb422016)
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
return qs422016
-//line templates/base.qtpl:80
+//line templates/base.qtpl:79
}