perf(web): cache static assets and defer wasm boot
Add long Cache-Control for /static, version client embed URLs from asset_version, and delay WASM mount until after load so LCP images win. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
981d754133
commit
453321c8c2
5 files changed
+87
-15
No files matched your search
@@ -1413,12 +1413,24 @@ export async function autoMount(doc) {
|
||||
|
||||
const g = typeof globalThis !== "undefined" ? globalThis : undefined;
|
||||
if (g && g.document) {
|
||||
// Defer WASM boot until after first paint / load so LCP images are not
|
||||
// contending with a ~1.6MB wasm download on the critical path.
|
||||
const start = () => {
|
||||
autoMount(g.document).catch((e) => console.error("marqdo autoMount:", e));
|
||||
const run = () => {
|
||||
autoMount(g.document).catch((e) => console.error("marqdo autoMount:", e));
|
||||
};
|
||||
const schedule = () => {
|
||||
if (typeof g.requestIdleCallback === "function") {
|
||||
g.requestIdleCallback(() => run(), { timeout: 1500 });
|
||||
} else {
|
||||
g.setTimeout(run, 0);
|
||||
}
|
||||
};
|
||||
if (g.document.readyState === "complete") {
|
||||
schedule();
|
||||
} else {
|
||||
g.addEventListener("load", schedule, { once: true });
|
||||
}
|
||||
};
|
||||
if (g.document.readyState === "loading") {
|
||||
g.document.addEventListener("DOMContentLoaded", start);
|
||||
} else {
|
||||
queueMicrotask(start);
|
||||
}
|
||||
start();
|
||||
}
|
||||
@@ -56,10 +56,24 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
if page == nil {
|
||||
page = map[string]any{}
|
||||
}
|
||||
if _, ok := page["asset_version"]; !ok {
|
||||
if v := strOpt(appBag, "asset_version", ""); v != "" {
|
||||
page["asset_version"] = v
|
||||
}
|
||||
}
|
||||
routes := map[string]any{}
|
||||
if r, ok := appBag["routes"].(map[string]any); ok {
|
||||
routes = r
|
||||
}
|
||||
if av, ok := page["asset_version"]; ok {
|
||||
for _, p := range routes {
|
||||
if pm, ok := p.(map[string]any); ok {
|
||||
if _, has := pm["asset_version"]; !has {
|
||||
pm["asset_version"] = av
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
forms := map[string]any{}
|
||||
if f, ok := appBag["forms"].(map[string]any); ok {
|
||||
forms = cloneMap(f)
|
||||
@@ -174,20 +188,18 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
fs := http.StripPrefix(staticMount, http.FileServer(http.Dir(staticDir)))
|
||||
// Register without method so GET+HEAD share one pattern (avoids conflicts
|
||||
// with more-specific GET/HEAD icon paths under the same prefix).
|
||||
mux.Handle(staticMount+"/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
serveStatic := func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// Long browser cache; FileServer still honors Last-Modified / 304.
|
||||
// Prefer ?v= / asset_version on HTML refs when assets change.
|
||||
w.Header().Set("Cache-Control", "public, max-age=604800")
|
||||
fs.ServeHTTP(w, r)
|
||||
}))
|
||||
mux.Handle(staticMount, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
fs.ServeHTTP(w, r)
|
||||
}))
|
||||
}
|
||||
mux.Handle(staticMount+"/", http.HandlerFunc(serveStatic))
|
||||
mux.Handle(staticMount, http.HandlerFunc(serveStatic))
|
||||
}
|
||||
|
||||
for _, from := range sortedKeys(st.redirects) {
|
||||
|
||||
@@ -58,6 +58,9 @@ func TestNewHandlerHomeRoutesStatic(t *testing.T) {
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("static code=%d", rec.Code)
|
||||
}
|
||||
if cc := rec.Header().Get("Cache-Control"); !strings.Contains(cc, "max-age=") {
|
||||
t.Fatalf("static cache-control=%q", cc)
|
||||
}
|
||||
b, _ := io.ReadAll(rec.Result().Body)
|
||||
if string(b) != "hello-static" {
|
||||
// httptest body is already in rec.Body
|
||||
|
||||
@@ -153,10 +153,35 @@ func clientEmbedScript(page map[string]any) string {
|
||||
if wasm == "" {
|
||||
wasm = "/static/marqdo_wasm.wasm"
|
||||
}
|
||||
ver := ""
|
||||
if v, ok := page["asset_version"].(string); ok {
|
||||
ver = strings.TrimSpace(v)
|
||||
} else if v, ok := page["资源版本"].(string); ok {
|
||||
ver = strings.TrimSpace(v)
|
||||
}
|
||||
if ver != "" {
|
||||
bridge = withAssetQuery(bridge, ver)
|
||||
wasm = withAssetQuery(wasm, ver)
|
||||
source = withAssetQuery(source, ver)
|
||||
}
|
||||
return `<script type="module" src="` + esc(bridge) + `" data-mq-wasm="` + esc(wasm) +
|
||||
`" data-mq-source-url="` + esc(source) + `"></script>`
|
||||
}
|
||||
|
||||
func withAssetQuery(url, ver string) string {
|
||||
if ver == "" || url == "" {
|
||||
return url
|
||||
}
|
||||
if strings.Contains(url, "v=") {
|
||||
return url
|
||||
}
|
||||
sep := "?"
|
||||
if strings.Contains(url, "?") {
|
||||
sep = "&"
|
||||
}
|
||||
return url + sep + "v=" + ver
|
||||
}
|
||||
|
||||
var firstPBlockRe = regexp.MustCompile(`(?is)^(\s*)(<p\b[^>]*>)(.*?)(</p>)`)
|
||||
|
||||
func shouldDropcap(plain string) bool {
|
||||
|
||||
@@ -297,3 +297,23 @@ func TestNavBrandClientChrome(t *testing.T) {
|
||||
t.Fatalf("missing drawer veil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientEmbedAssetVersion(t *testing.T) {
|
||||
page := map[string]any{
|
||||
"title": "v",
|
||||
"asset_version": "20260918d",
|
||||
"client": map[string]any{
|
||||
"source": "/static/client.mq.md",
|
||||
},
|
||||
}
|
||||
html := render.RenderPage(page, "", "")
|
||||
if !strings.Contains(html, `/static/marqdo-bridge.js?v=20260918d`) {
|
||||
t.Fatalf("missing versioned bridge: %s", html)
|
||||
}
|
||||
if !strings.Contains(html, `data-mq-wasm="/static/marqdo_wasm.wasm?v=20260918d"`) {
|
||||
t.Fatalf("missing versioned wasm")
|
||||
}
|
||||
if !strings.Contains(html, `data-mq-source-url="/static/client.mq.md?v=20260918d"`) {
|
||||
t.Fatalf("missing versioned source")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user