我正在尝试学习如何制作 Service Worker,虽然它在 Chrome 中按预期工作,但由于某种未知原因,它在 Firefox v56 中不起作用。这是应该在任何地方都可以使用的演示https://mdn.github.io/sw-test/
这是演示服务工作者代码
self.addEventListener('install', function(event) {
event.waitUntil(
caches.open('v1').then(function(cache) {
return cache.addAll([
'/sw-test/',
'/sw-test/index.html',
'/sw-test/style.css',
'/sw-test/app.js',
'/sw-test/image-list.js',
'/sw-test/star-wars-logo.jpg',
'/sw-test/gallery/bountyHunters.jpg',
'/sw-test/gallery/myLittleVader.jpg',
'/sw-test/gallery/snowTroopers.jpg'
]);
})
);
});
self.addEventListener('fetch', function(event) {
event.respondWith(caches.match(event.request).then(function(response) {
// caches.match() always resolves
// but in case of success response will have value
if (response !== undefined) {
return response;
} else {
return fetch(event.request).then(function (response) {
// response may be used only once
// we need to save clone to put one copy in cache
// and serve second one
let responseClone = response.clone();
caches.open('v1').then(function (cache) {
cache.put(event.request, responseClone);
});
return response;
}).catch(function () {
return caches.match('/sw-test/gallery/myLittleVader.jpg');
});
}
}));
});
当我打开开发者控制台时,我得到这个错误:
"Registration failed with TypeError: ServiceWorker script at https://mdn.github.io/sw-test/sw.js for scope https://mdn.github.io/sw-test/ encountered an error during installation."
同时,同样的代码在 Chrome 中也能正常工作。
在about:config
设置dom.serviceWorkers.enabled
中设置为true
。我还禁用了所有插件,但它仍然无法正常工作。
可能是什么问题呢?