Update Service Worker files

This commit is contained in:
Kevin Kandlbinder 2021-05-19 17:30:48 +02:00
parent ce7586bf13
commit de44b2c0a8

View file

@ -9,9 +9,19 @@
import { clientsClaim } from 'workbox-core'; import { clientsClaim } from 'workbox-core';
import { ExpirationPlugin } from 'workbox-expiration'; import { ExpirationPlugin } from 'workbox-expiration';
import {CacheableResponsePlugin} from 'workbox-cacheable-response';
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching'; import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing'; import { registerRoute } from 'workbox-routing';
import { StaleWhileRevalidate } from 'workbox-strategies'; import { StaleWhileRevalidate, CacheFirst } from 'workbox-strategies';
import {setCacheNameDetails} from 'workbox-core';
import {BackgroundSyncPlugin} from 'workbox-background-sync';
setCacheNameDetails({
prefix: 'kevins-toolbox',
suffix: 'v1',
precache: 'precache',
runtime: 'runtime'
});
clientsClaim(); clientsClaim();
@ -45,21 +55,52 @@ registerRoute(
}, },
createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html') createHandlerBoundToURL(process.env.PUBLIC_URL + '/index.html')
); );
// An example runtime caching route for requests that aren't handled by the
// precache, in this case same-origin .png requests like those from in public/
registerRoute( registerRoute(
// Add in any other file extensions or routing criteria as needed. ({request}) => request.destination === 'image',
({ url }) => url.origin === self.location.origin && url.pathname.endsWith('.png'), // Customize this strategy as needed, e.g., by changing to CacheFirst. new CacheFirst({
new StaleWhileRevalidate({ cacheName: 'images',
cacheName: 'images', plugins: [
plugins: [ new CacheableResponsePlugin({
// Ensure that once this runtime cache reaches a maximum size the statuses: [0, 200],
// least-recently used images are removed. }),
new ExpirationPlugin({ maxEntries: 50 }), new ExpirationPlugin({
], maxEntries: 60,
}) maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
); }),
],
}),
);
registerRoute(
({ url }) => url.origin === self.location.origin && url.pathname.endsWith('translation.json'),
new StaleWhileRevalidate({
cacheName: 'translations',
plugins: [
new ExpirationPlugin({ maxEntries: 5 }),
],
})
);
registerRoute(
({url}) => url.origin === 'https://fonts.googleapis.com' ||
url.origin === 'https://fonts.gstatic.com',
new StaleWhileRevalidate({
cacheName: 'google-fonts',
plugins: [
new ExpirationPlugin({maxEntries: 20}),
],
}),
);
registerRoute(
({request}) => request.destination === 'script' ||
request.destination === 'style',
new StaleWhileRevalidate({
plugins: [
new BackgroundSyncPlugin()
]
})
);
// This allows the web app to trigger skipWaiting via // This allows the web app to trigger skipWaiting via
// registration.waiting.postMessage({type: 'SKIP_WAITING'}) // registration.waiting.postMessage({type: 'SKIP_WAITING'})