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>'");
|
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/"');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+18
-15
@@ -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 () {
|
navigator.serviceWorker
|
||||||
var key = "wrnexus-pwa-controller-" + version;
|
.getRegistration()
|
||||||
if (reloading || sessionStorage.getItem(key)) return;
|
.then(function (registration) {
|
||||||
reloading = true;
|
if (
|
||||||
sessionStorage.setItem(key, "1");
|
registration &&
|
||||||
location.reload();
|
registration.active &&
|
||||||
});
|
registration.active.scriptURL.includes(version)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return navigator.serviceWorker.register(workerUrl, { updateViaCache: "none" });
|
||||||
|
})
|
||||||
|
.catch(function () {});
|
||||||
|
};
|
||||||
|
|
||||||
navigator.serviceWorker
|
if ("requestIdleCallback" in window) window.requestIdleCallback(recover, { timeout: 2000 });
|
||||||
.register(workerUrl, { updateViaCache: "none" })
|
else window.setTimeout(recover, 0);
|
||||||
.then(function (registration) {
|
|
||||||
return registration.update();
|
|
||||||
})
|
|
||||||
.catch(function () {});
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user