mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-24 00:50:54 +08:00
The embedded Next export ships ~1.55 MB of JS/CSS. Files read from embed.FS report a zero modtime, so http.ServeFileFS emitted no Last-Modified and nothing set an ETag: the browser had no validator and re-downloaded the whole bundle on every page load. The handler also ignored Accept-Encoding, so those bytes went out uncompressed. Measured from a 211 ms-RTT link: 17 assets / 1,554,053 B, 8.57 s serialised, 1.98 s at 6-way parallelism, 0.28 MB/s per SSH stream. Now each asset is digested (sha256, 16 bytes, quoted strong ETag) and, for compressible types over 1 KiB, gzipped once at BestCompression and cached in memory (~450 KB total). Responses carry Vary: Accept-Encoding and honour gzip;q=0. /_next/static/* gets public, max-age=31536000, immutable because Next content-hashes those filenames; HTML gets no-cache so a rebuild is never served stale. Range and HEAD still work (http.ServeContent underneath). Stdlib only, no new module dependencies. (cherry picked from commit 8ad714c606e69674e991c0e80da61c2c7a708c22)
54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
//go:build embedui
|
|
|
|
package server
|
|
|
|
import (
|
|
"embed"
|
|
"io/fs"
|
|
"net/http"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
// webuiDist holds the statically-exported frontend (built with
|
|
// `cd web && npm run build:static`, then copied into server/webui/dist). Compiled
|
|
// into the binary only when building with `-tags embedui`. The `all:` prefix is
|
|
// required so Next's `_next/` asset dir (leading underscore) is included.
|
|
//
|
|
//go:embed all:webui/dist
|
|
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 {
|
|
panic(err)
|
|
}
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
p := strings.TrimPrefix(path.Clean(r.URL.Path), "/")
|
|
if p == "" {
|
|
p = "index.html"
|
|
}
|
|
// 1) exact file (assets: _next/*, favicon.ico, ...)
|
|
// 2) route dir → <p>/index.html (trailingSlash export)
|
|
// 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 serveWebAsset(w, r, root, cand, webuiImmutable(cand)) {
|
|
return
|
|
}
|
|
}
|
|
if !serveWebAsset(w, r, root, "index.html", false) {
|
|
http.NotFound(w, r)
|
|
}
|
|
})
|
|
}
|