// The browser stops an idle Service Worker, and with it the bridges it held. On // restart the SW asks each open host page to register again, so a running app keeps // answering, and keeps loading modules, without a reload. import { test, expect } from "@playwright/test"; import "./guest"; const SERVER = ` import http from "node:http"; http.createServer(async (req, res) => { if (req.url === "/load") { const { later } = await import("./later.mjs"); res.end(later); return; } res.end("up"); }).listen(4500); `; test("after the SW is stopped, app fetches and a first-time module import work without a reload", async ({ page, context }) => { await page.goto("/demo/fixture.html"); await page.waitForFunction(() => window.__runtime !== undefined, undefined, { timeout: 20_000 }); const url = await page.evaluate(async (src) => { const runtime = window.__runtime!; runtime.writeTree({ "/app/server.mjs": src, "/app/later.mjs": 'export const later = "loaded later";\n' }); runtime.exec("/app/server.mjs"); return await runtime.waitForPort(4500); }, SERVER); const get = (path: string) => page.evaluate(async (u) => { const r = await fetch(u); return { status: r.status, text: await r.text() }; }, url + path); expect(await get("")).toEqual({ status: 200, text: "up" }); const cdp = await context.newCDPSession(page); await cdp.send("ServiceWorker.enable"); const stopped = new Promise((resolve) => { cdp.on("ServiceWorker.workerVersionUpdated", ({ versions }) => { if (versions.some((v: { runningStatus: string }) => v.runningStatus === "stopped")) resolve(); }); }); await cdp.send("ServiceWorker.stopAllWorkers"); await stopped; expect(await get("")).toEqual({ status: 200, text: "up" }); expect(await get("load")).toEqual({ status: 200, text: "loaded later" }); }); // A host bridges again whenever an SW start asks, and a live SW asks too, on any request // for a token it does not know (a closed page's URL after a restart, say). The bridge // that replaces the old one must not drop what the old one still carries: rolldown's // thread pool keeps its wasi-worker.mjs imports in flight nearly all the time, and one // lost import (a 404 after the bridge timeout) leaves its whole build waiting forever. const IMPORTS = 400; const IMPORTER = ` let ok = 0; const failed = []; const all = []; for (let i = 0; i < ${IMPORTS}; i++) { all.push(import("./m" + i + ".mjs").then(() => { ok++; }, (e) => { failed.push(e.message); })); await new Promise((r) => setTimeout(r, 10)); } await Promise.all(all); console.log(JSON.stringify({ ok, failed: failed.slice(0, 3) })); `; test("a rebridge while module imports are in flight loses none of them", async ({ page }) => { test.setTimeout(60_000); await page.goto("/demo/fixture.html"); await page.waitForFunction(() => window.__runtime !== undefined, undefined, { timeout: 20_000 }); const out = await page.evaluate(async ({ IMPORTER, IMPORTS }) => { const runtime = window.__runtime!; const tree: Record = { "/app/importer.mjs": IMPORTER }; for (let i = 0; i < IMPORTS; i++) tree[`/app/m${i}.mjs`] = `export const n = ${i};\n`; runtime.writeTree(tree); const proc = runtime.exec("/app/importer.mjs", { cwd: "/app" }); const stdout = new Response(proc.stdout).text(); // Twice, past the SW's one-broadcast-per-3-s limit: ask for a token no page owns, // and keep this page busy meanwhile, so imports queue on the bridge being replaced. for (const wait of [1_000, 3_200]) { await new Promise((r) => setTimeout(r, wait)); void fetch("/~/@0000000000000000/vfs/%2Fnothing.mjs").catch(() => {}); const until = performance.now() + 150; while (performance.now() < until) { /* busy */ } } await proc.exited; return JSON.parse(await stdout) as unknown; }, { IMPORTER, IMPORTS }); expect(out).toEqual({ ok: IMPORTS, failed: [] }); });