我正在尝试学习制作 Service Worker,虽然它在 Chrome 中按预期工作,但由于某种未知原因,它在 Firefox v56 中无法工作。 这里是应该可以在任何地方使用的演示。
这是演示服务人员代码:
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');
});
}
}));
});
当我打开开发者控制台时,出现此错误:
“注册失败,出现 TypeError:ServiceWorker 脚本位于 https://mdn.github.io/sw-test/sw.js 范围 https://mdn.github.io/sw-test/ 遇到错误安装过程中。”
同时,相同的代码在 Chrome 中也可以正常运行,不会出现错误。
在
about:config
中,设置 dom.serviceWorkers.enabled
被设为 true
。
我还禁用了所有插件,但它仍然无法工作。
可能是什么问题?
我刷新了(
about:support
)标准firefox v56,但它不起作用。但在浏览器刷新后,它已经开始在 Firefox 开发者版本 v57 中运行。这似乎是 Firefox 的一个错误。