Skip to content

Commit

Permalink
Add a ?v=hash cache breaker (that changes on restart) to static ass…
Browse files Browse the repository at this point in the history
…ets.
  • Loading branch information
knadh committed Dec 31, 2023
1 parent a2629c2 commit 0c9dc07
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions Makefile
Expand Up @@ -13,6 +13,7 @@ FRONTEND_YARN_MODULES = frontend/node_modules
FRONTEND_DIST = frontend/dist
FRONTEND_DEPS = \
$(FRONTEND_YARN_MODULES) \
frontend/index.html \
frontend/package.json \
frontend/vite.config.js \
frontend/.eslintrc.js \
Expand Down
3 changes: 3 additions & 0 deletions cmd/handlers.go
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"crypto/subtle"
"net/http"
"path"
Expand Down Expand Up @@ -237,6 +238,8 @@ func handleAdminPage(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

b = bytes.ReplaceAll(b, []byte("asset_version"), []byte(app.constants.AssetVersion))

return c.HTMLBlob(http.StatusOK, b)
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/init.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"html/template"
Expand Down Expand Up @@ -94,6 +95,7 @@ type constants struct {
OptinURL string
MessageURL string
ArchiveURL string
AssetVersion string

MediaUpload struct {
Provider string
Expand Down Expand Up @@ -414,6 +416,9 @@ func initConstants() *constants {
c.BounceSendgridEnabled = ko.Bool("bounce.sendgrid_enabled")
c.BouncePostmarkEnabled = ko.Bool("bounce.postmark.enabled")

b := md5.Sum([]byte(time.Now().String()))
c.AssetVersion = fmt.Sprintf("%x", b)[0:10]

return &c
}

Expand Down Expand Up @@ -756,6 +761,7 @@ func initHTTPServer(app *App) *echo.Echo {
RootURL: app.constants.RootURL,
LogoURL: app.constants.LogoURL,
FaviconURL: app.constants.FaviconURL,
AssetVersion: app.constants.AssetVersion,
EnablePublicSubPage: app.constants.EnablePublicSubPage,
EnablePublicArchive: app.constants.EnablePublicArchive,
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/public.go
Expand Up @@ -30,6 +30,7 @@ type tplRenderer struct {
RootURL string
LogoURL string
FaviconURL string
AssetVersion string
EnablePublicSubPage bool
EnablePublicArchive bool
}
Expand All @@ -41,6 +42,7 @@ type tplData struct {
RootURL string
LogoURL string
FaviconURL string
AssetVersion string
EnablePublicSubPage bool
EnablePublicArchive bool
Data interface{}
Expand Down Expand Up @@ -94,6 +96,7 @@ func (t *tplRenderer) Render(w io.Writer, name string, data interface{}, c echo.
RootURL: t.RootURL,
LogoURL: t.LogoURL,
FaviconURL: t.FaviconURL,
AssetVersion: t.AssetVersion,
EnablePublicSubPage: t.EnablePublicSubPage,
EnablePublicArchive: t.EnablePublicArchive,
Data: data,
Expand Down
6 changes: 3 additions & 3 deletions frontend/index.html
Expand Up @@ -4,9 +4,9 @@
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<link rel="icon" href="/admin/static/favicon.png" />
<link href="/admin/custom.css" rel="stylesheet" type="text/css">
<script src="/admin/custom.js" async defer></script>
<link rel="icon" href="/admin/static/favicon.png?v=asset_version" />
<link href="/admin/custom.css?v=asset_version" rel="stylesheet" type="text/css">
<script src="/admin/custom.js?v=asset_version" async defer></script>
<title>listmonk</title>
</head>
<body>
Expand Down
10 changes: 5 additions & 5 deletions static/public/templates/index.html
Expand Up @@ -12,14 +12,14 @@
href="{{ .RootURL }}/archive.xml" />
{{ end }}

<link href="/public/static/style.css?v2.3.0" rel="stylesheet" type="text/css" />
<link href="/public/custom.css" rel="stylesheet" type="text/css">
<script src="/public/custom.js" async defer></script>
<link href="/public/static/style.css?v={{ .AssetVersion }}" rel="stylesheet" type="text/css" />
<link href="/public/custom.css?v={{ .AssetVersion }}" rel="stylesheet" type="text/css">
<script src="/public/custom.js?v={{ .AssetVersion }}" async defer></script>

{{ if ne .FaviconURL "" }}
<link rel="shortcut icon" href="{{ .FaviconURL }}" type="image/x-icon" />
{{ else }}
<link rel="shortcut icon" href="/public/static/favicon.png" type="image/x-icon" />
<link rel="shortcut icon" href="/public/static/favicon.png?v={{ .AssetVersion }}" type="image/x-icon" />
{{ end }}
</head>
<body>
Expand All @@ -30,7 +30,7 @@
{{ if ne .LogoURL "" }}
<img src="{{ .LogoURL }}" alt="{{ .Data.Title }}" /></a>
{{ else }}
<img src="/public/static/logo.svg" alt="{{ .Data.Title }}" />
<img src="/public/static/logo.svg?v={{ .AssetVersion }}" alt="{{ .Data.Title }}" />
{{ end }}
</a>
</div>
Expand Down

0 comments on commit 0c9dc07

Please sign in to comment.