MCPcopy
hub / github.com/leaningtech/webvm / doRegister

Function doRegister

serviceWorker.js:50–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

48}
49
50async function doRegister() {
51 try {
52 const registration = await navigator.serviceWorker.register(window.document.currentScript.src);
53 console.log("Service Worker registered", registration.scope);
54 // EventListener to make sure that the page gets reloaded when a new serviceworker gets installed.
55 // f.e on first access.
56 registration.addEventListener("updatefound", () => {
57 console.log("Reloading the page to transfer control to the Service Worker.");
58 try {
59 window.location.reload();
60 } catch (err) {
61 console.log("Service Worker failed reloading the page. ERROR:" + err);
62 };
63 });
64 // When the registration is active, but it's not controlling the page, we reload the page to have it take control.
65 // This f.e occurs when you hard-reload (shift + refresh). https://www.w3.org/TR/service-workers/#navigator-service-worker-controller
66 if (registration.active && !navigator.serviceWorker.controller) {
67 console.log("Reloading the page to transfer control to the Service Worker.");
68 try {
69 window.location.reload();
70 } catch (err) {
71 console.log("Service Worker failed reloading the page. ERROR:" + err);
72 };
73 }
74 }
75 catch {
76 console.error("Service Worker failed to register:", e)
77 }
78}
79
80async function serviceWorkerRegister() {
81 if (window.crossOriginIsolated) return;

Callers 1

serviceWorkerRegisterFunction · 0.85

Calls 2

errorMethod · 0.80
registerMethod · 0.45

Tested by

no test coverage detected