Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

serviceWorker.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. 'use strict';
  2. // NOTE: CacheKey & FilesToCache Lists
  3. const cacheKey = 'omapp-v';
  4. const shellCacheKey = cacheKey + '1';
  5. const logicCasheKey = shellCacheKey + '.' + '0';
  6. const dataCacheKey = logicCasheKey + '.' + '0';
  7. const shellFilesToCache = [
  8. '/',
  9. '/manifest.json',
  10. '/index.html',
  11. '/message.html',
  12. '/bookmark.js',
  13. '/createMessage.js',
  14. '/files',
  15. '/home.js',
  16. '/main.js',
  17. '/profil.js',
  18. '/img/favicon.ico',
  19. '/img/th_nbg_ohmicon_amp.png',
  20. '/lib/jquery-3.3.1.min.js',
  21. '/lib/vue.js',
  22. '/lib/vue-router.js',
  23. '/style/style.css',
  24. ];
  25. const logicFilesToCache = [
  26. '',
  27. '',
  28. ];
  29. const dataFilesToCache = [
  30. '',
  31. '',
  32. ];
  33. // NOTE: Registration done in main.js
  34. // NOTE: Install and activate: Populating cache
  35. self.addEventListener('install', function (event) {
  36. console.log('[ServiceWorker] Install');
  37. event.waitUntil(
  38. caches.open(shellCacheKey).then(function (cache) {
  39. console.log('[ServiceWorker] Cache shell files.');
  40. return cache.addAll(shellFilesToCache);
  41. }),
  42. caches.open(logicCasheKey).then(function (cache) {
  43. console.log('[ServiceWorker] Cache logic files.');
  44. return cache.addAll(logicFilesToCache);
  45. }),
  46. caches.open(dataCacheKey).then(function (cache) {
  47. console.log('[ServiceWorker] Cache data files.');
  48. return cache.addAll(dataFilesToCache);
  49. })
  50. );
  51. });
  52. // NOTE: Activate structure and delete older cache versions
  53. self.addEventListener('activate', function (event) {
  54. console.log('[ServiceWorker] Activate');
  55. /*const cacheWhitelist = [staticCacheName];
  56. event.waitUntil(
  57. caches.keys().then(function (cacheNames) {
  58. return Promise.all(
  59. cacheNames.map(function (cacheName) {
  60. if (cacheWhitelist.indexOf(cacheName) === -1) {
  61. return caches.delete(cacheName);
  62. }
  63. })
  64. );
  65. })
  66. );*/
  67. event.waitUntil(
  68. caches.keys().then(function (cacheKeyList) {
  69. return Promise.all(
  70. cacheKeyList.map(function (key) {
  71. if (key !== shellCacheKey && key !== logicCasheKey && key !== dataCacheKey) {
  72. console.log('[ServiceWorker] Removing old cache', key);
  73. return caches.delete(key);
  74. }
  75. })
  76. );
  77. })
  78. );
  79. // Shorten activation time for initial [ServiceWorker] call.
  80. //return self.clients.claim();
  81. });
  82. // NOTE: Fetch structure // TODO: Setup fetching method
  83. self.addEventListener('fetch', function (event) {
  84. var dataUrl = 'localhost:8888'; // Provide HTTPS URL for query data.
  85. console.log('[ServiceWorker] Fetch', event.request.url);
  86. if (event.request.url.indexOf(dataUrl) > -1) {
  87. /*
  88. * When the request URL contains dataUrl, the app is asking for fresh
  89. * data. In this case, the service worker always goes to the network
  90. * and then caches the response. This is called
  91. * the "Cache then network" strategy:
  92. * https://jakearchibald.com/2014/offline-cookbook/#cache-then-network
  93. */
  94. event.respondWith(
  95. caches.open(dataCacheKey).then(function (cache) {
  96. return fetch(event.request).then(function (response) {
  97. cache.put(event.request.url, response.clone());
  98. return response;
  99. });
  100. })
  101. );
  102. } else {
  103. /*
  104. * The app is asking for app shell files. In this scenario the app uses the
  105. * "Cache, falling back to the network" offline strategy:
  106. * https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network
  107. */
  108. event.respondWith(
  109. caches.match(event.request).then(function (response) {
  110. return response || fetch(event.request);
  111. })
  112. );
  113. }
  114. });