fix(service-worker): do not report a first install as ACTIVE (R-6.4)
register() resolves while the first worker is still installing. With no
active or waiting worker, start() fell through to ACTIVE, and kept doing
so on every visit even when the install failed.
When there is no active worker, start() now waits for the installing
worker to reach `activated` or `redundant`, bounded by the worker's own
install deadline (installDeadlineMs) and by stop(). Activated -> the
existing RELOAD_TO_ENABLE path (no clients.claim()); redundant ->
FAILED INSTALL_FAILED; deadline -> FAILED INSTALL_TIMEOUT; no worker at
all -> FAILED INSTALL_FAILED. start() is fire-and-forget after mount,
so the wait does not block rendering.
Test fixture: pageContainer gains an `installing` option backed by a
statechange-capable fake worker.
RED before fix (both resolved ACTIVE immediately):
- does not report a first install as ACTIVE while it is installing (R-6.4)
- reports a failed first install instead of ACTIVE (R-6.4)
Gates: check:architecture, check:types:{app,test,service-worker}, lint,
test:component pass. test:unit passes except the pre-existing
removal-fixture.test.ts case that needs generated release evidence
under artifacts/ (absent in a fresh worktree; unrelated to this diff).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
39aa573746
commit
891e1363d2
@@ -188,6 +188,19 @@ export function createServiceWorkerPageController(
|
||||
observe("register", "UPDATE_WAITING");
|
||||
return Object.freeze({ kind: "UPDATE_WAITING" as const });
|
||||
}
|
||||
// R-6.4. On a first visit `register()` resolves while the only worker is
|
||||
// still installing, and that install can fail. Nothing is ACTIVE until it
|
||||
// settles.
|
||||
if (!registration.active) {
|
||||
const settled = registration.installing
|
||||
? await installSettled(registration.installing)
|
||||
: "INSTALL_FAILED";
|
||||
if (stopped) return failed("STOPPED");
|
||||
if (settled !== "ACTIVATED" || !registration.active) {
|
||||
observe("register", settled);
|
||||
return failed(settled);
|
||||
}
|
||||
}
|
||||
// §17.13. Without `clients.claim()` the first install leaves this page
|
||||
// uncontrolled. That is reported, never silently reloaded.
|
||||
if (registration.active && !container.controller) {
|
||||
@@ -201,6 +214,38 @@ export function createServiceWorkerPageController(
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* R-6.4. Waits for a first-install worker to become `activated` or
|
||||
* `redundant`, bounded by the worker's own install deadline and by stop().
|
||||
*/
|
||||
function installSettled(
|
||||
worker: ServiceWorker,
|
||||
): Promise<"ACTIVATED" | "INSTALL_FAILED" | "INSTALL_TIMEOUT" | "STOPPED"> {
|
||||
return new Promise((resolve) => {
|
||||
const finish = (
|
||||
outcome: "ACTIVATED" | "INSTALL_FAILED" | "INSTALL_TIMEOUT" | "STOPPED",
|
||||
) => {
|
||||
clearTimeout(timer);
|
||||
worker.removeEventListener("statechange", onStateChange);
|
||||
pendingStops.delete(onStop);
|
||||
resolve(outcome);
|
||||
};
|
||||
const onStateChange = () => {
|
||||
if (worker.state === "activated") finish("ACTIVATED");
|
||||
else if (worker.state === "redundant") finish("INSTALL_FAILED");
|
||||
};
|
||||
const onStop = () => finish("STOPPED");
|
||||
const timer = setTimeout(
|
||||
() => finish("INSTALL_TIMEOUT"),
|
||||
SERVICE_WORKER_BOUNDS.installDeadlineMs,
|
||||
);
|
||||
pendingStops.add(onStop);
|
||||
worker.addEventListener("statechange", onStateChange);
|
||||
// The worker may have settled before this listener existed.
|
||||
onStateChange();
|
||||
});
|
||||
}
|
||||
|
||||
function attachMessageListener(container: ServiceWorkerContainer): void {
|
||||
if (messageListener) return;
|
||||
messageListener = (event: MessageEvent) => {
|
||||
|
||||
@@ -38,18 +38,22 @@ function setWorkerState(
|
||||
worker.dispatchEvent(new Event("statechange"));
|
||||
}
|
||||
|
||||
function pageContainer(options: { waiting?: boolean; controlled?: boolean } = {}) {
|
||||
function pageContainer(
|
||||
options: { waiting?: boolean; controlled?: boolean; installing?: boolean } = {},
|
||||
) {
|
||||
const listeners = new Set<(event: MessageEvent) => void>();
|
||||
const waitingMessages: unknown[] = [];
|
||||
const controllerMessages: unknown[] = [];
|
||||
const worker = (messages: unknown[], state?: ServiceWorkerState) =>
|
||||
fakeWorker(messages, state) as unknown as ServiceWorker;
|
||||
const waiting = options.waiting ? worker(waitingMessages, "installed") : null;
|
||||
const active = options.waiting ? null : worker([]);
|
||||
// A first visit: `register()` resolves while the only worker is installing.
|
||||
const installing = options.installing ? fakeWorker([], "installing") : null;
|
||||
const active = options.waiting || options.installing ? null : worker([]);
|
||||
const controlled = options.controlled ? worker(controllerMessages) : null;
|
||||
const registration = Object.assign(new EventTarget(), {
|
||||
scope: `${ORIGIN}/`,
|
||||
installing: null,
|
||||
installing,
|
||||
waiting,
|
||||
active,
|
||||
update: vi.fn(async () => {}),
|
||||
@@ -67,6 +71,7 @@ function pageContainer(options: { waiting?: boolean; controlled?: boolean } = {}
|
||||
return {
|
||||
container,
|
||||
waiting,
|
||||
installing,
|
||||
controlled,
|
||||
registration,
|
||||
waitingMessages,
|
||||
@@ -993,6 +998,46 @@ describe("service worker static asset install", () => {
|
||||
await controller.stop();
|
||||
});
|
||||
|
||||
it("does not report a first install as ACTIVE while it is installing (R-6.4)", async () => {
|
||||
const browser = pageContainer({ installing: true });
|
||||
const controller = pageController(browser.container);
|
||||
let outcome: unknown = "pending";
|
||||
const starting = controller.start().then((value) => {
|
||||
outcome = value;
|
||||
return value;
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
expect(outcome).toBe("pending");
|
||||
|
||||
// Install finished; without clients.claim() this page stays uncontrolled.
|
||||
const registration = browser.registration as unknown as {
|
||||
installing: ServiceWorker | null;
|
||||
active: ServiceWorker | null;
|
||||
};
|
||||
registration.installing = null;
|
||||
registration.active = browser.installing as unknown as ServiceWorker;
|
||||
setWorkerState(browser.installing!, "activated");
|
||||
|
||||
await expect(starting).resolves.toEqual({ kind: "RELOAD_TO_ENABLE" });
|
||||
await controller.stop();
|
||||
});
|
||||
|
||||
it("reports a failed first install instead of ACTIVE (R-6.4)", async () => {
|
||||
const browser = pageContainer({ installing: true });
|
||||
const controller = pageController(browser.container);
|
||||
const starting = controller.start();
|
||||
await new Promise((resolve) => setTimeout(resolve, 0));
|
||||
|
||||
(browser.registration as unknown as { installing: null }).installing = null;
|
||||
setWorkerState(browser.installing!, "redundant");
|
||||
|
||||
await expect(starting).resolves.toEqual({
|
||||
kind: "FAILED",
|
||||
code: "INSTALL_FAILED",
|
||||
});
|
||||
await controller.stop();
|
||||
});
|
||||
|
||||
it("announces an update that finished installing as waiting (R-6.3)", async () => {
|
||||
const browser = pageContainer({ controlled: true });
|
||||
const controller = pageController(browser.container);
|
||||
|
||||
Reference in New Issue
Block a user