Actualizar 'app.js'

file-group-page
bat 3 years ago
parent f20065e017
commit 5fd9915634

@ -1,64 +1,38 @@
class Setup { class Setup {
async run() { async run() {
await this.register() this.statusEl = document.createElement("p")
const p = document.createElement("p") document.body.appendChild(this.statusEl)
p.innerText = this.serviceWorkerStatus this.updateStatus()
document.body.appendChild(p) this.addEventListener('controllerchange', () => {
this.statusEl = p this.updateStatus()
const btn = document.createElement('button') })
btn.addEventListener('click', () => this.unregister()) await this.register()
btn.innerText = 'unregister' this.updateStatus()
const p2 = document.createElement('p') }
document.body.appendChild(p2)
p2.appendChild(btn)
}
async register() { async register() {
this.serviceWorkerStatus = "unavailable" try {
if ("serviceWorker" in navigator) { this.registration = await navigator.serviceWorker.register(
try { "/sw.js",
this.registration = await navigator.serviceWorker.register( {scope: "/"}
"/sw.js", )
{scope: "/"} } catch (err) {
) console.error("error registering service worker", err)
if (this.registration.installing) {
this.serviceWorkerStatus = "installing"
} else if (this.registration.waiting) {
this.serviceWorkerStatus = "waiting"
} else if (this.registration.active) {
this.serviceWorkerStatus = "active"
} else {
this.serviceWorkerStatus = "unknown"
}
} catch (err) {
console.error("error registering service worker", err)
this.serviceWorkerStatus = "error"
}
} else {
console.error("serviceWorker not available")
}
} }
}
async unregister() { updateStatus() {
if ('serviceWorker' in navigator) { if (this.registration?.installing) {
try { this.serviceWorkerStatus = "installing"
const regs = await navigator.serviceWorker.getRegistrations() } else if (this.registration?.waiting) {
const res = await Promise.allSettled( this.serviceWorkerStatus = "waiting"
regs.map(reg => reg.unregister()) } else if (this.registration?.active) {
) this.serviceWorkerStatus = "active"
this.serviceWorkerStatus = ( } else {
res.every(pr => pr.status === 'fulfilled') ? this.serviceWorkerStatus = "not installed"
'unregistered' :
'error'
)
} catch (err) {
console.error("error in unregister", err)
}
} else {
this.serviceWorkerStatus = 'unavailable'
}
this.statusEl.innerText = this.serviceWorkerStatus
} }
this.statusEl.innerText = this.serviceWorkerStatus
}
} }
new Setup().run() new Setup().run()
Loading…
Cancel
Save