-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsw.js
84 lines (75 loc) · 2.93 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
if (typeof importScripts === 'function') {
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js');
/* global workbox */
if (workbox) {
console.log('Workbox is loaded');
/* injection point for manifest files. */
workbox.precaching.precacheAndRoute(self.__WB_MANIFEST);
// control the uncontrolled client side
workbox.core.clientsClaim();
// transit the status from waiting to activate
workbox.core.skipWaiting();
workbox.precaching.cleanupOutdatedCaches();
/* custom cache rules */
//For images
workbox.routing.registerRoute(
new RegExp('\.(?:png|gif|jpg|jpeg|webp|svg)$'),
new workbox.strategies.CacheFirst({
cacheName: 'image-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 12 * 60 * 60
})
]
}), 'GET');
//For JS/CSS
/*
Resources are requested from both the cache and the network in parallel.
The strategy will respond with the cached version if available, otherwise wait for the network response.
The cache is updated with the network response with each successful request
*/
workbox.routing.registerRoute(
new RegExp('\.(?:js|css)$'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'js-css-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 12 * 60 * 60
})
]
})
)
//For HTML
workbox.routing.registerRoute(
new RegExp('/'),
new workbox.strategies.NetworkFirst({
cacheName: 'html-caches',
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({
statuses: [0, 200]
}),
new workbox.expiration.ExpirationPlugin({
maxEntries: 20,
maxAgeSeconds: 12 * 60 * 60
})
]
}), 'GET');
//Other resources
workbox.routing.registerRoute(
new RegExp('/_next/static/'),
new workbox.strategies.StaleWhileRevalidate({
cacheName: 'static-caches',
})
);
} else {
// console.log('Workbox could not be loaded. No Offline support');
}
}