fix: update PWA workers without reloading pages
This commit is contained in:
@@ -312,18 +312,12 @@ export function renderPwaClient(version = "2") {
|
|||||||
swUrl = window.trustedTypes.createPolicy("wrnexus-pwa", { createScriptURL: function(value) { return value; } }).createScriptURL(swUrl);
|
swUrl = window.trustedTypes.createPolicy("wrnexus-pwa", { createScriptURL: function(value) { return value; } }).createScriptURL(swUrl);
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
var reloading = false;
|
|
||||||
navigator.serviceWorker.addEventListener("controllerchange", function () {
|
|
||||||
var key = "wrnexus-pwa-controller-" + swVersion;
|
|
||||||
if (reloading || sessionStorage.getItem(key)) return;
|
|
||||||
reloading = true;
|
|
||||||
sessionStorage.setItem(key, "1");
|
|
||||||
location.reload();
|
|
||||||
});
|
|
||||||
addEventListener("load", function () {
|
addEventListener("load", function () {
|
||||||
navigator.serviceWorker.register(swUrl, { updateViaCache: "none" }).then(function (registration) {
|
var register = function () {
|
||||||
return registration.update();
|
navigator.serviceWorker.register(swUrl, { updateViaCache: "none" }).catch(function () {});
|
||||||
}).catch(function () {});
|
};
|
||||||
|
if ("requestIdleCallback" in window) window.requestIdleCallback(register, { timeout: 2000 });
|
||||||
|
else setTimeout(register, 0);
|
||||||
});
|
});
|
||||||
}`;
|
}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ test("PWA registration is valid JavaScript and Trusted Types compatible", () =>
|
|||||||
expect(PWA_CLIENT).toContain('createPolicy("wrnexus-pwa"');
|
expect(PWA_CLIENT).toContain('createPolicy("wrnexus-pwa"');
|
||||||
expect(PWA_CLIENT).toContain("createScriptURL(swUrl)");
|
expect(PWA_CLIENT).toContain("createScriptURL(swUrl)");
|
||||||
expect(PWA_CLIENT).toContain('register(swUrl, { updateViaCache: "none" })');
|
expect(PWA_CLIENT).toContain('register(swUrl, { updateViaCache: "none" })');
|
||||||
expect(PWA_CLIENT).toContain("registration.update()");
|
expect(PWA_CLIENT).toContain("requestIdleCallback");
|
||||||
expect(PWA_CLIENT).toContain('addEventListener("controllerchange"');
|
expect(PWA_CLIENT).not.toContain("location.reload()");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("development PWA cleanup removes stale WRNexus service workers and caches", () => {
|
test("development PWA cleanup removes stale WRNexus service workers and caches", () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user