rishabh3112 / gridsome-plugin-pwa

PWA plugin for gridsome
MIT License
30 stars 18 forks source link

Override of service worker #23

Open idealley opened 4 years ago

idealley commented 4 years ago

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);
    }
  });
}
rishabh3112 commented 4 years ago

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

theshanergy commented 2 years ago

@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.

rishabh3112 commented 2 years ago

@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.

theshanergy commented 2 years ago

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.