Every text-[color:var(--ink)]/50, border-[color:var(--ink)]/10 and
bg-[color:var(--ink)]/5 in the templates compiled to nothing. Tailwind v3
cannot put an opacity on an arbitrary var() colour, so all 538 of them were
dropped from output.css without a word. Meta text rendered at full ink, card
borders fell back to Tailwind's default grey (bright white rings at night),
and the washes, backdrops and hover pills simply weren't there, on the news
site, the casino and the adventure pages alike.
The palette is now written once per phase and casino room as bare rgb
channels (--ink-rgb and friends), the plain vars are built from them, and
tailwind.config.js exposes ink, muted, accent, warn and card as real colours.
The classes are text-ink/80, border-ink/10, bg-accent/20 now, and they compile.
Restoring the intended opacities as-is would have traded flat for illegible:
ink at 50% measures 2.7 to 2.9:1 on the lit phases. So secondary text gets
its own colour, --muted, per phase and per casino room, taken as faint as it
can go while still clearing 4.5:1 on both the card and the page. Everything
that was ink at 35 to 70% is text-muted. The 20-30% ones are separators and
empty-cell dashes and stay faint.
The bg-theme-* chips carry white text everywhere they appear, and eight of
them measured 2.5 to 3.2:1. Same hues, taken just dark enough to pass (4.6 to
4.7:1). eu, lego and adventure already passed and are unchanged, and the
underline and border colours keep the old pastels since they carry no text.
Static files came out of the embedded FS with no Last-Modified, no ETag and
no Cache-Control, so every page view downloaded the stylesheet and every
script in full, and the service worker's "cheap 304" never happened. Each
file is hashed once now. Templates link through {{asset}}, which appends
?v=<hash>; a request naming the current hash is served immutable for a year,
and everything else gets the hash as its ETag and revalidates to a 304. The
service worker serves versioned URLs cache-first, drops older versions of the
same file as new ones land, and moves to cache v6.
86 lines
2.7 KiB
Go
86 lines
2.7 KiB
Go
package web
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"io/fs"
|
|
"net/http"
|
|
"strings"
|
|
"sync"
|
|
)
|
|
|
|
// Static assets are served out of the embedded FS, and embedded files have no
|
|
// modification time, so http.FileServer sends no Last-Modified and no ETag. With
|
|
// no validator and no Cache-Control, every page view re-downloaded the whole
|
|
// stylesheet and every script in full.
|
|
//
|
|
// So each file is hashed once, at first use, and the hash does two jobs:
|
|
//
|
|
// - Templates link assets through {{asset "/static/..."}}, which appends
|
|
// ?v=<hash>. A request carrying the current hash is content-addressed, so it
|
|
// is served immutable for a year: the next deploy that changes the file
|
|
// changes the URL, and the browser never has to ask.
|
|
// - Every static response carries the hash as its ETag. Anything fetched by a
|
|
// bare URL (audio samples, scripts loading scripts, a stale page asking for
|
|
// an old ?v=) is served no-cache, and the revalidation is a cheap 304.
|
|
|
|
var (
|
|
assetOnce sync.Once
|
|
assetHashes map[string]string // path under static/ -> short content hash
|
|
)
|
|
|
|
func loadAssetHashes() map[string]string {
|
|
assetOnce.Do(func() {
|
|
assetHashes = map[string]string{}
|
|
sub, err := fs.Sub(staticFS, "static")
|
|
if err != nil {
|
|
return
|
|
}
|
|
_ = fs.WalkDir(sub, ".", func(path string, d fs.DirEntry, err error) error {
|
|
if err != nil || d.IsDir() {
|
|
return err
|
|
}
|
|
b, err := fs.ReadFile(sub, path)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
sum := sha256.Sum256(b)
|
|
assetHashes[path] = hex.EncodeToString(sum[:])[:12]
|
|
return nil
|
|
})
|
|
})
|
|
return assetHashes
|
|
}
|
|
|
|
// assetURL is the {{asset}} template func: "/static/css/output.css" becomes
|
|
// "/static/css/output.css?v=<hash>". A path that isn't an embedded file comes
|
|
// back unchanged rather than breaking the page.
|
|
func assetURL(path string) string {
|
|
h, ok := loadAssetHashes()[strings.TrimPrefix(path, "/static/")]
|
|
if !ok {
|
|
return path
|
|
}
|
|
return path + "?v=" + h
|
|
}
|
|
|
|
// staticHandler serves the embedded static tree (mounted under /static/ with the
|
|
// prefix already stripped) with a content-hash ETag and a cache policy that
|
|
// depends on whether the URL named the current hash.
|
|
func staticHandler(sub fs.FS) http.Handler {
|
|
files := http.FileServer(http.FS(sub))
|
|
hashes := loadAssetHashes()
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if h, ok := hashes[strings.TrimPrefix(r.URL.Path, "/")]; ok {
|
|
// FileServer honours If-None-Match against an ETag already set on
|
|
// the response, so this alone turns revalidation into a 304.
|
|
w.Header().Set("ETag", `"`+h+`"`)
|
|
if r.URL.Query().Get("v") == h {
|
|
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
|
|
} else {
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
}
|
|
}
|
|
files.ServeHTTP(w, r)
|
|
})
|
|
}
|