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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 dataUrl = 'https://me.efi.th-nuernberg.de/om/';
  107. // Parse the URL:
  108. const requestURL = new URL(event.request.url);
  109. // Start the network request as soon as possible.
  110. //const networkPromise = fetch('/data.json');
  111. event.respondWith(
  112. caches.match(event.request)
  113. .then(cachedResponse => {
  114. if (cachedResponse) {
  115. return cachedResponse;
  116. }
  117. console.log('[ServiceWorker] Fetch', requestURL.href);
  118. return fetch(event.request).then(response => {
  119. // Check if we received a valid response
  120. // - Ensure the response is valid.
  121. // - Check the status is 200 on the response.
  122. // - Make sure the response type is basic, which indicates that it's a request from our origin.
  123. // This means that requests to third party assets aren't cached as well.
  124. if (!response || response.status !== 200 || response.type !== 'basic') {
  125. return response;
  126. }
  127. // We have to clone the response here because request bodies
  128. // can only be read once. Placing a response in the cache
  129. // counts as a read and so does the `res.json` call below.
  130. var responseToCache = response.clone();
  131. caches.open(staticCacheKey).then(cache => {
  132. cache.put(event.request, responseToCache);
  133. });
  134. return response;
  135. });
  136. })
  137. );
  138. });