-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
88 lines (76 loc) · 2.05 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.4.1/workbox-sw.js');
if (workbox) {
console.log(`Yay! Workbox is loaded 🎉`);
} else {
console.log(`Boo! Workbox didn't load 😬`);
}
//workbox.googleAnalytics.initialize();
workbox.routing.registerRoute(
//cache manifest file
/\.(?:json)$/,
workbox.strategies.staleWhileRevalidate({
cacheName: 'manifest-cache',
}),
);
workbox.routing.registerRoute(
//cache index file
/\.(?:html|md)$/,
workbox.strategies.staleWhileRevalidate({
cacheName: 'index-cache',
}),
);
workbox.routing.registerRoute(
/\.(?:js|css)$/,
workbox.strategies.staleWhileRevalidate({
cacheName: 'static-resources',
}),
);
workbox.routing.registerRoute(
// Cache image files
/.*\.(?:png|jpg|jpeg|svg|gif)/,
// Use the cache if it's available
workbox.strategies.cacheFirst({
// Use a custom cache name
cacheName: 'image-cache',
plugins: [
new workbox.expiration.Plugin({
// Cache only 10 images
maxEntries: 10,
// Cache for a maximum of three days
maxAgeSeconds: 3 * 24 * 60 * 60,
})
],
})
);
workbox.routing.registerRoute(
new RegExp('https://fonts.(?:googleapis|gstatic).com/(.*)'),
workbox.strategies.cacheFirst({
cacheName: 'google-fonts',
plugins: [
new workbox.expiration.Plugin({
maxEntries: 4,
}),
new workbox.cacheableResponse.Plugin({
statuses: [0, 200]
}),
],
}),
);
workbox.routing.setCatchHandler(({url, event, params}) => {
console.log("Unknown error, due to dev being lazy")
});
self.addEventListener('fetch', function(event) {
console.log('Fetch event for ', event.request.url);
event.respondWith(
caches.match(event.request).then(function(response) {
if (response) {
console.log('Found ', event.request.url, ' in cache');
return response;
}
console.log('Network request for ', event.request.url);
return fetch(event.request)
}).catch(function(error) {
return caches.match('index.html');
})
);
});