global.tsx 2.9 KB

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