Ku3*_*u3a 12 javascript service-worker progressive-web-apps
我开始学习PWA(渐进式Web应用程序),我遇到问题,控制台"抛出"错误未捕获(在承诺中)TypeError:无法获取.
任何人都知道原因可能是什么?
let CACHE = 'cache';
self.addEventListener('install', function(evt) {
console.log('The service worker is being installed.');
evt.waitUntil(precache());
});
self.addEventListener('fetch', function(evt) {
console.log('The service worker is serving the asset.');
evt.respondWith(fromCache(evt.request));
});
function precache() {
return caches.open(CACHE).then(function (cache) {
return cache.addAll([
'/media/wysiwyg/homepage/desktop.jpg',
'/media/wysiwyg/homepage/bottom2_desktop.jpg'
]);
});
}
function fromCache(request) {
return caches.open(CACHE).then(function (cache) {
return cache.match(request).then(function (matching) {
return matching || Promise.reject('no-match');
});
});
}
Run Code Online (Sandbox Code Playgroud)
Bla*_*ard 16
我认为这是因为你没有后备策略.event.respondWith有一个承诺,如果有一些错误,你必须抓住.
所以,我建议你改变你的代码:
self.addEventListener('fetch', function(evt) {
console.log('The service worker is serving the asset.');
evt.respondWith(fromCache(evt.request));
});
Run Code Online (Sandbox Code Playgroud)
对于这样的事情:
addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request)
.then(function(response) {
if (response) {
return response; // if valid response is found in cache return it
} else {
return fetch(event.request) //fetch from internet
.then(function(res) {
return caches.open(CACHE_DYNAMIC_NAME)
.then(function(cache) {
cache.put(event.request.url, res.clone()); //save the response for future
return res; // return the fetched data
})
})
.catch(function(err) { // fallback mechanism
return caches.open(CACHE_CONTAINING_ERROR_MESSAGES)
.then(function(cache) {
return cache.match('/offline.html');
});
});
}
})
);
});
Run Code Online (Sandbox Code Playgroud)
注意:有许多缓存策略,我在这里展示的是脱机第一种方法.欲了解更多信息这和这是一个必须阅读.
小智 16
我找到了同一错误的解决方案,在我的情况下,错误显示当服务工作者找不到文件时,通过在chrome会话的开发工具中跟踪网络来修复它,并确定服务工作者找不到的不存在的文件并删除要注册的文件数组.
'/processos/themes/base/js/processos/step/Validation.min.js',
'/processos/themes/base/js/processos/Acoes.min.js',
'/processos/themes/base/js/processos/Processos.min.js',
'/processos/themes/base/js/processos/jBPM.min.js',
'/processos/themes/base/js/highcharts/highcharts-options-white.js',
'/processos/themes/base/js/publico/ProcessoJsController.js',
// '/processos/gzip_457955466/bundles/plugins.jawrjs',
// '/processos/gzip_N1378055855/bundles/publico.jawrjs',
// '/processos/gzip_457955466/bundles/plugins.jawrjs',
'/mobile/js/about.js',
'/mobile/js/access.js',
Run Code Online (Sandbox Code Playgroud)
小智 7
我遇到了同样的错误,在我的情况下,Adblock阻止了对以“ ad”开头的网址的提取(例如/adsomething.php)
| 归档时间: |
|
| 查看次数: |
20555 次 |
| 最近记录: |