Skip to content

Instantly share code, notes, and snippets.

@Gummibeer
Created April 9, 2021 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gummibeer/99004a456c745e3582da4f5d730c668d to your computer and use it in GitHub Desktop.
Save Gummibeer/99004a456c745e3582da4f5d730c668d to your computer and use it in GitHub Desktop.
import {registerRoute} from 'workbox-routing';
import {StaleWhileRevalidate} from 'workbox-strategies';
import {CacheableResponsePlugin} from 'workbox-cacheable-response';
import {CacheFirst} from 'workbox-strategies';
import {ExpirationPlugin} from 'workbox-expiration';
self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});
registerRoute(
({request}) => request.destination === 'script',
new StaleWhileRevalidate({
cacheName: 'scripts',
}),
);
registerRoute(
({request}) => request.destination === 'style',
new StaleWhileRevalidate({
cacheName: 'styles',
}),
);
registerRoute(
({request}) => request.destination === 'image',
new CacheFirst({
cacheName: 'images',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200],
}),
new ExpirationPlugin({
maxEntries: 100,
maxAgeSeconds: 30 * 24 * 60 * 60, // 30 Days
}),
],
}),
);
registerRoute(
new RegExp('/*'),
new StaleWhileRevalidate({
cacheName: 'pwa-offline'
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment