2018-10-12 16:42:54 +02:00
|
|
|
/*
|
|
|
|
|
* This file (which will be your service worker)
|
|
|
|
|
* is picked up by the build system ONLY if
|
|
|
|
|
* quasar.conf > pwa > workboxPluginMode is set to "InjectManifest"
|
|
|
|
|
*/
|
|
|
|
|
|
2019-01-31 13:52:52 +01:00
|
|
|
// Questo è il swSrc
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
console.log('05 ___________________________ PAO: this is my custom service worker');
|
|
|
|
|
|
|
|
|
|
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js'); //++Todo: Replace with local workbox.js
|
|
|
|
|
importScripts('js/idb.js');
|
|
|
|
|
importScripts('js/utility.js');
|
2019-01-31 13:52:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!workbox) {
|
2019-02-01 04:10:31 +01:00
|
|
|
let workbox = new self.WorkboxSW();
|
|
|
|
|
|
2019-01-31 13:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (workbox) {
|
2019-02-01 04:10:31 +01:00
|
|
|
// const url = new URL(location.href);
|
|
|
|
|
// const debug = url.searchParams.has('debug');
|
|
|
|
|
const debug = false;
|
|
|
|
|
workbox.setConfig({ debug: debug });
|
|
|
|
|
|
2019-01-31 17:08:10 +01:00
|
|
|
workbox.core.setCacheNameDetails({ prefix: "freeplanet" });
|
2019-01-31 13:52:52 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The workboxSW.precacheAndRoute() method efficiently caches and responds to
|
|
|
|
|
* requests for URLs in the manifest.
|
|
|
|
|
* See https://goo.gl/S9QRab
|
|
|
|
|
*/
|
|
|
|
|
self.__precacheManifest = [].concat(self.__precacheManifest || []);
|
|
|
|
|
workbox.precaching.suppressWarnings();
|
|
|
|
|
workbox.precaching.precacheAndRoute(self.__precacheManifest, {});
|
|
|
|
|
|
2019-01-31 17:08:10 +01:00
|
|
|
// workbox.routing.registerRoute(/^http/, workbox.strategies.networkFirst(), 'GET');
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
/\.(?:png|gif|jpg|jpeg|svg)$/,
|
|
|
|
|
workbox.strategies.staleWhileRevalidate({
|
|
|
|
|
cacheName: 'images',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
|
maxEntries: 60,
|
|
|
|
|
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
|
|
|
|
|
}),
|
|
|
|
|
],
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
2019-01-31 17:08:10 +01:00
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp(/.*(?:googleapis|gstatic)\.com.*$/),
|
|
|
|
|
workbox.strategies.staleWhileRevalidate({
|
|
|
|
|
cacheName: 'google-fonts',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
2019-02-01 04:10:31 +01:00
|
|
|
maxEntries: 30,
|
2019-01-31 17:08:10 +01:00
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
workbox.routing.registerRoute('http://localhost:3000/todos', function (args) {
|
|
|
|
|
return fetch(args.event.request)
|
|
|
|
|
.then(function (res) {
|
|
|
|
|
var clonedRes = res.clone();
|
|
|
|
|
clearAllData('todos')
|
|
|
|
|
.then(function () {
|
|
|
|
|
return clonedRes.json();
|
|
|
|
|
})
|
|
|
|
|
.then(function (data) {
|
|
|
|
|
for (let key in data) {
|
|
|
|
|
writeData('todos', data[key])
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return res;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2019-01-31 17:08:10 +01:00
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp(/.*\/(?:statics\/icons).*$/),
|
|
|
|
|
workbox.strategies.cacheFirst({
|
|
|
|
|
cacheName: 'image-cache',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
|
maxAgeSeconds: 30 * 24 * 60 * 60,
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp(/.*\/(?:css|font).*/),
|
|
|
|
|
workbox.strategies.cacheFirst({
|
|
|
|
|
cacheName: 'css-fonts',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
|
maxAgeSeconds: 30 * 24 * 60 * 60,
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp('https://cdnjs.coudflare.com/ajax/libs/material-design-lite/1.3.0/material.indigo-pink.min.css'),
|
|
|
|
|
workbox.strategies.staleWhileRevalidate({
|
|
|
|
|
cacheName: 'material-css',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
|
maxAgeSeconds: 30 * 24 * 60 * 60,
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Storage
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp(/.*(?:storage\.freeplanet)\.app.*$/),
|
|
|
|
|
workbox.strategies.staleWhileRevalidate({
|
|
|
|
|
cacheName: 'storage',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
|
maxAgeSeconds: 30 * 24 * 60 * 60,
|
|
|
|
|
// Only cache 10 requests.
|
|
|
|
|
maxEntries: 200,
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp(/.*\/(?:statics).*$/),
|
|
|
|
|
workbox.strategies.cacheFirst({
|
|
|
|
|
cacheName: 'statics',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
|
maxAgeSeconds: 10 * 24 * 60 * 60,
|
|
|
|
|
// Only cache 10 requests.
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp(/^http/),
|
|
|
|
|
workbox.strategies.networkFirst({
|
|
|
|
|
cacheName: 'all-stuff',
|
|
|
|
|
plugins: [
|
|
|
|
|
new workbox.expiration.Plugin({
|
|
|
|
|
maxAgeSeconds: 10 * 24 * 60 * 60,
|
|
|
|
|
// Only cache 10 requests.
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
2019-01-31 13:52:52 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
// workbox.core.setLogLevel(workbox.core.LOG_LEVELS.debug);
|
|
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp('http://localhost:8080/todos'),
|
|
|
|
|
function (args) {
|
|
|
|
|
return fetch(args.event.request)
|
|
|
|
|
.then(function (res) {
|
|
|
|
|
console.log('******* fetch: ', args.event)
|
|
|
|
|
var clonedRes = res.clone();
|
|
|
|
|
clearAllData('todos')
|
|
|
|
|
.then(function () {
|
|
|
|
|
return clonedRes.json();
|
|
|
|
|
})
|
|
|
|
|
.then(function (data) {
|
|
|
|
|
for (let key in data) {
|
|
|
|
|
writeData('todos', data[key])
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return res
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2019-01-31 13:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
|
|
|
|
|
console.log('***************** Entering in custom-service-worker.js:')
|
|
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-01-31 17:08:10 +01:00
|
|
|
// self.addEventListener('fetch', function (event) {
|
|
|
|
|
// console.log('[Service Worker] Fetching something ....', event);
|
|
|
|
|
// console.log('event.request.cache=', event.request.cache)
|
|
|
|
|
// if (event.request.cache === 'only-if-cached' && event.request.mode !== 'same-origin') {
|
|
|
|
|
// console.log('SAME ORIGIN!', event);
|
|
|
|
|
// return;
|
|
|
|
|
// }
|
2019-02-01 04:10:31 +01:00
|
|
|
// event.respondWith(caches.match(event.request));
|
2019-01-31 17:08:10 +01:00
|
|
|
// });
|
2019-01-31 13:52:52 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
self.addEventListener('sync', function (event) {
|
|
|
|
|
console.log('[Service Worker] Background syncing', event);
|
|
|
|
|
|
|
|
|
|
if (event.tag === 'sync-new-todos') {
|
|
|
|
|
console.log('[Service Worker] Syncing new Todos');
|
|
|
|
|
|
|
|
|
|
let authHeader = []
|
|
|
|
|
authHeader['content-type'] = 'application/json';
|
|
|
|
|
authHeader['accept-language'] = 'en';
|
|
|
|
|
// authHeader.append('x-auth', mytok)
|
|
|
|
|
|
|
|
|
|
event.waitUntil(
|
|
|
|
|
readAllData('sync_todos')
|
|
|
|
|
.then(function (data) {
|
|
|
|
|
for (var dt of data) {
|
|
|
|
|
// var postData = new FormData();
|
|
|
|
|
// postData.append('_id', dt._id);
|
|
|
|
|
// postData.append('title', dt.title);
|
|
|
|
|
// postData.append('location', dt.location);
|
|
|
|
|
// postData.append('rawLocationLat', dt.rawLocation.lat);
|
|
|
|
|
// postData.append('rawLocationLng', dt.rawLocation.lng);
|
|
|
|
|
// postData.append('file', dt.picture, dt._id + '.png');
|
|
|
|
|
|
|
|
|
|
console.log('Data to Send 6: ', JSON.stringify(dt))
|
|
|
|
|
|
|
|
|
|
// Update myTodo to the server
|
|
|
|
|
fetch('http://localhost:3000/todos', {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
// mode: 'no-cors',
|
|
|
|
|
mode: 'cors',
|
|
|
|
|
body: JSON.stringify(dt)
|
|
|
|
|
})
|
|
|
|
|
.then(function (resData) {
|
|
|
|
|
console.log('Sent data Todo:', resData);
|
|
|
|
|
if (resData.ok) {
|
|
|
|
|
deleteItemFromData('sync_todos', dt.id);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch(function (err) {
|
|
|
|
|
console.log('Error while sending data', err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// }
|
2018-10-12 16:42:54 +02:00
|
|
|
|