-
Notifications
You must be signed in to change notification settings - Fork 0
/
service-worker.js
42 lines (38 loc) · 1.13 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/3.6.1/workbox-sw.js');
if (workbox) {
workbox.setConfig({
debug: false
});
var defaultStrategy = workbox.strategies.networkFirst({
cacheName: "fallback",
plugins: [
new workbox.expiration.Plugin({
maxEntries: 128,
maxAgeSeconds: 7 * 24 * 60 * 60, // 1 week
purgeOnQuotaError: true, // Opt-in to automatic cleanup
}),
new workbox.cacheableResponse.Plugin({
statuses: [0, 200] // for opague requests
}),
],
});
workbox.routing.setDefaultHandler(
(args) => {
if (args.event.request.method === 'GET') {
return defaultStrategy.handle(args); // use default strategy
} else {
return null
}
}
);
workbox.routing.registerRoute(
new RegExp(/.*\.(?:js|css)/g),
workbox.strategies.networkFirst()
);
workbox.routing.registerRoute(
new RegExp(/.*\.(?:png|jpg|jpeg|svg|gif|webp)/g),
workbox.strategies.cacheFirst()
);
} else {
console.log(`No workbox on this browser 😬`);
}