fix: 静态资源加缓存破坏版本号,解决浏览器缓存旧 JS 导致功能不生效

- 启动时生成 AssetVersion(base36 时间戳),注入模板 {{.V}}
- 所有 JS/CSS 引用加 ?v={{.V}},重启即刷新缓存
- 涉及前端、管理后台共 12 个模板文件
This commit is contained in:
2026-05-31 01:46:47 +08:00
parent 0bcaadceb4
commit b8658712bb
15 changed files with 32 additions and 21 deletions

View File

@ -5,12 +5,21 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"regexp" "regexp"
"strconv"
"time"
"metazone.cc/metalab/internal/model" "metazone.cc/metalab/internal/model"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
// AssetVersion 静态资源版本号(服务启动时生成,重启即刷新缓存)
var AssetVersion string
func init() {
AssetVersion = strconv.FormatInt(time.Now().Unix(), 36)
}
// mdImageRe 匹配 Markdown 图片语法 ![alt](url) // mdImageRe 匹配 Markdown 图片语法 ![alt](url)
var mdImageRe = regexp.MustCompile(`!\[.*?\]\((.*?)\)`) var mdImageRe = regexp.MustCompile(`!\[.*?\]\((.*?)\)`)
@ -48,6 +57,8 @@ func BuildPageData(c *gin.Context, extra gin.H) gin.H {
if isMaintenance, exists := c.Get("is_maintenance"); exists { if isMaintenance, exists := c.Get("is_maintenance"); exists {
data["IsMaintenance"] = isMaintenance data["IsMaintenance"] = isMaintenance
} }
// 注入静态资源版本号
data["V"] = AssetVersion
return data return data
} }

View File

@ -42,7 +42,7 @@
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="19" x2="12" y2="5"/><polyline points="5 12 12 5 19 12"/></svg> <svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="19" x2="12" y2="5"/><polyline points="5 12 12 5 19 12"/></svg>
</button> </button>
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
<script src="/static/js/login.js"></script> <script src="/static/js/login.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -89,7 +89,7 @@
<svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="19" x2="12" y2="5"/><polyline points="5 12 12 5 19 12"/></svg> <svg class="icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="12" y1="19" x2="12" y2="5"/><polyline points="5 12 12 5 19 12"/></svg>
</button> </button>
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
<script src="/static/js/register.js"></script> <script src="/static/js/register.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -37,6 +37,6 @@
{{template "layout/footer.html" .}} {{template "layout/footer.html" .}}
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -13,9 +13,9 @@
{{if .OgURL}}<meta property="og:url" content="{{.OgURL}}">{{end}} {{if .OgURL}}<meta property="og:url" content="{{.OgURL}}">{{end}}
{{end}} {{end}}
<title>{{.Title}} - MetaLab</title> <title>{{.Title}} - MetaLab</title>
<link rel="stylesheet" href="/static/css/common.css"> <link rel="stylesheet" href="/static/css/common.css?v={{.V}}">
{{if .ExtraCSS}} {{if .ExtraCSS}}
<link rel="stylesheet" href="{{.ExtraCSS}}"> <link rel="stylesheet" href="{{.ExtraCSS}}?v={{.V}}">
{{end}} {{end}}
<script src="/shared/static/js/utils.js"></script> <script src="/shared/static/js/utils.js"></script>
</head> </head>

View File

@ -202,6 +202,6 @@
} }
})(); })();
</script> </script>
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -9,6 +9,6 @@
</div> </div>
</div> </div>
{{template "layout/footer.html" .}} {{template "layout/footer.html" .}}
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -53,6 +53,6 @@
{{template "layout/footer.html" .}} {{template "layout/footer.html" .}}
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -64,7 +64,7 @@
<link rel="stylesheet" href="/static/vditor/dist/index.css"> <link rel="stylesheet" href="/static/vditor/dist/index.css">
<script src="/static/vditor/dist/index.min.js"></script> <script src="/static/vditor/dist/index.min.js"></script>
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
<script type="module" src="/static/js/editor.js"></script> <script type="module" src="/static/js/editor.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -50,8 +50,8 @@
<link rel="stylesheet" href="/static/vditor/dist/index.css"> <link rel="stylesheet" href="/static/vditor/dist/index.css">
<script src="/static/vditor/dist/method.min.js"></script> <script src="/static/vditor/dist/method.min.js"></script>
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
<script src="/static/js/shortcode.js"></script> <script src="/static/js/shortcode.js?v={{.V}}"></script>
<script> <script>
(function() { (function() {
var el = document.getElementById('postContent'); var el = document.getElementById('postContent');

View File

@ -253,7 +253,7 @@
</div> </div>
</div> </div>
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
<script> <script>
(function () { (function () {
'use strict'; 'use strict';

View File

@ -88,6 +88,6 @@
{{template "layout/footer.html" .}} {{template "layout/footer.html" .}}
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -66,7 +66,7 @@
<link rel="stylesheet" href="/static/vditor/dist/index.css"> <link rel="stylesheet" href="/static/vditor/dist/index.css">
<script src="/static/vditor/dist/index.min.js"></script> <script src="/static/vditor/dist/index.min.js"></script>
<script src="/static/js/common.js"></script> <script src="/static/js/common.js?v={{.V}}"></script>
<script src="/static/js/studio-editor.js"></script> <script src="/static/js/studio-editor.js?v={{.V}}"></script>
</body> </body>
</html> </html>

View File

@ -1,7 +1,7 @@
</main> </main>
</div> </div>
<script src="/shared/static/js/utils.js"></script> <script src="/shared/static/js/utils.js?v={{.V}}"></script>
<script src="/admin/static/js/common.js"></script> <script src="/admin/static/js/common.js?v={{.V}}"></script>
{{if .ExtraJS}} {{if .ExtraJS}}
<script src="{{.ExtraJS}}"></script> <script src="{{.ExtraJS}}"></script>
{{end}} {{end}}

View File

@ -82,5 +82,5 @@
</div> </div>
{{end}} {{end}}
<script src="/admin/static/js/posts.js"></script> <script src="/admin/static/js/posts.js?v={{.V}}"></script>
{{template "admin/layout/footer.html" .}} {{template "admin/layout/footer.html" .}}