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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict';
  2. /* ================================== */
  3. /* NOTE: Registration done in main.js */
  4. /* ================================== */
  5. /* =================================== */
  6. /* NOTE: CacheKey & FilesToCache Lists */
  7. /* =================================== */
  8. const version = '1';
  9. const appPrefix = 'ohmnews-';
  10. const staticCacheKey = appPrefix + 'static-v' + version;
  11. const dataCacheKey = appPrefix + 'content-data';
  12. var allCacheKey = [
  13. staticCacheKey,
  14. dataCacheKey
  15. ];
  16. /* ================================== */
  17. /* NOTE: Install: Save files to cache */
  18. /* ================================== */
  19. // Install new service worker even when old version still in use.
  20. // Install happens only once a lifetime of a service worker.
  21. const cacheResources = async () => {
  22. const staticFilesToCache = [
  23. './',
  24. 'favicon.ico',
  25. 'index.html',
  26. 'main.js',
  27. 'manifest.json',
  28. 'font/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2',
  29. 'font/KFOlCnqEu92Fr1MmEU9fBBc4.woff2',
  30. 'font/KFOlCnqEu92Fr1MmEU9fChc4EsA.woff2',
  31. 'font/KFOlCnqEu92Fr1MmSU5fBBc4.woff2',
  32. 'font/KFOlCnqEu92Fr1MmSU5fChc4EsA.woff2',
  33. 'font/KFOlCnqEu92Fr1MmWUlfBBc4.woff2',
  34. 'font/KFOlCnqEu92Fr1MmWUlfChc4EsA.woff2',
  35. 'font/KFOmCnqEu92Fr1Mu4mxK.woff2',
  36. 'font/KFOmCnqEu92Fr1Mu7GxKOzY.woff2',
  37. 'img/launcher/launcher_app_icon_152.png',
  38. 'img/launcher/launcher_app_icon_167.png',
  39. 'img/launcher/launcher_app_icon_180.png',
  40. 'img/launcher/launcher_app_icon_192.png',
  41. 'img/launcher/launcher_app_icon_512.png',
  42. 'img/app_icon.png',
  43. 'img/launcher_app_icon.png',
  44. 'img/profil_icon.png',
  45. 'lib/bootstrap-font-and-icons.css',
  46. 'lib/bootstrap-material-design.js',
  47. 'lib/bootstrap-material-design.min.css',
  48. 'lib/jquery-3.3.1.min.js',
  49. 'lib/popper-1.12.6.js',
  50. 'lib/vue-router.js',
  51. 'lib/vue.js',
  52. 'routes/bookmark.js',
  53. 'routes/createMessage.js',
  54. 'routes/files.js',
  55. 'routes/home.js',
  56. 'routes/messageData.js',
  57. 'routes/msgCard.js',
  58. 'routes/profil.js',
  59. 'routes/profilCard.js',
  60. 'routes/profilData.js',
  61. 'style/style.css',
  62. ];
  63. const dataFilesToCache = [];
  64. const cacheStatic = await caches.open(staticCacheKey);
  65. cacheStatic.addAll(staticFilesToCache);
  66. console.log('[ServiceWorker] Cache static files.');
  67. const cacheData = await caches.open(dataCacheKey);
  68. cacheData.addAll(dataFilesToCache);
  69. console.log('[ServiceWorker] Cache data files.');
  70. return;
  71. }
  72. self.addEventListener('install', event => {
  73. // don't wait
  74. self.skipWaiting();
  75. // cache static files
  76. event.waitUntil(cacheResources())
  77. console.log('[ServiceWorker] Install');
  78. });
  79. /* ================================================================ */
  80. /* NOTE: Activate: new service worker and delete old cache versions */
  81. /* ================================================================ */
  82. // No fetch or pull is called before succesfull activate event.
  83. const cacheCleanUp = async () => {
  84. const cacheKeyList = await caches.keys();
  85. const deletions = cacheKeyList
  86. .filter(key => key.startsWith(appPrefix) && !allCacheKey.includes(key))
  87. .map(key => {
  88. caches.delete(key)
  89. console.log('[ServiceWorker] Removing old cache', key);
  90. });
  91. for (const success of deletions) {
  92. await success;
  93. }
  94. return;
  95. }
  96. self.addEventListener('activate', event => {
  97. event.waitUntil(cacheCleanUp());
  98. clients.claim();
  99. console.log('[ServiceWorker] Activate');
  100. });
  101. /* ========================================= */
  102. /* NOTE: Fetch: Update logic for cache files */
  103. /* ========================================= */
  104. self.addEventListener('fetch', event => {
  105. // Provide HTTPS URL for query data.
  106. //const postMsgURL = 'https://me.efi.th-nuernberg.de/om/api/createMsg';
  107. //const postMsgURL = 'http://localhost:8013/api/createMsg';
  108. /* We should only cache GET requests, and deal with the rest of method in the
  109. client-side, by handling failed POST,PUT,PATCH,etc. requests. */
  110. if (event.request.method !== 'GET') { // && event.request.url !== postMsgURL
  111. /* If we don't block the event as shown below, then the request will go to
  112. the network as usual. */
  113. console.log('[ServiceWorker] Fetch event ignored.', event.request.method, event.request.url);
  114. return;
  115. }
  116. event.respondWith(async function update() {
  117. try {
  118. var requestURL = new URL(event.request.url);
  119. /*if (requestURL.href === postMsgURL) {
  120. console.log('New Msg created. ReqURL: ', event.request.method, event.request.url);
  121. }*/
  122. // Start the network request as soon as possible.
  123. const networkPromise = fetch(requestURL);
  124. const cachedResponse = await caches.match(event.request);
  125. const networkResponse = await networkPromise;
  126. // Check if response is valid, status is 200, response type is basic
  127. // (indicates request is from origin, means that requests to third party
  128. // assets aren't cached as well.
  129. if (!networkResponse || networkResponse.status !== 200
  130. || networkResponse.type !== 'basic') return networkResponse;
  131. const cache = await caches.open(staticCacheKey);
  132. // We have to clone the response here because request bodies can only
  133. // be read once. Placing a response in the cache counts as a read.
  134. cache.put(event.request, networkResponse.clone());
  135. if (cachedResponse) return cachedResponse;
  136. console.log('[ServiceWorker] Fetch', event.request);
  137. return networkResponse;
  138. } catch (err) {
  139. // Report a lack of connectivity to the user.
  140. console.log('No Network connection: ',err);
  141. }
  142. }());
  143. });