简体   繁体   中英

How to start Service Worker in Firefox?

I am trying to learn how to make Service Worker and while it is working as intended in Chrome, for some unknown reason it is not working in Firefox v56. Here is the demo that should be working everywhere https://mdn.github.io/sw-test/

This is demo service worker code

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');
      });
    }
  }));
});

When I open developer console, I get this error: "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."

In the same time, this same code works without errors in Chrome.

In about:config setting dom.serviceWorkers.enabled is set to true . I have also disabled all plugins and it still isn't working.

What could be the problem?

I have refreshed ( about:support ) standard firefox v56 and it didn't work. But it has started to work in Firefox developer edition v57 after browser was refreshed. It seems to be a Firefox bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM