blob: afa1fab56655ff4d418a91dcf16467f3340b184c [file] [log] [blame]
86133aaa3f5d2025-04-20 21:33:29 +08001import { useIntl } from '@umijs/max';
2import { Button, message, notification } from 'antd';
3import defaultSettings from '../config/defaultSettings';
4
5const { pwa } = defaultSettings;
6const isHttps = document.location.protocol === 'https:';
7
8const clearCache = () => {
9 // remove all caches
10 if (window.caches) {
11 caches
12 .keys()
13 .then((keys) => {
14 keys.forEach((key) => {
15 caches.delete(key);
16 });
17 })
18 .catch((e) => console.log(e));
19 }
20};
21
22// if pwa is true
23if (pwa) {
24 // Notify user if offline now
25 window.addEventListener('sw.offline', () => {
26 message.warning(useIntl().formatMessage({ id: 'app.pwa.offline' }));
27 });
28
29 // Pop up a prompt on the page asking the user if they want to use the latest version
30 window.addEventListener('sw.updated', (event: Event) => {
31 const e = event as CustomEvent;
32 const reloadSW = async () => {
33 // Check if there is sw whose state is waiting in ServiceWorkerRegistration
34 // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
35 const worker = e.detail && e.detail.waiting;
36 if (!worker) {
37 return true;
38 }
39 // Send skip-waiting event to waiting SW with MessageChannel
40 await new Promise((resolve, reject) => {
41 const channel = new MessageChannel();
42 channel.port1.onmessage = (msgEvent) => {
43 if (msgEvent.data.error) {
44 reject(msgEvent.data.error);
45 } else {
46 resolve(msgEvent.data);
47 }
48 };
49 worker.postMessage({ type: 'skip-waiting' }, [channel.port2]);
50 });
51
52 clearCache();
53 window.location.reload();
54 return true;
55 };
56 const key = `open${Date.now()}`;
57 const btn = (
58 <Button
59 type="primary"
60 onClick={() => {
61 notification.destroy(key);
62 reloadSW();
63 }}
64 >
65 {useIntl().formatMessage({ id: 'app.pwa.serviceworker.updated.ok' })}
66 </Button>
67 );
68 notification.open({
69 message: useIntl().formatMessage({ id: 'app.pwa.serviceworker.updated' }),
70 description: useIntl().formatMessage({ id: 'app.pwa.serviceworker.updated.hint' }),
71 btn,
72 key,
73 onClose: async () => null,
74 });
75 });
76} else if ('serviceWorker' in navigator && isHttps) {
77 // unregister service worker
78 const { serviceWorker } = navigator;
79 if (serviceWorker.getRegistrations) {
80 serviceWorker.getRegistrations().then((sws) => {
81 sws.forEach((sw) => {
82 sw.unregister();
83 });
84 });
85 }
86 serviceWorker.getRegistration().then((sw) => {
87 if (sw) sw.unregister();
88 });
89
90 clearCache();
91}