global.jsx 2.7KB

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