|
1 | 1 | /* tslint:disable:no-console */
|
2 | 2 |
|
3 |
| -import { register } from 'register-service-worker'; |
| 3 | +import { register } from "register-service-worker"; |
| 4 | +import { notify } from "./functions/general-helpers"; |
4 | 5 |
|
5 |
| -if (process.env.NODE_ENV === 'production') { |
| 6 | +const notifyUserAboutUpdate = (worker: any) => { |
| 7 | + notify({ |
| 8 | + text: "New version available 🥳, Click here to run the new version!", |
| 9 | + close: false, |
| 10 | + onClick: () => { |
| 11 | + console.log("upgrade confirmed"); |
| 12 | + worker.postMessage({ action: "skipWaiting" }); |
| 13 | + } |
| 14 | + }); |
| 15 | +}; |
| 16 | + |
| 17 | +if (process.env.NODE_ENV === "production") { |
6 | 18 | register(`${process.env.BASE_URL}service-worker.js`, {
|
7 | 19 | ready() {
|
8 | 20 | console.log(
|
9 |
| - 'App is being served from cache by a service worker.\n' + |
10 |
| - 'For more details, visit https://goo.gl/AFskqB', |
| 21 | + "App is being served from cache by a service worker.\n" + |
| 22 | + "For more details, visit https://goo.gl/AFskqB" |
11 | 23 | );
|
12 | 24 | },
|
13 | 25 | registered() {
|
14 |
| - console.log('Service worker has been registered.'); |
| 26 | + console.log("Service worker has been registered."); |
15 | 27 | },
|
16 | 28 | cached() {
|
17 |
| - console.log('Content has been cached for offline use.'); |
| 29 | + console.log("Content has been cached for offline use."); |
18 | 30 | },
|
19 | 31 | updatefound() {
|
20 |
| - console.log('New content is downloading.'); |
| 32 | + console.log("New content is downloading."); |
21 | 33 | },
|
22 |
| - updated() { |
23 |
| - console.log('New content is available; please refresh.'); |
| 34 | + updated(registration) { |
| 35 | + console.log("New content is available; please refresh."); |
| 36 | + notifyUserAboutUpdate(registration.waiting); |
24 | 37 | },
|
25 | 38 | offline() {
|
26 |
| - console.log('No internet connection found. App is running in offline mode.'); |
| 39 | + console.log("No internet connection found. App is running in offline mode."); |
27 | 40 | },
|
28 | 41 | error(error) {
|
29 |
| - console.error('Error during service worker registration:', error); |
30 |
| - }, |
| 42 | + console.error("Error during service worker registration:", error); |
| 43 | + } |
| 44 | + }); |
| 45 | + |
| 46 | + let refreshing: any; |
| 47 | + navigator.serviceWorker.addEventListener("controllerchange", () => { |
| 48 | + console.log('controllerchange...'); |
| 49 | + if (refreshing) { |
| 50 | + return; |
| 51 | + } |
| 52 | + window.location.reload(); |
| 53 | + refreshing = true; |
31 | 54 | });
|
32 | 55 | }
|
0 commit comments