sw.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const CACHE_NAME = 'MeeChat_v1.2.2';
  2. let urlsToCache = [];
  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. })