gridsome-plugin-pwa
gridsome-plugin-pwa copied to clipboard
Override of service worker
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);
}
});
}
Hi @idealley,
You can use the disableServiceWorker
option to disable service worker generated from the plugin.
Thanks!
@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 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.
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.