当我使用服务工作者时,会出现“无法访问网站”的情况。

4

大家好,我刚接触这项技术,希望能请教一下我的代码问题。我的目标是将资源文件缓存,并从服务工作者返回。

以下是我用来注册服务工作者的代码:

if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/serviceworker.js')
.then(function(reg) {
// registration worked
console.log('Registration succeeded. Scope is ' + reg.scope);
}).catch(function(error) {
// registration failed
console.log('Registration failed with ' + error);
});
}

以下是 Service Worker 中的代码

importScripts('/cache-poli.js');

var CACHE_VERSION = 'app-v2';
var CACHE_FILES = [
'/',
    '/js/plugins/bootstrap/js/bootstrap.min.js',
    '/js/plugins/bootstrap-select/bootstrap-select.min.js',
    '/js/plugins/prettyphoto/js/jquery.prettyPhoto.js',
    '/js/plugins/jquery.sticky.min.js',
    '/js/plugins/jquery.easing.min.js',
    '/js/plugins/animate/js/animate.js',
    '/js/jquery.fancybox.js',
    '/js/plugins/jquery/jquery-ui-1.11.1.min.js',
    '/js/jquery.scrollbar.min.js',
    '/js/plugins/owlcarousel2/owl.carousel.min.js',
    '/js/plugins/elevateZoom/jquery.elevateZoom-3.0.8.min.js',
    '/js/theme.js',
    '/js/cmsfuncs.js',
    '/js/theme-config.js',
    '/js/jquery.mCustomScrollbar.concat.min.js',
    '/js/plugins/jquery/jquery-2.1.4.min.js',
    '/js/jquery.cookie.js',

    '/js/plugins/bootstrap/css/bootstrap.min.css',
    '/fonts/fontawesome/css/font-awesome.min.css',
    '/fonts/webfont/css/simple-line-icons.css',
    '/fonts/elegantfont/css/elegantfont.css',
    '/js/plugins/bootstrap-select/bootstrap-select.min.css',
    '/js/plugins/owlcarousel2/assets/owl.carousel.min.css',
    '/js/plugins/prettyphoto/css/prettyPhoto.css',
    '/js/plugins/animate/css/animate.css',
    '/s/plugins/accordion/css/magicaccordion.css',
    '/css/jquery.scrollbar.css',
    '/css/megamenu.css',
    '/css/theme.css',
    '/css/slider/slide.css',
    '/css/jquery.mCustomScrollbar.css',
    '/css/responsive.css',
    '/css/theme.css'
];

self.addEventListener('install', function (event) {
event.waitUntil(
    caches.open(CACHE_VERSION)
        .then(function (cache) {
            console.log('Opened cache');
            return cache.addAll(CACHE_FILES);
        })
);
});


self.addEventListener('activate', function (event) {
event.waitUntil(
    caches.keys().then(function(keys){
        return Promise.all(keys.map(function(key, i){
            if(key !== CACHE_VERSION){
                return caches.delete(keys[i]);
            }
        }))
    })
)
});


self.addEventListener('fetch', function (event) {
event.respondWith(
    caches.open(CACHE_VERSION).then(function(cache){
        caches.match(event.request).then(function(response) {
            return response || fetch(event.request);
        })
    })
)
});

我正在使用Google Chrome开发工具查看安装过程,一切都已缓存,并且服务工作者没有显示任何错误,但是当我再次尝试访问该网站时,它会给我一个错误信息。
此网站无法访问。 网页“domain.com”可能暂时无法访问,或者已永久移动到新的 Web 地址。
1个回答

4

我也曾经遇到过同样的错误。

实际上,问题非常明显。浏览器告诉你的是,你试图访问的路径无法到达。

在你的代码中,看起来你已经缓存了根目录'/'。我猜想你在尝试访问其他路径,比如'/somepath'时遇到了这个问题。
因为你没有缓存那些路径,所以你会得到这个错误。

所以在你的数组中,如果你也添加:

var CACHE_FILES = ['/',
                   '/somepath', ...];

这个错误不会再出现。

我使用了完全相同的方法,错误已经消失了。


1
我遇到了同样的问题,但我已经缓存了我使用的所有路由。每当我按Ctrl + F5刷新缓存时,它都有效,但如果我正常刷新它就不行。 - Alarm-1202

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接