update index.html

This commit is contained in:
Pooya Parsa 2020-11-04 00:46:01 +01:00
parent dd7f32324f
commit 2f329dec26

View File

@ -1,47 +1,32 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body>
<head>
<meta charset="utf-8">
<link rel="prefetch" href="/nuxt.sw.js">
<link rel="preload" href="/nuxt.sw.js" as="worker">
<script> <script>
if (!('serviceWorker' in navigator)) { if (!('serviceWorker' in navigator)) {
throw new Error('Browser not supported!') throw new Error('Browser not supported!')
} }
let timeout let timeout
const stopTimer = () => { const stopTimer = () => { if (timeout) { clearTimeout(timeout); timeout = undefined } }
if (timeout) { const startTimer = () => { stopTimer(); timeout = setTimeout(() => { document.write('Loading...') }, 3000) }
clearTimeout(timeout)
}
}
const startTimer = () => {
stopTimer()
timeout = setTimeout(() => { document.write('Loading...') }, 3000)
}
startTimer()
window.addEventListener('load', () => {
startTimer() startTimer()
navigator.serviceWorker.register('/nuxt.sw.js').then((registration) => { navigator.serviceWorker.register('/nuxt.sw.js').then((registration) => {
startTimer() stopTimer()
console.log('ServiceWorker registration successful with scope:', registration.scope) console.log('ServiceWorker registration successful with scope:', registration.scope)
fetch(window.location.href).then(r => r.text()).then(html => {
stopTimer()
if (html.includes('/nuxt.sw.js')) {
console.error('Soon!')
return
}
document.open()
document.write(html)
document.close()
}).catch(() => {
stopTimer()
window.location.reload(false) window.location.reload(false)
})
}).catch(error => { }).catch(error => {
stopTimer()
console.error('ServiceWorker registration failed:', error) console.error('ServiceWorker registration failed:', error)
document.write(error) document.write(error)
}) })
})
</script> </script>
</body> </head>
<body></body>
</html> </html>