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-->')) {
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) async function register() {
window.location.reload(false) const registration = await navigator.serviceWorker.register('/nuxt.sw.js')
}).catch(error => { await navigator.serviceWorker.ready
console.error('ServiceWorker registration failed:', error) registration.active.addEventListener('statechange', (event) => {
document.write(error) if (event.target.state === 'activated') {
}) reload()
}
})
}
register()
</script> </script>
</head> </head>
<body> <body>
Loading... Loading...
</body> </body>
</html> </html>