fix: recover PWA clients without navigation reload
This commit is contained in:
+2
-1
@@ -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
@@ -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);
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user