mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-09-24 16:05:44 +08:00
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
32 lines
1.1 KiB
Go
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)
|
|
}
|