diff --git a/crates/marqdo-wasm/js/marqdo-bridge.js b/crates/marqdo-wasm/js/marqdo-bridge.js index 8f18e11..3a5784a 100644 --- a/crates/marqdo-wasm/js/marqdo-bridge.js +++ b/crates/marqdo-wasm/js/marqdo-bridge.js @@ -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(); } diff --git a/plugins/web/internal/httpx/listen.go b/plugins/web/internal/httpx/listen.go index 17588d1..2ea1169 100644 --- a/plugins/web/internal/httpx/listen.go +++ b/plugins/web/internal/httpx/listen.go @@ -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) { diff --git a/plugins/web/internal/httpx/listen_test.go b/plugins/web/internal/httpx/listen_test.go index 19e3705..a544c58 100644 --- a/plugins/web/internal/httpx/listen_test.go +++ b/plugins/web/internal/httpx/listen_test.go @@ -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 diff --git a/plugins/web/internal/render/chrome_brand.go b/plugins/web/internal/render/chrome_brand.go index e56d2a1..00195ec 100644 --- a/plugins/web/internal/render/chrome_brand.go +++ b/plugins/web/internal/render/chrome_brand.go @@ -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 `` } +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*)(
]*>)(.*?)(
)`) func shouldDropcap(plain string) bool { diff --git a/plugins/web/internal/render/render_test.go b/plugins/web/internal/render/render_test.go index 9555518..b7523fe 100644 --- a/plugins/web/internal/render/render_test.go +++ b/plugins/web/internal/render/render_test.go @@ -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") + } +}