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>'");
const recovery = readFileSync(join(root, "public", "pwa-recover.js"), "utf8");
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/"');
});
+18 -15
View File
@@ -1,4 +1,4 @@
/* global location, navigator, sessionStorage, window */
/* global navigator, window */
(function () {
if (!("serviceWorker" in navigator)) return;
@@ -19,19 +19,22 @@
}
}
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();
});
var recover = function () {
navigator.serviceWorker
.getRegistration()
.then(function (registration) {
if (
registration &&
registration.active &&
registration.active.scriptURL.includes(version)
) {
return;
}
return navigator.serviceWorker.register(workerUrl, { updateViaCache: "none" });
})
.catch(function () {});
};
navigator.serviceWorker
.register(workerUrl, { updateViaCache: "none" })
.then(function (registration) {
return registration.update();
})
.catch(function () {});
if ("requestIdleCallback" in window) window.requestIdleCallback(recover, { timeout: 2000 });
else window.setTimeout(recover, 0);
})();