82 lines
2.0 KiB
JavaScript
82 lines
2.0 KiB
JavaScript
|
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.1.1/workbox-sw.js');
|
||
|
|
||
|
workbox.precaching.precacheAndRoute([]);
|
||
|
|
||
|
addEventListener('message', (event) => {
|
||
|
if (event.data && event.data.type === 'SKIP_WAITING') {
|
||
|
skipWaiting();
|
||
|
}
|
||
|
});
|
||
|
|
||
|
workbox.routing.registerRoute(
|
||
|
({url}) => url.origin === 'https://fonts.gstatic.com',
|
||
|
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),
|
||
|
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],
|
||
|
}),
|
||
|
],
|
||
|
}),
|
||
|
);
|