Files
sub2api/backend/internal/web/static_cache.go
T
Drswith 1e97e4cee4 fix(web): set long-lived Cache-Control for embedded static assets
Direct Go/Docker deployments were serving Vite-hashed /assets without
Cache-Control, so browsers re-downloaded the console on every visit.
Align with deploy/Caddyfile while keeping index.html on no-cache.

Closes #4129
2026-07-13 11:14:15 +08:00

32 lines
1.1 KiB
Go

//go:build embed || unit
package web
import (
"net/http"
"strings"
)
// staticAssetsCacheControl matches deploy/Caddyfile for hashed frontend assets.
// Vite emits content-hashed filenames under assets/, so long-lived immutable
// caching is safe without relying on a reverse proxy.
const staticAssetsCacheControl = "public, max-age=31536000, immutable"
// isLongCacheStaticPath reports whether a cleaned URL path (no leading slash)
// should receive long-lived Cache-Control headers. Aligned with deploy/Caddyfile.
func isLongCacheStaticPath(cleanPath string) bool {
cleanPath = strings.TrimPrefix(cleanPath, "/")
return strings.HasPrefix(cleanPath, "assets/") ||
cleanPath == "logo.png" ||
cleanPath == "favicon.ico"
}
// applyStaticAssetCacheHeaders sets Cache-Control for long-cacheable static paths.
// index.html / SPA routes must keep no-cache and are not handled here.
func applyStaticAssetCacheHeaders(header http.Header, cleanPath string) {
if header == nil || !isLongCacheStaticPath(cleanPath) {
return
}
header.Set("Cache-Control", staticAssetsCacheControl)
}