fast replace instead of reload

This commit is contained in:
Pooya Parsa 2020-11-04 01:24:40 +01:00
parent 10fddd223e
commit 885f0aba66

View File

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