2019-10-16 20:25:10 +02:00
|
|
|
/* eslint-disable no-console */
|
2019-10-16 23:27:21 +02:00
|
|
|
/* eslint-disable no-undef */
|
2019-10-16 20:25:10 +02:00
|
|
|
|
2021-07-25 15:27:15 +02:00
|
|
|
importScripts( '/workbox-v6.1.5/workbox-sw.js')
|
|
|
|
workbox.setConfig({modulePathPrefix: '/workbox-v6.1.5'})
|
|
|
|
|
|
|
|
import { precacheAndRoute } from 'workbox-precaching'
|
|
|
|
precacheAndRoute(self.__WB_MANIFEST)
|
|
|
|
|
2019-10-16 23:27:21 +02:00
|
|
|
// Cache assets
|
|
|
|
workbox.routing.registerRoute(
|
2020-09-05 22:35:52 +02:00
|
|
|
// This regexp matches all files in precache-manifest
|
2021-01-30 21:45:54 +01:00
|
|
|
new RegExp('.+\\.(css|json|js|svg|woff2|png|html|txt|wav)$'),
|
2020-09-05 22:35:52 +02:00
|
|
|
new workbox.strategies.StaleWhileRevalidate(),
|
|
|
|
)
|
2019-10-16 20:25:10 +02:00
|
|
|
|
2019-10-16 23:27:21 +02:00
|
|
|
// Always send api reqeusts through the network
|
|
|
|
workbox.routing.registerRoute(
|
2020-05-10 18:10:29 +02:00
|
|
|
new RegExp('api\\/v1\\/.*$'),
|
2020-09-05 22:35:52 +02:00
|
|
|
new workbox.strategies.NetworkOnly(),
|
|
|
|
)
|
2019-10-16 23:27:21 +02:00
|
|
|
|
2019-12-18 22:30:20 +01:00
|
|
|
// This code listens for the user's confirmation to update the app.
|
|
|
|
self.addEventListener('message', (e) => {
|
|
|
|
if (!e.data) {
|
2020-09-05 22:35:52 +02:00
|
|
|
return
|
2019-12-18 22:30:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (e.data) {
|
|
|
|
case 'skipWaiting':
|
2020-09-05 22:35:52 +02:00
|
|
|
self.skipWaiting()
|
|
|
|
break
|
2019-12-18 22:30:20 +01:00
|
|
|
default:
|
|
|
|
// NOOP
|
2020-09-05 22:35:52 +02:00
|
|
|
break
|
2019-12-18 22:30:20 +01:00
|
|
|
}
|
2020-09-05 22:35:52 +02:00
|
|
|
})
|
2019-12-18 22:30:20 +01:00
|
|
|
|
2020-02-08 18:28:17 +01:00
|
|
|
const getBearerToken = async () => {
|
|
|
|
// we can't get a client that sent the current request, therefore we need
|
|
|
|
// to ask any controlled page for auth token
|
2020-09-05 22:35:52 +02:00
|
|
|
const allClients = await self.clients.matchAll()
|
|
|
|
const client = allClients.filter(client => client.type === 'window')[0]
|
2020-02-08 18:28:17 +01:00
|
|
|
|
|
|
|
// if there is no page in scope, we can't get any token
|
|
|
|
// and we indicate it with null value
|
2020-09-05 22:35:52 +02:00
|
|
|
if (!client) {
|
|
|
|
return null
|
2020-02-08 18:28:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// to communicate with a page we will use MessageChannels
|
|
|
|
// they expose pipe-like interface, where a receiver of
|
|
|
|
// a message uses one end of a port for messaging and
|
|
|
|
// we use the other end for listening
|
2020-09-05 22:35:52 +02:00
|
|
|
const channel = new MessageChannel()
|
2020-02-08 18:28:17 +01:00
|
|
|
|
|
|
|
client.postMessage({
|
2020-09-05 22:35:52 +02:00
|
|
|
'action': 'getBearerToken',
|
|
|
|
}, [channel.port1])
|
2020-02-08 18:28:17 +01:00
|
|
|
|
|
|
|
// ports support only onmessage callback which
|
|
|
|
// is cumbersome to use, so we wrap it with Promise
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
channel.port2.onmessage = event => {
|
|
|
|
if (event.data.error) {
|
2020-09-05 22:35:52 +02:00
|
|
|
console.error('Port error', event.error)
|
|
|
|
reject(event.data.error)
|
2020-02-08 18:28:17 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
resolve(event.data.authToken)
|
2020-02-08 18:28:17 +01:00
|
|
|
}
|
2020-09-05 22:35:52 +02:00
|
|
|
})
|
2020-02-08 18:28:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notification action
|
2020-09-05 22:35:52 +02:00
|
|
|
self.addEventListener('notificationclick', function (event) {
|
2020-04-17 12:19:53 +02:00
|
|
|
const taskId = event.notification.data.taskId
|
2020-02-08 18:28:17 +01:00
|
|
|
event.notification.close()
|
|
|
|
|
|
|
|
switch (event.action) {
|
|
|
|
case 'mark-as-done':
|
|
|
|
// FIXME: Ugly as hell, but no other way of doing this, since we can't use modules
|
2021-01-30 18:33:21 +01:00
|
|
|
// in service workers for now.
|
2020-02-08 18:28:17 +01:00
|
|
|
fetch('/config.json')
|
|
|
|
.then(r => r.json())
|
|
|
|
.then(config => {
|
|
|
|
|
|
|
|
getBearerToken()
|
|
|
|
.then(token => {
|
2020-04-17 12:19:53 +02:00
|
|
|
fetch(`${config.VIKUNJA_API_BASE_URL}tasks/${taskId}`, {
|
2020-02-08 18:28:17 +01:00
|
|
|
method: 'post',
|
|
|
|
headers: {
|
|
|
|
'Accept': 'application/json',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': `Bearer ${token}`,
|
|
|
|
},
|
2020-09-05 22:35:52 +02:00
|
|
|
body: JSON.stringify({id: taskId, done: true}),
|
2020-02-08 18:28:17 +01:00
|
|
|
})
|
2020-09-05 22:35:52 +02:00
|
|
|
.then(r => r.json())
|
|
|
|
.then(r => {
|
|
|
|
console.debug('Task marked as done from notification', r)
|
|
|
|
})
|
|
|
|
.catch(e => {
|
|
|
|
console.debug('Error marking task as done from notification', e)
|
|
|
|
})
|
2020-02-08 18:28:17 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
break
|
|
|
|
case 'show-task':
|
2020-04-17 12:19:53 +02:00
|
|
|
clients.openWindow(`/tasks/${taskId}`)
|
2020-02-08 18:28:17 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-09-05 22:35:52 +02:00
|
|
|
workbox.core.clientsClaim()
|
2019-12-18 22:30:20 +01:00
|
|
|
// The precaching code provided by Workbox.
|
2020-09-05 22:35:52 +02:00
|
|
|
self.__precacheManifest = [].concat(self.__precacheManifest || [])
|
|
|
|
workbox.precaching.precacheAndRoute(self.__precacheManifest, {})
|