fix: recover PWA clients without navigation reload

This commit is contained in:
2026-08-03 00:54:29 +05:30
parent eb325a8412
commit 0cca2caa2e
2 changed files with 20 additions and 16 deletions
+2 -1
View File
@@ -139,7 +139,8 @@ test("site includes core guides and production configuration", () => {
expect(config).toContain("head: '<script src=\"/pwa-recover.js\" defer></script>'"); expect(config).toContain("head: '<script src=\"/pwa-recover.js\" defer></script>'");
const recovery = readFileSync(join(root, "public", "pwa-recover.js"), "utf8"); const recovery = readFileSync(join(root, "public", "pwa-recover.js"), "utf8");
expect(recovery).toContain('updateViaCache: "none"'); expect(recovery).toContain('updateViaCache: "none"');
expect(recovery).toContain("registration.update()"); expect(recovery).toContain("requestIdleCallback");
expect(recovery).not.toContain("location.reload()");
expect(config).toContain('pattern: "^https://wrnexusjs\\\\.dev/"'); expect(config).toContain('pattern: "^https://wrnexusjs\\\\.dev/"');
}); });
+15 -12
View File
@@ -1,4 +1,4 @@
/* global location, navigator, sessionStorage, window */ /* global navigator, window */
(function () { (function () {
if (!("serviceWorker" in navigator)) return; if (!("serviceWorker" in navigator)) return;
@@ -19,19 +19,22 @@
} }
} }
var reloading = false; var recover = function () {
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 navigator.serviceWorker
.register(workerUrl, { updateViaCache: "none" }) .getRegistration()
.then(function (registration) { .then(function (registration) {
return registration.update(); if (
registration &&
registration.active &&
registration.active.scriptURL.includes(version)
) {
return;
}
return navigator.serviceWorker.register(workerUrl, { updateViaCache: "none" });
}) })
.catch(function () {}); .catch(function () {});
};
if ("requestIdleCallback" in window) window.requestIdleCallback(recover, { timeout: 2000 });
else window.setTimeout(recover, 0);
})(); })();