最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Service Worker "install" event not triggering on Firefox (works on Chrome) - Stack Overflow

programmeradmin2浏览0评论

I'm starting to learn about service workers and created a simple one to test. It seems to register, activate and work properly on Chrome, but not on Firefox.

This is sw.js (served from my root folder):

console.log("sw");

self.addEventListener("install", (event) => {
  self.skipWaiting();
  console.log("Installed!");
});

self.addEventListener("activate", function () {
  clients.claim();
  console.log("Activated!");
});

self.addEventListener("fetch", (event) => {
  console.log("fetch");
});

self.addEventListener("push", (event) => {
  console.log("push");
});

I load this in my entry point:

    if ("serviceWorker" in navigator) {
      window.addEventListener("load", () => {
        navigator.serviceWorker
          .register("/sw.js")
          .then((registration) => {
            console.log("SW registered: ", registration);
          })
          .catch((registrationError) => {
            console.log("SW registration failed: ", registrationError);
          });
      });
    }

The Chrome console displays what I expect:

SW registered: >ServiceWorkerRegistration
    [...] 
    state: "activated"
    scope: "/"
sw
Installed!
Activated!
fetch
fetch

The Firefox console only displays this:

SW registered: >ServiceWorkerRegistration
    [...] 
    state: "activated"
    scope: "/"

I can't figure out why Firefox would register the service worker, show it as Running in Application > Service Worker, but not fire the install/activate events and not intercept any fetches, even after reloading the page and/or closing and reopening the tab.

I'm running this from a Gitpod instance logged in and via HTTPS if that makes any difference.

I'm starting to learn about service workers and created a simple one to test. It seems to register, activate and work properly on Chrome, but not on Firefox.

This is sw.js (served from my root folder):

console.log("sw");

self.addEventListener("install", (event) => {
  self.skipWaiting();
  console.log("Installed!");
});

self.addEventListener("activate", function () {
  clients.claim();
  console.log("Activated!");
});

self.addEventListener("fetch", (event) => {
  console.log("fetch");
});

self.addEventListener("push", (event) => {
  console.log("push");
});

I load this in my entry point:

    if ("serviceWorker" in navigator) {
      window.addEventListener("load", () => {
        navigator.serviceWorker
          .register("/sw.js")
          .then((registration) => {
            console.log("SW registered: ", registration);
          })
          .catch((registrationError) => {
            console.log("SW registration failed: ", registrationError);
          });
      });
    }

The Chrome console displays what I expect:

SW registered: >ServiceWorkerRegistration
    [...] 
    state: "activated"
    scope: "https://my.web.site/"
sw
Installed!
Activated!
fetch
fetch

The Firefox console only displays this:

SW registered: >ServiceWorkerRegistration
    [...] 
    state: "activated"
    scope: "https://my.web.site/"

I can't figure out why Firefox would register the service worker, show it as Running in Application > Service Worker, but not fire the install/activate events and not intercept any fetches, even after reloading the page and/or closing and reopening the tab.

I'm running this from a Gitpod instance logged in and via HTTPS if that makes any difference.

Share Improve this question asked Dec 30, 2022 at 5:52 KirsdarkenvaarKirsdarkenvaar 1155 bronze badges 2
  • 1 The issue might not be that it isn't doing any work on Firefox, but that simply nothing is being displayed on the DevTools' console. Or in other words, it's being displayed somewhere else, though I don't know exactly where. This is what brought me here actually. – Hime-sama Commented Feb 15, 2023 at 14:14
  • related: stackoverflow./q/68412841 – djvg Commented May 22, 2023 at 15:13
Add a ment  | 

1 Answer 1

Reset to default 9

Indeed, console message from Service Worker are not shown in the page Dev Tools. You can find your site worker in this page about:debugging#/runtime/this-firefox and click "Inspect" to open new Dev Tools dedicated to the particular Service Worker.

发布评论

评论列表(0)

  1. 暂无评论