sw.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const CACHE_NAME = 'MeeChat_v1.2.2';
  2. let urlsToCache = ["//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-09f52bd0.ca822529.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-16c7788e.652db6b3.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-1d9b7d3a.2141a0c4.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-1e160aad.9d966e19.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-352466e2.33bf20c7.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-392b6146.2b7cc37a.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-3c3559fc.c581b33e.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-3d694970.cd5067d4.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-4031c6b3.16b86625.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-580cf024.3e00a85d.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-5a6c5be1.5efbb31d.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-60610c8a.1eb38ece.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-6361f770.e85683b9.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-66871504.0c677439.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-6c021ec0.2806d288.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-6f498702.a8887c6a.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-74a922ec.4b0c5d76.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-a81e4a76.8bcff4db.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-b17cf818.1737301f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-b401ae72.94a16efd.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-cb34f022.745242a7.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-common.3e30cf5b.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-e1cff4e2.f84fbbfb.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-vendors.7cb03c3e.css","//static.meechat.me/cdn/new.mee.chat/dist/css/h5.48da3384.css","//static.meechat.me/cdn/new.mee.chat/dist/css/mini.ada854d2.css","//static.meechat.me/cdn/new.mee.chat/dist/css/pc.beaa8f19.css","//static.meechat.me/cdn/new.mee.chat/dist/favicon.ico","//static.meechat.me/cdn/new.mee.chat/dist/fonts/element-icons.2fad952a.woff","//static.meechat.me/cdn/new.mee.chat/dist/fonts/element-icons.6f0a7632.ttf","//static.meechat.me/cdn/new.mee.chat/dist/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/bg.eb029f3a.png","//static.meechat.me/cdn/new.mee.chat/dist/img/bg2.0b28333d.png","//static.meechat.me/cdn/new.mee.chat/dist/img/h5_bg.e9487380.png","//static.meechat.me/cdn/new.mee.chat/dist/img/h5_bg2.21b33271.png","//static.meechat.me/cdn/new.mee.chat/dist/img/h5_bg3.6f597ff4.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/user-ethereum-icon.fdf8b24e.png","//static.meechat.me/cdn/new.mee.chat/dist/img/user-tron-icon.92300839.png","//static.meechat.me/cdn/new.mee.chat/dist/index.html","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-09f52bd0.9c00bf68.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-16c7788e.435a1a7e.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-1d9b7d3a.7a6ad702.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-1e160aad.1a7361d6.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-352466e2.fdd374fe.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-392b6146.e339a89c.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-3c3559fc.0f399dba.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-3d694970.ad0e5512.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-4031c6b3.0b1e3043.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-437e2b39.9d30634e.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-580cf024.14d7734a.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-5a6c5be1.44dac6b5.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-60610c8a.a47d6ec6.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-6361f770.f496f1c3.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-66871504.8a1d69f1.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-6c021ec0.66abd4ea.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-6f498702.4911296f.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-74a922ec.75d6a583.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-a81e4a76.bbf68338.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-b17cf818.913b8d09.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-b401ae72.0dd96389.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-cb34f022.cb90cc1e.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-common.d71e6f00.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-d42f0a14.fca71a2d.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-e1cff4e2.f3763875.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-vendors.a47fe035.js","//static.meechat.me/cdn/new.mee.chat/dist/js/h5.8c6f0b13.js","//static.meechat.me/cdn/new.mee.chat/dist/js/mini.1dbbc78a.js","//static.meechat.me/cdn/new.mee.chat/dist/js/pc.4cc8c1cc.js","//static.meechat.me/cdn/new.mee.chat/dist/manifest_h5.json","//static.meechat.me/cdn/new.mee.chat/dist/manifest_pc.json","//static.meechat.me/cdn/new.mee.chat/dist/mini.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('https:', '')
  37. if (urlsToCache.indexOf(url) !== -1) {
  38. return _cacheFirst(event)
  39. } else {
  40. return _networkFirst(event)
  41. }
  42. })
  43. function _cacheFirst(event, response) {
  44. event.respondWith(
  45. caches.match(event.request).then(function (response) {
  46. // Cache hit - return response
  47. if (response) {
  48. return response;
  49. }
  50. return fetch(event.request);
  51. })
  52. );
  53. }
  54. function _networkFirst(event) {
  55. event.respondWith(
  56. fetch(event.request).then((response) => {
  57. // console.log('[ServiceWorker] network first, request: ', event.request.url);
  58. return response
  59. }).catch (() => {
  60. caches.match(event.request).then(function (response) {
  61. // Cache hit - return response
  62. if (response) {
  63. // console.log('[ServiceWorker] cache first, From cache:', event.request.url)
  64. return response;
  65. }
  66. // console.log('[ServiceWorker] cache first, From network:', event.request.url)
  67. return fetch(event.request);
  68. })
  69. })
  70. );
  71. }
  72. self.addEventListener('error', function (event) {
  73. console.log('[ServiceWorker] error', event)
  74. })
  75. self.addEventListener('push', function (event) {
  76. console.log('[Service Worker] 收到通知', event);
  77. function decodeUnicode (str) {
  78. str = str.replace(/\\/g, '%')
  79. str = unescape(str)
  80. str = str.replace(/%/g, '\\')
  81. str = str.replace(/\\/g, '')
  82. return str
  83. }
  84. let data = JSON.parse(event.data.text())
  85. const title = decodeUnicode(data.title)
  86. const options = {
  87. body: decodeUnicode(data.content),
  88. icon: 'img/icons/meechat.png',
  89. tag: data.tag,
  90. data: data.data,
  91. }
  92. event.waitUntil(self.registration.showNotification(title, options))
  93. })
  94. // 点击跳转
  95. self.addEventListener('notificationclick', function (event) {
  96. console.log('[Service Worker] 点击通知', event);
  97. let data = event.notification.data
  98. let url = data && data.url || 'https://mee.chat/'
  99. event.notification.close()
  100. event.waitUntil(
  101. clients.openWindow(url)
  102. )
  103. })