-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
49 lines (45 loc) · 1.24 KB
/
sw.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
importScripts('https://storage.googleapis.com/workbox-cdn/releases/6.2.0/workbox-sw.js')
// workbox.setConfig({ debug: true })
const {
routing: { registerRoute, setCatchHandler },
strategies: { CacheFirst, NetworkFirst, StaleWhileRevalidate },
cacheableResponse: { CacheableResponse, CacheableResponsePlugin },
expiration: { ExpirationPlugin, CacheExpiration },
precaching: { matchPrecache, precacheAndRoute }
} = workbox
// Cache html, script.js
registerRoute(
({ request, url }) =>
request.mode === 'navigate' ||
url.pathname === '/josaa-cutoffs/script.js' ||
url.pathname === '/josaa-cutoffs/styles.css' ||
url.pathname === '/josaa-cutoffs/data.db.gz',
new StaleWhileRevalidate({
cacheName: 'stale-while-revalidate',
plugins: [
new CacheableResponsePlugin({
statuses: [200]
})
]
})
)
// Cache cdns and the rest
registerRoute(
({ request }) =>
request.destination === 'script' ||
request.destination === 'style' ||
request.destination === 'manifest' ||
request.destination === 'worker',
new CacheFirst({
cacheName: 'static-assets',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxEntries: 32,
maxAgeSeconds: 24 * 60 * 60 * 7 // 1 week
})
]
})
)