-
Notifications
You must be signed in to change notification settings - Fork 3
/
service-worker-src.js
54 lines (49 loc) · 1.45 KB
/
service-worker-src.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
43
44
45
46
47
48
49
50
51
52
53
54
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.6.3/workbox-sw.js');
workbox.skipWaiting();
workbox.clientsClaim();
// cache name
workbox.core.setCacheNameDetails({
prefix: 'My-awesome-cache',
precache: 'precache',
runtime: 'runtime',
});
// runtime cache
// 1. stylesheet
workbox.routing.registerRoute(
new RegExp('\.css$'),
workbox.strategies.cacheFirst({
cacheName: 'My-awesome-cache-Stylesheets',
plugins: [
new workbox.expiration.Plugin({
maxAgeSeconds: 60 * 60 * 24 * 7, // cache for one week
maxEntries: 20, // only cache 20 request
purgeOnQuotaError: true
})
]
})
);
// 2. images
workbox.routing.registerRoute(
new RegExp('\.(png|svg|jpg|jpeg)$'),
workbox.strategies.cacheFirst({
cacheName: 'My-awesome-cache-Images',
plugins: [
new workbox.expiration.Plugin({
maxAgeSeconds: 60 * 60 * 24 * 7,
maxEntries: 50,
purgeOnQuotaError: true
})
]
})
);
// 3. cache news articles result
workbox.routing.registerRoute(
new RegExp('https://newsapi.org/v2/top-headlines'),
workbox.strategies.staleWhileRevalidate({
cacheName: 'My-awesome-cache-news-headline',
cacheExpiration: {
maxAgeSeconds: 60 * 30 //cache the news content for 30mn
}
})
);
workbox.precaching.precacheAndRoute([]);