...
|
...
|
@@ -7,6 +7,9 @@ const config = { |
|
|
/^https:\/\/(.*)cdn\.yoho\.cn/i,
|
|
|
/^https:\/\/(.*)static\.yhbimg\.com/i
|
|
|
],
|
|
|
precachePage: [
|
|
|
'/offline.html'
|
|
|
],
|
|
|
precacheStaticFile: [
|
|
|
'/index.css',
|
|
|
'/common.css',
|
...
|
...
|
@@ -31,13 +34,17 @@ const cacheFirstStrategy = workboxSW.strategies.cacheFirst({ |
|
|
// 特殊路径的预缓存文件文件
|
|
|
const precacheFile = [{
|
|
|
url: '/sw.js?t=' + qs.t + '&staticServer=' + qs.staticServer
|
|
|
}].concat(config.precacheStaticFile.map(function(file) {
|
|
|
}].concat(config.precacheStaticFile.map(file => {
|
|
|
// 根据业务自定义资源路径
|
|
|
const url = self.location.protocol + qs.staticServer + file + '?t=' + qs.t;
|
|
|
|
|
|
return {
|
|
|
url: url
|
|
|
};
|
|
|
})).concat(config.precachePage.map(page => {
|
|
|
return {
|
|
|
url: page
|
|
|
};
|
|
|
}));
|
|
|
|
|
|
// 预加载文件
|
...
|
...
|
@@ -50,3 +57,14 @@ config.customCacheUrl.forEach(function(urlRegExp) { |
|
|
cacheFirstStrategy
|
|
|
);
|
|
|
});
|
|
|
|
|
|
workboxSW.router.registerRoute(/.*/, args => {
|
|
|
return workboxSW.strategies.networkFirst().handle(args).then(res => {
|
|
|
if (res || args.event.request.mode !== 'navigate') {
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
// navigate 请求失败后,返回网络异常页面
|
|
|
return caches.match('offline.html');
|
|
|
});
|
|
|
}); |
...
|
...
|
|