Javascript 如何在开发环境中测试更新服务人员?
我想实现的是,当我的React网页有更新时,能够向用户推送通知,我正试图通过serviceWorkers这样做 我面临的挑战是让serviceWorkerRegistration接口的installation属性不返回null。我只是想触发Javascript 如何在开发环境中测试更新服务人员?,javascript,reactjs,typescript,service-worker,Javascript,Reactjs,Typescript,Service Worker,我想实现的是,当我的React网页有更新时,能够向用户推送通知,我正试图通过serviceWorkers这样做 我面临的挑战是让serviceWorkerRegistration接口的installation属性不返回null。我只是想触发 console.log('新内容可用;请刷新') 我对在React中与服务人员一起工作很陌生。有人知道如何在开发环境中测试serviceWorkers的更新功能吗 console.log('I am in registerValidSW!') nav
console.log('新内容可用;请刷新')
我对在React中与服务人员一起工作很陌生。有人知道如何在开发环境中测试serviceWorkers的更新功能吗
console.log('I am in registerValidSW!')
navigator.serviceWorker
.register(swUrl)
.then((registration) => {
console.log(registration)
registration.onupdatefound = () => {
const installingWorker = registration.installing //This returns null
if (installingWorker) {
console.log('installingServiceWorker!!!')
installingWorker.onstatechange = () => {
if (installingWorker.state === 'installed') {
if (navigator.serviceWorker.controller) {
// At this point, the old content will have been purged and
// the fresh content will have been added to the cache.
// It's the perfect time to display a 'New content is
// available; please refresh.' message in your web app.
console.log('New content is available; please refresh.')
} else {
// At this point, everything has been precached.
// It's the perfect time to display a
// 'Content is cached for offline use.' message.
console.log('Content is cached for offline use.')
}
}
}
}
}
})
.catch((error) => {
console.error('Error during service worker registration:', error)
})
@krish根据新文档->服务人员仅在生产环境中启用,例如npm运行构建的输出。建议不要在开发环境中启用脱机first service worker,因为如果使用以前缓存的资产,并且不包括您在本地所做的最新更改,可能会导致失败。请参阅此链接:-OP不是询问您是否应该在开发中启用服务工作者,而是询问如果您不应该激活服务工作者,如何测试开发中的服务工作者