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-12 19:09:43 +01:00
|
|
|
console.log(' [ VER-0.0.21 ] _---------________------ PAO: this is my custom service worker');
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js'); //++Todo: Replace with local workbox.js
|
2019-02-02 20:13:06 +01:00
|
|
|
importScripts('../statics/js/idb.js');
|
|
|
|
|
importScripts('../statics/js/storage.js');
|
|
|
|
|
|
2019-02-08 17:10:25 +01:00
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
let port = 3000;
|
|
|
|
|
if (self.location.hostname === 'test.freeplanet.app') {
|
|
|
|
|
port = 3001;
|
2019-02-13 01:51:59 +01:00
|
|
|
}
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('SW-06 1');
|
2019-02-02 20:13:06 +01:00
|
|
|
const cfgenv = {
|
2019-02-13 01:51:59 +01:00
|
|
|
serverweb: self.location.protocol + "//" + self.location.hostname + ':' + port,
|
2019-02-02 20:13:06 +01:00
|
|
|
dbname: 'mydb3',
|
|
|
|
|
dbversion: 11,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function writeData(table, data) {
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('writeData', table, data);
|
2019-02-02 20:13:06 +01:00
|
|
|
await idbKeyval.setdata(table, data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function readAllData(table) {
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('readAllData', table);
|
2019-02-02 20:13:06 +01:00
|
|
|
return await idbKeyval.getalldata(table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function clearAllData(table) {
|
2019-02-12 19:09:43 +01:00
|
|
|
// console.log('clearAllData', table);
|
2019-02-02 20:13:06 +01:00
|
|
|
await idbKeyval.clearalldata(table)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function deleteItemFromData(table, id) {
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('deleteItemFromData', table, 'ID:', id);
|
2019-02-02 20:13:06 +01:00
|
|
|
|
|
|
|
|
await idbKeyval.deletedata(table, id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// self.addEventListener('activate', function(event) {
|
|
|
|
|
// event.waitUntil(
|
|
|
|
|
// // createDB()
|
|
|
|
|
// );
|
|
|
|
|
// });
|
2019-01-31 13:52:52 +01:00
|
|
|
|
|
|
|
|
if (!workbox) {
|
2019-02-01 04:10:31 +01:00
|
|
|
let workbox = new self.WorkboxSW();
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('SW-06 3');
|
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(
|
2019-02-02 20:13:06 +01:00
|
|
|
new RegExp(/\.(?:png|gif|jpg|jpeg|svg)$/),
|
2019-02-01 04:10:31 +01:00
|
|
|
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-02 20:13:06 +01:00
|
|
|
|
|
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp(cfgenv.serverweb + '/todos/'),
|
|
|
|
|
function (args) {
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('registerRoute!')
|
2019-02-11 02:58:53 +01:00
|
|
|
// console.log('DATABODY:', args.event.request.body)
|
|
|
|
|
let myres = null
|
2019-02-12 12:06:01 +01:00
|
|
|
// return fetch(args.event.request, args.event.headers)
|
2019-02-02 20:13:06 +01:00
|
|
|
return fetch(args.event.request, args.event.headers)
|
|
|
|
|
.then(function (res) {
|
2019-02-11 02:58:53 +01:00
|
|
|
myres = res
|
2019-02-12 19:09:43 +01:00
|
|
|
// console.log('1° ******* [[[ SERVICE-WORKER ]]] registerRoute fetch: -> ', args.event.request, res)
|
2019-02-04 03:09:15 +01:00
|
|
|
// LOAD FROM SERVER , AND SAVE INTO INDEXEDDB
|
2019-02-12 19:09:43 +01:00
|
|
|
// console.log('res.status', res.status)
|
2019-02-08 17:10:25 +01:00
|
|
|
if (res.status === 200) {
|
2019-02-09 18:04:49 +01:00
|
|
|
const clonedRes = res.clone();
|
2019-02-11 02:58:53 +01:00
|
|
|
clearAllData('todos')
|
|
|
|
|
return clonedRes
|
2019-02-08 17:10:25 +01:00
|
|
|
}
|
2019-02-02 20:13:06 +01:00
|
|
|
})
|
2019-02-11 02:58:53 +01:00
|
|
|
.then((clonedRes) => {
|
2019-02-12 12:06:01 +01:00
|
|
|
if (clonedRes !== undefined)
|
2019-02-11 02:58:53 +01:00
|
|
|
return clonedRes.json();
|
|
|
|
|
return null
|
|
|
|
|
})
|
|
|
|
|
.then(data => {
|
|
|
|
|
if (data) {
|
|
|
|
|
if (data.todos) {
|
|
|
|
|
console.log('Records TODOS Received from Server [', data.todos.length, 'record]', data.todos)
|
|
|
|
|
for (let key in data.todos) {
|
|
|
|
|
writeData('todos', data.todos[key])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.then(() => {
|
|
|
|
|
return myres
|
|
|
|
|
})
|
|
|
|
|
.catch(err => {
|
|
|
|
|
console.log('ERROR registerRoute FETCH:', err)
|
|
|
|
|
return myres
|
|
|
|
|
})
|
|
|
|
|
})
|
2019-02-01 04:10:31 +01:00
|
|
|
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
workbox.routing.registerRoute(function (routeData) {
|
|
|
|
|
return (routeData.event.request.headers.get('accept').includes('text/html'));
|
|
|
|
|
}, function (args) {
|
|
|
|
|
return caches.match(args.event.request)
|
|
|
|
|
.then(function (response) {
|
|
|
|
|
if (response) {
|
|
|
|
|
return response;
|
|
|
|
|
} else {
|
|
|
|
|
return fetch(args.event.request)
|
|
|
|
|
.then(function (res) {
|
|
|
|
|
return caches.open('dynamic')
|
|
|
|
|
.then(function (cache) {
|
|
|
|
|
cache.put(args.event.request.url, res.clone());
|
|
|
|
|
return res;
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
.catch(function (err) {
|
|
|
|
|
return caches.match('/offline')
|
|
|
|
|
.then(function (res) {
|
|
|
|
|
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.
|
|
|
|
|
}),
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
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
|
|
|
|
2019-02-04 16:47:15 +01:00
|
|
|
workbox.routing.registerRoute(
|
|
|
|
|
new RegExp('/admin/'),
|
|
|
|
|
workbox.strategies.networkOnly()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
2019-01-31 13:52:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ('serviceWorker' in navigator) {
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
// console.log('***************** Entering in custom-service-worker.js:')
|
2019-01-31 13:52:52 +01:00
|
|
|
|
2019-02-01 04:10:31 +01:00
|
|
|
}
|
|
|
|
|
|
2019-02-04 16:47:15 +01:00
|
|
|
|
2019-02-11 02:58:53 +01:00
|
|
|
// self.addEventListener('fetch', (event) => {
|
|
|
|
|
// if (event.request.url === '/') {
|
|
|
|
|
// const staleWhileRevalidate = new workbox.strategies.StaleWhileRevalidate();
|
|
|
|
|
// event.respondWith(staleWhileRevalidate.handle({ event }));
|
|
|
|
|
// }
|
|
|
|
|
// });
|
|
|
|
|
|
2019-02-02 20:13:06 +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;
|
|
|
|
|
// }
|
|
|
|
|
// event.respondWith(caches.match(event.request));
|
|
|
|
|
// });
|
2019-02-12 12:06:01 +01:00
|
|
|
//
|
2019-02-02 20:13:06 +01:00
|
|
|
|
|
|
|
|
// const syncStore = {}
|
|
|
|
|
// self.addEventListener('message', event => {
|
|
|
|
|
// if (event.data.type === 'sync') {
|
|
|
|
|
// // get a unique id to save the data
|
|
|
|
|
// const id = uuid()
|
|
|
|
|
// syncStore[id] = event.data
|
|
|
|
|
// // register a sync and pass the id as tag for it to get the data
|
|
|
|
|
// self.registration.sync.register(id)
|
|
|
|
|
// }
|
|
|
|
|
// console.log(event.data)
|
|
|
|
|
// })
|
|
|
|
|
|
2019-02-12 12:06:01 +01:00
|
|
|
// addEventListener('fetch', event => {
|
|
|
|
|
// // Prevent the default, and handle the request ourselves.
|
|
|
|
|
// event.respondWith(async function() {
|
|
|
|
|
// // Try to get the response from a cache.
|
|
|
|
|
// const cachedResponse = await caches.match(event.request);
|
|
|
|
|
// // Return it if we found one.
|
|
|
|
|
// if (cachedResponse && (event.request.cache !== 'no-cache'))
|
|
|
|
|
// return cachedResponse;
|
|
|
|
|
//
|
|
|
|
|
// // If we didn't find a match in the cache, use the network.
|
|
|
|
|
// return fetch(event.request);
|
|
|
|
|
// }());
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
// self.addEventListener('fetch', function (event) {
|
|
|
|
|
// event.respondWith(
|
|
|
|
|
// caches.match(event.request).then(function (response) {
|
|
|
|
|
// return response ||
|
|
|
|
|
// fetch(event.request, event.headers)
|
|
|
|
|
// .catch(err => {
|
|
|
|
|
// console.log('_______________________ ERRORE FETCH SW: ', event.request, err)
|
|
|
|
|
// writeData('config', { _id: 2, stateconn: 'offline' })
|
|
|
|
|
// return caches.match(event.request);
|
|
|
|
|
// })
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// self.addEventListener('fetch', function (event) {
|
|
|
|
|
// event.respondWith(
|
|
|
|
|
// fetch(event.request, event.headers)
|
|
|
|
|
// .catch(err => {
|
|
|
|
|
// console.log('_______________________ ERRORE FETCH SW: ', event.request, err)
|
|
|
|
|
// writeData('config', {_id: 2, stateconn: 'offline'})
|
|
|
|
|
// return caches.match(event.request);
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
2019-02-12 19:09:43 +01:00
|
|
|
// self.addEventListener('sync', function (event) {
|
|
|
|
|
// console.log('[Service Worker V5] Background syncing', event.tag);
|
|
|
|
|
//
|
|
|
|
|
// let mystrparam = event.tag
|
|
|
|
|
// let multiparams = mystrparam.split('|')
|
|
|
|
|
// if (multiparams) {
|
|
|
|
|
// if (multiparams.length > 3) {
|
|
|
|
|
// let cmd = multiparams[0]
|
|
|
|
|
// let table = multiparams[1]
|
|
|
|
|
// let method = multiparams[2]
|
|
|
|
|
// let token = multiparams[3]
|
|
|
|
|
// // let lang = multiparams[3]
|
|
|
|
|
//
|
|
|
|
|
// if (cmd === 'sync-todos') {
|
|
|
|
|
// console.log('[Service Worker] Syncing', cmd, table, method);
|
|
|
|
|
//
|
|
|
|
|
// const headers = new Headers()
|
|
|
|
|
// headers.append('content-Type', 'application/json')
|
|
|
|
|
// headers.append('Accept', 'application/json')
|
|
|
|
|
// headers.append('x-auth', token)
|
|
|
|
|
//
|
|
|
|
|
//
|
|
|
|
|
// // console.log('A1) INIZIO.............................................................');
|
|
|
|
|
//
|
|
|
|
|
// event.waitUntil(
|
|
|
|
|
// readAllData(table)
|
|
|
|
|
// .then(function (alldata) {
|
|
|
|
|
// const myrecs = [...alldata]
|
|
|
|
|
// console.log('----------------------- LEGGO QUALCOSA DAL WAITUNTIL ')
|
|
|
|
|
// let errorfromserver = false
|
|
|
|
|
// if (myrecs) {
|
|
|
|
|
// for (let rec of myrecs) {
|
|
|
|
|
// //console.log('syncing', table, '', rec.descr)
|
|
|
|
|
// let link = cfgenv.serverweb + '/todos'
|
|
|
|
|
//
|
|
|
|
|
// if (method !== 'POST')
|
|
|
|
|
// link += '/' + rec._id
|
|
|
|
|
//
|
|
|
|
|
// console.log('++++++++++++++++++ SYNCING !!!! ', rec.descr, table, 'FETCH: ', method, link, 'data:')
|
|
|
|
|
//
|
|
|
|
|
// // console.log('DATATOSAVE:', JSON.stringify(rec))
|
|
|
|
|
//
|
|
|
|
|
// // Insert/Delete/Update table to the server
|
|
|
|
|
// fetch(link, {
|
|
|
|
|
// method: method,
|
|
|
|
|
// headers: headers,
|
|
|
|
|
// cache: 'no-cache',
|
|
|
|
|
// mode: 'cors', // 'no-cors',
|
|
|
|
|
// body: JSON.stringify(rec)
|
|
|
|
|
// })
|
|
|
|
|
// .then(() => {
|
|
|
|
|
// deleteItemFromData(table, rec._id)
|
|
|
|
|
// })
|
|
|
|
|
// .then(() => {
|
|
|
|
|
// deleteItemFromData('swmsg', mystrparam)
|
|
|
|
|
// })
|
|
|
|
|
// .catch(function (err) {
|
|
|
|
|
// console.log('!!!!!!!!!!!!!!! Error while sending data', err, err.message);
|
|
|
|
|
// if (err.message === 'Failed to fetch') {
|
|
|
|
|
// errorfromserver = true
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
// return errorfromserver
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
// .then((errorfromserver) => {
|
|
|
|
|
// const mystate = !errorfromserver ? 'online' : 'offline'
|
|
|
|
|
// writeData('config', { _id: 2, stateconn: mystate })
|
|
|
|
|
// })
|
|
|
|
|
// );
|
|
|
|
|
// // console.log('A2) ?????????????????????????? ESCO DAL LOOP !!!!!!!!! err=')
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// })
|
|
|
|
|
// ;
|
2019-02-02 20:13:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
// send message to serviceWorker
|
|
|
|
|
function sync (url, options) {
|
|
|
|
|
navigator.serviceWorker.controller.postMessage({type: 'sync', url, options})
|
|
|
|
|
}
|
2019-02-01 04:10:31 +01:00
|
|
|
|
2018-10-12 16:42:54 +02:00
|
|
|
|
2019-02-02 20:13:06 +01:00
|
|
|
const syncStore = {}
|
|
|
|
|
self.addEventListener('message', event => {
|
|
|
|
|
if(event.data.type === 'sync') {
|
|
|
|
|
// get a unique id to save the data
|
|
|
|
|
const id = uuid()
|
|
|
|
|
syncStore[id] = event.data
|
|
|
|
|
// register a sync and pass the id as tag for it to get the data
|
|
|
|
|
self.registration.sync.register(id)
|
|
|
|
|
}
|
|
|
|
|
console.log(event.data)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.addEventListener('sync', event => {
|
|
|
|
|
// get the data by tag
|
|
|
|
|
const {url, options} = syncStore[event.tag]
|
|
|
|
|
event.waitUntil(fetch(url, options))
|
|
|
|
|
})
|
|
|
|
|
*/
|
2019-02-04 16:47:15 +01:00
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
self.addEventListener('notificationclick', function (event) {
|
2019-02-04 16:47:15 +01:00
|
|
|
var notification = event.notification;
|
|
|
|
|
var action = event.action;
|
|
|
|
|
|
|
|
|
|
console.log(notification);
|
|
|
|
|
|
|
|
|
|
if (action === 'confirm') {
|
|
|
|
|
console.log('Confirm was chosen');
|
|
|
|
|
notification.close();
|
|
|
|
|
} else {
|
|
|
|
|
console.log(action);
|
|
|
|
|
event.waitUntil(
|
|
|
|
|
clients.matchAll()
|
2019-02-09 18:04:49 +01:00
|
|
|
.then(function (clis) {
|
|
|
|
|
var client = clis.find(function (c) {
|
2019-02-04 16:47:15 +01:00
|
|
|
return c.visibilityState === 'visible';
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (client !== undefined) {
|
|
|
|
|
client.navigate(notification.data.url);
|
|
|
|
|
client.focus();
|
|
|
|
|
} else {
|
|
|
|
|
clients.openWindow(notification.data.url);
|
|
|
|
|
}
|
|
|
|
|
notification.close();
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
self.addEventListener('notificationclose', function (event) {
|
2019-02-04 16:47:15 +01:00
|
|
|
console.log('Notification was closed', event);
|
|
|
|
|
});
|
|
|
|
|
|
2019-02-09 18:04:49 +01:00
|
|
|
self.addEventListener('push', function (event) {
|
2019-02-04 16:47:15 +01:00
|
|
|
console.log('Push Notification received', event);
|
|
|
|
|
|
2019-02-13 18:48:30 +01:00
|
|
|
var data = { title: 'New!', content: 'Something new happened!', url: '/' };
|
2019-02-04 16:47:15 +01:00
|
|
|
|
|
|
|
|
if (event.data) {
|
|
|
|
|
data = JSON.parse(event.data.text());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var options = {
|
|
|
|
|
body: data.content,
|
2019-02-05 03:39:56 +01:00
|
|
|
icon: '/statics/icons/android-chrome-192x192.png',
|
|
|
|
|
badge: '/statics/icons/android-chrome-192x192.png',
|
2019-02-04 16:47:15 +01:00
|
|
|
data: {
|
2019-02-13 18:48:30 +01:00
|
|
|
url: data.url
|
2019-02-04 16:47:15 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
event.waitUntil(
|
|
|
|
|
self.registration.showNotification(data.title, options)
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|