A worker that stops the page from booting had no exit: every off switch was page code. A REMOVE_REGISTRATION release now writes a service-worker.js that skips waiting and unregisters itself (D-5), checked end to end in the browser. The runbook and rollback doc now name only paths that exist in code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
152 lines
4.9 KiB
TypeScript
152 lines
4.9 KiB
TypeScript
import path from "node:path";
|
|
|
|
import { expect, test, type Page } from "../support/browser/strict-browser-test.ts";
|
|
import {
|
|
ACTIVE_SELECTION,
|
|
prepareServiceWorkerBuilds,
|
|
removeServiceWorkerBuilds,
|
|
replaceOnce,
|
|
startBuildServer,
|
|
type ServiceWorkerBuilds,
|
|
} from "./support/service-worker-builds.ts";
|
|
|
|
/**
|
|
* 06-03 (D-5). The emergency exit for a worker that stops the page from
|
|
* booting. Page code cannot help then: the page never runs. Build A is the
|
|
* ACTIVE worker with its fetch handler broken (every intercepted request is a
|
|
* network error, the entry script included). Build B is a
|
|
* `REMOVE_REGISTRATION` release, which ships the self-unregistering
|
|
* `service-worker.js` (`scripts/lib/self-unregistering-service-worker.ts`).
|
|
* The browser's update check alone replaces A with it, and the next load boots
|
|
* B from the network.
|
|
*/
|
|
|
|
type Probe = typeof globalThis & {
|
|
__caServiceWorkerTest?: { events: string[] };
|
|
};
|
|
|
|
let builds: ServiceWorkerBuilds | undefined;
|
|
|
|
test.beforeAll(async () => {
|
|
test.setTimeout(180_000);
|
|
builds = await prepareServiceWorkerBuilds({
|
|
async beforeBuild(name, root) {
|
|
if (name === "a") {
|
|
await replaceOnce(
|
|
path.join(root, "src/adapters/service-worker/service-worker-entry.ts"),
|
|
"runtime.onFetch(described).then((cached) => cached ?? fetch(request)),",
|
|
"Promise.resolve(Response.error()),",
|
|
);
|
|
return;
|
|
}
|
|
await replaceOnce(
|
|
path.join(root, "src/features/installed-runtime-capabilities.ts"),
|
|
ACTIVE_SELECTION,
|
|
`const SERVICE_WORKER: InstalledServiceWorkerSelection | null = {
|
|
mode: "REMOVE_REGISTRATION",
|
|
scriptPath: "service-worker.js",
|
|
};`,
|
|
);
|
|
},
|
|
});
|
|
});
|
|
|
|
test.afterAll(async () => {
|
|
await removeServiceWorkerBuilds(builds);
|
|
});
|
|
|
|
async function startOutcome(page: Page): Promise<unknown> {
|
|
await page.waitForFunction(
|
|
() =>
|
|
(globalThis as Probe).__caServiceWorkerTest?.events.some((event) =>
|
|
event.startsWith("start:"),
|
|
) === true,
|
|
undefined,
|
|
{ timeout: 60_000 },
|
|
);
|
|
const event = await page.evaluate(() =>
|
|
(globalThis as Probe).__caServiceWorkerTest!.events.find((entry) =>
|
|
entry.startsWith("start:"),
|
|
)!,
|
|
);
|
|
return JSON.parse(event.slice("start:".length));
|
|
}
|
|
|
|
test("a REMOVE_REGISTRATION release unregisters a worker that blocks boot", async ({
|
|
browser,
|
|
browserName,
|
|
}) => {
|
|
// Same quarantine as service-worker-update.spec.ts.
|
|
// quarantine(owner=frontend-platform, defect=playwright-firefox-no-sw-control, expires=2027-03-31)
|
|
test.skip(
|
|
browserName === "firefox",
|
|
"Playwright Firefox pages are never controlled by a service worker.",
|
|
);
|
|
if (!builds) throw new Error("Service Worker builds were not prepared.");
|
|
const server = await startBuildServer(builds.dist);
|
|
// The broken phase fails requests on purpose, so this test drives its own
|
|
// context instead of the strict `page` fixture and checks the recovered
|
|
// load for browser errors itself.
|
|
const context = await browser.newContext();
|
|
try {
|
|
const page = await context.newPage();
|
|
await page.goto(`${server.origin}/`);
|
|
expect(await startOutcome(page)).toEqual({ kind: "RELOAD_TO_ENABLE" });
|
|
await page.evaluate(async () => {
|
|
await navigator.serviceWorker.ready;
|
|
});
|
|
|
|
// Worker A controls the next load and fails the entry script.
|
|
await page.goto(`${server.origin}/`);
|
|
const blocked = await page.evaluate(async () => {
|
|
const entry = document.querySelector<HTMLScriptElement>(
|
|
'script[type="module"][src]',
|
|
);
|
|
if (!entry) return "no entry script";
|
|
try {
|
|
await fetch(entry.src);
|
|
return "loaded";
|
|
} catch {
|
|
return navigator.serviceWorker.controller ? "blocked" : "uncontrolled";
|
|
}
|
|
});
|
|
expect(blocked).toBe("blocked");
|
|
|
|
// Release B. Only the browser's update check runs; no code of B does.
|
|
server.serve("b");
|
|
await page.evaluate(async () => {
|
|
await (await navigator.serviceWorker.getRegistration())?.update();
|
|
});
|
|
await expect
|
|
.poll(
|
|
() =>
|
|
page.evaluate(
|
|
async () =>
|
|
(await navigator.serviceWorker.getRegistration()) === undefined,
|
|
),
|
|
{ timeout: 30_000 },
|
|
)
|
|
.toBe(true);
|
|
|
|
const failures: string[] = [];
|
|
page.on("console", (message) => {
|
|
if (["error", "warning"].includes(message.type())) {
|
|
failures.push(`console:${message.type()}:${message.text()}`);
|
|
}
|
|
});
|
|
page.on("pageerror", (error) => failures.push(`pageerror:${error.name}`));
|
|
page.on("requestfailed", (request) =>
|
|
failures.push(`requestfailed:${new URL(request.url()).pathname}`),
|
|
);
|
|
await page.goto(`${server.origin}/`);
|
|
expect(await startOutcome(page)).toEqual({ kind: "DISABLED" });
|
|
expect(
|
|
await page.evaluate(() => navigator.serviceWorker.controller === null),
|
|
).toBe(true);
|
|
expect(failures).toEqual([]);
|
|
} finally {
|
|
await context.close();
|
|
await server.close();
|
|
}
|
|
});
|