-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathservice-worker.js
42 lines (36 loc) · 1.15 KB
/
service-worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.4/workbox-sw.js');
if (workbox) {
console.log(`Yay! Workbox is loaded 🎉Service Worker is working!`);
} else {
console.log(`Boo! Workbox didn't load 😬Service Worker won't work properly...`);
}
const { registerRoute } = workbox.routing;
const { CacheFirst, StaleWhileRevalidate } = workbox.strategies;
const { ExpirationPlugin } = workbox.expiration;
const { precacheAndRoute, matchPrecache } = workbox.precaching;
precacheAndRoute(self.__WB_MANIFEST);
registerRoute(
/\.css$/,
// Use cache but update in the background.
new StaleWhileRevalidate({
// Use a custom cache name.
cacheName: 'css-cache',
})
);
registerRoute(
/\.(?:png|jpg|jpeg|svg|gif|woff2?|ttf)$/,
// Use the cache if it's available.
new CacheFirst({
cacheName: 'image-cache',
plugins: [
new ExpirationPlugin({
// Cache only a few images.
maxEntries: 100,
// Cache for a maximum of a week.
maxAgeSeconds: 7 * 24 * 60 * 60,
}),
],
})
);
registerRoute(/\.js$/, new StaleWhileRevalidate());
registerRoute(/(^\/$|index.html)/, new StaleWhileRevalidate());