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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.js',
  15. '/home.js',
  16. '/main.js',
  17. '/profil.js',
  18. '/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. const dataFilesToCache = [
  29. '',
  30. ];
  31. // NOTE: Registration done in main.js
  32. // NOTE: Install and activate: Populating cache
  33. self.addEventListener('install', function (event) {
  34. console.log('[ServiceWorker] Install');
  35. event.waitUntil(
  36. caches.open(shellCacheKey).then(function (cache) {
  37. console.log('[ServiceWorker] Cache shell files.');
  38. return cache.addAll(shellFilesToCache);
  39. }).then(self.skipWaiting()),
  40. caches.open(logicCasheKey).then(function (cache) {
  41. console.log('[ServiceWorker] Cache logic files.');
  42. return cache.addAll(logicFilesToCache);
  43. }).then(self.skipWaiting()),
  44. caches.open(dataCacheKey).then(function (cache) {
  45. console.log('[ServiceWorker] Cache data files.');
  46. return cache.addAll(dataFilesToCache);
  47. }).then(self.skipWaiting())
  48. );
  49. });
  50. // NOTE: Activate structure and delete older cache versions
  51. self.addEventListener('activate', function (event) {
  52. console.log('[ServiceWorker] Activate');
  53. event.waitUntil(
  54. caches.keys().then(function (cacheKeyList) {
  55. return Promise.all(
  56. cacheKeyList.map(function (key) {
  57. if (key !== shellCacheKey && key !== logicCasheKey && key !== dataCacheKey) {
  58. console.log('[ServiceWorker] Removing old cache', key);
  59. return caches.delete(key);
  60. }
  61. })
  62. );
  63. })
  64. );
  65. // Shorten activation time for initial [ServiceWorker] call.
  66. //return self.clients.claim();
  67. });
  68. // NOTE: Fetch structure // TODO: Setup fetching method
  69. self.addEventListener('fetch', function (event) {
  70. var dataUrl = 'localhost:8888'; // Provide HTTPS URL for query data.
  71. console.log('[ServiceWorker] Fetch', event.request.url);
  72. //if (event.request.url.indexOf(dataUrl) > -1) {
  73. /*
  74. * When the request URL contains dataUrl, the app is asking for fresh
  75. * data. In this case, the service worker always goes to the network
  76. * and then caches the response. This is called
  77. * the "Cache then network" strategy:
  78. * https://jakearchibald.com/2014/offline-cookbook/#cache-then-network
  79. */
  80. /*event.respondWith(
  81. caches.open(shellCacheKey).then(function (cache) {
  82. return fetch(event.request).then(function (response) {
  83. cache.put(event.request.url, response.clone());
  84. return response;
  85. });
  86. })
  87. );
  88. } else {
  89. */
  90. /*
  91. * The app is asking for app shell files. In this scenario the app uses the
  92. * "Cache, falling back to the network" offline strategy:
  93. * https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network
  94. */
  95. event.respondWith(
  96. caches.match(event.request).then(function (response) {
  97. return response || fetch(event.request);
  98. })
  99. );
  100. //}
  101. /*
  102. caches.match(event.request).then(cachedResponse => {
  103. if (cachedResponse) {
  104. return cachedResponse;
  105. }
  106. return caches.open(shellCacheKey).then(cache => {
  107. return fetch(event.request).then(response => {
  108. // Put a copy of the response in the runtime cache.
  109. return cache.put(event.request, response.clone()).then(() => {
  110. return response;
  111. });
  112. });
  113. });
  114. })*/
  115. });