如何在Firefox中启动Service Worker?

时间:2017-10-16 18:05:04

标签: javascript firefox service-worker service-worker-events

我正在尝试了解如何使服务工作者在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。 我也禁用了所有插件,它仍然无法正常工作。

可能是什么问题?

1 个答案:

答案 0 :(得分:0)

我已刷新(about:support)标准的firefox v56,但它无效。但是在刷新浏览器之后,它已经开始在Firefox开发人员版v57中运行。这似乎是一个Firefox的bug。