feature: sw cache all

This commit is contained in:
Jean-Baptiste Pasquier 2021-05-10 12:10:53 +02:00
parent b8dd2aca56
commit eb92447b73
3 changed files with 56 additions and 9 deletions

View File

@ -0,0 +1,21 @@
if ('serviceWorker' in navigator) {
navigator.serviceWorker.addEventListener('message', async (event) => {
if (event.data.meta === 'workbox-broadcast-update') {
const {
cacheName,
updatedURL
} = event.data.payload;
const cache = await caches.open(cacheName);
const updatedResponse = await cache.match(updatedURL);
try {
const updatedData = await updatedResponse.json();
if (sibStore && "setResource" in sibStore) {
sibStore.setResource(updatedURL, updatedData);
}
} catch (e) {
console.error(e);
}
}
});
}

View File

@ -9,7 +9,13 @@ addEventListener('message', (event) => {
}); });
workbox.routing.registerRoute( workbox.routing.registerRoute(
({url}) => url.origin === 'https://fonts.gstatic.com', ({
url
}) => [
'https://fonts.gstatic.com',
'https://fonts.googleapis.com',
'https://storage.googleapis.com'
].includes(url.origin),
new workbox.strategies.CacheFirst({ new workbox.strategies.CacheFirst({
cacheName: 'google-fonts-webfonts', cacheName: 'google-fonts-webfonts',
plugins: [ plugins: [
@ -25,21 +31,25 @@ workbox.routing.registerRoute(
); );
workbox.routing.registerRoute( workbox.routing.registerRoute(
({url}) => [ ({
url
}) => [
'https://cdn.jsdelivr.net', 'https://cdn.jsdelivr.net',
'https://unpkg.com', 'https://unpkg.com',
'https://cdn.skypack.dev', 'https://cdn.skypack.dev',
'https://jspm.dev', 'https://jspm.dev',
'https://fonts.googleapis.com', 'https://fonts.googleapis.com',
'https://cdn.startinblox.com' 'https://cdn.startinblox.com'
].includes(url.origin), ].includes(url.origin) || url.origin.test(/cdn/),
new workbox.strategies.StaleWhileRevalidate({ new workbox.strategies.StaleWhileRevalidate({
cacheName: 'cdn', cacheName: 'cdn',
}) })
); );
workbox.routing.registerRoute( workbox.routing.registerRoute(
({ request }) => request.destination === 'image', ({
request
}) => request.destination === 'image',
new workbox.strategies.CacheFirst({ new workbox.strategies.CacheFirst({
cacheName: 'images', cacheName: 'images',
plugins: [ plugins: [
@ -55,7 +65,9 @@ workbox.routing.registerRoute(
); );
workbox.routing.registerRoute( workbox.routing.registerRoute(
({ request }) => ({
request
}) =>
request.destination === 'style' || request.destination === 'style' ||
request.destination === 'script', request.destination === 'script',
new workbox.strategies.StaleWhileRevalidate({ new workbox.strategies.StaleWhileRevalidate({
@ -69,7 +81,9 @@ workbox.routing.registerRoute(
); );
workbox.routing.registerRoute( workbox.routing.registerRoute(
({ request }) => request.mode === 'navigate', ({
request
}) => request.mode === 'navigate',
new workbox.strategies.NetworkFirst({ new workbox.strategies.NetworkFirst({
cacheName: 'pages', cacheName: 'pages',
plugins: [ plugins: [
@ -79,3 +93,15 @@ workbox.routing.registerRoute(
], ],
}), }),
); );
workbox.routing.registerRoute(() => true,
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'apis',
plugins: [
new workbox.broadcastUpdate.BroadcastUpdatePlugin(),
new workbox.expiration.ExpirationPlugin({
maxAgeSeconds: 60 * 60 * 24 * 30,
}),
],
})
);