Skip to content

Commit

Permalink
fix: only register sw if available
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 12, 2020
1 parent ce41994 commit 2efe9b3
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@
import { HMRRuntime } from 'vue'

// register service worker
const hasExistingSw = !!navigator.serviceWorker.controller
if (__SW_ENABLED__ || hasExistingSw) {
// if not enabled but has existing sw, registering the sw will force the
// cache to be busted.
navigator.serviceWorker.register('/sw.js').catch((e) => {
console.log('[vite] failed to register service worker:', e)
})

// Notify the user to reload the page if a new service worker has taken
// control.
if (hasExistingSw) {
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (window.confirm('[vite] Service worker cache updated. Reload?')) {
window.location.reload()
}
// in case the user dismisses it, or the prompt failed to pop because
// the tab was inactive
console.warn(`[vite] Service worker cache updated. A reload is required.`)
if ('serviceWorker' in navigator) {
const hasExistingSw = !!navigator.serviceWorker.controller
if (__SW_ENABLED__ || hasExistingSw) {
// if not enabled but has existing sw, registering the sw will force the
// cache to be busted.
navigator.serviceWorker.register('/sw.js').catch((e) => {
console.log('[vite] failed to register service worker:', e)
})

// Notify the user to reload the page if a new service worker has taken
// control.
if (hasExistingSw) {
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (window.confirm('[vite] Service worker cache updated. Reload?')) {
window.location.reload()
}
// in case the user dismisses it, or the prompt failed to pop because
// the tab was inactive
console.warn(
`[vite] Service worker cache updated. A reload is required.`
)
})
}
}
}

Expand Down Expand Up @@ -188,7 +192,7 @@ export const hot = {
}

function bustSwCache(path: string) {
const sw = navigator.serviceWorker.controller
const sw = navigator.serviceWorker && navigator.serviceWorker.controller
if (sw) {
return new Promise((r) => {
const channel = new MessageChannel()
Expand Down

0 comments on commit 2efe9b3

Please sign in to comment.