sw.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const CACHE_NAME = 'MeeChat_v1.3.3';
  2. let urlsToCache = ["//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-074cda02.71019e7c.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-0a2f1101.a926c3e5.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-0a63248c.d6883bf2.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-1a710e5c.6c4d2f2f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-1e6a2ebb.35199dcf.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-20f2b187.3f1792fa.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-2e7e4d8a.1657fb5f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-3028e170.7b7b6769.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-38822a98.601b91da.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-42e7d288.90421a20.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-451c71fa.ab91c1a1.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-5aae9c76.ca822529.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-6e987cd2.9dccaae5.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-6f498702.a8887c6a.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-70ed3fb8.1e2497e0.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-72b48984.43d8db4c.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-79914fca.2512f81b.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-7bbde94a.eb6f0950.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-7d4da030.e64410a5.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-90cf752e.94ab0962.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-a7081a9a.ecdaf94d.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-acbed89a.9d1d67ea.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-c173b1d8.41b3f531.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-common.a39d5ac5.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-d3efe52c.feb38348.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-e59b4412.ac7535b9.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-f0006e14.70577f6e.css","//static.meechat.me/cdn/new.mee.chat/dist/css/chunk-vendors.cb2042b6.css","//static.meechat.me/cdn/new.mee.chat/dist/css/h5.906f4d0f.css","//static.meechat.me/cdn/new.mee.chat/dist/css/mini.6b1598b2.css","//static.meechat.me/cdn/new.mee.chat/dist/css/pc.654ee091.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","/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-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-074cda02.f1509b9b.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-0a2f1101.8895c45b.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-0a63248c.eba745db.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-1a710e5c.ed2608cc.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-1e6a2ebb.eeb87ac5.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-20f2b187.a8bb31ae.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-2781551f.bf9c7b06.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-2e7e4d8a.d0745a7d.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-3028e170.a8ff3abf.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-335f5c5d.ab9bd1ab.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-38822a98.8187d07d.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-42e7d288.dcc9507d.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-451c71fa.f357f955.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-5aae9c76.6ccc574b.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-6e987cd2.bf8416b4.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-6f498702.ffa6ccd4.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-70ed3fb8.3c99f763.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-72b48984.53dc30cb.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-79914fca.60886ca8.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-7bbde94a.fb9db95d.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-7d4da030.d91617fa.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-90cf752e.81f6962e.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-a7081a9a.1d57e685.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-acbed89a.c056034b.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-c173b1d8.0223e2e5.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-common.cc84a9e2.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-d3efe52c.83f4abfb.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-e59b4412.25c2d70b.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-f0006e14.b66d43ac.js","//static.meechat.me/cdn/new.mee.chat/dist/js/chunk-vendors.67faea15.js","//static.meechat.me/cdn/new.mee.chat/dist/js/h5.a5418ae4.js","//static.meechat.me/cdn/new.mee.chat/dist/js/mini.19bc9742.js","//static.meechat.me/cdn/new.mee.chat/dist/js/pc.b8bdd38d.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. })