mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-26 01:41:04 +08:00
Merge pull request #404 from Lusitaniae/pr/webui-caching
web: add ETag, gzip and immutable caching to the embedded SPA handler
This commit is contained in:
@@ -0,0 +1,176 @@
|
||||
package server
|
||||
|
||||
// Static-asset serving for the embedded SPA.
|
||||
//
|
||||
// The Next.js export ships ~1.55 MB of JS/CSS. Two properties made that painful
|
||||
// on a high-latency link (measured from the lab: 211 ms RTT, 0.28 MB/s per SSH
|
||||
// stream, 17 assets totalling 1,554,053 B):
|
||||
//
|
||||
// - no compression: the handler served raw bytes and ignored Accept-Encoding,
|
||||
// so 1.55 MB crossed the wire where ~450 KB would do;
|
||||
// - no cache validators: files read out of embed.FS report a zero modtime, so
|
||||
// http.ServeContent emitted no Last-Modified, and nothing set an ETag
|
||||
// either. With no validator the browser re-downloaded the whole bundle on
|
||||
// every page load.
|
||||
//
|
||||
// Assets are digested and gzipped once, lazily, and held in memory (~450 KB
|
||||
// total). Content-hashed Next chunks get immutable caching; HTML stays
|
||||
// revalidate-only so a rebuild is never served stale.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"io/fs"
|
||||
"mime"
|
||||
"net/http"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
assetGzipMinBytes = 1024
|
||||
immutablePrefix = "_next/static/"
|
||||
immutableValue = "public, max-age=31536000, immutable"
|
||||
revalidateValue = "no-cache"
|
||||
)
|
||||
|
||||
// assetEntry is one embedded file, digested and pre-compressed.
|
||||
//
|
||||
// etag and gzETag are distinct strong validators, one per representation: bytes
|
||||
// on the wire for the raw body differ from those for the gzip body, so a single
|
||||
// validator shared by both would let a range or a conditional request validated
|
||||
// against one representation be answered from the other. Vary is not a
|
||||
// substitute — it only tells caches that the representations differ, it does not
|
||||
// make one ETag describe both.
|
||||
type assetEntry struct {
|
||||
raw []byte
|
||||
gz []byte
|
||||
etag string
|
||||
gzETag string
|
||||
ctype string
|
||||
}
|
||||
|
||||
// assetCache holds entries keyed by path within the embedded tree.
|
||||
var assetCache sync.Map
|
||||
|
||||
func loadAsset(fsys fs.FS, name string) (*assetEntry, error) {
|
||||
if v, ok := assetCache.Load(name); ok {
|
||||
return v.(*assetEntry), nil
|
||||
}
|
||||
raw, err := fs.ReadFile(fsys, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e := &assetEntry{raw: raw, ctype: assetContentType(name)}
|
||||
sum := sha256.Sum256(raw)
|
||||
e.etag = `"` + hex.EncodeToString(sum[:16]) + `"`
|
||||
if len(raw) >= assetGzipMinBytes && assetCompressible(e.ctype) {
|
||||
var buf bytes.Buffer
|
||||
if zw, zerr := gzip.NewWriterLevel(&buf, gzip.BestCompression); zerr == nil {
|
||||
_, _ = zw.Write(raw)
|
||||
_ = zw.Close()
|
||||
if buf.Len() < len(raw) {
|
||||
e.gz = buf.Bytes()
|
||||
// Digest the compressed bytes ourselves rather than suffixing
|
||||
// the raw validator: the plaintext hash would say nothing about
|
||||
// what this representation actually contains.
|
||||
gzSum := sha256.Sum256(e.gz)
|
||||
e.gzETag = `"` + hex.EncodeToString(gzSum[:16]) + `-gzip"`
|
||||
}
|
||||
}
|
||||
}
|
||||
actual, _ := assetCache.LoadOrStore(name, e)
|
||||
return actual.(*assetEntry), nil
|
||||
}
|
||||
|
||||
func assetContentType(name string) string {
|
||||
if ct := mime.TypeByExtension(path.Ext(name)); ct != "" {
|
||||
return ct
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
func assetCompressible(ctype string) bool {
|
||||
base := ctype
|
||||
if i := strings.IndexByte(base, ';'); i >= 0 {
|
||||
base = base[:i]
|
||||
}
|
||||
switch base {
|
||||
case "text/html", "text/css", "text/javascript", "application/javascript",
|
||||
"application/json", "image/svg+xml", "text/plain":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// gzipAccepted reports whether the client accepts gzip with a non-zero q-value.
|
||||
func gzipAccepted(r *http.Request) bool {
|
||||
for _, part := range strings.Split(r.Header.Get("Accept-Encoding"), ",") {
|
||||
fields := strings.Split(strings.TrimSpace(part), ";")
|
||||
coding := strings.ToLower(strings.TrimSpace(fields[0]))
|
||||
if coding != "gzip" && coding != "*" {
|
||||
continue
|
||||
}
|
||||
q := 1.0
|
||||
for _, attr := range fields[1:] {
|
||||
attr = strings.TrimSpace(attr)
|
||||
if strings.HasPrefix(attr, "q=") {
|
||||
if v, err := strconv.ParseFloat(strings.TrimPrefix(attr, "q="), 64); err == nil {
|
||||
q = v
|
||||
}
|
||||
}
|
||||
}
|
||||
if q > 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// serveWebAsset writes name from fsys with validators, and gzipped when the
|
||||
// client accepts it. Returns false when the file is absent or unreadable so the
|
||||
// caller can try the next candidate path.
|
||||
func serveWebAsset(w http.ResponseWriter, r *http.Request, fsys fs.FS, name string, immutable bool) bool {
|
||||
e, err := loadAsset(fsys, name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
h := w.Header()
|
||||
h.Set("Content-Type", e.ctype)
|
||||
h.Add("Vary", "Accept-Encoding")
|
||||
if immutable {
|
||||
h.Set("Cache-Control", immutableValue)
|
||||
} else {
|
||||
h.Set("Cache-Control", revalidateValue)
|
||||
}
|
||||
// Pick the representation first, then publish the validator for it. The raw
|
||||
// and gzip bodies are different entity-tags: sharing one would let a client
|
||||
// that holds the gzip ETag send If-Range and be served raw bytes (or the
|
||||
// reverse) as a 206.
|
||||
body := e.raw
|
||||
etag := e.etag
|
||||
if e.gz != nil && gzipAccepted(r) {
|
||||
h.Set("Content-Encoding", "gzip")
|
||||
body = e.gz
|
||||
etag = e.gzETag
|
||||
}
|
||||
h.Set("ETag", etag)
|
||||
// ServeContent handles Range, HEAD and If-None-Match against the ETag we
|
||||
// just set for this representation.
|
||||
// Zero modtime: content-hashed assets have no meaningful mtime and we do
|
||||
// not want a second, weaker validator in play.
|
||||
http.ServeContent(w, r, name, time.Time{}, bytes.NewReader(body))
|
||||
return true
|
||||
}
|
||||
|
||||
// webuiImmutable reports whether name may be cached forever. Only the
|
||||
// content-hashed Next build output qualifies; HTML must revalidate so a rebuild
|
||||
// is never served stale.
|
||||
func webuiImmutable(name string) bool {
|
||||
return strings.HasPrefix(name, immutablePrefix)
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"compress/gzip"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
)
|
||||
|
||||
func webuiTestFS() fstest.MapFS {
|
||||
return fstest.MapFS{
|
||||
"_next/static/chunks/app.js": {Data: bytes.Repeat([]byte("const a=1;\n"), 400)},
|
||||
"index.html": {Data: []byte("<html><body>hi</body></html>")},
|
||||
}
|
||||
}
|
||||
|
||||
const testAssetPath = "_next/static/chunks/app.js"
|
||||
|
||||
func TestServeWebAssetGzipAndValidators(t *testing.T) {
|
||||
fsys := webuiTestFS()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req.Header.Set("Accept-Encoding", "gzip, deflate, br")
|
||||
rec := httptest.NewRecorder()
|
||||
if !serveWebAsset(rec, req, fsys, testAssetPath, true) {
|
||||
t.Fatal("asset not served")
|
||||
}
|
||||
res := rec.Result()
|
||||
if got := res.Header.Get("Content-Encoding"); got != "gzip" {
|
||||
t.Errorf("Content-Encoding = %q, want gzip", got)
|
||||
}
|
||||
if got := res.Header.Get("Cache-Control"); got != immutableValue {
|
||||
t.Errorf("Cache-Control = %q, want %q", got, immutableValue)
|
||||
}
|
||||
etag := res.Header.Get("ETag")
|
||||
if etag == "" {
|
||||
t.Error("ETag missing")
|
||||
}
|
||||
if !strings.Contains(res.Header.Get("Vary"), "Accept-Encoding") {
|
||||
t.Error("Vary missing Accept-Encoding")
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("read body: %v", err)
|
||||
}
|
||||
zr, err := gzip.NewReader(bytes.NewReader(body))
|
||||
if err != nil {
|
||||
t.Fatalf("body is not gzip: %v", err)
|
||||
}
|
||||
got, err := io.ReadAll(zr)
|
||||
if err != nil {
|
||||
t.Fatalf("gunzip: %v", err)
|
||||
}
|
||||
if want := fsys[testAssetPath].Data; !bytes.Equal(got, want) {
|
||||
t.Errorf("round-trip mismatch: got %d bytes, want %d", len(got), len(want))
|
||||
}
|
||||
if len(body) >= len(fsys[testAssetPath].Data)/2 {
|
||||
t.Errorf("compression ineffective: %d of %d bytes", len(body), len(fsys[testAssetPath].Data))
|
||||
}
|
||||
|
||||
// The same representation carries the same ETag on a second, unrelated
|
||||
// request; the identity representation carries a different one (covered in
|
||||
// TestServeWebAssetPerRepresentationETag).
|
||||
rec2 := httptest.NewRecorder()
|
||||
req2 := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req2.Header.Set("Accept-Encoding", "gzip")
|
||||
serveWebAsset(rec2, req2, fsys, testAssetPath, true)
|
||||
if got := rec2.Result().Header.Get("ETag"); got != etag {
|
||||
t.Errorf("gzip ETag not stable: %q vs %q", got, etag)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeWebAssetConditional(t *testing.T) {
|
||||
fsys := webuiTestFS()
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
serveWebAsset(rec, httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil), fsys, testAssetPath, true)
|
||||
etag := rec.Result().Header.Get("ETag")
|
||||
if etag == "" {
|
||||
t.Fatal("no ETag to revalidate against")
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req.Header.Set("If-None-Match", etag)
|
||||
rec304 := httptest.NewRecorder()
|
||||
serveWebAsset(rec304, req, fsys, testAssetPath, true)
|
||||
if rec304.Code != http.StatusNotModified {
|
||||
t.Errorf("revalidation status = %d, want 304", rec304.Code)
|
||||
}
|
||||
if n := rec304.Body.Len(); n != 0 {
|
||||
t.Errorf("304 carried %d body bytes, want 0", n)
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeWebAssetHonoursEncodingAndCachePolicy(t *testing.T) {
|
||||
fsys := webuiTestFS()
|
||||
|
||||
// A client that does not accept gzip gets the raw bytes.
|
||||
req := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req.Header.Set("Accept-Encoding", "identity")
|
||||
rec := httptest.NewRecorder()
|
||||
serveWebAsset(rec, req, fsys, testAssetPath, true)
|
||||
if got := rec.Result().Header.Get("Content-Encoding"); got != "" {
|
||||
t.Errorf("Content-Encoding = %q, want empty for identity", got)
|
||||
}
|
||||
body, _ := io.ReadAll(rec.Result().Body)
|
||||
if want := fsys[testAssetPath].Data; !bytes.Equal(body, want) {
|
||||
t.Errorf("identity body mismatch: %d vs %d bytes", len(body), len(want))
|
||||
}
|
||||
|
||||
// gzip;q=0 must be treated as a refusal.
|
||||
req0 := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req0.Header.Set("Accept-Encoding", "gzip;q=0")
|
||||
rec0 := httptest.NewRecorder()
|
||||
serveWebAsset(rec0, req0, fsys, testAssetPath, true)
|
||||
if got := rec0.Result().Header.Get("Content-Encoding"); got != "" {
|
||||
t.Errorf("gzip;q=0 still compressed (Content-Encoding=%q)", got)
|
||||
}
|
||||
|
||||
// HTML is revalidate-only, never immutable.
|
||||
recHTML := httptest.NewRecorder()
|
||||
serveWebAsset(recHTML, httptest.NewRequest(http.MethodGet, "/index.html", nil), fsys, "index.html", false)
|
||||
if got := recHTML.Result().Header.Get("Cache-Control"); got != revalidateValue {
|
||||
t.Errorf("html Cache-Control = %q, want %q", got, revalidateValue)
|
||||
}
|
||||
|
||||
if webuiImmutable("index.html") {
|
||||
t.Error("index.html must not be immutable")
|
||||
}
|
||||
if !webuiImmutable("_next/static/chunks/app.js") {
|
||||
t.Error("_next/static assets must be immutable")
|
||||
}
|
||||
}
|
||||
|
||||
// bothRepresentations returns the ETag and body of the identity and gzip
|
||||
// representations of testAssetPath.
|
||||
func bothRepresentations(t *testing.T, fsys fstest.MapFS) (rawETag, gzETag string, rawBody, gzBody []byte) {
|
||||
t.Helper()
|
||||
for _, enc := range []string{"identity", "gzip"} {
|
||||
req := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req.Header.Set("Accept-Encoding", enc)
|
||||
rec := httptest.NewRecorder()
|
||||
if !serveWebAsset(rec, req, fsys, testAssetPath, true) {
|
||||
t.Fatalf("%s: asset not served", enc)
|
||||
}
|
||||
res := rec.Result()
|
||||
body, err := io.ReadAll(res.Body)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: read body: %v", enc, err)
|
||||
}
|
||||
if enc == "identity" {
|
||||
rawETag, rawBody = res.Header.Get("ETag"), body
|
||||
} else {
|
||||
gzETag, gzBody = res.Header.Get("ETag"), body
|
||||
}
|
||||
if len(body) == 0 {
|
||||
t.Fatalf("%s: empty body", enc)
|
||||
}
|
||||
}
|
||||
return rawETag, gzETag, rawBody, gzBody
|
||||
}
|
||||
|
||||
// TestServeWebAssetPerRepresentationETag pins the property that the raw and gzip
|
||||
// bodies never share a strong validator: a request validated against one must
|
||||
// not be answered from the other.
|
||||
func TestServeWebAssetPerRepresentationETag(t *testing.T) {
|
||||
fsys := webuiTestFS()
|
||||
rawETag, gzETag, rawBody, gzBody := bothRepresentations(t, fsys)
|
||||
|
||||
if rawETag == "" || gzETag == "" {
|
||||
t.Fatalf("missing validator: raw %q gzip %q", rawETag, gzETag)
|
||||
}
|
||||
if rawETag == gzETag {
|
||||
t.Fatalf("both representations share the strong validator %q", rawETag)
|
||||
}
|
||||
|
||||
type request struct {
|
||||
name string
|
||||
enc string
|
||||
ifRange string
|
||||
wantStatus int
|
||||
wantBody []byte
|
||||
}
|
||||
cases := []request{
|
||||
// Sanity: a range validated against its own representation is honoured.
|
||||
{"range on raw, own etag", "identity", rawETag, http.StatusPartialContent, rawBody[:10]},
|
||||
{"range on gzip, own etag", "gzip", gzETag, http.StatusPartialContent, gzBody[:10]},
|
||||
// The regression: the ETag of the other representation must not
|
||||
// validate a range, so the full, self-consistent body comes back.
|
||||
{"range on raw, gzip etag", "identity", gzETag, http.StatusOK, rawBody},
|
||||
{"range on gzip, raw etag", "gzip", rawETag, http.StatusOK, gzBody},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
req := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req.Header.Set("Accept-Encoding", tc.enc)
|
||||
req.Header.Set("Range", "bytes=0-9")
|
||||
req.Header.Set("If-Range", tc.ifRange)
|
||||
rec := httptest.NewRecorder()
|
||||
if !serveWebAsset(rec, req, fsys, testAssetPath, true) {
|
||||
t.Fatalf("%s: asset not served", tc.name)
|
||||
}
|
||||
res := rec.Result()
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
if res.StatusCode != tc.wantStatus {
|
||||
t.Errorf("%s: status = %d, want %d", tc.name, res.StatusCode, tc.wantStatus)
|
||||
}
|
||||
if !bytes.Equal(body, tc.wantBody) {
|
||||
t.Errorf("%s: body = %d bytes, want %d", tc.name, len(body), len(tc.wantBody))
|
||||
}
|
||||
if got := res.Header.Get("Content-Encoding"); tc.enc == "gzip" && got != "gzip" {
|
||||
t.Errorf("%s: Content-Encoding = %q, want gzip", tc.name, got)
|
||||
}
|
||||
if got := res.Header.Get("ETag"); got == "" {
|
||||
t.Errorf("%s: ETag missing", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestServeWebAssetConditionalIsPerRepresentation is the 304 half of the same
|
||||
// property: a client holding the gzip validator must not revalidate the raw
|
||||
// representation against it.
|
||||
func TestServeWebAssetConditionalIsPerRepresentation(t *testing.T) {
|
||||
fsys := webuiTestFS()
|
||||
rawETag, gzETag, _, _ := bothRepresentations(t, fsys)
|
||||
|
||||
type conditional struct {
|
||||
name string
|
||||
enc string
|
||||
validator string
|
||||
want int
|
||||
}
|
||||
cases := []conditional{
|
||||
{"raw against own etag", "identity", rawETag, http.StatusNotModified},
|
||||
{"gzip against own etag", "gzip", gzETag, http.StatusNotModified},
|
||||
{"raw against gzip etag", "identity", gzETag, http.StatusOK},
|
||||
{"gzip against raw etag", "gzip", rawETag, http.StatusOK},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
req := httptest.NewRequest(http.MethodGet, "/"+testAssetPath, nil)
|
||||
req.Header.Set("Accept-Encoding", tc.enc)
|
||||
req.Header.Set("If-None-Match", tc.validator)
|
||||
rec := httptest.NewRecorder()
|
||||
serveWebAsset(rec, req, fsys, testAssetPath, true)
|
||||
if rec.Code != tc.want {
|
||||
t.Errorf("%s: status = %d, want %d", tc.name, rec.Code, tc.want)
|
||||
}
|
||||
if tc.want == http.StatusOK && rec.Body.Len() == 0 {
|
||||
t.Errorf("%s: 200 carried no body", tc.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestServeWebAssetMissing(t *testing.T) {
|
||||
if serveWebAsset(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, "/nope.js", nil), webuiTestFS(), "nope.js", false) {
|
||||
t.Error("missing asset reported as served")
|
||||
}
|
||||
}
|
||||
+10
-17
@@ -21,6 +21,12 @@ var webuiDist embed.FS
|
||||
// webuiHandler serves the embedded SPA. Public (no JWT) — auth is enforced
|
||||
// client-side and on /api. Serves the exported per-route index.html files and
|
||||
// falls back to index.html so client-side routing still resolves unknown paths.
|
||||
//
|
||||
// Responses carry an ETag and, for the content-hashed Next chunks, immutable
|
||||
// caching; compressible bodies are gzipped when the client accepts it. See
|
||||
// webui_assets.go for why (embed.FS has no mtime, so the previous
|
||||
// http.ServeFileFS path emitted no validator at all and re-sent ~1.5 MB of
|
||||
// assets on every load).
|
||||
func (s *Server) webuiHandler() http.Handler {
|
||||
root, err := fs.Sub(webuiDist, "webui/dist")
|
||||
if err != nil {
|
||||
@@ -36,25 +42,12 @@ func (s *Server) webuiHandler() http.Handler {
|
||||
// 3) <p>.html
|
||||
// 4) SPA fallback → index.html (hand it to client-side routing)
|
||||
for _, cand := range []string{p, p + "/index.html", p + ".html"} {
|
||||
if serveFileIfExists(w, r, root, cand) {
|
||||
if serveWebAsset(w, r, root, cand, webuiImmutable(cand)) {
|
||||
return
|
||||
}
|
||||
}
|
||||
http.ServeFileFS(w, r, root, "index.html")
|
||||
if !serveWebAsset(w, r, root, "index.html", false) {
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// serveFileIfExists serves name from fsys when it exists as a regular file.
|
||||
func serveFileIfExists(w http.ResponseWriter, r *http.Request, fsys fs.FS, name string) bool {
|
||||
f, err := fsys.Open(name)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
st, statErr := f.Stat()
|
||||
_ = f.Close()
|
||||
if statErr != nil || st.IsDir() {
|
||||
return false
|
||||
}
|
||||
http.ServeFileFS(w, r, fsys, name)
|
||||
return true
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user