gridsome-plugin-pwa icon indicating copy to clipboard operation
gridsome-plugin-pwa copied to clipboard

Override of service worker

Open idealley opened this issue 4 years ago • 4 comments

How could I override the service worker to do something like:

/* eslint-disable no-console */

import { register } from "register-service-worker";
import store from "./store";

if (process.env.NODE_ENV === "production") {
  register(`${process.env.VUE_APP_DOMAIN}/service-worker.js`, {
    ready() {
      console.log("App is being served from cache by a service worker.\n" + "For more details, visit https://goo.gl/AFskqB");
    },
    registered() {
      console.log("Service worker has been registered.");
    },
    cached() {
      console.log("Content has been cached for offline use.");
    },
    updatefound() {
      console.log("New content is downloading.");
    },
    updated() {
      console.log("New content is available; please refresh.");
    },
    offline() {
      store.dispatch("base/setOffline");
      console.log("No internet connection found. App is running in offline mode.");
    },
    error(error) {
      console.error("Error during service worker registration:", error);
    }
  });
}

idealley avatar Jan 24 '20 23:01 idealley

Hi @idealley, You can use the disableServiceWorker option to disable service worker generated from the plugin. Thanks!

rishabh3112 avatar Jan 25 '20 06:01 rishabh3112

@rishabh3112 Should disableServiceWorker unregister the service worker? Right now it looks like the service worker sticks around in clients browsers and continues showing stale cached content.

theshanergy avatar Nov 05 '21 23:11 theshanergy

@theshanergy Assuming you have published build with service worker on production, now you are publishing without service worker. In that case we should not be doing this for every non service worker build. Other than that, we can add a new option for adding cleanup.

rishabh3112 avatar Nov 06 '21 11:11 rishabh3112

Thanks @rishabh3112 , I would propose adding support for a self destroying service worker, but also checking disableServiceWorker in the gridsome.client.js clientConfig so that it doesn't attempt to register a service worker that does not exist. Currently it attempts to register options.serviceWorkerPath regardless of whether disableServiceWorker has been set.

theshanergy avatar Nov 08 '21 19:11 theshanergy