2018-11-12 00:17:21 +01:00
|
|
|
'use strict';
|
|
|
|
|
2018-12-05 19:45:41 +01:00
|
|
|
// NOTE: CacheKey & FilesToCache Lists
|
|
|
|
const cacheKey = 'omapp-v';
|
|
|
|
const shellCacheKey = cacheKey + '1';
|
|
|
|
const logicCasheKey = shellCacheKey + '.' + '0';
|
|
|
|
const dataCacheKey = logicCasheKey + '.' + '0';
|
|
|
|
const shellFilesToCache = [
|
2018-11-14 11:48:32 +01:00
|
|
|
'/',
|
2018-12-05 19:45:41 +01:00
|
|
|
'/manifest.json',
|
2018-11-14 11:48:32 +01:00
|
|
|
'/index.html',
|
2018-11-21 15:54:09 +01:00
|
|
|
'/message.html',
|
2018-12-05 19:45:41 +01:00
|
|
|
'/bookmark.js',
|
|
|
|
'/createMessage.js',
|
2018-12-05 21:30:53 +01:00
|
|
|
'/files.js',
|
2018-12-05 19:45:41 +01:00
|
|
|
'/home.js',
|
2018-11-14 11:48:32 +01:00
|
|
|
'/main.js',
|
2018-12-05 19:45:41 +01:00
|
|
|
'/profil.js',
|
2018-12-05 21:30:53 +01:00
|
|
|
'/favicon.ico',
|
2018-12-05 19:45:41 +01:00
|
|
|
'/img/th_nbg_ohmicon_amp.png',
|
|
|
|
'/lib/jquery-3.3.1.min.js',
|
|
|
|
'/lib/vue.js',
|
|
|
|
'/lib/vue-router.js',
|
|
|
|
'/style/style.css',
|
|
|
|
];
|
|
|
|
const logicFilesToCache = [
|
|
|
|
'',
|
|
|
|
];
|
|
|
|
const dataFilesToCache = [
|
|
|
|
'',
|
2018-11-14 11:48:32 +01:00
|
|
|
];
|
2018-11-12 00:17:21 +01:00
|
|
|
|
2018-11-14 11:48:32 +01:00
|
|
|
// NOTE: Registration done in main.js
|
2018-11-12 00:17:21 +01:00
|
|
|
|
|
|
|
// NOTE: Install and activate: Populating cache
|
|
|
|
self.addEventListener('install', function (event) {
|
2018-12-05 19:45:41 +01:00
|
|
|
console.log('[ServiceWorker] Install');
|
2018-11-12 00:17:21 +01:00
|
|
|
event.waitUntil(
|
2018-12-05 19:45:41 +01:00
|
|
|
caches.open(shellCacheKey).then(function (cache) {
|
|
|
|
console.log('[ServiceWorker] Cache shell files.');
|
|
|
|
return cache.addAll(shellFilesToCache);
|
2018-12-05 21:30:53 +01:00
|
|
|
}).then(self.skipWaiting()),
|
2018-12-05 19:45:41 +01:00
|
|
|
caches.open(logicCasheKey).then(function (cache) {
|
|
|
|
console.log('[ServiceWorker] Cache logic files.');
|
|
|
|
return cache.addAll(logicFilesToCache);
|
2018-12-05 21:30:53 +01:00
|
|
|
}).then(self.skipWaiting()),
|
2018-12-05 19:45:41 +01:00
|
|
|
caches.open(dataCacheKey).then(function (cache) {
|
|
|
|
console.log('[ServiceWorker] Cache data files.');
|
|
|
|
return cache.addAll(dataFilesToCache);
|
2018-12-05 21:30:53 +01:00
|
|
|
}).then(self.skipWaiting())
|
2018-11-12 00:17:21 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2018-11-14 11:48:32 +01:00
|
|
|
// NOTE: Activate structure and delete older cache versions
|
|
|
|
self.addEventListener('activate', function (event) {
|
2018-12-05 19:45:41 +01:00
|
|
|
console.log('[ServiceWorker] Activate');
|
|
|
|
event.waitUntil(
|
|
|
|
caches.keys().then(function (cacheKeyList) {
|
|
|
|
return Promise.all(
|
|
|
|
cacheKeyList.map(function (key) {
|
|
|
|
if (key !== shellCacheKey && key !== logicCasheKey && key !== dataCacheKey) {
|
|
|
|
console.log('[ServiceWorker] Removing old cache', key);
|
|
|
|
return caches.delete(key);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
|
|
|
})
|
2018-11-14 11:48:32 +01:00
|
|
|
);
|
2018-12-05 19:45:41 +01:00
|
|
|
// Shorten activation time for initial [ServiceWorker] call.
|
|
|
|
//return self.clients.claim();
|
2018-11-12 00:17:21 +01:00
|
|
|
});
|
|
|
|
|
2018-11-14 11:48:32 +01:00
|
|
|
|
|
|
|
// NOTE: Fetch structure // TODO: Setup fetching method
|
|
|
|
self.addEventListener('fetch', function (event) {
|
2018-12-05 19:45:41 +01:00
|
|
|
var dataUrl = 'localhost:8888'; // Provide HTTPS URL for query data.
|
|
|
|
console.log('[ServiceWorker] Fetch', event.request.url);
|
|
|
|
|
2018-12-05 21:30:53 +01:00
|
|
|
//if (event.request.url.indexOf(dataUrl) > -1) {
|
2018-12-05 19:45:41 +01:00
|
|
|
/*
|
|
|
|
* When the request URL contains dataUrl, the app is asking for fresh
|
|
|
|
* data. In this case, the service worker always goes to the network
|
|
|
|
* and then caches the response. This is called
|
|
|
|
* the "Cache then network" strategy:
|
|
|
|
* https://jakearchibald.com/2014/offline-cookbook/#cache-then-network
|
|
|
|
*/
|
|
|
|
|
2018-12-05 21:30:53 +01:00
|
|
|
/*event.respondWith(
|
|
|
|
caches.open(shellCacheKey).then(function (cache) {
|
2018-12-05 19:45:41 +01:00
|
|
|
return fetch(event.request).then(function (response) {
|
|
|
|
cache.put(event.request.url, response.clone());
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
} else {
|
2018-12-05 21:30:53 +01:00
|
|
|
*/
|
2018-12-05 19:45:41 +01:00
|
|
|
/*
|
|
|
|
* The app is asking for app shell files. In this scenario the app uses the
|
|
|
|
* "Cache, falling back to the network" offline strategy:
|
|
|
|
* https://jakearchibald.com/2014/offline-cookbook/#cache-falling-back-to-network
|
|
|
|
*/
|
|
|
|
event.respondWith(
|
|
|
|
caches.match(event.request).then(function (response) {
|
|
|
|
return response || fetch(event.request);
|
|
|
|
})
|
|
|
|
);
|
2018-12-05 21:30:53 +01:00
|
|
|
//}
|
|
|
|
/*
|
|
|
|
caches.match(event.request).then(cachedResponse => {
|
|
|
|
if (cachedResponse) {
|
|
|
|
return cachedResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
return caches.open(shellCacheKey).then(cache => {
|
|
|
|
return fetch(event.request).then(response => {
|
|
|
|
// Put a copy of the response in the runtime cache.
|
|
|
|
return cache.put(event.request, response.clone()).then(() => {
|
|
|
|
return response;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
})*/
|
2018-11-14 11:48:32 +01:00
|
|
|
});
|
2018-12-05 19:45:41 +01:00
|
|
|
|