贝娄是我如何注册我的serviceworker:serviceworker`onupdatefound`不会触发移动
if ('serviceWorker' in navigator) {
window.addEventListener('load',() => {
function updateOnlineStatus() {
SnackBar.show({
text: navigator.onLine ? onlineMsg : offlineMsg,
backgroundColor: '#000000',
actionText: close,
actionTextColor: '#d2de2f',
customClass: 'custom-snackbar',
});
}
window.addEventListener('online', updateOnlineStatus);
window.addEventListener('offline', updateOnlineStatus);
navigator.serviceWorker.register('sw.js').then((reg) => {
reg.onupdatefound =() => {
const installingWorker = reg.installing;
installingWorker.onstatechange =() => {
switch (installingWorker.state) {
case 'installed':
if (navigator.serviceWorker.controller) {
SnackBar.show({
text: refreshMsg,
backgroundColor: '#000000',
actionText: refresh,
actionTextColor: '#d2de2f',
onActionClick:() => { location.reload(); },
customClass: 'custom-snackbar',
});
} else {
console.info(availableMsg);
}
break;
case 'redundant':
console.info(redundantMsg);
break;
default:
break;
}
};
};
}).catch((e) => {
console.error(errorMsg, e);
});
});
}
由针线构建过程中生成此serviceworker。
我有点担心,因为这些通知都显示在我的电脑(Ubuntu的+ chrome 59.0.3071.115
),但是从来没有在我的手机(android 6.0.1
+ chrome 59.0.3071.125
)
远程调试功能分析后,它看起来像onupdatefound
是永远在我的手机上触发。
我觉得UX很不好,想在移动设备上的游客,将不知道该网站的新版本正在等待...
任何建议或意见可以理解
编辑1:我在本网页的“浏览器兼容性”部分找到了更多详细信息:https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/onupdatefound。 Chrome看起来像支持Android这个功能是未知的。
当浏览器不支持onupdatefound
事件时,你们是否有一种回退/解决方法?
编辑2:
<Files sw.js>
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</Files>
产生:针对杰夫,
sw.js
头在.htaccess
文件管理
accept-ranges: bytes
cache-control: max-age=0, no-cache, no-store, must-revalidate
content-encoding: br
content-language: fr-FR
content-length: 1636
content-type: application/javascript; charset=utf-8
date: Fri, 21 Jul 2017 13:04:06 GMT
expires: Wed, 11 Jan 1984 05:00:00 GMT
last-modified: Tue, 18 Jul 2017 02:58:30 GMT
pragma: no-cache
关于箱里微调你的建议,我没有找到时间挖掘,所以我在一个吞咽文件中使用简单的workbox-build
。但我希望会:)
// Service Worker generation
gulp.task('bundle-sw', gulp.series('generate-sitemap',() => {
return wbBuild.generateSW({
globDirectory: gulpOutputDir,
swDest: `${gulpOutputDir}/sw.js`,
globPatterns: ['**/*.{html,png,xml,css,ico,txt,json,svg,js,map,gif,jpeg,jpg,bmp,cur,webp,woff2}'],
skipWaiting: true,
clientsClaim: true,
})
.then(() => {
console.warn('Service worker generated.');
})
.catch((err) => {
console.error(`[ERROR] This happened: ${err}`);
});
}));
你认为这可能是由skipWaiting: true
引起的?
编辑3:广播信试图
navigator.serviceWorker.register('/sw.js').then((reg) => {
console.log('test');
const updateChannel = new BroadcastChannel('precache-updates');
updateChannel.addEventListener('message', event => {
console.log(`Cache updated: ${event.data.payload.updatedUrl}`);
});
}).catch((e) => {
console.error(errorMsg, e);
});
即使test
在日志outputed在Chrome,手机,或Firefox,更新消息仅在Chrome
https://developers.google .com/web/fundamentals/instant-and-offline/service-worker/lifecycle#handling_updates你可能想在修改文档后修改你的观察者。 –