fast replace instead of reload

This commit is contained in:
Pooya Parsa 2020-11-04 01:24:40 +01:00
parent 10fddd223e
commit 885f0aba66
1 changed files with 25 additions and 10 deletions

View File

@ -1,23 +1,38 @@
<!DOCTYPE html>
<html>
<head>
<!-- SW_INIT-->
<meta charset="utf-8">
<link rel="prefetch" href="/nuxt.sw.js">
<link rel="preload" href="/nuxt.sw.js" as="worker">
<script>
if (!('serviceWorker' in navigator)) {
throw new Error('Browser not supported!')
async function reload() {
const html = await fetch(window.location.href).then(r => r.text())
if (html.includes('<!-- SW_INIT-->')) {
window.location.reload(false)
} else {
document.open()
document.write(html)
document.close()
}
}
navigator.serviceWorker.register('/nuxt.sw.js').then((registration) => {
console.log('ServiceWorker registration successful with scope:', registration.scope)
window.location.reload(false)
}).catch(error => {
console.error('ServiceWorker registration failed:', error)
document.write(error)
})
async function register() {
const registration = await navigator.serviceWorker.register('/nuxt.sw.js')
await navigator.serviceWorker.ready
registration.active.addEventListener('statechange', (event) => {
if (event.target.state === 'activated') {
reload()
}
})
}
register()
</script>
</head>
<body>
Loading...
</body>
</html>