mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
fast replace instead of reload
This commit is contained in:
parent
10fddd223e
commit
885f0aba66
@ -1,23 +1,38 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<!-- SW_INIT-->
|
||||
<meta charset="utf-8">
|
||||
<link rel="prefetch" href="/nuxt.sw.js">
|
||||
<link rel="preload" href="/nuxt.sw.js" as="worker">
|
||||
<script>
|
||||
if (!('serviceWorker' in navigator)) {
|
||||
throw new Error('Browser not supported!')
|
||||
}
|
||||
navigator.serviceWorker.register('/nuxt.sw.js').then((registration) => {
|
||||
console.log('ServiceWorker registration successful with scope:', registration.scope)
|
||||
async function reload() {
|
||||
const html = await fetch(window.location.href).then(r => r.text())
|
||||
if (html.includes('<!-- SW_INIT-->')) {
|
||||
window.location.reload(false)
|
||||
}).catch(error => {
|
||||
console.error('ServiceWorker registration failed:', error)
|
||||
document.write(error)
|
||||
} else {
|
||||
document.open()
|
||||
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>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
Loading...
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user