fix(service-worker): ship a self-unregistering worker for REMOVE_REGISTRATION (06-03)
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>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cebfa67549
commit
9f2305a562
@@ -1,9 +1,11 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { rm } from "node:fs/promises";
|
||||
import { rm, writeFile } from "node:fs/promises";
|
||||
import process from "node:process";
|
||||
|
||||
import { SERVICE_WORKER_SCRIPT_PATH } from "../src/contracts/service-worker.ts";
|
||||
import { INSTALLED_RUNTIME_CAPABILITIES } from "../src/features/installed-runtime-capabilities.ts";
|
||||
import { makeDistFilesPublic } from "./lib/dist-permissions.ts";
|
||||
import { SELF_UNREGISTERING_SERVICE_WORKER } from "./lib/self-unregistering-service-worker.ts";
|
||||
|
||||
/**
|
||||
* §17.2.2. Deterministic two-pass build.
|
||||
@@ -17,11 +19,14 @@ import { makeDistFilesPublic } from "./lib/dist-permissions.ts";
|
||||
* 4. materialize dist/config.json from the declared APP_PROFILE
|
||||
* 5. scan app dist and generate the static asset source
|
||||
* 6. ACTIVE only: Vite Service Worker build (emptyOutDir = false)
|
||||
* REMOVE_REGISTRATION only: write the self-unregistering worker (06-03)
|
||||
* 7. generate Release Manifest V2 and the build manifest
|
||||
* 8. make every dist file readable by other users (13-02)
|
||||
*
|
||||
* Steps 5 and 6 are skipped for `REMOVE_REGISTRATION`, `PURGE_OWNED_RESOURCES`
|
||||
* and `null`: those modes never run an active worker build.
|
||||
* `REMOVE_REGISTRATION` still ships a `service-worker.js` so the browser's
|
||||
* update check can replace a worker that stops the page from booting (D-5).
|
||||
*
|
||||
* Step 4 has to follow the Vite build. Vite copies `public/` verbatim, so
|
||||
* without it every build — including a production one — ships the local
|
||||
@@ -62,13 +67,19 @@ if (buildsActiveWorker) {
|
||||
run("node", ["scripts/generate-service-worker-assets.ts", "dist"]);
|
||||
// 6. service worker build
|
||||
run("npx", ["vite", "build", "--config", "vite.service-worker.config.ts"]);
|
||||
} else if (selection?.mode === "REMOVE_REGISTRATION") {
|
||||
// 6. self-unregistering worker in place of the active one
|
||||
await writeFile(
|
||||
`dist/${SERVICE_WORKER_SCRIPT_PATH}`,
|
||||
SELF_UNREGISTERING_SERVICE_WORKER,
|
||||
);
|
||||
} else {
|
||||
process.stdout.write(
|
||||
`service worker mode ${selection?.mode ?? "null"}: skipping worker build\n`,
|
||||
);
|
||||
}
|
||||
|
||||
// 6. release + build manifest
|
||||
// 7. release + build manifest
|
||||
run("node", ["scripts/generate-build-manifest.ts"]);
|
||||
|
||||
// 8. publishable permissions: the validated writer creates 0600 files
|
||||
|
||||
@@ -32,7 +32,8 @@ const PUBLIC_SERVICE_WORKER = /(?:^|\/)(?:service-worker|sw)(?:[.-][^/]*)?\.js$/
|
||||
|
||||
/**
|
||||
* A built `dist` may ship a worker script only when the static selection is
|
||||
* `ACTIVE`, and then only the selected script at the app root.
|
||||
* `ACTIVE` or `REMOVE_REGISTRATION` (the self-unregistering worker, 06-03), and
|
||||
* then only the selected script at the app root.
|
||||
*/
|
||||
export function checkPublicServiceWorkers(
|
||||
distFiles: readonly string[],
|
||||
@@ -40,7 +41,9 @@ export function checkPublicServiceWorkers(
|
||||
) {
|
||||
const found = distFiles.filter((file) => PUBLIC_SERVICE_WORKER.test(file));
|
||||
const allowed: string[] =
|
||||
selection?.mode === "ACTIVE" ? [selection.scriptPath] : [];
|
||||
selection?.mode === "ACTIVE" || selection?.mode === "REMOVE_REGISTRATION"
|
||||
? [selection.scriptPath]
|
||||
: [];
|
||||
const passed =
|
||||
found.length === allowed.length &&
|
||||
found.every((file) => allowed.includes(file));
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* 06-03 (D-5). The emergency exit for a worker that stops the page from
|
||||
* booting. A `REMOVE_REGISTRATION` release writes this as `service-worker.js`.
|
||||
* The browser's own update check (navigation, `updateViaCache: "none"`) then
|
||||
* installs it over any worker of this scope without a page: it skips waiting,
|
||||
* and on activation unregisters the registration. It has no fetch handler, so
|
||||
* every request goes to the network. Owned caches stay for the
|
||||
* `PURGE_OWNED_RESOURCES` release, as with the page-side removal.
|
||||
*/
|
||||
export const SELF_UNREGISTERING_SERVICE_WORKER = `// Generated by scripts/build-frontend.ts (REMOVE_REGISTRATION).
|
||||
self.addEventListener("install", (event) => {
|
||||
event.waitUntil(self.skipWaiting());
|
||||
});
|
||||
self.addEventListener("activate", (event) => {
|
||||
event.waitUntil(self.registration.unregister());
|
||||
});
|
||||
`;
|
||||
Reference in New Issue
Block a user