preload service-worker

This commit is contained in:
Pooya Parsa 2020-11-04 00:43:16 +01:00
parent c43cf33c95
commit dd7f32324f

View File

@ -1,16 +1,41 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<body> <body>
Loading...
<script> <script>
if (!('serviceWorker' in navigator)) { if (!('serviceWorker' in navigator)) {
throw new Error('Browser not supported!') throw new Error('Browser not supported!')
} }
let timeout
const stopTimer = () => {
if (timeout) {
clearTimeout(timeout)
}
}
const startTimer = () => {
stopTimer()
timeout = setTimeout(() => { document.write('Loading...') }, 3000)
}
startTimer()
window.addEventListener('load', () => { window.addEventListener('load', () => {
startTimer()
navigator.serviceWorker.register('/nuxt.sw.js').then((registration) => { navigator.serviceWorker.register('/nuxt.sw.js').then((registration) => {
startTimer()
console.log('ServiceWorker registration successful with scope:', registration.scope) console.log('ServiceWorker registration successful with scope:', registration.scope)
window.location.reload() 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)
})
}).catch(error => { }).catch(error => {
console.error('ServiceWorker registration failed:', error) console.error('ServiceWorker registration failed:', error)
document.write(error) document.write(error)