Skip to content

Instantly share code, notes, and snippets.

@dayitv89
Last active July 19, 2022 15:38
Show Gist options
  • Save dayitv89/b13612eb63a954742049bac76f7df74d to your computer and use it in GitHub Desktop.
Save dayitv89/b13612eb63a954742049bac76f7df74d to your computer and use it in GitHub Desktop.
vue.js react.js or any PWA for service worker cache updates use this.
/* eslint-disable no-console */
import { register } from "register-service-worker";
if (process.env.NODE_ENV === "production") {
register(`${process.env.BASE_URL}service-worker.js`, {
registrationOptions: { scope: './' },
ready() {
console.log("App is being served from cache by a service worker. 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(registration) {
console.log("New content is available; please refresh.");
registration.waiting.postMessage({ type: 'SKIP_WAITING' })
if (confirm("new update available update now in less than a second?")) {
window.location.reload()
}
},
offline() {
console.log("No internet connection found. App is running in offline mode.");
},
error(error) {
console.error("Error during service worker registration:", error);
},
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment