fix: recover stale service worker clients

This commit is contained in:
2026-08-03 00:47:00 +05:30
parent 13aa75c366
commit eb325a8412
3 changed files with 46 additions and 2 deletions
+5 -1
View File
@@ -135,7 +135,11 @@ test("site includes core guides and production configuration", () => {
expect(config).toContain( expect(config).toContain(
'"style-src": ["\'self\'", "\'unsafe-inline\'", "https://fonts.googleapis.com"]', '"style-src": ["\'self\'", "\'unsafe-inline\'", "https://fonts.googleapis.com"]',
); );
expect(config).toContain('cacheName: "wrnexus-pwa-v2"'); expect(config).toContain('cacheName: "wrnexus-pwa-v3"');
expect(config).toContain("head: '<script src=\"/pwa-recover.js\" defer></script>'");
const recovery = readFileSync(join(root, "public", "pwa-recover.js"), "utf8");
expect(recovery).toContain('updateViaCache: "none"');
expect(recovery).toContain("registration.update()");
expect(config).toContain('pattern: "^https://wrnexusjs\\\\.dev/"'); expect(config).toContain('pattern: "^https://wrnexusjs\\\\.dev/"');
}); });
+37
View File
@@ -0,0 +1,37 @@
/* global location, navigator, sessionStorage, window */
(function () {
if (!("serviceWorker" in navigator)) return;
var version = "docs-v3";
var workerUrl = "/sw.js?v=" + encodeURIComponent(version);
if (window.trustedTypes) {
try {
workerUrl = window.trustedTypes
.createPolicy("wrnexus-pwa-recovery", {
createScriptURL: function (value) {
return value;
},
})
.createScriptURL(workerUrl);
} catch {
// A previously-created policy can make creation fail; the same-origin
// string remains valid when Trusted Types enforcement is not active.
}
}
var reloading = false;
navigator.serviceWorker.addEventListener("controllerchange", function () {
var key = "wrnexus-pwa-controller-" + version;
if (reloading || sessionStorage.getItem(key)) return;
reloading = true;
sessionStorage.setItem(key, "1");
location.reload();
});
navigator.serviceWorker
.register(workerUrl, { updateViaCache: "none" })
.then(function (registration) {
return registration.update();
})
.catch(function () {});
})();
+4 -1
View File
@@ -1,6 +1,9 @@
import type { AppConfig } from "@wrnexus/styles"; import type { AppConfig } from "@wrnexus/styles";
const config: AppConfig = { const config: AppConfig = {
// Repair clients that are still controlled by an older cached service worker.
// This can be removed after every preview client has advanced beyond v3.
head: '<script src="/pwa-recover.js" defer></script>',
mobile: { mobile: {
enabled: true, enabled: true,
appId: "com.example.wrnexusjs", appId: "com.example.wrnexusjs",
@@ -15,7 +18,7 @@ const config: AppConfig = {
pwa: { pwa: {
name: "WRNexusJS Documentation", name: "WRNexusJS Documentation",
shortName: "WRNexusJS Docs", shortName: "WRNexusJS Docs",
cacheName: "wrnexus-pwa-v2", cacheName: "wrnexus-pwa-v3",
runtimeCaching: [ runtimeCaching: [
{ {
pattern: "^https://wrnexusjs\\.dev/", pattern: "^https://wrnexusjs\\.dev/",