2018-11-12 00:17:21 +01:00
|
|
|
'use strict';
|
2019-02-09 19:33:29 +01:00
|
|
|
/* ================================== */
|
|
|
|
/* NOTE: Registration done in main.js */
|
|
|
|
/* ================================== */
|
|
|
|
|
|
|
|
/* =================================== */
|
|
|
|
/* NOTE: CacheKey & FilesToCache Lists */
|
|
|
|
/* =================================== */
|
|
|
|
const version = '1';
|
|
|
|
const appPrefix = 'ohmnews-';
|
|
|
|
const staticCacheKey = appPrefix + 'static-v' + version;
|
|
|
|
const dataCacheKey = appPrefix + 'content-data';
|
|
|
|
var allCacheKey = [
|
2019-02-10 17:34:20 +01:00
|
|
|
staticCacheKey,
|
|
|
|
dataCacheKey
|
2018-11-14 11:48:32 +01:00
|
|
|
];
|
2018-11-12 00:17:21 +01:00
|
|
|
|
2019-02-09 19:33:29 +01:00
|
|
|
/* ================================== */
|
|
|
|
/* NOTE: Install: Save files to cache */
|
|
|
|
/* ================================== */
|
2019-02-03 00:59:23 +01:00
|
|
|
// Install new service worker even when old version still in use.
|
|
|
|
// Install happens only once a lifetime of a service worker.
|
2019-02-09 19:33:29 +01:00
|
|
|
const cacheResources = async () => {
|
2019-02-10 17:34:20 +01:00
|
|
|
const staticFilesToCache = [
|
|
|
|
'./',
|
|
|
|
'favicon.ico',
|
|
|
|
'index.html',
|
|
|
|
'main.js',
|
|
|
|
'manifest.json',
|
|
|
|
'font/flUhRq6tzZclQEJ-Vdg-IuiaDsNc.woff2',
|
|
|
|
'font/KFOlCnqEu92Fr1MmEU9fBBc4.woff2',
|
|
|
|
'font/KFOlCnqEu92Fr1MmEU9fChc4EsA.woff2',
|
|
|
|
'font/KFOlCnqEu92Fr1MmSU5fBBc4.woff2',
|
|
|
|
'font/KFOlCnqEu92Fr1MmSU5fChc4EsA.woff2',
|
|
|
|
'font/KFOlCnqEu92Fr1MmWUlfBBc4.woff2',
|
|
|
|
'font/KFOlCnqEu92Fr1MmWUlfChc4EsA.woff2',
|
|
|
|
'font/KFOmCnqEu92Fr1Mu4mxK.woff2',
|
|
|
|
'font/KFOmCnqEu92Fr1Mu7GxKOzY.woff2',
|
|
|
|
'img/launcher/launcher_app_icon_152.png',
|
|
|
|
'img/launcher/launcher_app_icon_167.png',
|
|
|
|
'img/launcher/launcher_app_icon_180.png',
|
|
|
|
'img/launcher/launcher_app_icon_192.png',
|
|
|
|
'img/launcher/launcher_app_icon_512.png',
|
|
|
|
'img/app_icon.png',
|
|
|
|
'img/launcher_app_icon.png',
|
|
|
|
'img/profil_icon.png',
|
|
|
|
'lib/bootstrap-font-and-icons.css',
|
|
|
|
'lib/bootstrap-material-design.js',
|
|
|
|
'lib/bootstrap-material-design.min.css',
|
|
|
|
'lib/jquery-3.3.1.min.js',
|
|
|
|
'lib/popper-1.12.6.js',
|
|
|
|
'lib/vue-router.js',
|
|
|
|
'lib/vue.js',
|
|
|
|
'routes/bookmark.js',
|
|
|
|
'routes/createMessage.js',
|
|
|
|
'routes/files.js',
|
|
|
|
'routes/home.js',
|
|
|
|
'routes/messageData.js',
|
|
|
|
'routes/msgCard.js',
|
|
|
|
'routes/profil.js',
|
|
|
|
'routes/profilCard.js',
|
|
|
|
'routes/profilData.js',
|
|
|
|
'style/style.css',
|
|
|
|
];
|
|
|
|
const dataFilesToCache = [];
|
2019-02-09 19:33:29 +01:00
|
|
|
|
2019-02-10 17:34:20 +01:00
|
|
|
const cacheStatic = await caches.open(staticCacheKey);
|
|
|
|
cacheStatic.addAll(staticFilesToCache);
|
|
|
|
console.log('[ServiceWorker] Cache static files.');
|
2019-02-09 19:33:29 +01:00
|
|
|
|
2019-02-10 17:34:20 +01:00
|
|
|
const cacheData = await caches.open(dataCacheKey);
|
|
|
|
cacheData.addAll(dataFilesToCache);
|
|
|
|
console.log('[ServiceWorker] Cache data files.');
|
|
|
|
return;
|
2019-02-09 19:33:29 +01:00
|
|
|
}
|
|
|
|
self.addEventListener('install', event => {
|
2019-02-10 17:34:20 +01:00
|
|
|
// don't wait
|
|
|
|
self.skipWaiting();
|
|
|
|
// cache static files
|
|
|
|
event.waitUntil(cacheResources())
|
|
|
|
console.log('[ServiceWorker] Install');
|
2018-11-12 00:17:21 +01:00
|
|
|
});
|
|
|
|
|
2019-02-09 19:33:29 +01:00
|
|
|
/* ================================================================ */
|
|
|
|
/* NOTE: Activate: new service worker and delete old cache versions */
|
|
|
|
/* ================================================================ */
|
2019-02-03 00:59:23 +01:00
|
|
|
// No fetch or pull is called before succesfull activate event.
|
2019-02-09 19:33:29 +01:00
|
|
|
const cacheCleanUp = async () => {
|
2019-02-10 17:34:20 +01:00
|
|
|
const cacheKeyList = await caches.keys();
|
2019-02-09 19:33:29 +01:00
|
|
|
const deletions = cacheKeyList
|
|
|
|
.filter(key => key.startsWith(appPrefix) && !allCacheKey.includes(key))
|
2019-02-10 17:34:20 +01:00
|
|
|
.map(key => {
|
|
|
|
caches.delete(key)
|
|
|
|
console.log('[ServiceWorker] Removing old cache', key);
|
|
|
|
});
|
2019-02-09 19:33:29 +01:00
|
|
|
for (const success of deletions) {
|
2019-02-10 17:34:20 +01:00
|
|
|
await success;
|
2019-02-09 19:33:29 +01:00
|
|
|
}
|
2019-02-10 17:34:20 +01:00
|
|
|
return;
|
2019-02-09 19:33:29 +01:00
|
|
|
}
|
|
|
|
self.addEventListener('activate', event => {
|
2019-02-10 17:34:20 +01:00
|
|
|
event.waitUntil(cacheCleanUp());
|
|
|
|
clients.claim();
|
|
|
|
console.log('[ServiceWorker] Activate');
|
2018-11-12 00:17:21 +01:00
|
|
|
});
|
|
|
|
|
2019-02-09 19:33:29 +01:00
|
|
|
/* ========================================= */
|
|
|
|
/* NOTE: Fetch: Update logic for cache files */
|
|
|
|
/* ========================================= */
|
|
|
|
self.addEventListener('fetch', event => {
|
2019-02-11 00:28:54 +01:00
|
|
|
// Provide HTTPS URL for query data.
|
|
|
|
//const postMsgURL = 'https://me.efi.th-nuernberg.de/om/api/createMsg';
|
2019-02-11 15:40:19 +01:00
|
|
|
//const postMsgURL = 'http://localhost:8013/api/createMsg';
|
2019-02-09 19:33:29 +01:00
|
|
|
|
2019-02-11 00:28:54 +01:00
|
|
|
/* We should only cache GET requests, and deal with the rest of method in the
|
|
|
|
client-side, by handling failed POST,PUT,PATCH,etc. requests. */
|
|
|
|
if (event.request.method !== 'GET') { // && event.request.url !== postMsgURL
|
|
|
|
/* If we don't block the event as shown below, then the request will go to
|
|
|
|
the network as usual. */
|
|
|
|
console.log('[ServiceWorker] Fetch event ignored.', event.request.method, event.request.url);
|
|
|
|
return;
|
|
|
|
}
|
2019-02-09 19:33:29 +01:00
|
|
|
|
2019-02-11 00:28:54 +01:00
|
|
|
event.respondWith(async function update() {
|
|
|
|
try {
|
|
|
|
var requestURL = new URL(event.request.url);
|
2019-02-09 19:33:29 +01:00
|
|
|
|
2019-02-11 00:28:54 +01:00
|
|
|
/*if (requestURL.href === postMsgURL) {
|
|
|
|
console.log('New Msg created. ReqURL: ', event.request.method, event.request.url);
|
|
|
|
}*/
|
2019-02-03 00:59:23 +01:00
|
|
|
|
2019-02-11 00:28:54 +01:00
|
|
|
// Start the network request as soon as possible.
|
|
|
|
const networkPromise = fetch(requestURL);
|
2019-02-03 00:59:23 +01:00
|
|
|
|
2019-02-11 00:28:54 +01:00
|
|
|
const cachedResponse = await caches.match(event.request);
|
2018-12-05 21:30:53 +01:00
|
|
|
|
2019-02-11 00:28:54 +01:00
|
|
|
const networkResponse = await networkPromise;
|
|
|
|
// Check if response is valid, status is 200, response type is basic
|
|
|
|
// (indicates request is from origin, means that requests to third party
|
|
|
|
// assets aren't cached as well.
|
|
|
|
if (!networkResponse || networkResponse.status !== 200
|
|
|
|
|| networkResponse.type !== 'basic') return networkResponse;
|
|
|
|
|
|
|
|
const cache = await caches.open(staticCacheKey);
|
|
|
|
// We have to clone the response here because request bodies can only
|
|
|
|
// be read once. Placing a response in the cache counts as a read.
|
|
|
|
cache.put(event.request, networkResponse.clone());
|
|
|
|
|
2019-02-11 15:40:19 +01:00
|
|
|
if (cachedResponse) return cachedResponse;
|
2019-02-11 00:28:54 +01:00
|
|
|
console.log('[ServiceWorker] Fetch', event.request);
|
|
|
|
return networkResponse;
|
|
|
|
} catch (err) {
|
|
|
|
// Report a lack of connectivity to the user.
|
|
|
|
console.log('No Network connection: ',err);
|
|
|
|
}
|
|
|
|
}());
|
2018-11-14 11:48:32 +01:00
|
|
|
});
|