Skip to content

Commit

Permalink
add version to base.css stylesheet link
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasHiltl committed Sep 13, 2024
1 parent c67b534 commit 22ae4e4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 28 deletions.
13 changes: 7 additions & 6 deletions internal/handler/web/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@ func index(e *env, w http.ResponseWriter, r *http.Request) error {
}

return e.render.RenderIndex(r.Context(), w, render.RenderIndexArgs{
FeedURL: handler.ChangelogToFeedURL(r),
CL: parsed.CL,
Articles: parsed.Articles,
HasMore: parsed.HasMore,
PageSize: pageSize,
NextPage: page + 1,
FeedURL: handler.ChangelogToFeedURL(r),
CL: parsed.CL,
Articles: parsed.Articles,
HasMore: parsed.HasMore,
PageSize: pageSize,
NextPage: page + 1,
BaseCSSVersion: e.baseCSSVersion,
})
}

Expand Down
29 changes: 23 additions & 6 deletions internal/handler/web/routes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package web

import (
"crypto/md5"
"embed"
"encoding/hex"
"errors"
"net/http"

Expand All @@ -27,16 +29,31 @@ func NewEnv(
render render.Renderer,
) *env {
return &env{
cfg: cfg,
loader: loader,
render: render,
cfg: cfg,
loader: loader,
render: render,
baseCSSVersion: calculateBaseCSSVersion(),
}
}

// Calculates the md5 hash of the static/base.css file.
// Needed for cache busting if base.css changes.
func calculateBaseCSSVersion() string {
fileContent, err := staticAssets.ReadFile("static/base.css")
if err != nil {
return ""
}

hash := md5.New()
hash.Write(fileContent)
return hex.EncodeToString(hash.Sum(nil))
}

type env struct {
loader *changelog.Loader
cfg config.Config
render render.Renderer
loader *changelog.Loader
cfg config.Config
render render.Renderer
baseCSSVersion string
}

func serveHTTP(env *env, h func(e *env, w http.ResponseWriter, r *http.Request) error) func(http.ResponseWriter, *http.Request) {
Expand Down
9 changes: 6 additions & 3 deletions internal/handler/web/views/layout/main.templ
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package layout

import "fmt"

type MainArgs struct {
Title string
Description string
Title string
Description string
BaseCSSVersion string
}

templ Main(args MainArgs) {
Expand All @@ -17,7 +20,7 @@ templ Main(args MainArgs) {
<meta name="description" content={ args.Description }/>
}
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="stylesheet" type="text/css" media="screen" href="/static/base.css"/>
<link rel="stylesheet" type="text/css" media="screen" href={ fmt.Sprintf("/static/base.css?v=%s", args.BaseCSSVersion) }/>
<link rel="stylesheet" href="https://rsms.me/inter/inter.css"/>
<script src="https://unpkg.com/htmx.org@1.9.12" integrity="sha384-ujb1lZYygJmzgSwoxRggbCHcjc0rB2XoQrxeTUQyRjrOnlCoYta87iKBWq3EsdM2" crossorigin="anonymous"></script>
</head>
Expand Down
26 changes: 21 additions & 5 deletions internal/handler/web/views/layout/main_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions render/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ type Renderer interface {
}

type RenderIndexArgs struct {
CL store.Changelog
Articles []changelog.ParsedArticle
HasMore bool
NextPage int
PageSize int
FeedURL string
CL store.Changelog
Articles []changelog.ParsedArticle
HasMore bool
NextPage int
PageSize int
FeedURL string
BaseCSSVersion string
}

type RenderArticleListArgs struct {
Expand Down Expand Up @@ -81,8 +82,9 @@ func (r *renderer) RenderIndex(ctx context.Context, w io.Writer, args RenderInde
FeedURL: args.FeedURL,
},
MainArgs: layout.MainArgs{
Title: args.CL.Title.V(),
Description: args.CL.Subtitle.V(),
Title: args.CL.Title.V(),
Description: args.CL.Subtitle.V(),
BaseCSSVersion: args.BaseCSSVersion,
},
ThemeArgs: components.ThemeArgs{
ColorScheme: args.CL.ColorScheme.ToApiTypes(),
Expand Down

0 comments on commit 22ae4e4

Please sign in to comment.