sw.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const CACHE_NAME = 'MeeChat_v1.3.4';
  2. let urlsToCache = ["//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-17f20f02.0b938788.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-21bd4ca5.19ff19e6.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-23212ac5.7b7b6769.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-3a2cfe68.4d1c99dd.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-3ab2e3e3.621d2337.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-3ae6835b.601b91da.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-484a20ba.6c4d2f2f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-4be0e358.e275999c.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-4eaa1259.4462b17e.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-55ee6bb8.f31f92a7.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-579c6ae2.f3c7912e.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-5b86aac9.a00b706f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-5c065784.1e2497e0.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-650cd7d0.f8b796f1.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-652c94c5.9dccaae5.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-6aeec874.735872c4.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-6cae8a9b.a8887c6a.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-74d0d27c.cad96ca9.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-7fdb0984.f80ccfd7.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-a6830cb4.feb38348.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-b5c34688.af0499b2.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-ba45f5e6.4ad04c39.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-common.15737a0f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-d7186f64.9ac0224d.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-dcb2943e.fd3b7528.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-eab6c874.835e2cbb.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-ed0382aa.4b654aae.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-fbfb9796.8e1bb200.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-vendors.be5bb01d.css","//static.meechat.me/cdn/new.mee.chat/dist/css/h5.906f4d0f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/mini.7e846a75.css","//static.meechat.me/cdn/new.mee.chat/dist/css/pc.bdb56b85.css","//static.meechat.me/cdn/new.mee.chat/dist/favicon.ico","//static.meechat.me/cdn/new.mee.chat/dist/fonts/element-icons.535877f5.woff","//static.meechat.me/cdn/new.mee.chat/dist/fonts/element-icons.732389de.ttf","/h5.html","//static.meechat.me/cdn/new.mee.chat/dist/img/bg.261295d3.jpg","//static.meechat.me/cdn/new.mee.chat/dist/img/bg.71de928a.jpg","//static.meechat.me/cdn/new.mee.chat/dist/img/icon-eth.f49c5cf6.png","//static.meechat.me/cdn/new.mee.chat/dist/img/icon-meetone.f0168873.png","//static.meechat.me/cdn/new.mee.chat/dist/img/icon-tele.0c036f6a.png","//static.meechat.me/cdn/new.mee.chat/dist/img/icon-tele.88177f4a.png","//static.meechat.me/cdn/new.mee.chat/dist/img/icons/meechat.png","//static.meechat.me/cdn/new.mee.chat/dist/img/icons/meechat_icon.png","//static.meechat.me/cdn/new.mee.chat/dist/img/icons/safari-pinned-tab.svg","//static.meechat.me/cdn/new.mee.chat/dist/img/loading.b4490fa6.gif","//static.meechat.me/cdn/new.mee.chat/dist/img/login-logo.6f40e8ff.png","//static.meechat.me/cdn/new.mee.chat/dist/img/meetone-icon.f0168873.jpg","//static.meechat.me/cdn/new.mee.chat/dist/img/user-ethereum-icon.fdf8b24e.png","//static.meechat.me/cdn/new.mee.chat/dist/img/user-tron-icon.92300839.png","/index.html","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-17f20f02.028ca99e.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-21bd4ca5.5edf431f.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-23212ac5.495e6717.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-335f5c5d.ae359ac3.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-3a2cfe68.8856c27f.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-3ab2e3e3.7bdfd765.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-3ae6835b.65cce310.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-484a20ba.f98c653b.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-4be0e358.0134820c.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-4eaa1259.f6897660.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-55ee6bb8.642b9d3f.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-579c6ae2.801520b4.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-5b86aac9.6cd73d35.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-5c065784.1ecea1c6.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-650cd7d0.4edc6889.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-652c94c5.a2d760b7.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-6aeec874.50a341d2.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-6cae8a9b.5515e975.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-74d0d27c.e822f118.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-7fdb0984.21c4c9c5.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-a6830cb4.1ed61dfc.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-b5c34688.368c663d.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-ba45f5e6.1718c2d7.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-common.0280e57f.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-d7186f64.9705837b.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-dcb2943e.3636e365.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-eab6c874.9277e1e9.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-ed0382aa.62c0e7d4.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-fbfb9796.bc1a3bfd.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-vendors.eae0cca2.js","//static.meechat.me/cdn/new.mee.chat/dist/js/h5.cb14af83.js","//static.meechat.me/cdn/new.mee.chat/dist/js/mini.9ed160b4.js","//static.meechat.me/cdn/new.mee.chat/dist/js/pc.d387f279.js","/manifest_h5.json","/manifest_pc.json","/mini.html","/pc.html","//static.meechat.me/cdn/new.mee.chat/dist/robots.txt","//static.meechat.me/cdn/new.mee.chat/dist/sw.js"];
  3. // let networkFirstList = ['', 'h5.html', 'mini.html'];
  4. let networkFirstList = [];
  5. self.addEventListener('install', function (event) {
  6. self.skipWaiting();
  7. console.log('[ServiceWorker] install')
  8. event.waitUntil(
  9. caches.open(CACHE_NAME).then(function(caches){
  10. console.log('open cache:' + CACHE_NAME);
  11. let allList = urlsToCache.concat(networkFirstList)
  12. return caches.addAll(allList);
  13. })
  14. )
  15. })
  16. self.addEventListener('activate', function (event) {
  17. console.log('[ServiceWorker] activate')
  18. // 只保留白名单的版本
  19. const cacheWhitelist = [CACHE_NAME];
  20. event.waitUntil(
  21. caches.keys().then(function(cacheNames) {
  22. return Promise.all(
  23. cacheNames.map(function(cacheName) {
  24. console.log('遍历缓存:' + cacheName)
  25. if (cacheWhitelist.indexOf(cacheName) === -1) {
  26. console.log('删除缓存:' + cacheName)
  27. return caches.delete(cacheName)
  28. }
  29. })
  30. )
  31. })
  32. )
  33. })
  34. self.addEventListener('fetch', function (event) {
  35. // console.log('[ServiceWorker] Fetch', event.request.url)
  36. let url = event.request.url.replace(location.origin, '')
  37. url = url.replace('https:', '')
  38. let pos = url.indexOf('#')
  39. if (pos > 0) {
  40. url = url.substr(0, pos)
  41. }
  42. if (urlsToCache.indexOf(url) !== -1) {
  43. return _cacheFirst(event)
  44. } else {
  45. return _networkFirst(event)
  46. }
  47. })
  48. function _cacheFirst(event) {
  49. event.respondWith(
  50. caches.match(event.request).then(function (response) {
  51. // Cache hit - return response
  52. if (response) {
  53. return response;
  54. }
  55. return fetch(event.request);
  56. })
  57. );
  58. }
  59. function _networkFirst(event) {
  60. event.respondWith(
  61. fetch(event.request).then((response) => {
  62. // console.log('[ServiceWorker] network first, request: ', event.request.url);
  63. return response
  64. }).catch (() => {
  65. caches.match(event.request).then(function (response) {
  66. // Cache hit - return response
  67. if (response) {
  68. // console.log('[ServiceWorker] cache first, From cache:', event.request.url)
  69. return response;
  70. }
  71. // console.log('[ServiceWorker] cache first, From network:', event.request.url)
  72. return fetch(event.request);
  73. })
  74. })
  75. );
  76. }
  77. self.addEventListener('error', function (event) {
  78. console.log('[ServiceWorker] error', event)
  79. })
  80. self.addEventListener('push', function (event) {
  81. console.log('[Service Worker] 收到通知', event);
  82. function decodeUnicode (str) {
  83. str = str.replace(/\\/g, '%')
  84. str = unescape(str)
  85. str = str.replace(/%/g, '\\')
  86. str = str.replace(/\\/g, '')
  87. return str
  88. }
  89. let data = JSON.parse(event.data.text())
  90. const title = decodeUnicode(data.title)
  91. const options = {
  92. body: decodeUnicode(data.content),
  93. icon: 'img/icons/meechat.png',
  94. tag: data.tag,
  95. data: data.data,
  96. }
  97. event.waitUntil(self.registration.showNotification(title, options))
  98. })
  99. // 点击跳转
  100. self.addEventListener('notificationclick', function (event) {
  101. console.log('[Service Worker] 点击通知', event);
  102. let data = event.notification.data
  103. let url = data && data.url || 'https://mee.chat/'
  104. event.notification.close()
  105. event.waitUntil(
  106. clients.openWindow(url)
  107. )
  108. })