feat: 推广 V3.5 实时氢耗并完善导出

This commit is contained in:
lingniu
2026-09-04 15:42:02 +08:00
parent a26179c8fe
commit e402eb5e60
53 changed files with 2855 additions and 320 deletions
@@ -19,9 +19,21 @@ func Handler(dir string, fallback http.Handler) http.Handler {
}
path := filepath.Join(dir, filepath.Clean(r.URL.Path))
if info, err := os.Stat(path); err == nil && !info.IsDir() {
setCachePolicy(w, r.URL.Path)
fs.ServeHTTP(w, r)
return
}
w.Header().Set("Cache-Control", "no-store")
http.ServeFile(w, r, filepath.Join(dir, "index.html"))
})
}
func setCachePolicy(w http.ResponseWriter, path string) {
if path == "/" || path == "/index.html" || path == "/app-config.js" {
w.Header().Set("Cache-Control", "no-store")
return
}
if strings.HasPrefix(path, "/assets/") {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
}
}
@@ -0,0 +1,45 @@
package static
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
func TestHandlerDisablesCachingForApplicationShell(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "index.html"), []byte("index"), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "app-config.js"), []byte("config"), 0o600); err != nil {
t.Fatal(err)
}
handler := Handler(dir, http.NotFoundHandler())
for _, path := range []string{"/", "/index.html", "/app-config.js", "/users"} {
response := httptest.NewRecorder()
handler.ServeHTTP(response, httptest.NewRequest(http.MethodGet, path, nil))
if got := response.Header().Get("Cache-Control"); got != "no-store" {
t.Fatalf("%s Cache-Control=%q, want no-store", path, got)
}
}
}
func TestHandlerCachesHashedAssetsImmutably(t *testing.T) {
dir := t.TempDir()
assets := filepath.Join(dir, "assets")
if err := os.Mkdir(assets, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(assets, "app-1234.js"), []byte("app"), 0o600); err != nil {
t.Fatal(err)
}
response := httptest.NewRecorder()
Handler(dir, http.NotFoundHandler()).ServeHTTP(response, httptest.NewRequest(http.MethodGet, "/assets/app-1234.js", nil))
if got := response.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
t.Fatalf("asset Cache-Control=%q", got)
}
}