Add a ?v=hash cache breaker (that changes on restart) to static assets.

This commit is contained in:
Kailash Nadh 2023-12-31 22:20:37 +05:30
parent a2629c2c31
commit 0c9dc07479
6 changed files with 21 additions and 8 deletions

View file

@ -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 \

View file

@ -1,6 +1,7 @@
package main
import (
"bytes"
"crypto/subtle"
"net/http"
"path"
@ -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)
}

View file

@ -2,6 +2,7 @@ package main
import (
"bytes"
"crypto/md5"
"encoding/json"
"fmt"
"html/template"
@ -94,6 +95,7 @@ type constants struct {
OptinURL string
MessageURL string
ArchiveURL string
AssetVersion string
MediaUpload struct {
Provider string
@ -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
}
@ -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,
}

View file

@ -30,6 +30,7 @@ type tplRenderer struct {
RootURL string
LogoURL string
FaviconURL string
AssetVersion string
EnablePublicSubPage bool
EnablePublicArchive bool
}
@ -41,6 +42,7 @@ type tplData struct {
RootURL string
LogoURL string
FaviconURL string
AssetVersion string
EnablePublicSubPage bool
EnablePublicArchive bool
Data interface{}
@ -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,

6
frontend/index.html vendored
View file

@ -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>

View file

@ -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>
@ -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>