hubl/src/service-worker.js

128 lines
3.4 KiB
JavaScript

importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.1.5/workbox-sw.js');
workbox.precaching.precacheAndRoute([]);
addEventListener('push', function (event) {
// Retrieve the textual payload from event.data (a PushMessageData object).
// Other formats are supported (ArrayBuffer, Blob, JSON), check out the documentation
// on https://developer.mozilla.org/en-US/docs/Web/API/PushMessageData.
const eventInfo = event.data.text();
const data = JSON.parse(eventInfo);
const head = data.head || 'New Notification 🕺🕺';
const body = data.body || 'This is default content. Your notification didn\'t have one 🙄🙄';
console.log('hello from sw.js: push');
// Keep the service worker alive until the notification is created.
event.waitUntil(
self.registration.showNotification(head, {
body: body,
icon: 'https://i.imgur.com/MZM3K5w.png'
})
);
});
addEventListener('message', (event) => {
if (event.data && event.data.type === 'SKIP_WAITING') {
skipWaiting();
}
});
workbox.routing.registerRoute(
({
url
}) => [
'https://fonts.gstatic.com',
'https://fonts.googleapis.com',
'https://storage.googleapis.com'
].includes(url.origin),
new workbox.strategies.CacheFirst({
cacheName: 'google-fonts-webfonts',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200],
}),
new workbox.expiration.ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 365,
maxEntries: 30,
}),
],
})
);
// workbox.routing.registerRoute(
// ({
// url
// }) => [
// 'https://cdn.jsdelivr.net',
// 'https://unpkg.com',
// 'https://cdn.skypack.dev',
// 'https://jspm.dev',
// 'https://fonts.googleapis.com',
// 'https://cdn.startinblox.com'
// ].includes(url.origin) || /cdn/.test(url.origin),
// new workbox.strategies.StaleWhileRevalidate({
// cacheName: 'cdn',
// })
// );
workbox.routing.registerRoute(
({
request
}) => request.destination === 'image',
new workbox.strategies.CacheFirst({
cacheName: 'images',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [200],
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 300,
maxAgeSeconds: 60 * 60 * 24 * 30,
}),
],
}),
);
// workbox.routing.registerRoute(
// ({
// request
// }) =>
// request.destination === 'style' ||
// request.destination === 'script',
// new workbox.strategies.StaleWhileRevalidate({
// cacheName: 'assets',
// plugins: [
// new workbox.cacheableResponse.CacheableResponsePlugin({
// statuses: [200],
// }),
// ],
// }),
// );
// workbox.routing.registerRoute(
// ({
// request
// }) => request.mode === 'navigate',
// new workbox.strategies.NetworkFirst({
// cacheName: 'pages',
// plugins: [
// new workbox.cacheableResponse.CacheableResponsePlugin({
// statuses: [200],
// }),
// ],
// }),
// );
// workbox.routing.registerRoute(() => true,
// new workbox.strategies.StaleWhileRevalidate({
// cacheName: 'apis',
// plugins: [
// new workbox.broadcastUpdate.BroadcastUpdatePlugin(),
// new workbox.expiration.ExpirationPlugin({
// maxAgeSeconds: 60 * 60 * 24 * 30,
// }),
// ],
// })
// );