From 352428af86265b3214241b22ddb09e969d1db26b Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 16 Nov 2026 05:32:16 +0000 Subject: [PATCH] Draw the first preview on a first visit: wait for the worker On a first visit the preview frame could load before the service worker was running. The frame's address is served only by the worker, so it got a 404 and stayed blank until the person pressed refresh. This was seen on the live site about half the time, and it was reproduced locally with network latency. The worker's registration is now kept, and the live frame waits until the worker runs or its install fails. When service workers are blocked, the register call rejects and nothing waits. Under latency the preview drew the app 3 times out of 3, where before it stayed blank 3 times out of 3. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_013Asa3LdusN9UGDiUFDM7bZ --- studio/shell/host.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/studio/shell/host.js b/studio/shell/host.js index 4c5daf97..377cb68f 100644 --- a/studio/shell/host.js +++ b/studio/shell/host.js @@ -684,7 +684,8 @@ function liveFrame() { const fonts = {}; for (const p of FONTS) { const r = await fetch(new URL(p, BASE)); if (r.ok) fonts[p] = b64(new Uint8Array(await r.arrayBuffer())); } const html = await withCoverResponder(frameHtml({ brandCss: await text("brand.css"), componentsCss: await text("live/components.css"), componentsJs: await text("live/components.js"), interpreter: prism.INTERPRETER, fonts })); - await coreReady; + // The worker serves the frame: on a first visit the frame waits for it, or it would load nothing. + await coreReady; await workerActive; const { path } = core.run({ op: "preview", kappa: await kappa(enc.encode(html)) }); const url = new URL(path.slice(1), BASE); await grantPreview(previewId(url.pathname), []); @@ -700,7 +701,7 @@ const liveTimes = []; // patch to pixel, as each frame reports it const themeOf = () => (document.documentElement.classList.contains("dark") ? "dark" : "light"); // A frame is trusted to receive an app's spec only when it is an iframe of this page showing the live frame. const isLiveFrame = (source) => !!liveFrameUrl && [...document.querySelectorAll("iframe")].some((f) => f.contentWindow === source && f.src.startsWith(liveFrameUrl)); -let liveFrameUrl = null; +let liveFrameUrl = null, workerActive = null; function sendLive(appId) { const latest = liveLatest.get(appId); if (!latest) return; @@ -2119,7 +2120,8 @@ if ("serviceWorker" in navigator) { const drawnByWorker = !!navigator.serviceWorker.controller; let reloading = false; navigator.serviceWorker.addEventListener("controllerchange", () => { if (drawnByWorker && !reloading) { reloading = true; location.reload(); } }); - navigator.serviceWorker.register(new URL("sw.js", BASE), { updateViaCache: "none" }).then((r) => r.update()).catch(() => {}); + // A first visit has no worker running yet; previews wait until it runs, or until its install fails. + workerActive = navigator.serviceWorker.register(new URL("sw.js", BASE), { updateViaCache: "none" }).then((r) => { r.update().catch(() => {}); const w = r.installing || r.waiting; return r.active || (w && new Promise((ok) => w.addEventListener("statechange", () => /activated|redundant/.test(w.state) && ok()))); }).catch(() => {}); } // The worker installs only what a first paint needs; once this page has loaded and is idle it is asked to // fill the rest of the closure (the engine, the chat app, the photos), so offline use follows soon after.