1 changed files with 31 additions and 21 deletions
@ -1,37 +1,47 @@ |
|||||
|
function e(tag, attrs, content) { |
||||
|
const el = document.createElement(tag) |
||||
|
el.innerText = content |
||||
|
return el |
||||
|
} |
||||
|
|
||||
class Setup { |
class Setup { |
||||
|
constructor() { |
||||
|
this.loaded = false |
||||
|
} |
||||
|
|
||||
async run() { |
async run() { |
||||
this.statusEl = document.createElement("p") |
navigator.serviceWorker |
||||
document.body.appendChild(this.statusEl) |
.addEventListener('controllerchange', () => { |
||||
this.updateStatus() |
if (this.registration.active) { |
||||
navigator.serviceWorker.addEventListener('controllerchange', () => { |
this.loadIfReady() |
||||
this.updateStatus() |
} |
||||
}) |
}) |
||||
await this.register() |
await this.register() |
||||
this.updateStatus() |
this.loadIfReady() |
||||
} |
} |
||||
|
|
||||
async register() { |
async register() { |
||||
try { |
try { |
||||
this.registration = await navigator.serviceWorker.register( |
this.registration = |
||||
"/sw.js", |
await navigator.serviceWorker.register( |
||||
{scope: "/"} |
'/sw.js', |
||||
) |
{scope: '/'} |
||||
|
) |
||||
} catch (err) { |
} catch (err) { |
||||
console.error("error registering service worker", err) |
console.error( |
||||
|
'error registering service worker', err |
||||
|
) |
||||
} |
} |
||||
} |
} |
||||
|
|
||||
updateStatus() { |
loadIfReady() { |
||||
if (this.registration?.installing) { |
if ( |
||||
this.serviceWorkerStatus = "installing" |
!this.loaded && this.registration.active |
||||
} else if (this.registration?.waiting) { |
) { |
||||
this.serviceWorkerStatus = "waiting" |
document.body.appendChild( |
||||
} else if (this.registration?.active) { |
e('p', {}, 'loaded') |
||||
this.serviceWorkerStatus = "active" |
) |
||||
} else { |
|
||||
this.serviceWorkerStatus = "initializing" |
|
||||
} |
} |
||||
this.statusEl.innerText = this.serviceWorkerStatus |
|
||||
} |
} |
||||
} |
} |
||||
|
|
||||
|
|||||
Loading…
Reference in new issue