mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-29 09:02:03 +00:00
preload service-worker
This commit is contained in:
parent
c43cf33c95
commit
dd7f32324f
@ -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)
|
||||||
|
Loading…
Reference in New Issue
Block a user