global.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * @Author: your name
  3. * @Date: 2021-09-03 14:28:27
  4. * @LastEditTime: 2022-01-01 16:17:14
  5. * @LastEditors: Please set LastEditors
  6. * @Description: In User Settings Edit
  7. * @FilePath: /MedicalWisdomCheckSys/src/global.tsx
  8. */
  9. import { Button, message, notification } from 'antd';
  10. import defaultSettings from '../config/defaultSettings';
  11. const { pwa } = defaultSettings;
  12. const isHttps = document.location.protocol === 'https:';
  13. // if pwa is true
  14. if (pwa) {
  15. // Notify user if offline now
  16. window.addEventListener('sw.offline', () => {
  17. message.warning('app.pwa.offline');
  18. });
  19. // Pop up a prompt on the page asking the user if they want to use the latest version
  20. window.addEventListener('sw.updated',(event: Event) => {
  21. const e = event as CustomEvent;
  22. const reloadSW = async () => {
  23. // Check if there is sw whose state is waiting in ServiceWorkerRegistration
  24. // https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration
  25. const worker = e.detail && e.detail.waiting;
  26. if (!worker) {
  27. return true;
  28. }
  29. // Send skip-waiting event to waiting SW with MessageChannel
  30. await new Promise((resolve, reject) => {
  31. const channel = new MessageChannel();
  32. channel.port1.onmessage = (msgEvent) => {
  33. if (msgEvent.data.error) {
  34. reject(msgEvent.data.error);
  35. } else {
  36. resolve(msgEvent.data);
  37. }
  38. };
  39. worker.postMessage({ type: 'skip-waiting' }, [channel.port2]);
  40. });
  41. // Refresh current page to use the updated HTML and other assets after SW has skiped waiting
  42. window.location.reload();
  43. return true;
  44. };
  45. const key = `open${Date.now()}`;
  46. const btn = (
  47. <Button
  48. type="primary"
  49. onClick={() => {
  50. notification.close(key);
  51. reloadSW();
  52. }}
  53. >
  54. app.pwa.serviceworker.updated.ok
  55. </Button>
  56. );
  57. notification.open({
  58. message: 'app.pwa.serviceworker.updated',
  59. description: 'pp.pwa.serviceworker.updated.hint',
  60. btn,
  61. key,
  62. onClose: async () => null,
  63. });
  64. });
  65. } else if ('serviceWorker' in navigator && isHttps) {
  66. // unregister service worker
  67. const { serviceWorker } = navigator;
  68. if (serviceWorker.getRegistrations) {
  69. serviceWorker.getRegistrations().then((sws) => {
  70. sws.forEach((sw) => {
  71. sw.unregister();
  72. });
  73. });
  74. }
  75. serviceWorker.getRegistration().then((sw) => {
  76. if (sw) sw.unregister();
  77. });
  78. //remove all caches
  79. // if (window.caches && await window.caches.keys()) {
  80. // caches.keys().then((keys) => {
  81. // keys.forEach((key) => {
  82. // caches.delete(key);
  83. // });
  84. // });
  85. // }
  86. }