hubl/src/service-worker.js

108 lines
2.4 KiB
JavaScript

importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.1.5/workbox-sw.js');
workbox.precaching.precacheAndRoute([]);
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,
}),
],
})
);