我正试图在我的Nuxt JS网站中实现一个自定义的内置服务工作程序。生成网站后出现以下错误:
Cannot read property 'register' of undefined
function Notify (siteOptions) {
/* Set Dependancies */
this.siteOptions = siteOptions /* Register Applicant */
this.register()
} /** * Register Service Worker */
Notify.prototype.register = function () {
/** * Test Registration */
navigator.serviceWorker.register('Notify-service-worker.js').then(function (registration) {
console.log('SW: Available')
}).catch(function (error) {
console.error('SW: Not Available', error)
})
看来这是引起问题的行:
navigator.serviceWorker.register('Notify-service-worker.js')
答案 0 :(得分:0)
在注册之前检查当前浏览器是否支持服务人员是best practice:
if ('serviceWorker' in navigator) {
// Only call navigator.serviceWorker.register() if that's true.
}